dsound: Nuke old implementation
[wine/multimedia.git] / dlls / dsound / dsound_main.c
blob5d96a1e7a3958e35b719f580b7590a6d9654bff9
1 /* DirectSound
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
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.
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.
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
21 * Most thread locking is complete. There may be a few race
22 * conditions still lurking.
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
35 #define COBJMACROS
36 #define NONAMELESSSTRUCT
37 #define NONAMELESSUNION
38 #include "config.h"
39 #include "wine/port.h"
40 #include <stdarg.h>
41 #include "wine/library.h"
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winuser.h"
46 #include "winnls.h"
47 #include "winreg.h"
48 #include "mmsystem.h"
49 #include "winternl.h"
50 #include "mmddk.h"
51 #include "wine/debug.h"
52 #include "dsound.h"
53 #include "dsconf.h"
54 #include "ks.h"
55 #include "rpcproxy.h"
56 #include "rpc.h"
57 #include "rpcndr.h"
58 #include "unknwn.h"
59 #include "oleidl.h"
60 #include "shobjidl.h"
62 #include "initguid.h"
63 #include "ksmedia.h"
64 #include "propkey.h"
65 #include "devpkey.h"
67 #include "dsound_private.h"
69 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
71 #ifdef HAVE_OPENAL
73 IID DSPROPSETID_EAX20_ListenerProperties = {
74 0x0306a6a8, 0xb224, 0x11d2, { 0x99, 0xe5, 0x00, 0x00, 0xe8, 0xd8, 0xc7, 0x22 }
77 const EAXLISTENERPROPERTIES EnvironmentDefaults[EAX_ENVIRONMENT_COUNT] =
79 REVERB_PRESET_GENERIC,
80 REVERB_PRESET_PADDEDCELL,
81 REVERB_PRESET_ROOM,
82 REVERB_PRESET_BATHROOM,
83 REVERB_PRESET_LIVINGROOM,
84 REVERB_PRESET_STONEROOM,
85 REVERB_PRESET_AUDITORIUM,
86 REVERB_PRESET_CONCERTHALL,
87 REVERB_PRESET_CAVE,
88 REVERB_PRESET_ARENA,
89 REVERB_PRESET_HANGAR,
90 REVERB_PRESET_CARPETEDHALLWAY,
91 REVERB_PRESET_HALLWAY,
92 REVERB_PRESET_STONECORRIDOR,
93 REVERB_PRESET_ALLEY,
94 REVERB_PRESET_FOREST,
95 REVERB_PRESET_CITY,
96 REVERB_PRESET_MOUNTAINS,
97 REVERB_PRESET_QUARRY,
98 REVERB_PRESET_PLAIN,
99 REVERB_PRESET_PARKINGLOT,
100 REVERB_PRESET_SEWERPIPE,
101 REVERB_PRESET_UNDERWATER,
102 REVERB_PRESET_DRUGGED,
103 REVERB_PRESET_DIZZY,
104 REVERB_PRESET_PSYCHOTIC
107 static CRITICAL_SECTION_DEBUG openal_crst_debug =
109 0, 0, &openal_crst,
110 { &openal_crst_debug.ProcessLocksList,
111 &openal_crst_debug.ProcessLocksList },
112 0, 0, { (DWORD_PTR)(__FILE__ ": openal_crst_debug") }
114 CRITICAL_SECTION openal_crst = { &openal_crst_debug, -1, 0, 0, 0, 0 };
116 static void *openal_handle = RTLD_DEFAULT;
117 int openal_loaded = 0;
118 #ifdef SONAME_LIBOPENAL
119 LPALCCREATECONTEXT palcCreateContext = NULL;
120 LPALCMAKECONTEXTCURRENT palcMakeContextCurrent = NULL;
121 LPALCPROCESSCONTEXT palcProcessContext = NULL;
122 LPALCSUSPENDCONTEXT palcSuspendContext = NULL;
123 LPALCDESTROYCONTEXT palcDestroyContext = NULL;
124 LPALCGETCURRENTCONTEXT palcGetCurrentContext = NULL;
125 LPALCGETCONTEXTSDEVICE palcGetContextsDevice = NULL;
126 LPALCOPENDEVICE palcOpenDevice = NULL;
127 LPALCCLOSEDEVICE palcCloseDevice = NULL;
128 LPALCGETERROR palcGetError = NULL;
129 LPALCISEXTENSIONPRESENT palcIsExtensionPresent = NULL;
130 LPALCGETPROCADDRESS palcGetProcAddress = NULL;
131 LPALCGETENUMVALUE palcGetEnumValue = NULL;
132 LPALCGETSTRING palcGetString = NULL;
133 LPALCGETINTEGERV palcGetIntegerv = NULL;
134 LPALCCAPTUREOPENDEVICE palcCaptureOpenDevice = NULL;
135 LPALCCAPTURECLOSEDEVICE palcCaptureCloseDevice = NULL;
136 LPALCCAPTURESTART palcCaptureStart = NULL;
137 LPALCCAPTURESTOP palcCaptureStop = NULL;
138 LPALCCAPTURESAMPLES palcCaptureSamples = NULL;
139 LPALENABLE palEnable = NULL;
140 LPALDISABLE palDisable = NULL;
141 LPALISENABLED palIsEnabled = NULL;
142 LPALGETSTRING palGetString = NULL;
143 LPALGETBOOLEANV palGetBooleanv = NULL;
144 LPALGETINTEGERV palGetIntegerv = NULL;
145 LPALGETFLOATV palGetFloatv = NULL;
146 LPALGETDOUBLEV palGetDoublev = NULL;
147 LPALGETBOOLEAN palGetBoolean = NULL;
148 LPALGETINTEGER palGetInteger = NULL;
149 LPALGETFLOAT palGetFloat = NULL;
150 LPALGETDOUBLE palGetDouble = NULL;
151 LPALGETERROR palGetError = NULL;
152 LPALISEXTENSIONPRESENT palIsExtensionPresent = NULL;
153 LPALGETPROCADDRESS palGetProcAddress = NULL;
154 LPALGETENUMVALUE palGetEnumValue = NULL;
155 LPALLISTENERF palListenerf = NULL;
156 LPALLISTENER3F palListener3f = NULL;
157 LPALLISTENERFV palListenerfv = NULL;
158 LPALLISTENERI palListeneri = NULL;
159 LPALLISTENER3I palListener3i = NULL;
160 LPALLISTENERIV palListeneriv = NULL;
161 LPALGETLISTENERF palGetListenerf = NULL;
162 LPALGETLISTENER3F palGetListener3f = NULL;
163 LPALGETLISTENERFV palGetListenerfv = NULL;
164 LPALGETLISTENERI palGetListeneri = NULL;
165 LPALGETLISTENER3I palGetListener3i = NULL;
166 LPALGETLISTENERIV palGetListeneriv = NULL;
167 LPALGENSOURCES palGenSources = NULL;
168 LPALDELETESOURCES palDeleteSources = NULL;
169 LPALISSOURCE palIsSource = NULL;
170 LPALSOURCEF palSourcef = NULL;
171 LPALSOURCE3F palSource3f = NULL;
172 LPALSOURCEFV palSourcefv = NULL;
173 LPALSOURCEI palSourcei = NULL;
174 LPALSOURCE3I palSource3i = NULL;
175 LPALSOURCEIV palSourceiv = NULL;
176 LPALGETSOURCEF palGetSourcef = NULL;
177 LPALGETSOURCE3F palGetSource3f = NULL;
178 LPALGETSOURCEFV palGetSourcefv = NULL;
179 LPALGETSOURCEI palGetSourcei = NULL;
180 LPALGETSOURCE3I palGetSource3i = NULL;
181 LPALGETSOURCEIV palGetSourceiv = NULL;
182 LPALSOURCEPLAYV palSourcePlayv = NULL;
183 LPALSOURCESTOPV palSourceStopv = NULL;
184 LPALSOURCEREWINDV palSourceRewindv = NULL;
185 LPALSOURCEPAUSEV palSourcePausev = NULL;
186 LPALSOURCEPLAY palSourcePlay = NULL;
187 LPALSOURCESTOP palSourceStop = NULL;
188 LPALSOURCEREWIND palSourceRewind = NULL;
189 LPALSOURCEPAUSE palSourcePause = NULL;
190 LPALSOURCEQUEUEBUFFERS palSourceQueueBuffers = NULL;
191 LPALSOURCEUNQUEUEBUFFERS palSourceUnqueueBuffers = NULL;
192 LPALGENBUFFERS palGenBuffers = NULL;
193 LPALDELETEBUFFERS palDeleteBuffers = NULL;
194 LPALISBUFFER palIsBuffer = NULL;
195 LPALBUFFERF palBufferf = NULL;
196 LPALBUFFER3F palBuffer3f = NULL;
197 LPALBUFFERFV palBufferfv = NULL;
198 LPALBUFFERI palBufferi = NULL;
199 LPALBUFFER3I palBuffer3i = NULL;
200 LPALBUFFERIV palBufferiv = NULL;
201 LPALGETBUFFERF palGetBufferf = NULL;
202 LPALGETBUFFER3F palGetBuffer3f = NULL;
203 LPALGETBUFFERFV palGetBufferfv = NULL;
204 LPALGETBUFFERI palGetBufferi = NULL;
205 LPALGETBUFFER3I palGetBuffer3i = NULL;
206 LPALGETBUFFERIV palGetBufferiv = NULL;
207 LPALBUFFERDATA palBufferData = NULL;
208 LPALDOPPLERFACTOR palDopplerFactor = NULL;
209 LPALDOPPLERVELOCITY palDopplerVelocity = NULL;
210 LPALDISTANCEMODEL palDistanceModel = NULL;
211 LPALSPEEDOFSOUND palSpeedOfSound = NULL;
212 #endif
214 LPALCMAKECONTEXTCURRENT set_context;
215 LPALCGETCURRENTCONTEXT get_context;
216 BOOL local_contexts;
218 static void load_libopenal(void)
220 DWORD failed = 0;
222 #ifdef SONAME_LIBOPENAL
223 char error[128];
224 openal_handle = wine_dlopen(SONAME_LIBOPENAL, RTLD_NOW, error, sizeof(error));
225 if (!openal_handle)
226 ERR("Couldn't load " SONAME_LIBOPENAL ": %s\n", error);
227 #define LOAD_FUNCPTR(f) \
228 if((p##f = wine_dlsym(openal_handle, #f, NULL, 0)) == NULL) { \
229 ERR("Couldn't lookup %s in libopenal\n", #f); \
230 failed = 1; \
233 LOAD_FUNCPTR(alcCreateContext);
234 LOAD_FUNCPTR(alcMakeContextCurrent);
235 LOAD_FUNCPTR(alcProcessContext);
236 LOAD_FUNCPTR(alcSuspendContext);
237 LOAD_FUNCPTR(alcDestroyContext);
238 LOAD_FUNCPTR(alcGetCurrentContext);
239 LOAD_FUNCPTR(alcGetContextsDevice);
240 LOAD_FUNCPTR(alcOpenDevice);
241 LOAD_FUNCPTR(alcCloseDevice);
242 LOAD_FUNCPTR(alcGetError);
243 LOAD_FUNCPTR(alcIsExtensionPresent);
244 LOAD_FUNCPTR(alcGetProcAddress);
245 LOAD_FUNCPTR(alcGetEnumValue);
246 LOAD_FUNCPTR(alcGetString);
247 LOAD_FUNCPTR(alcGetIntegerv);
248 LOAD_FUNCPTR(alcCaptureOpenDevice);
249 LOAD_FUNCPTR(alcCaptureCloseDevice);
250 LOAD_FUNCPTR(alcCaptureStart);
251 LOAD_FUNCPTR(alcCaptureStop);
252 LOAD_FUNCPTR(alcCaptureSamples);
253 LOAD_FUNCPTR(alEnable);
254 LOAD_FUNCPTR(alDisable);
255 LOAD_FUNCPTR(alIsEnabled);
256 LOAD_FUNCPTR(alGetString);
257 LOAD_FUNCPTR(alGetBooleanv);
258 LOAD_FUNCPTR(alGetIntegerv);
259 LOAD_FUNCPTR(alGetFloatv);
260 LOAD_FUNCPTR(alGetDoublev);
261 LOAD_FUNCPTR(alGetBoolean);
262 LOAD_FUNCPTR(alGetInteger);
263 LOAD_FUNCPTR(alGetFloat);
264 LOAD_FUNCPTR(alGetDouble);
265 LOAD_FUNCPTR(alGetError);
266 LOAD_FUNCPTR(alIsExtensionPresent);
267 LOAD_FUNCPTR(alGetProcAddress);
268 LOAD_FUNCPTR(alGetEnumValue);
269 LOAD_FUNCPTR(alListenerf);
270 LOAD_FUNCPTR(alListener3f);
271 LOAD_FUNCPTR(alListenerfv);
272 LOAD_FUNCPTR(alListeneri);
273 LOAD_FUNCPTR(alListener3i);
274 LOAD_FUNCPTR(alListeneriv);
275 LOAD_FUNCPTR(alGetListenerf);
276 LOAD_FUNCPTR(alGetListener3f);
277 LOAD_FUNCPTR(alGetListenerfv);
278 LOAD_FUNCPTR(alGetListeneri);
279 LOAD_FUNCPTR(alGetListener3i);
280 LOAD_FUNCPTR(alGetListeneriv);
281 LOAD_FUNCPTR(alGenSources);
282 LOAD_FUNCPTR(alDeleteSources);
283 LOAD_FUNCPTR(alIsSource);
284 LOAD_FUNCPTR(alSourcef);
285 LOAD_FUNCPTR(alSource3f);
286 LOAD_FUNCPTR(alSourcefv);
287 LOAD_FUNCPTR(alSourcei);
288 LOAD_FUNCPTR(alSource3i);
289 LOAD_FUNCPTR(alSourceiv);
290 LOAD_FUNCPTR(alGetSourcef);
291 LOAD_FUNCPTR(alGetSource3f);
292 LOAD_FUNCPTR(alGetSourcefv);
293 LOAD_FUNCPTR(alGetSourcei);
294 LOAD_FUNCPTR(alGetSource3i);
295 LOAD_FUNCPTR(alGetSourceiv);
296 LOAD_FUNCPTR(alSourcePlayv);
297 LOAD_FUNCPTR(alSourceStopv);
298 LOAD_FUNCPTR(alSourceRewindv);
299 LOAD_FUNCPTR(alSourcePausev);
300 LOAD_FUNCPTR(alSourcePlay);
301 LOAD_FUNCPTR(alSourceStop);
302 LOAD_FUNCPTR(alSourceRewind);
303 LOAD_FUNCPTR(alSourcePause);
304 LOAD_FUNCPTR(alSourceQueueBuffers);
305 LOAD_FUNCPTR(alSourceUnqueueBuffers);
306 LOAD_FUNCPTR(alGenBuffers);
307 LOAD_FUNCPTR(alDeleteBuffers);
308 LOAD_FUNCPTR(alIsBuffer);
309 LOAD_FUNCPTR(alBufferf);
310 LOAD_FUNCPTR(alBuffer3f);
311 LOAD_FUNCPTR(alBufferfv);
312 LOAD_FUNCPTR(alBufferi);
313 LOAD_FUNCPTR(alBuffer3i);
314 LOAD_FUNCPTR(alBufferiv);
315 LOAD_FUNCPTR(alGetBufferf);
316 LOAD_FUNCPTR(alGetBuffer3f);
317 LOAD_FUNCPTR(alGetBufferfv);
318 LOAD_FUNCPTR(alGetBufferi);
319 LOAD_FUNCPTR(alGetBuffer3i);
320 LOAD_FUNCPTR(alGetBufferiv);
321 LOAD_FUNCPTR(alBufferData);
322 LOAD_FUNCPTR(alDopplerFactor);
323 LOAD_FUNCPTR(alDopplerVelocity);
324 LOAD_FUNCPTR(alDistanceModel);
325 LOAD_FUNCPTR(alSpeedOfSound);
326 #undef LOAD_FUNCPTR
327 #endif
328 if (failed)
330 WARN("Unloading openal\n");
331 if (openal_handle != RTLD_DEFAULT)
332 wine_dlclose(openal_handle, NULL, 0);
333 openal_handle = NULL;
335 else {
336 local_contexts = palcIsExtensionPresent(NULL, "ALC_EXT_thread_local_context");
337 if (local_contexts) {
338 set_context = palcGetProcAddress(NULL, "alcSetThreadContext");
339 get_context = palcGetProcAddress(NULL, "alcGetThreadContext");
340 if(!set_context || !get_context)
342 ERR("TLS advertised but functions not found, disabling thread local context\n");
343 local_contexts = 0;
346 if(!local_contexts)
348 set_context = palcMakeContextCurrent;
349 get_context = palcGetCurrentContext;
351 openal_loaded = 1;
355 #endif /*HAVE_OPENAL*/
357 GUID DSOUND_renderer_guids[MAXWAVEDRIVERS];
358 GUID DSOUND_capture_guids[MAXWAVEDRIVERS];
360 WCHAR wine_vxd_drv[] = { 'w','i','n','e','m','m','.','v','x','d', 0 };
362 static HINSTANCE instance;
364 static HRESULT get_mmdevenum(IMMDeviceEnumerator **devenum)
366 HRESULT hr, init_hr;
368 init_hr = CoInitialize(NULL);
370 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
371 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)devenum);
372 if(FAILED(hr)){
373 CoUninitialize();
374 *devenum = NULL;
375 ERR("CoCreateInstance failed: %08x\n", hr);
376 return hr;
379 return init_hr;
382 static void release_mmdevenum(IMMDeviceEnumerator *devenum, HRESULT init_hr)
384 IMMDeviceEnumerator_Release(devenum);
385 if(SUCCEEDED(init_hr))
386 CoUninitialize();
389 static HRESULT get_mmdevice_guid(IMMDevice *device, IPropertyStore *ps,
390 GUID *guid)
392 PROPVARIANT pv;
393 HRESULT hr;
395 if(!ps){
396 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
397 if(FAILED(hr)){
398 WARN("OpenPropertyStore failed: %08x\n", hr);
399 return hr;
401 }else
402 IPropertyStore_AddRef(ps);
404 PropVariantInit(&pv);
406 hr = IPropertyStore_GetValue(ps, &PKEY_AudioEndpoint_GUID, &pv);
407 if(FAILED(hr)){
408 IPropertyStore_Release(ps);
409 WARN("GetValue(GUID) failed: %08x\n", hr);
410 return hr;
413 CLSIDFromString(pv.u.pwszVal, guid);
415 PropVariantClear(&pv);
416 IPropertyStore_Release(ps);
418 return S_OK;
421 /***************************************************************************
422 * GetDeviceID [DSOUND.9]
424 * Retrieves unique identifier of default device specified
426 * PARAMS
427 * pGuidSrc [I] Address of device GUID.
428 * pGuidDest [O] Address to receive unique device GUID.
430 * RETURNS
431 * Success: DS_OK
432 * Failure: DSERR_INVALIDPARAM
434 * NOTES
435 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
436 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
437 * DSDEVID_DefaultVoiceCapture.
438 * Returns pGuidSrc if pGuidSrc is a valid device or the device
439 * GUID for the specified constants.
441 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
443 IMMDeviceEnumerator *devenum;
444 EDataFlow flow = (EDataFlow)-1;
445 ERole role = (ERole)-1;
446 HRESULT hr, init_hr;
448 TRACE("(%s,%p)\n", debugstr_guid(pGuidSrc),pGuidDest);
450 if(!pGuidSrc || !pGuidDest)
451 return DSERR_INVALIDPARAM;
453 init_hr = get_mmdevenum(&devenum);
454 if(!devenum)
455 return init_hr;
457 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuidSrc)){
458 role = eMultimedia;
459 flow = eRender;
460 }else if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuidSrc)){
461 role = eCommunications;
462 flow = eRender;
463 }else if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuidSrc)){
464 role = eMultimedia;
465 flow = eCapture;
466 }else if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuidSrc)){
467 role = eCommunications;
468 flow = eCapture;
471 if(role != (ERole)-1 && flow != (EDataFlow)-1){
472 IMMDevice *device;
474 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum,
475 flow, role, &device);
476 if(FAILED(hr)){
477 WARN("GetDefaultAudioEndpoint failed: %08x\n", hr);
478 release_mmdevenum(devenum, init_hr);
479 return DSERR_NODRIVER;
482 hr = get_mmdevice_guid(device, NULL, pGuidDest);
483 IMMDevice_Release(device);
485 release_mmdevenum(devenum, init_hr);
487 return (hr == S_OK) ? DS_OK : hr;
490 release_mmdevenum(devenum, init_hr);
492 *pGuidDest = *pGuidSrc;
494 return DS_OK;
497 struct morecontext
499 LPDSENUMCALLBACKA callA;
500 LPVOID data;
503 static BOOL CALLBACK a_to_w_callback(LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
505 struct morecontext *context = data;
506 char descA[MAXPNAMELEN], modA[MAXPNAMELEN];
508 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, sizeof(descA), NULL, NULL);
509 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, sizeof(modA), NULL, NULL);
511 return context->callA(guid, descA, modA, context->data);
514 /***************************************************************************
515 * DirectSoundEnumerateA [DSOUND.2]
517 * Enumerate all DirectSound drivers installed in the system
519 * PARAMS
520 * lpDSEnumCallback [I] Address of callback function.
521 * lpContext [I] Address of user defined context passed to callback function.
523 * RETURNS
524 * Success: DS_OK
525 * Failure: DSERR_INVALIDPARAM
527 HRESULT WINAPI DirectSoundEnumerateA(
528 LPDSENUMCALLBACKA lpDSEnumCallback,
529 LPVOID lpContext)
531 struct morecontext context;
533 if (lpDSEnumCallback == NULL) {
534 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
535 return DSERR_INVALIDPARAM;
538 context.callA = lpDSEnumCallback;
539 context.data = lpContext;
541 return DirectSoundEnumerateW(a_to_w_callback, &context);
544 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device)
546 IMMDeviceEnumerator *devenum;
547 IMMDeviceCollection *coll;
548 UINT count, i;
549 HRESULT hr, init_hr;
551 init_hr = get_mmdevenum(&devenum);
552 if(!devenum)
553 return init_hr;
555 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
556 DEVICE_STATE_ACTIVE, &coll);
557 if(FAILED(hr)){
558 WARN("EnumAudioEndpoints failed: %08x\n", hr);
559 release_mmdevenum(devenum, init_hr);
560 return hr;
563 hr = IMMDeviceCollection_GetCount(coll, &count);
564 if(FAILED(hr)){
565 IMMDeviceCollection_Release(coll);
566 release_mmdevenum(devenum, init_hr);
567 WARN("GetCount failed: %08x\n", hr);
568 return hr;
571 for(i = 0; i < count; ++i){
572 GUID guid;
574 hr = IMMDeviceCollection_Item(coll, i, device);
575 if(FAILED(hr))
576 continue;
578 hr = get_mmdevice_guid(*device, NULL, &guid);
579 if(FAILED(hr)){
580 IMMDevice_Release(*device);
581 continue;
584 if(IsEqualGUID(&guid, tgt)){
585 IMMDeviceCollection_Release(coll);
586 release_mmdevenum(devenum, init_hr);
587 return DS_OK;
590 IMMDevice_Release(*device);
593 WARN("No device with GUID %s found!\n", wine_dbgstr_guid(tgt));
595 IMMDeviceCollection_Release(coll);
596 release_mmdevenum(devenum, init_hr);
598 return DSERR_INVALIDPARAM;
601 static BOOL send_device(IMMDevice *device, GUID *guid,
602 LPDSENUMCALLBACKW cb, void *user)
604 IPropertyStore *ps;
605 PROPVARIANT pv;
606 BOOL keep_going;
607 HRESULT hr;
609 PropVariantInit(&pv);
611 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
612 if(FAILED(hr)){
613 WARN("OpenPropertyStore failed: %08x\n", hr);
614 return TRUE;
617 hr = get_mmdevice_guid(device, ps, guid);
618 if(FAILED(hr)){
619 IPropertyStore_Release(ps);
620 return TRUE;
623 hr = IPropertyStore_GetValue(ps,
624 (const PROPERTYKEY *)&DEVPKEY_Device_FriendlyName, &pv);
625 if(FAILED(hr)){
626 IPropertyStore_Release(ps);
627 WARN("GetValue(FriendlyName) failed: %08x\n", hr);
628 return TRUE;
631 TRACE("Calling back with %s (%s)\n", wine_dbgstr_guid(guid),
632 wine_dbgstr_w(pv.u.pwszVal));
634 keep_going = cb(guid, pv.u.pwszVal, wine_vxd_drv, user);
636 PropVariantClear(&pv);
637 IPropertyStore_Release(ps);
639 return keep_going;
642 /* S_FALSE means the callback returned FALSE at some point
643 * S_OK means the callback always returned TRUE */
644 HRESULT enumerate_mmdevices(EDataFlow flow, GUID *guids,
645 LPDSENUMCALLBACKW cb, void *user)
647 IMMDeviceEnumerator *devenum;
648 IMMDeviceCollection *coll;
649 IMMDevice *defdev = NULL;
650 UINT count, i, n;
651 BOOL keep_going;
652 HRESULT hr, init_hr;
654 static const WCHAR primary_desc[] = {'P','r','i','m','a','r','y',' ',
655 'S','o','u','n','d',' ','D','r','i','v','e','r',0};
656 static const WCHAR empty_drv[] = {0};
658 init_hr = get_mmdevenum(&devenum);
659 if(!devenum)
660 return init_hr;
662 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
663 DEVICE_STATE_ACTIVE, &coll);
664 if(FAILED(hr)){
665 release_mmdevenum(devenum, init_hr);
666 WARN("EnumAudioEndpoints failed: %08x\n", hr);
667 return DS_OK;
670 hr = IMMDeviceCollection_GetCount(coll, &count);
671 if(FAILED(hr)){
672 IMMDeviceCollection_Release(coll);
673 release_mmdevenum(devenum, init_hr);
674 WARN("GetCount failed: %08x\n", hr);
675 return DS_OK;
678 if(count == 0){
679 release_mmdevenum(devenum, init_hr);
680 return DS_OK;
683 TRACE("Calling back with NULL (%s)\n", wine_dbgstr_w(primary_desc));
684 keep_going = cb(NULL, primary_desc, empty_drv, user);
686 /* always send the default device first */
687 if(keep_going){
688 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow,
689 eMultimedia, &defdev);
690 if(FAILED(hr)){
691 defdev = NULL;
692 n = 0;
693 }else{
694 keep_going = send_device(defdev, &guids[0], cb, user);
695 n = 1;
699 for(i = 0; keep_going && i < count; ++i){
700 IMMDevice *device;
702 hr = IMMDeviceCollection_Item(coll, i, &device);
703 if(FAILED(hr)){
704 WARN("Item failed: %08x\n", hr);
705 continue;
708 if(device != defdev){
709 send_device(device, &guids[n], cb, user);
710 ++n;
713 IMMDevice_Release(device);
716 if(defdev)
717 IMMDevice_Release(defdev);
718 IMMDeviceCollection_Release(coll);
720 release_mmdevenum(devenum, init_hr);
722 return (keep_going == TRUE) ? S_OK : S_FALSE;
725 /***************************************************************************
726 * DirectSoundEnumerateW [DSOUND.3]
728 * Enumerate all DirectSound drivers installed in the system
730 * PARAMS
731 * lpDSEnumCallback [I] Address of callback function.
732 * lpContext [I] Address of user defined context passed to callback function.
734 * RETURNS
735 * Success: DS_OK
736 * Failure: DSERR_INVALIDPARAM
738 HRESULT WINAPI DirectSoundEnumerateW(
739 LPDSENUMCALLBACKW lpDSEnumCallback,
740 LPVOID lpContext )
742 HRESULT hr;
744 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext);
746 if (lpDSEnumCallback == NULL) {
747 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
748 return DSERR_INVALIDPARAM;
751 hr = enumerate_mmdevices(eRender, DSOUND_renderer_guids,
752 lpDSEnumCallback, lpContext);
753 return SUCCEEDED(hr) ? DS_OK : hr;
756 /***************************************************************************
757 * DirectSoundCaptureEnumerateA [DSOUND.7]
759 * Enumerate all DirectSound drivers installed in the system.
761 * PARAMS
762 * lpDSEnumCallback [I] Address of callback function.
763 * lpContext [I] Address of user defined context passed to callback function.
765 * RETURNS
766 * Success: DS_OK
767 * Failure: DSERR_INVALIDPARAM
769 HRESULT WINAPI DirectSoundCaptureEnumerateA(
770 LPDSENUMCALLBACKA lpDSEnumCallback,
771 LPVOID lpContext)
773 struct morecontext context;
775 if (lpDSEnumCallback == NULL) {
776 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
777 return DSERR_INVALIDPARAM;
780 context.callA = lpDSEnumCallback;
781 context.data = lpContext;
783 return DirectSoundCaptureEnumerateW(a_to_w_callback, &context);
786 /***************************************************************************
787 * DirectSoundCaptureEnumerateW [DSOUND.8]
789 * Enumerate all DirectSound drivers installed in the system.
791 * PARAMS
792 * lpDSEnumCallback [I] Address of callback function.
793 * lpContext [I] Address of user defined context passed to callback function.
795 * RETURNS
796 * Success: DS_OK
797 * Failure: DSERR_INVALIDPARAM
799 HRESULT WINAPI
800 DirectSoundCaptureEnumerateW(
801 LPDSENUMCALLBACKW lpDSEnumCallback,
802 LPVOID lpContext)
804 HRESULT hr;
806 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
808 if (lpDSEnumCallback == NULL) {
809 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
810 return DSERR_INVALIDPARAM;
813 hr = enumerate_mmdevices(eCapture, DSOUND_capture_guids,
814 lpDSEnumCallback, lpContext);
815 return SUCCEEDED(hr) ? DS_OK : hr;
818 /*******************************************************************************
819 * DirectSound ClassFactory
822 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
824 typedef struct {
825 IClassFactory IClassFactory_iface;
826 REFCLSID rclsid;
827 FnCreateInstance pfnCreateInstance;
828 } IClassFactoryImpl;
830 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
832 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
835 static HRESULT WINAPI
836 DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
838 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
839 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
840 if (ppobj == NULL)
841 return E_POINTER;
842 if (IsEqualIID(riid, &IID_IUnknown) ||
843 IsEqualIID(riid, &IID_IClassFactory))
845 *ppobj = iface;
846 IUnknown_AddRef(iface);
847 return S_OK;
849 *ppobj = NULL;
850 return E_NOINTERFACE;
853 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
855 return 2;
858 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
860 /* static class, won't be freed */
861 return 1;
864 static HRESULT WINAPI DSCF_CreateInstance(
865 LPCLASSFACTORY iface,
866 LPUNKNOWN pOuter,
867 REFIID riid,
868 LPVOID *ppobj)
870 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
871 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
873 if (pOuter)
874 return CLASS_E_NOAGGREGATION;
876 if (ppobj == NULL) {
877 WARN("invalid parameter\n");
878 return DSERR_INVALIDPARAM;
880 *ppobj = NULL;
881 return This->pfnCreateInstance(riid, ppobj);
884 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
886 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
887 FIXME("(%p, %d) stub!\n", This, dolock);
888 return S_OK;
891 static const IClassFactoryVtbl DSCF_Vtbl = {
892 DSCF_QueryInterface,
893 DSCF_AddRef,
894 DSCF_Release,
895 DSCF_CreateInstance,
896 DSCF_LockServer
899 static IClassFactoryImpl DSOUND_CF[] = {
900 { { &DSCF_Vtbl }, &CLSID_DirectSound, (FnCreateInstance)DSOUND_Create },
901 { { &DSCF_Vtbl }, &CLSID_DirectSound8, (FnCreateInstance)DSOUND_Create8 },
902 { { &DSCF_Vtbl }, &CLSID_DirectSoundCapture, (FnCreateInstance)DSOUND_CaptureCreate },
903 { { &DSCF_Vtbl }, &CLSID_DirectSoundCapture8, (FnCreateInstance)DSOUND_CaptureCreate8 },
904 { { &DSCF_Vtbl }, &CLSID_DirectSoundFullDuplex, (FnCreateInstance)DSOUND_FullDuplexCreate },
905 { { &DSCF_Vtbl }, &CLSID_DirectSoundPrivate, (FnCreateInstance)IKsPrivatePropertySetImpl_Create },
906 { { NULL }, NULL, NULL }
909 /*******************************************************************************
910 * DllGetClassObject [DSOUND.@]
911 * Retrieves class object from a DLL object
913 * NOTES
914 * Docs say returns STDAPI
916 * PARAMS
917 * rclsid [I] CLSID for the class object
918 * riid [I] Reference to identifier of interface for class object
919 * ppv [O] Address of variable to receive interface pointer for riid
921 * RETURNS
922 * Success: S_OK
923 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
924 * E_UNEXPECTED
926 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
928 int i = 0;
929 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
931 if (ppv == NULL) {
932 WARN("invalid parameter\n");
933 return E_INVALIDARG;
936 *ppv = NULL;
938 if (!IsEqualIID(riid, &IID_IClassFactory) &&
939 !IsEqualIID(riid, &IID_IUnknown)) {
940 WARN("no interface for %s\n", debugstr_guid(riid));
941 return E_NOINTERFACE;
944 while (NULL != DSOUND_CF[i].rclsid) {
945 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
946 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
947 *ppv = &DSOUND_CF[i];
948 return S_OK;
950 i++;
953 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
954 debugstr_guid(riid), ppv);
955 return CLASS_E_CLASSNOTAVAILABLE;
959 /*******************************************************************************
960 * DllCanUnloadNow [DSOUND.4]
961 * Determines whether the DLL is in use.
963 * RETURNS
964 * Can unload now: S_OK
965 * Cannot unload now (the DLL is still active): S_FALSE
967 HRESULT WINAPI DllCanUnloadNow(void)
969 return S_FALSE;
972 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
973 guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2; \
974 guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3; \
975 guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6; \
976 guid.Data4[6] = b7; guid.Data4[7] = b8;
978 /***********************************************************************
979 * DllMain (DSOUND.init)
981 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
983 TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpvReserved);
985 switch (fdwReason) {
986 case DLL_PROCESS_ATTACH:
987 TRACE("DLL_PROCESS_ATTACH\n");
988 instance = hInstDLL;
989 DisableThreadLibraryCalls(hInstDLL);
990 /* Increase refcount on dsound by 1 */
991 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
992 #ifdef HAVE_OPENAL
993 load_libopenal();
994 #endif /*HAVE_OPENAL*/
995 break;
996 case DLL_PROCESS_DETACH:
997 TRACE("DLL_PROCESS_DETACH\n");
998 #ifdef SONAME_LIBOPENAL
999 if (openal_handle)
1000 wine_dlclose(openal_handle, NULL, 0);
1001 #endif /*SONAME_LIBOPENAL*/
1002 break;
1003 default:
1004 TRACE("UNKNOWN REASON\n");
1005 break;
1007 return TRUE;
1010 /***********************************************************************
1011 * DllRegisterServer (DSOUND.@)
1013 HRESULT WINAPI DllRegisterServer(void)
1015 return __wine_register_resources( instance );
1018 /***********************************************************************
1019 * DllUnregisterServer (DSOUND.@)
1021 HRESULT WINAPI DllUnregisterServer(void)
1023 return __wine_unregister_resources( instance );