43e143276f6d317ef7b992bc8267a80278965584
[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 CoUninitialize();
154 *devenum = NULL;
155 ERR("CoCreateInstance failed: %08x\n", hr);
156 return hr;
157 }
158
159 return init_hr;
160 }
161
162 static void release_mmdevenum(IMMDeviceEnumerator *devenum, HRESULT init_hr)
163 {
164 IMMDeviceEnumerator_Release(devenum);
165 if(SUCCEEDED(init_hr))
166 CoUninitialize();
167 }
168
169 static HRESULT get_mmdevice_guid(IMMDevice *device, IPropertyStore *ps,
170 GUID *guid)
171 {
172 PROPVARIANT pv;
173 HRESULT hr;
174
175 if(!ps){
176 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
177 if(FAILED(hr)){
178 WARN("OpenPropertyStore failed: %08x\n", hr);
179 return hr;
180 }
181 }else
182 IPropertyStore_AddRef(ps);
183
184 PropVariantInit(&pv);
185
186 hr = IPropertyStore_GetValue(ps, &PKEY_AudioEndpoint_GUID, &pv);
187 if(FAILED(hr)){
188 IPropertyStore_Release(ps);
189 WARN("GetValue(GUID) failed: %08x\n", hr);
190 return hr;
191 }
192
193 CLSIDFromString(pv.u.pwszVal, guid);
194
195 PropVariantClear(&pv);
196 IPropertyStore_Release(ps);
197
198 return S_OK;
199 }
200
201 /***************************************************************************
202 * GetDeviceID [DSOUND.9]
203 *
204 * Retrieves unique identifier of default device specified
205 *
206 * PARAMS
207 * pGuidSrc [I] Address of device GUID.
208 * pGuidDest [O] Address to receive unique device GUID.
209 *
210 * RETURNS
211 * Success: DS_OK
212 * Failure: DSERR_INVALIDPARAM
213 *
214 * NOTES
215 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
216 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
217 * DSDEVID_DefaultVoiceCapture.
218 * Returns pGuidSrc if pGuidSrc is a valid device or the device
219 * GUID for the specified constants.
220 */
221 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
222 {
223 IMMDeviceEnumerator *devenum;
224 EDataFlow flow = (EDataFlow)-1;
225 ERole role = (ERole)-1;
226 HRESULT hr, init_hr;
227
228 TRACE("(%s,%p)\n", get_device_id(pGuidSrc),pGuidDest);
229
230 if(!pGuidSrc || !pGuidDest)
231 return DSERR_INVALIDPARAM;
232
233 init_hr = get_mmdevenum(&devenum);
234 if(!devenum)
235 return init_hr;
236
237 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuidSrc)){
238 role = eMultimedia;
239 flow = eRender;
240 }else if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuidSrc)){
241 role = eCommunications;
242 flow = eRender;
243 }else if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuidSrc)){
244 role = eMultimedia;
245 flow = eCapture;
246 }else if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuidSrc)){
247 role = eCommunications;
248 flow = eCapture;
249 }
250
251 if(role != (ERole)-1 && flow != (EDataFlow)-1){
252 IMMDevice *device;
253
254 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum,
255 flow, role, &device);
256 if(FAILED(hr)){
257 WARN("GetDefaultAudioEndpoint failed: %08x\n", hr);
258 release_mmdevenum(devenum, init_hr);
259 return DSERR_NODRIVER;
260 }
261
262 hr = get_mmdevice_guid(device, NULL, pGuidDest);
263 IMMDevice_Release(device);
264
265 release_mmdevenum(devenum, init_hr);
266
267 return (hr == S_OK) ? DS_OK : hr;
268 }
269
270 release_mmdevenum(devenum, init_hr);
271
272 *pGuidDest = *pGuidSrc;
273
274 return DS_OK;
275 }
276
277 struct morecontext
278 {
279 LPDSENUMCALLBACKA callA;
280 LPVOID data;
281 };
282
283 static BOOL CALLBACK a_to_w_callback(LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
284 {
285 struct morecontext *context = data;
286 char descA[MAXPNAMELEN], modA[MAXPNAMELEN];
287
288 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, sizeof(descA), NULL, NULL);
289 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, sizeof(modA), NULL, NULL);
290
291 return context->callA(guid, descA, modA, context->data);
292 }
293
294 /***************************************************************************
295 * DirectSoundEnumerateA [DSOUND.2]
296 *
297 * Enumerate all DirectSound drivers installed in the system
298 *
299 * PARAMS
300 * lpDSEnumCallback [I] Address of callback function.
301 * lpContext [I] Address of user defined context passed to callback function.
302 *
303 * RETURNS
304 * Success: DS_OK
305 * Failure: DSERR_INVALIDPARAM
306 */
307 HRESULT WINAPI DirectSoundEnumerateA(
308 LPDSENUMCALLBACKA lpDSEnumCallback,
309 LPVOID lpContext)
310 {
311 struct morecontext context;
312
313 if (lpDSEnumCallback == NULL) {
314 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
315 return DSERR_INVALIDPARAM;
316 }
317
318 context.callA = lpDSEnumCallback;
319 context.data = lpContext;
320
321 return DirectSoundEnumerateW(a_to_w_callback, &context);
322 }
323
324 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device)
325 {
326 IMMDeviceEnumerator *devenum;
327 IMMDeviceCollection *coll;
328 UINT count, i;
329 HRESULT hr, init_hr;
330
331 init_hr = get_mmdevenum(&devenum);
332 if(!devenum)
333 return init_hr;
334
335 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
336 DEVICE_STATE_ACTIVE, &coll);
337 if(FAILED(hr)){
338 WARN("EnumAudioEndpoints failed: %08x\n", hr);
339 release_mmdevenum(devenum, init_hr);
340 return hr;
341 }
342
343 hr = IMMDeviceCollection_GetCount(coll, &count);
344 if(FAILED(hr)){
345 IMMDeviceCollection_Release(coll);
346 release_mmdevenum(devenum, init_hr);
347 WARN("GetCount failed: %08x\n", hr);
348 return hr;
349 }
350
351 for(i = 0; i < count; ++i){
352 GUID guid;
353
354 hr = IMMDeviceCollection_Item(coll, i, device);
355 if(FAILED(hr))
356 continue;
357
358 hr = get_mmdevice_guid(*device, NULL, &guid);
359 if(FAILED(hr)){
360 IMMDevice_Release(*device);
361 continue;
362 }
363
364 if(IsEqualGUID(&guid, tgt)){
365 IMMDeviceCollection_Release(coll);
366 release_mmdevenum(devenum, init_hr);
367 return DS_OK;
368 }
369
370 IMMDevice_Release(*device);
371 }
372
373 WARN("No device with GUID %s found!\n", wine_dbgstr_guid(tgt));
374
375 IMMDeviceCollection_Release(coll);
376 release_mmdevenum(devenum, init_hr);
377
378 return DSERR_INVALIDPARAM;
379 }
380
381 static BOOL send_device(IMMDevice *device, GUID *guid,
382 LPDSENUMCALLBACKW cb, void *user)
383 {
384 IPropertyStore *ps;
385 PROPVARIANT pv;
386 BOOL keep_going;
387 HRESULT hr;
388
389 PropVariantInit(&pv);
390
391 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
392 if(FAILED(hr)){
393 WARN("OpenPropertyStore failed: %08x\n", hr);
394 return TRUE;
395 }
396
397 hr = get_mmdevice_guid(device, ps, guid);
398 if(FAILED(hr)){
399 IPropertyStore_Release(ps);
400 return TRUE;
401 }
402
403 hr = IPropertyStore_GetValue(ps,
404 (const PROPERTYKEY *)&DEVPKEY_Device_FriendlyName, &pv);
405 if(FAILED(hr)){
406 IPropertyStore_Release(ps);
407 WARN("GetValue(FriendlyName) failed: %08x\n", hr);
408 return TRUE;
409 }
410
411 TRACE("Calling back with %s (%s)\n", wine_dbgstr_guid(guid),
412 wine_dbgstr_w(pv.u.pwszVal));
413
414 keep_going = cb(guid, pv.u.pwszVal, wine_vxd_drv, user);
415
416 PropVariantClear(&pv);
417 IPropertyStore_Release(ps);
418
419 return keep_going;
420 }
421
422 /* S_FALSE means the callback returned FALSE at some point
423 * S_OK means the callback always returned TRUE */
424 HRESULT enumerate_mmdevices(EDataFlow flow, GUID *guids,
425 LPDSENUMCALLBACKW cb, void *user)
426 {
427 IMMDeviceEnumerator *devenum;
428 IMMDeviceCollection *coll;
429 IMMDevice *defdev = NULL;
430 UINT count, i, n;
431 BOOL keep_going;
432 HRESULT hr, init_hr;
433
434 static const WCHAR primary_desc[] = {'P','r','i','m','a','r','y',' ',
435 'S','o','u','n','d',' ','D','r','i','v','e','r',0};
436 static const WCHAR empty_drv[] = {0};
437
438 init_hr = get_mmdevenum(&devenum);
439 if(!devenum)
440 return init_hr;
441
442 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
443 DEVICE_STATE_ACTIVE, &coll);
444 if(FAILED(hr)){
445 release_mmdevenum(devenum, init_hr);
446 WARN("EnumAudioEndpoints failed: %08x\n", hr);
447 return DS_OK;
448 }
449
450 hr = IMMDeviceCollection_GetCount(coll, &count);
451 if(FAILED(hr)){
452 IMMDeviceCollection_Release(coll);
453 release_mmdevenum(devenum, init_hr);
454 WARN("GetCount failed: %08x\n", hr);
455 return DS_OK;
456 }
457
458 if(count == 0){
459 release_mmdevenum(devenum, init_hr);
460 return DS_OK;
461 }
462
463 TRACE("Calling back with NULL (%s)\n", wine_dbgstr_w(primary_desc));
464 keep_going = cb(NULL, primary_desc, empty_drv, user);
465
466 /* always send the default device first */
467 if(keep_going){
468 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow,
469 eMultimedia, &defdev);
470 if(FAILED(hr)){
471 defdev = NULL;
472 n = 0;
473 }else{
474 keep_going = send_device(defdev, &guids[0], cb, user);
475 n = 1;
476 }
477 }
478
479 for(i = 0; keep_going && i < count; ++i){
480 IMMDevice *device;
481
482 hr = IMMDeviceCollection_Item(coll, i, &device);
483 if(FAILED(hr)){
484 WARN("Item failed: %08x\n", hr);
485 continue;
486 }
487
488 if(device != defdev){
489 send_device(device, &guids[n], cb, user);
490 ++n;
491 }
492
493 IMMDevice_Release(device);
494 }
495
496 if(defdev)
497 IMMDevice_Release(defdev);
498 IMMDeviceCollection_Release(coll);
499
500 release_mmdevenum(devenum, init_hr);
501
502 return (keep_going == TRUE) ? S_OK : S_FALSE;
503 }
504
505 /***************************************************************************
506 * DirectSoundEnumerateW [DSOUND.3]
507 *
508 * Enumerate all DirectSound drivers installed in the system
509 *
510 * PARAMS
511 * lpDSEnumCallback [I] Address of callback function.
512 * lpContext [I] Address of user defined context passed to callback function.
513 *
514 * RETURNS
515 * Success: DS_OK
516 * Failure: DSERR_INVALIDPARAM
517 */
518 HRESULT WINAPI DirectSoundEnumerateW(
519 LPDSENUMCALLBACKW lpDSEnumCallback,
520 LPVOID lpContext )
521 {
522 HRESULT hr;
523
524 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext);
525
526 if (lpDSEnumCallback == NULL) {
527 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
528 return DSERR_INVALIDPARAM;
529 }
530
531 setup_dsound_options();
532
533 hr = enumerate_mmdevices(eRender, DSOUND_renderer_guids,
534 lpDSEnumCallback, lpContext);
535 return SUCCEEDED(hr) ? DS_OK : hr;
536 }
537
538 /***************************************************************************
539 * DirectSoundCaptureEnumerateA [DSOUND.7]
540 *
541 * Enumerate all DirectSound drivers installed in the system.
542 *
543 * PARAMS
544 * lpDSEnumCallback [I] Address of callback function.
545 * lpContext [I] Address of user defined context passed to callback function.
546 *
547 * RETURNS
548 * Success: DS_OK
549 * Failure: DSERR_INVALIDPARAM
550 */
551 HRESULT WINAPI DirectSoundCaptureEnumerateA(
552 LPDSENUMCALLBACKA lpDSEnumCallback,
553 LPVOID lpContext)
554 {
555 struct morecontext context;
556
557 if (lpDSEnumCallback == NULL) {
558 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
559 return DSERR_INVALIDPARAM;
560 }
561
562 context.callA = lpDSEnumCallback;
563 context.data = lpContext;
564
565 return DirectSoundCaptureEnumerateW(a_to_w_callback, &context);
566 }
567
568 /***************************************************************************
569 * DirectSoundCaptureEnumerateW [DSOUND.8]
570 *
571 * Enumerate all DirectSound drivers installed in the system.
572 *
573 * PARAMS
574 * lpDSEnumCallback [I] Address of callback function.
575 * lpContext [I] Address of user defined context passed to callback function.
576 *
577 * RETURNS
578 * Success: DS_OK
579 * Failure: DSERR_INVALIDPARAM
580 */
581 HRESULT WINAPI
582 DirectSoundCaptureEnumerateW(
583 LPDSENUMCALLBACKW lpDSEnumCallback,
584 LPVOID lpContext)
585 {
586 HRESULT hr;
587
588 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
589
590 if (lpDSEnumCallback == NULL) {
591 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
592 return DSERR_INVALIDPARAM;
593 }
594
595 setup_dsound_options();
596
597 hr = enumerate_mmdevices(eCapture, DSOUND_capture_guids,
598 lpDSEnumCallback, lpContext);
599 return SUCCEEDED(hr) ? DS_OK : hr;
600 }
601
602 /*******************************************************************************
603 * DirectSound ClassFactory
604 */
605
606 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
607
608 typedef struct {
609 IClassFactory IClassFactory_iface;
610 REFCLSID rclsid;
611 FnCreateInstance pfnCreateInstance;
612 } IClassFactoryImpl;
613
614 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
615 {
616 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
617 }
618
619 static HRESULT WINAPI
620 DSCF_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppobj)
621 {
622 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
623 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
624 if (ppobj == NULL)
625 return E_POINTER;
626 if (IsEqualIID(riid, &IID_IUnknown) ||
627 IsEqualIID(riid, &IID_IClassFactory))
628 {
629 *ppobj = iface;
630 IClassFactory_AddRef(iface);
631 return S_OK;
632 }
633 *ppobj = NULL;
634 return E_NOINTERFACE;
635 }
636
637 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
638 {
639 return 2;
640 }
641
642 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
643 {
644 /* static class, won't be freed */
645 return 1;
646 }
647
648 static HRESULT WINAPI DSCF_CreateInstance(
649 LPCLASSFACTORY iface,
650 LPUNKNOWN pOuter,
651 REFIID riid,
652 LPVOID *ppobj)
653 {
654 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
655 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
656
657 if (pOuter)
658 return CLASS_E_NOAGGREGATION;
659
660 if (ppobj == NULL) {
661 WARN("invalid parameter\n");
662 return DSERR_INVALIDPARAM;
663 }
664 *ppobj = NULL;
665 return This->pfnCreateInstance(riid, ppobj);
666 }
667
668 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
669 {
670 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
671 FIXME("(%p, %d) stub!\n", This, dolock);
672 return S_OK;
673 }
674
675 static const IClassFactoryVtbl DSCF_Vtbl = {
676 DSCF_QueryInterface,
677 DSCF_AddRef,
678 DSCF_Release,
679 DSCF_CreateInstance,
680 DSCF_LockServer
681 };
682
683 static IClassFactoryImpl DSOUND_CF[] = {
684 { { &DSCF_Vtbl }, &CLSID_DirectSound, DSOUND_Create },
685 { { &DSCF_Vtbl }, &CLSID_DirectSound8, DSOUND_Create8 },
686 { { &DSCF_Vtbl }, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
687 { { &DSCF_Vtbl }, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
688 { { &DSCF_Vtbl }, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
689 { { &DSCF_Vtbl }, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
690 { { NULL }, NULL, NULL }
691 };
692
693 /*******************************************************************************
694 * DllGetClassObject [DSOUND.@]
695 * Retrieves class object from a DLL object
696 *
697 * NOTES
698 * Docs say returns STDAPI
699 *
700 * PARAMS
701 * rclsid [I] CLSID for the class object
702 * riid [I] Reference to identifier of interface for class object
703 * ppv [O] Address of variable to receive interface pointer for riid
704 *
705 * RETURNS
706 * Success: S_OK
707 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
708 * E_UNEXPECTED
709 */
710 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
711 {
712 int i = 0;
713 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
714
715 if (ppv == NULL) {
716 WARN("invalid parameter\n");
717 return E_INVALIDARG;
718 }
719
720 *ppv = NULL;
721
722 if (!IsEqualIID(riid, &IID_IClassFactory) &&
723 !IsEqualIID(riid, &IID_IUnknown)) {
724 WARN("no interface for %s\n", debugstr_guid(riid));
725 return E_NOINTERFACE;
726 }
727
728 while (NULL != DSOUND_CF[i].rclsid) {
729 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
730 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
731 *ppv = &DSOUND_CF[i];
732 return S_OK;
733 }
734 i++;
735 }
736
737 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
738 debugstr_guid(riid), ppv);
739 return CLASS_E_CLASSNOTAVAILABLE;
740 }
741
742
743 /*******************************************************************************
744 * DllCanUnloadNow [DSOUND.4]
745 * Determines whether the DLL is in use.
746 *
747 * RETURNS
748 * Can unload now: S_OK
749 * Cannot unload now (the DLL is still active): S_FALSE
750 */
751 HRESULT WINAPI DllCanUnloadNow(void)
752 {
753 return S_FALSE;
754 }
755
756 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
757 guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2; \
758 guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3; \
759 guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6; \
760 guid.Data4[6] = b7; guid.Data4[7] = b8;
761
762 /***********************************************************************
763 * DllMain (DSOUND.init)
764 */
765 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
766 {
767 TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpvReserved);
768
769 switch (fdwReason) {
770 case DLL_PROCESS_ATTACH:
771 TRACE("DLL_PROCESS_ATTACH\n");
772 instance = hInstDLL;
773 DisableThreadLibraryCalls(hInstDLL);
774 /* Increase refcount on dsound by 1 */
775 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
776 break;
777 case DLL_PROCESS_DETACH:
778 TRACE("DLL_PROCESS_DETACH\n");
779 DeleteCriticalSection(&DSOUND_renderers_lock);
780 DeleteCriticalSection(&DSOUND_capturers_lock);
781 break;
782 default:
783 TRACE("UNKNOWN REASON\n");
784 break;
785 }
786 return TRUE;
787 }
788
789 /***********************************************************************
790 * DllRegisterServer (DSOUND.@)
791 */
792 HRESULT WINAPI DllRegisterServer(void)
793 {
794 return __wine_register_resources( instance );
795 }
796
797 /***********************************************************************
798 * DllUnregisterServer (DSOUND.@)
799 */
800 HRESULT WINAPI DllUnregisterServer(void)
801 {
802 return __wine_unregister_resources( instance );
803 }