c25418d480b28e3d7dce90d6f8561efda7874457
[reactos.git] / reactos / dll / directx / wine / dsound / dsound_main.c
1 /* DirectSound
2 *
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 *
21 * Most thread locking is complete. There may be a few race
22 * conditions still lurking.
23 *
24 * TODO:
25 * Implement SetCooperativeLevel properly (need to address focus issues)
26 * Implement DirectSound3DBuffers (stubs in place)
27 * Use hardware 3D support if available
28 * Add critical section locking inside Release and AddRef methods
29 * Handle static buffers - put those in hardware, non-static not in hardware
30 * Hardware DuplicateSoundBuffer
31 * Proper volume calculation for 3d buffers
32 * Remove DS_HEL_FRAGS and use mixer fragment length for it
33 */
34
35 #include "dsound_private.h"
36
37 #include <winreg.h>
38 #include <rpcproxy.h>
39
40 struct list DSOUND_renderers = LIST_INIT(DSOUND_renderers);
41 CRITICAL_SECTION DSOUND_renderers_lock;
42 static CRITICAL_SECTION_DEBUG DSOUND_renderers_lock_debug =
43 {
44 0, 0, &DSOUND_renderers_lock,
45 { &DSOUND_renderers_lock_debug.ProcessLocksList, &DSOUND_renderers_lock_debug.ProcessLocksList },
46 0, 0, { (DWORD_PTR)(__FILE__ ": DSOUND_renderers_lock") }
47 };
48 CRITICAL_SECTION DSOUND_renderers_lock = { &DSOUND_renderers_lock_debug, -1, 0, 0, 0, 0 };
49
50 struct list DSOUND_capturers = LIST_INIT(DSOUND_capturers);
51 CRITICAL_SECTION DSOUND_capturers_lock;
52 static CRITICAL_SECTION_DEBUG DSOUND_capturers_lock_debug =
53 {
54 0, 0, &DSOUND_capturers_lock,
55 { &DSOUND_capturers_lock_debug.ProcessLocksList, &DSOUND_capturers_lock_debug.ProcessLocksList },
56 0, 0, { (DWORD_PTR)(__FILE__ ": DSOUND_capturers_lock") }
57 };
58 CRITICAL_SECTION DSOUND_capturers_lock = { &DSOUND_capturers_lock_debug, -1, 0, 0, 0, 0 };
59
60 GUID DSOUND_renderer_guids[MAXWAVEDRIVERS];
61 GUID DSOUND_capture_guids[MAXWAVEDRIVERS];
62
63 WCHAR wine_vxd_drv[] = { 'w','i','n','e','m','m','.','v','x','d', 0 };
64
65 /* All default settings, you most likely don't want to touch these, see wiki on UsefulRegistryKeys */
66 int ds_hel_buflen = 32768 * 2;
67 int ds_snd_queue_max = 10;
68 static HINSTANCE instance;
69
70 /*
71 * Get a config key from either the app-specific or the default config
72 */
73
74 static inline DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
75 char *buffer, DWORD size )
76 {
77 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
78 if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
79 return ERROR_FILE_NOT_FOUND;
80 }
81
82
83 /*
84 * Setup the dsound options.
85 */
86
87 void setup_dsound_options(void)
88 {
89 char buffer[MAX_PATH+16];
90 HKEY hkey, appkey = 0;
91 DWORD len;
92
93 buffer[MAX_PATH]='\0';
94
95 /* @@ Wine registry key: HKCU\Software\Wine\DirectSound */
96 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectSound", &hkey )) hkey = 0;
97
98 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
99 if (len && len < MAX_PATH)
100 {
101 HKEY tmpkey;
102 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectSound */
103 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
104 {
105 char *p, *appname = buffer;
106 if ((p = strrchr( appname, '/' ))) appname = p + 1;
107 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
108 strcat( appname, "\\DirectSound" );
109 TRACE("appname = [%s]\n", appname);
110 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
111 RegCloseKey( tmpkey );
112 }
113 }
114
115 /* get options */
116
117 if (!get_config_key( hkey, appkey, "HelBuflen", buffer, MAX_PATH ))
118 ds_hel_buflen = atoi(buffer);
119
120 if (!get_config_key( hkey, appkey, "SndQueueMax", buffer, MAX_PATH ))
121 ds_snd_queue_max = atoi(buffer);
122
123
124 if (appkey) RegCloseKey( appkey );
125 if (hkey) RegCloseKey( hkey );
126
127 TRACE("ds_hel_buflen = %d\n", ds_hel_buflen);
128 TRACE("ds_snd_queue_max = %d\n", ds_snd_queue_max);
129 }
130
131 static const char * get_device_id(LPCGUID pGuid)
132 {
133 if (IsEqualGUID(&DSDEVID_DefaultPlayback, pGuid))
134 return "DSDEVID_DefaultPlayback";
135 else if (IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuid))
136 return "DSDEVID_DefaultVoicePlayback";
137 else if (IsEqualGUID(&DSDEVID_DefaultCapture, pGuid))
138 return "DSDEVID_DefaultCapture";
139 else if (IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuid))
140 return "DSDEVID_DefaultVoiceCapture";
141 return debugstr_guid(pGuid);
142 }
143
144 static HRESULT get_mmdevenum(IMMDeviceEnumerator **devenum)
145 {
146 HRESULT hr, init_hr;
147
148 init_hr = CoInitialize(NULL);
149
150 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
151 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)devenum);
152 if(FAILED(hr)){
153 if(SUCCEEDED(init_hr))
154 CoUninitialize();
155 *devenum = NULL;
156 ERR("CoCreateInstance failed: %08x\n", hr);
157 return hr;
158 }
159
160 return init_hr;
161 }
162
163 static void release_mmdevenum(IMMDeviceEnumerator *devenum, HRESULT init_hr)
164 {
165 IMMDeviceEnumerator_Release(devenum);
166 if(SUCCEEDED(init_hr))
167 CoUninitialize();
168 }
169
170 static HRESULT get_mmdevice_guid(IMMDevice *device, IPropertyStore *ps,
171 GUID *guid)
172 {
173 PROPVARIANT pv;
174 HRESULT hr;
175
176 if(!ps){
177 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
178 if(FAILED(hr)){
179 WARN("OpenPropertyStore failed: %08x\n", hr);
180 return hr;
181 }
182 }else
183 IPropertyStore_AddRef(ps);
184
185 PropVariantInit(&pv);
186
187 hr = IPropertyStore_GetValue(ps, &PKEY_AudioEndpoint_GUID, &pv);
188 if(FAILED(hr)){
189 IPropertyStore_Release(ps);
190 WARN("GetValue(GUID) failed: %08x\n", hr);
191 return hr;
192 }
193
194 CLSIDFromString(pv.u.pwszVal, guid);
195
196 PropVariantClear(&pv);
197 IPropertyStore_Release(ps);
198
199 return S_OK;
200 }
201
202 /***************************************************************************
203 * GetDeviceID [DSOUND.9]
204 *
205 * Retrieves unique identifier of default device specified
206 *
207 * PARAMS
208 * pGuidSrc [I] Address of device GUID.
209 * pGuidDest [O] Address to receive unique device GUID.
210 *
211 * RETURNS
212 * Success: DS_OK
213 * Failure: DSERR_INVALIDPARAM
214 *
215 * NOTES
216 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
217 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
218 * DSDEVID_DefaultVoiceCapture.
219 * Returns pGuidSrc if pGuidSrc is a valid device or the device
220 * GUID for the specified constants.
221 */
222 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
223 {
224 IMMDeviceEnumerator *devenum;
225 EDataFlow flow = (EDataFlow)-1;
226 ERole role = (ERole)-1;
227 HRESULT hr, init_hr;
228
229 TRACE("(%s,%p)\n", get_device_id(pGuidSrc),pGuidDest);
230
231 if(!pGuidSrc || !pGuidDest)
232 return DSERR_INVALIDPARAM;
233
234 init_hr = get_mmdevenum(&devenum);
235 if(!devenum)
236 return init_hr;
237
238 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuidSrc)){
239 role = eMultimedia;
240 flow = eRender;
241 }else if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuidSrc)){
242 role = eCommunications;
243 flow = eRender;
244 }else if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuidSrc)){
245 role = eMultimedia;
246 flow = eCapture;
247 }else if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuidSrc)){
248 role = eCommunications;
249 flow = eCapture;
250 }
251
252 if(role != (ERole)-1 && flow != (EDataFlow)-1){
253 IMMDevice *device;
254
255 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum,
256 flow, role, &device);
257 if(FAILED(hr)){
258 WARN("GetDefaultAudioEndpoint failed: %08x\n", hr);
259 release_mmdevenum(devenum, init_hr);
260 return DSERR_NODRIVER;
261 }
262
263 hr = get_mmdevice_guid(device, NULL, pGuidDest);
264 IMMDevice_Release(device);
265
266 release_mmdevenum(devenum, init_hr);
267
268 return (hr == S_OK) ? DS_OK : hr;
269 }
270
271 release_mmdevenum(devenum, init_hr);
272
273 *pGuidDest = *pGuidSrc;
274
275 return DS_OK;
276 }
277
278 struct morecontext
279 {
280 LPDSENUMCALLBACKA callA;
281 LPVOID data;
282 };
283
284 static BOOL CALLBACK a_to_w_callback(LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
285 {
286 struct morecontext *context = data;
287 char descA[MAXPNAMELEN], modA[MAXPNAMELEN];
288
289 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, sizeof(descA), NULL, NULL);
290 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, sizeof(modA), NULL, NULL);
291
292 return context->callA(guid, descA, modA, context->data);
293 }
294
295 /***************************************************************************
296 * DirectSoundEnumerateA [DSOUND.2]
297 *
298 * Enumerate all DirectSound drivers installed in the system
299 *
300 * PARAMS
301 * lpDSEnumCallback [I] Address of callback function.
302 * lpContext [I] Address of user defined context passed to callback function.
303 *
304 * RETURNS
305 * Success: DS_OK
306 * Failure: DSERR_INVALIDPARAM
307 */
308 HRESULT WINAPI DirectSoundEnumerateA(
309 LPDSENUMCALLBACKA lpDSEnumCallback,
310 LPVOID lpContext)
311 {
312 struct morecontext context;
313
314 if (lpDSEnumCallback == NULL) {
315 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
316 return DSERR_INVALIDPARAM;
317 }
318
319 context.callA = lpDSEnumCallback;
320 context.data = lpContext;
321
322 return DirectSoundEnumerateW(a_to_w_callback, &context);
323 }
324
325 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device)
326 {
327 IMMDeviceEnumerator *devenum;
328 IMMDeviceCollection *coll;
329 UINT count, i;
330 HRESULT hr, init_hr;
331
332 init_hr = get_mmdevenum(&devenum);
333 if(!devenum)
334 return init_hr;
335
336 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
337 DEVICE_STATE_ACTIVE, &coll);
338 if(FAILED(hr)){
339 WARN("EnumAudioEndpoints failed: %08x\n", hr);
340 release_mmdevenum(devenum, init_hr);
341 return hr;
342 }
343
344 hr = IMMDeviceCollection_GetCount(coll, &count);
345 if(FAILED(hr)){
346 IMMDeviceCollection_Release(coll);
347 release_mmdevenum(devenum, init_hr);
348 WARN("GetCount failed: %08x\n", hr);
349 return hr;
350 }
351
352 for(i = 0; i < count; ++i){
353 GUID guid;
354
355 hr = IMMDeviceCollection_Item(coll, i, device);
356 if(FAILED(hr))
357 continue;
358
359 hr = get_mmdevice_guid(*device, NULL, &guid);
360 if(FAILED(hr)){
361 IMMDevice_Release(*device);
362 continue;
363 }
364
365 if(IsEqualGUID(&guid, tgt)){
366 IMMDeviceCollection_Release(coll);
367 release_mmdevenum(devenum, init_hr);
368 return DS_OK;
369 }
370
371 IMMDevice_Release(*device);
372 }
373
374 WARN("No device with GUID %s found!\n", wine_dbgstr_guid(tgt));
375
376 IMMDeviceCollection_Release(coll);
377 release_mmdevenum(devenum, init_hr);
378
379 return DSERR_INVALIDPARAM;
380 }
381
382 static BOOL send_device(IMMDevice *device, GUID *guid,
383 LPDSENUMCALLBACKW cb, void *user)
384 {
385 IPropertyStore *ps;
386 PROPVARIANT pv;
387 BOOL keep_going;
388 HRESULT hr;
389
390 PropVariantInit(&pv);
391
392 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
393 if(FAILED(hr)){
394 WARN("OpenPropertyStore failed: %08x\n", hr);
395 return TRUE;
396 }
397
398 hr = get_mmdevice_guid(device, ps, guid);
399 if(FAILED(hr)){
400 IPropertyStore_Release(ps);
401 return TRUE;
402 }
403
404 hr = IPropertyStore_GetValue(ps,
405 (const PROPERTYKEY *)&DEVPKEY_Device_FriendlyName, &pv);
406 if(FAILED(hr)){
407 IPropertyStore_Release(ps);
408 WARN("GetValue(FriendlyName) failed: %08x\n", hr);
409 return TRUE;
410 }
411
412 TRACE("Calling back with %s (%s)\n", wine_dbgstr_guid(guid),
413 wine_dbgstr_w(pv.u.pwszVal));
414
415 keep_going = cb(guid, pv.u.pwszVal, wine_vxd_drv, user);
416
417 PropVariantClear(&pv);
418 IPropertyStore_Release(ps);
419
420 return keep_going;
421 }
422
423 /* S_FALSE means the callback returned FALSE at some point
424 * S_OK means the callback always returned TRUE */
425 HRESULT enumerate_mmdevices(EDataFlow flow, GUID *guids,
426 LPDSENUMCALLBACKW cb, void *user)
427 {
428 IMMDeviceEnumerator *devenum;
429 IMMDeviceCollection *coll;
430 IMMDevice *defdev = NULL;
431 UINT count, i, n;
432 BOOL keep_going;
433 HRESULT hr, init_hr;
434
435 static const WCHAR primary_desc[] = {'P','r','i','m','a','r','y',' ',
436 'S','o','u','n','d',' ','D','r','i','v','e','r',0};
437 static const WCHAR empty_drv[] = {0};
438
439 init_hr = get_mmdevenum(&devenum);
440 if(!devenum)
441 return init_hr;
442
443 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
444 DEVICE_STATE_ACTIVE, &coll);
445 if(FAILED(hr)){
446 release_mmdevenum(devenum, init_hr);
447 WARN("EnumAudioEndpoints failed: %08x\n", hr);
448 return DS_OK;
449 }
450
451 hr = IMMDeviceCollection_GetCount(coll, &count);
452 if(FAILED(hr)){
453 IMMDeviceCollection_Release(coll);
454 release_mmdevenum(devenum, init_hr);
455 WARN("GetCount failed: %08x\n", hr);
456 return DS_OK;
457 }
458
459 if(count == 0){
460 release_mmdevenum(devenum, init_hr);
461 return DS_OK;
462 }
463
464 TRACE("Calling back with NULL (%s)\n", wine_dbgstr_w(primary_desc));
465 keep_going = cb(NULL, primary_desc, empty_drv, user);
466
467 /* always send the default device first */
468 if(keep_going){
469 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow,
470 eMultimedia, &defdev);
471 if(FAILED(hr)){
472 defdev = NULL;
473 n = 0;
474 }else{
475 keep_going = send_device(defdev, &guids[0], cb, user);
476 n = 1;
477 }
478 }
479
480 for(i = 0; keep_going && i < count; ++i){
481 IMMDevice *device;
482
483 hr = IMMDeviceCollection_Item(coll, i, &device);
484 if(FAILED(hr)){
485 WARN("Item failed: %08x\n", hr);
486 continue;
487 }
488
489 if(device != defdev){
490 send_device(device, &guids[n], cb, user);
491 ++n;
492 }
493
494 IMMDevice_Release(device);
495 }
496
497 if(defdev)
498 IMMDevice_Release(defdev);
499 IMMDeviceCollection_Release(coll);
500
501 release_mmdevenum(devenum, init_hr);
502
503 return (keep_going == TRUE) ? S_OK : S_FALSE;
504 }
505
506 /***************************************************************************
507 * DirectSoundEnumerateW [DSOUND.3]
508 *
509 * Enumerate all DirectSound drivers installed in the system
510 *
511 * PARAMS
512 * lpDSEnumCallback [I] Address of callback function.
513 * lpContext [I] Address of user defined context passed to callback function.
514 *
515 * RETURNS
516 * Success: DS_OK
517 * Failure: DSERR_INVALIDPARAM
518 */
519 HRESULT WINAPI DirectSoundEnumerateW(
520 LPDSENUMCALLBACKW lpDSEnumCallback,
521 LPVOID lpContext )
522 {
523 HRESULT hr;
524
525 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext);
526
527 if (lpDSEnumCallback == NULL) {
528 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
529 return DSERR_INVALIDPARAM;
530 }
531
532 setup_dsound_options();
533
534 hr = enumerate_mmdevices(eRender, DSOUND_renderer_guids,
535 lpDSEnumCallback, lpContext);
536 return SUCCEEDED(hr) ? DS_OK : hr;
537 }
538
539 /***************************************************************************
540 * DirectSoundCaptureEnumerateA [DSOUND.7]
541 *
542 * Enumerate all DirectSound drivers installed in the system.
543 *
544 * PARAMS
545 * lpDSEnumCallback [I] Address of callback function.
546 * lpContext [I] Address of user defined context passed to callback function.
547 *
548 * RETURNS
549 * Success: DS_OK
550 * Failure: DSERR_INVALIDPARAM
551 */
552 HRESULT WINAPI DirectSoundCaptureEnumerateA(
553 LPDSENUMCALLBACKA lpDSEnumCallback,
554 LPVOID lpContext)
555 {
556 struct morecontext context;
557
558 if (lpDSEnumCallback == NULL) {
559 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
560 return DSERR_INVALIDPARAM;
561 }
562
563 context.callA = lpDSEnumCallback;
564 context.data = lpContext;
565
566 return DirectSoundCaptureEnumerateW(a_to_w_callback, &context);
567 }
568
569 /***************************************************************************
570 * DirectSoundCaptureEnumerateW [DSOUND.8]
571 *
572 * Enumerate all DirectSound drivers installed in the system.
573 *
574 * PARAMS
575 * lpDSEnumCallback [I] Address of callback function.
576 * lpContext [I] Address of user defined context passed to callback function.
577 *
578 * RETURNS
579 * Success: DS_OK
580 * Failure: DSERR_INVALIDPARAM
581 */
582 HRESULT WINAPI
583 DirectSoundCaptureEnumerateW(
584 LPDSENUMCALLBACKW lpDSEnumCallback,
585 LPVOID lpContext)
586 {
587 HRESULT hr;
588
589 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
590
591 if (lpDSEnumCallback == NULL) {
592 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
593 return DSERR_INVALIDPARAM;
594 }
595
596 setup_dsound_options();
597
598 hr = enumerate_mmdevices(eCapture, DSOUND_capture_guids,
599 lpDSEnumCallback, lpContext);
600 return SUCCEEDED(hr) ? DS_OK : hr;
601 }
602
603 /*******************************************************************************
604 * DirectSound ClassFactory
605 */
606
607 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
608
609 typedef struct {
610 IClassFactory IClassFactory_iface;
611 REFCLSID rclsid;
612 FnCreateInstance pfnCreateInstance;
613 } IClassFactoryImpl;
614
615 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
616 {
617 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
618 }
619
620 static HRESULT WINAPI
621 DSCF_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppobj)
622 {
623 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
624 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
625 if (ppobj == NULL)
626 return E_POINTER;
627 if (IsEqualIID(riid, &IID_IUnknown) ||
628 IsEqualIID(riid, &IID_IClassFactory))
629 {
630 *ppobj = iface;
631 IClassFactory_AddRef(iface);
632 return S_OK;
633 }
634 *ppobj = NULL;
635 return E_NOINTERFACE;
636 }
637
638 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
639 {
640 return 2;
641 }
642
643 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
644 {
645 /* static class, won't be freed */
646 return 1;
647 }
648
649 static HRESULT WINAPI DSCF_CreateInstance(
650 LPCLASSFACTORY iface,
651 LPUNKNOWN pOuter,
652 REFIID riid,
653 LPVOID *ppobj)
654 {
655 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
656 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
657
658 if (pOuter)
659 return CLASS_E_NOAGGREGATION;
660
661 if (ppobj == NULL) {
662 WARN("invalid parameter\n");
663 return DSERR_INVALIDPARAM;
664 }
665 *ppobj = NULL;
666 return This->pfnCreateInstance(riid, ppobj);
667 }
668
669 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
670 {
671 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
672 FIXME("(%p, %d) stub!\n", This, dolock);
673 return S_OK;
674 }
675
676 static const IClassFactoryVtbl DSCF_Vtbl = {
677 DSCF_QueryInterface,
678 DSCF_AddRef,
679 DSCF_Release,
680 DSCF_CreateInstance,
681 DSCF_LockServer
682 };
683
684 static IClassFactoryImpl DSOUND_CF[] = {
685 { { &DSCF_Vtbl }, &CLSID_DirectSound, DSOUND_Create },
686 { { &DSCF_Vtbl }, &CLSID_DirectSound8, DSOUND_Create8 },
687 { { &DSCF_Vtbl }, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
688 { { &DSCF_Vtbl }, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
689 { { &DSCF_Vtbl }, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
690 { { &DSCF_Vtbl }, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
691 { { NULL }, NULL, NULL }
692 };
693
694 /*******************************************************************************
695 * DllGetClassObject [DSOUND.@]
696 * Retrieves class object from a DLL object
697 *
698 * NOTES
699 * Docs say returns STDAPI
700 *
701 * PARAMS
702 * rclsid [I] CLSID for the class object
703 * riid [I] Reference to identifier of interface for class object
704 * ppv [O] Address of variable to receive interface pointer for riid
705 *
706 * RETURNS
707 * Success: S_OK
708 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
709 * E_UNEXPECTED
710 */
711 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
712 {
713 int i = 0;
714 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
715
716 if (ppv == NULL) {
717 WARN("invalid parameter\n");
718 return E_INVALIDARG;
719 }
720
721 *ppv = NULL;
722
723 if (!IsEqualIID(riid, &IID_IClassFactory) &&
724 !IsEqualIID(riid, &IID_IUnknown)) {
725 WARN("no interface for %s\n", debugstr_guid(riid));
726 return E_NOINTERFACE;
727 }
728
729 while (NULL != DSOUND_CF[i].rclsid) {
730 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
731 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
732 *ppv = &DSOUND_CF[i];
733 return S_OK;
734 }
735 i++;
736 }
737
738 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
739 debugstr_guid(riid), ppv);
740 return CLASS_E_CLASSNOTAVAILABLE;
741 }
742
743
744 /*******************************************************************************
745 * DllCanUnloadNow [DSOUND.4]
746 * Determines whether the DLL is in use.
747 *
748 * RETURNS
749 * Can unload now: S_OK
750 * Cannot unload now (the DLL is still active): S_FALSE
751 */
752 HRESULT WINAPI DllCanUnloadNow(void)
753 {
754 return S_FALSE;
755 }
756
757 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
758 guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2; \
759 guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3; \
760 guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6; \
761 guid.Data4[6] = b7; guid.Data4[7] = b8;
762
763 /***********************************************************************
764 * DllMain (DSOUND.init)
765 */
766 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
767 {
768 TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpvReserved);
769
770 switch (fdwReason) {
771 case DLL_PROCESS_ATTACH:
772 TRACE("DLL_PROCESS_ATTACH\n");
773 instance = hInstDLL;
774 DisableThreadLibraryCalls(hInstDLL);
775 /* Increase refcount on dsound by 1 */
776 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
777 break;
778 case DLL_PROCESS_DETACH:
779 TRACE("DLL_PROCESS_DETACH\n");
780 DeleteCriticalSection(&DSOUND_renderers_lock);
781 DeleteCriticalSection(&DSOUND_capturers_lock);
782 break;
783 default:
784 TRACE("UNKNOWN REASON\n");
785 break;
786 }
787 return TRUE;
788 }
789
790 /***********************************************************************
791 * DllRegisterServer (DSOUND.@)
792 */
793 HRESULT WINAPI DllRegisterServer(void)
794 {
795 return __wine_register_resources( instance );
796 }
797
798 /***********************************************************************
799 * DllUnregisterServer (DSOUND.@)
800 */
801 HRESULT WINAPI DllUnregisterServer(void)
802 {
803 return __wine_unregister_resources( instance );
804 }