Remove an errant early-out check
[dsound-openal.git] / dsound_main.c
blob905ac642d6d8eaa76a0d85c9ff6cd23b9105f7a5
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 CONST_VTABLE
36 #define INITGUID
37 #include <stdarg.h>
38 #include <string.h>
40 #include <windows.h>
41 #include <dsound.h>
42 #include <mmsystem.h>
43 #include <mmdeviceapi.h>
44 #include <devpropdef.h>
46 #include "dsound_private.h"
47 #include "eax-presets.h"
49 #ifndef DECLSPEC_EXPORT
50 #ifdef _WIN32
51 #define DECLSPEC_EXPORT __declspec(dllexport)
52 #else
53 #define DECLSPEC_EXPORT
54 #endif
55 #endif
58 int LogLevel = 1;
59 FILE *LogFile;
62 typedef struct DeviceList {
63 GUID *Guids;
64 size_t Count;
65 } DeviceList;
66 static DeviceList PlaybackDevices = { NULL, 0 };
67 static DeviceList CaptureDevices = { NULL, 0 };
69 const WCHAR aldriver_name[] = L"dsoal-aldrv.dll";
72 const EAXREVERBPROPERTIES EnvironmentDefaults[EAX_ENVIRONMENT_UNDEFINED] = {
73 REVERB_PRESET_GENERIC,
74 REVERB_PRESET_PADDEDCELL,
75 REVERB_PRESET_ROOM,
76 REVERB_PRESET_BATHROOM,
77 REVERB_PRESET_LIVINGROOM,
78 REVERB_PRESET_STONEROOM,
79 REVERB_PRESET_AUDITORIUM,
80 REVERB_PRESET_CONCERTHALL,
81 REVERB_PRESET_CAVE,
82 REVERB_PRESET_ARENA,
83 REVERB_PRESET_HANGAR,
84 REVERB_PRESET_CARPETEDHALLWAY,
85 REVERB_PRESET_HALLWAY,
86 REVERB_PRESET_STONECORRIDOR,
87 REVERB_PRESET_ALLEY,
88 REVERB_PRESET_FOREST,
89 REVERB_PRESET_CITY,
90 REVERB_PRESET_MOUNTAINS,
91 REVERB_PRESET_QUARRY,
92 REVERB_PRESET_PLAIN,
93 REVERB_PRESET_PARKINGLOT,
94 REVERB_PRESET_SEWERPIPE,
95 REVERB_PRESET_UNDERWATER,
96 REVERB_PRESET_DRUGGED,
97 REVERB_PRESET_DIZZY,
98 REVERB_PRESET_PSYCHOTIC
101 CRITICAL_SECTION openal_crst;
103 int openal_loaded = 0;
104 static HANDLE openal_handle = NULL;
105 LPALCCREATECONTEXT palcCreateContext = NULL;
106 LPALCMAKECONTEXTCURRENT palcMakeContextCurrent = NULL;
107 LPALCPROCESSCONTEXT palcProcessContext = NULL;
108 LPALCSUSPENDCONTEXT palcSuspendContext = NULL;
109 LPALCDESTROYCONTEXT palcDestroyContext = NULL;
110 LPALCGETCURRENTCONTEXT palcGetCurrentContext = NULL;
111 LPALCGETCONTEXTSDEVICE palcGetContextsDevice = NULL;
112 LPALCOPENDEVICE palcOpenDevice = NULL;
113 LPALCCLOSEDEVICE palcCloseDevice = NULL;
114 LPALCGETERROR palcGetError = NULL;
115 LPALCISEXTENSIONPRESENT palcIsExtensionPresent = NULL;
116 LPALCGETPROCADDRESS palcGetProcAddress = NULL;
117 LPALCGETENUMVALUE palcGetEnumValue = NULL;
118 LPALCGETSTRING palcGetString = NULL;
119 LPALCGETINTEGERV palcGetIntegerv = NULL;
120 LPALCCAPTUREOPENDEVICE palcCaptureOpenDevice = NULL;
121 LPALCCAPTURECLOSEDEVICE palcCaptureCloseDevice = NULL;
122 LPALCCAPTURESTART palcCaptureStart = NULL;
123 LPALCCAPTURESTOP palcCaptureStop = NULL;
124 LPALCCAPTURESAMPLES palcCaptureSamples = NULL;
125 LPALENABLE palEnable = NULL;
126 LPALDISABLE palDisable = NULL;
127 LPALISENABLED palIsEnabled = NULL;
128 LPALGETSTRING palGetString = NULL;
129 LPALGETBOOLEANV palGetBooleanv = NULL;
130 LPALGETINTEGERV palGetIntegerv = NULL;
131 LPALGETFLOATV palGetFloatv = NULL;
132 LPALGETDOUBLEV palGetDoublev = NULL;
133 LPALGETBOOLEAN palGetBoolean = NULL;
134 LPALGETINTEGER palGetInteger = NULL;
135 LPALGETFLOAT palGetFloat = NULL;
136 LPALGETDOUBLE palGetDouble = NULL;
137 LPALGETERROR palGetError = NULL;
138 LPALISEXTENSIONPRESENT palIsExtensionPresent = NULL;
139 LPALGETPROCADDRESS palGetProcAddress = NULL;
140 LPALGETENUMVALUE palGetEnumValue = NULL;
141 LPALLISTENERF palListenerf = NULL;
142 LPALLISTENER3F palListener3f = NULL;
143 LPALLISTENERFV palListenerfv = NULL;
144 LPALLISTENERI palListeneri = NULL;
145 LPALLISTENER3I palListener3i = NULL;
146 LPALLISTENERIV palListeneriv = NULL;
147 LPALGETLISTENERF palGetListenerf = NULL;
148 LPALGETLISTENER3F palGetListener3f = NULL;
149 LPALGETLISTENERFV palGetListenerfv = NULL;
150 LPALGETLISTENERI palGetListeneri = NULL;
151 LPALGETLISTENER3I palGetListener3i = NULL;
152 LPALGETLISTENERIV palGetListeneriv = NULL;
153 LPALGENSOURCES palGenSources = NULL;
154 LPALDELETESOURCES palDeleteSources = NULL;
155 LPALISSOURCE palIsSource = NULL;
156 LPALSOURCEF palSourcef = NULL;
157 LPALSOURCE3F palSource3f = NULL;
158 LPALSOURCEFV palSourcefv = NULL;
159 LPALSOURCEI palSourcei = NULL;
160 LPALSOURCE3I palSource3i = NULL;
161 LPALSOURCEIV palSourceiv = NULL;
162 LPALGETSOURCEF palGetSourcef = NULL;
163 LPALGETSOURCE3F palGetSource3f = NULL;
164 LPALGETSOURCEFV palGetSourcefv = NULL;
165 LPALGETSOURCEI palGetSourcei = NULL;
166 LPALGETSOURCE3I palGetSource3i = NULL;
167 LPALGETSOURCEIV palGetSourceiv = NULL;
168 LPALSOURCEPLAYV palSourcePlayv = NULL;
169 LPALSOURCESTOPV palSourceStopv = NULL;
170 LPALSOURCEREWINDV palSourceRewindv = NULL;
171 LPALSOURCEPAUSEV palSourcePausev = NULL;
172 LPALSOURCEPLAY palSourcePlay = NULL;
173 LPALSOURCESTOP palSourceStop = NULL;
174 LPALSOURCEREWIND palSourceRewind = NULL;
175 LPALSOURCEPAUSE palSourcePause = NULL;
176 LPALSOURCEQUEUEBUFFERS palSourceQueueBuffers = NULL;
177 LPALSOURCEUNQUEUEBUFFERS palSourceUnqueueBuffers = NULL;
178 LPALGENBUFFERS palGenBuffers = NULL;
179 LPALDELETEBUFFERS palDeleteBuffers = NULL;
180 LPALISBUFFER palIsBuffer = NULL;
181 LPALBUFFERF palBufferf = NULL;
182 LPALBUFFER3F palBuffer3f = NULL;
183 LPALBUFFERFV palBufferfv = NULL;
184 LPALBUFFERI palBufferi = NULL;
185 LPALBUFFER3I palBuffer3i = NULL;
186 LPALBUFFERIV palBufferiv = NULL;
187 LPALGETBUFFERF palGetBufferf = NULL;
188 LPALGETBUFFER3F palGetBuffer3f = NULL;
189 LPALGETBUFFERFV palGetBufferfv = NULL;
190 LPALGETBUFFERI palGetBufferi = NULL;
191 LPALGETBUFFER3I palGetBuffer3i = NULL;
192 LPALGETBUFFERIV palGetBufferiv = NULL;
193 LPALBUFFERDATA palBufferData = NULL;
194 LPALDOPPLERFACTOR palDopplerFactor = NULL;
195 LPALDOPPLERVELOCITY palDopplerVelocity = NULL;
196 LPALDISTANCEMODEL palDistanceModel = NULL;
197 LPALSPEEDOFSOUND palSpeedOfSound = NULL;
199 LPEAXSET pEAXSet = NULL;
200 LPEAXGET pEAXGet = NULL;
201 LPALDEFERUPDATESSOFT palDeferUpdatesSOFT = NULL;
202 LPALPROCESSUPDATESSOFT palProcessUpdatesSOFT = NULL;
203 LPALBUFFERSTORAGESOFT palBufferStorageSOFT = NULL;
204 LPALMAPBUFFERSOFT palMapBufferSOFT = NULL;
205 LPALUNMAPBUFFERSOFT palUnmapBufferSOFT = NULL;
206 LPALFLUSHMAPPEDBUFFERSOFT palFlushMappedBufferSOFT = NULL;
208 LPALCMAKECONTEXTCURRENT set_context;
209 LPALCGETCURRENTCONTEXT get_context;
210 BOOL local_contexts;
213 static void AL_APIENTRY wrap_DeferUpdates(void)
214 { alcSuspendContext(alcGetCurrentContext()); }
215 static void AL_APIENTRY wrap_ProcessUpdates(void)
216 { alcProcessContext(alcGetCurrentContext()); }
218 static void EnterALSectionTLS(ALCcontext *ctx);
219 static void LeaveALSectionTLS(void);
220 static void EnterALSectionGlob(ALCcontext *ctx);
221 static void LeaveALSectionGlob(void);
223 DWORD TlsThreadPtr;
224 void (*EnterALSection)(ALCcontext *ctx) = EnterALSectionGlob;
225 void (*LeaveALSection)(void) = LeaveALSectionGlob;
228 static BOOL load_libopenal(void)
230 BOOL failed = FALSE;
231 const char *str;
233 str = getenv("DSOAL_LOGLEVEL");
234 if(str && *str)
235 LogLevel = atoi(str);
237 openal_handle = LoadLibraryW(aldriver_name);
238 if(!openal_handle)
240 ERR("Couldn't load %ls: %lu\n", aldriver_name, GetLastError());
241 return FALSE;
244 #define LOAD_FUNCPTR(f) do { \
245 union { void *ptr; FARPROC *proc; } func = { &p##f }; \
246 if((*func.proc = GetProcAddress(openal_handle, #f)) == NULL) \
248 ERR("Couldn't lookup %s in %ls\n", #f, aldriver_name); \
249 failed = TRUE; \
251 } while(0)
253 LOAD_FUNCPTR(alcCreateContext);
254 LOAD_FUNCPTR(alcMakeContextCurrent);
255 LOAD_FUNCPTR(alcProcessContext);
256 LOAD_FUNCPTR(alcSuspendContext);
257 LOAD_FUNCPTR(alcDestroyContext);
258 LOAD_FUNCPTR(alcGetCurrentContext);
259 LOAD_FUNCPTR(alcGetContextsDevice);
260 LOAD_FUNCPTR(alcOpenDevice);
261 LOAD_FUNCPTR(alcCloseDevice);
262 LOAD_FUNCPTR(alcGetError);
263 LOAD_FUNCPTR(alcIsExtensionPresent);
264 LOAD_FUNCPTR(alcGetProcAddress);
265 LOAD_FUNCPTR(alcGetEnumValue);
266 LOAD_FUNCPTR(alcGetString);
267 LOAD_FUNCPTR(alcGetIntegerv);
268 LOAD_FUNCPTR(alcCaptureOpenDevice);
269 LOAD_FUNCPTR(alcCaptureCloseDevice);
270 LOAD_FUNCPTR(alcCaptureStart);
271 LOAD_FUNCPTR(alcCaptureStop);
272 LOAD_FUNCPTR(alcCaptureSamples);
273 LOAD_FUNCPTR(alEnable);
274 LOAD_FUNCPTR(alDisable);
275 LOAD_FUNCPTR(alIsEnabled);
276 LOAD_FUNCPTR(alGetString);
277 LOAD_FUNCPTR(alGetBooleanv);
278 LOAD_FUNCPTR(alGetIntegerv);
279 LOAD_FUNCPTR(alGetFloatv);
280 LOAD_FUNCPTR(alGetDoublev);
281 LOAD_FUNCPTR(alGetBoolean);
282 LOAD_FUNCPTR(alGetInteger);
283 LOAD_FUNCPTR(alGetFloat);
284 LOAD_FUNCPTR(alGetDouble);
285 LOAD_FUNCPTR(alGetError);
286 LOAD_FUNCPTR(alIsExtensionPresent);
287 LOAD_FUNCPTR(alGetProcAddress);
288 LOAD_FUNCPTR(alGetEnumValue);
289 LOAD_FUNCPTR(alListenerf);
290 LOAD_FUNCPTR(alListener3f);
291 LOAD_FUNCPTR(alListenerfv);
292 LOAD_FUNCPTR(alListeneri);
293 LOAD_FUNCPTR(alListener3i);
294 LOAD_FUNCPTR(alListeneriv);
295 LOAD_FUNCPTR(alGetListenerf);
296 LOAD_FUNCPTR(alGetListener3f);
297 LOAD_FUNCPTR(alGetListenerfv);
298 LOAD_FUNCPTR(alGetListeneri);
299 LOAD_FUNCPTR(alGetListener3i);
300 LOAD_FUNCPTR(alGetListeneriv);
301 LOAD_FUNCPTR(alGenSources);
302 LOAD_FUNCPTR(alDeleteSources);
303 LOAD_FUNCPTR(alIsSource);
304 LOAD_FUNCPTR(alSourcef);
305 LOAD_FUNCPTR(alSource3f);
306 LOAD_FUNCPTR(alSourcefv);
307 LOAD_FUNCPTR(alSourcei);
308 LOAD_FUNCPTR(alSource3i);
309 LOAD_FUNCPTR(alSourceiv);
310 LOAD_FUNCPTR(alGetSourcef);
311 LOAD_FUNCPTR(alGetSource3f);
312 LOAD_FUNCPTR(alGetSourcefv);
313 LOAD_FUNCPTR(alGetSourcei);
314 LOAD_FUNCPTR(alGetSource3i);
315 LOAD_FUNCPTR(alGetSourceiv);
316 LOAD_FUNCPTR(alSourcePlayv);
317 LOAD_FUNCPTR(alSourceStopv);
318 LOAD_FUNCPTR(alSourceRewindv);
319 LOAD_FUNCPTR(alSourcePausev);
320 LOAD_FUNCPTR(alSourcePlay);
321 LOAD_FUNCPTR(alSourceStop);
322 LOAD_FUNCPTR(alSourceRewind);
323 LOAD_FUNCPTR(alSourcePause);
324 LOAD_FUNCPTR(alSourceQueueBuffers);
325 LOAD_FUNCPTR(alSourceUnqueueBuffers);
326 LOAD_FUNCPTR(alGenBuffers);
327 LOAD_FUNCPTR(alDeleteBuffers);
328 LOAD_FUNCPTR(alIsBuffer);
329 LOAD_FUNCPTR(alBufferf);
330 LOAD_FUNCPTR(alBuffer3f);
331 LOAD_FUNCPTR(alBufferfv);
332 LOAD_FUNCPTR(alBufferi);
333 LOAD_FUNCPTR(alBuffer3i);
334 LOAD_FUNCPTR(alBufferiv);
335 LOAD_FUNCPTR(alGetBufferf);
336 LOAD_FUNCPTR(alGetBuffer3f);
337 LOAD_FUNCPTR(alGetBufferfv);
338 LOAD_FUNCPTR(alGetBufferi);
339 LOAD_FUNCPTR(alGetBuffer3i);
340 LOAD_FUNCPTR(alGetBufferiv);
341 LOAD_FUNCPTR(alBufferData);
342 LOAD_FUNCPTR(alDopplerFactor);
343 LOAD_FUNCPTR(alDopplerVelocity);
344 LOAD_FUNCPTR(alDistanceModel);
345 LOAD_FUNCPTR(alSpeedOfSound);
346 #undef LOAD_FUNCPTR
347 if (failed)
349 WARN("Unloading %ls\n", aldriver_name);
350 if (openal_handle != NULL)
351 FreeLibrary(openal_handle);
352 openal_handle = NULL;
353 return FALSE;
356 openal_loaded = 1;
357 TRACE("Loaded %ls\n", aldriver_name);
359 #define LOAD_FUNCPTR(f) p##f = alcGetProcAddress(NULL, #f)
360 LOAD_FUNCPTR(EAXSet);
361 LOAD_FUNCPTR(EAXGet);
362 LOAD_FUNCPTR(alDeferUpdatesSOFT);
363 LOAD_FUNCPTR(alProcessUpdatesSOFT);
364 LOAD_FUNCPTR(alBufferStorageSOFT);
365 LOAD_FUNCPTR(alMapBufferSOFT);
366 LOAD_FUNCPTR(alUnmapBufferSOFT);
367 LOAD_FUNCPTR(alFlushMappedBufferSOFT);
368 #undef LOAD_FUNCPTR
369 if(!palDeferUpdatesSOFT || !palProcessUpdatesSOFT)
371 palDeferUpdatesSOFT = wrap_DeferUpdates;
372 palProcessUpdatesSOFT = wrap_ProcessUpdates;
375 local_contexts = alcIsExtensionPresent(NULL, "ALC_EXT_thread_local_context");
376 if(local_contexts)
378 TRACE("Found ALC_EXT_thread_local_context\n");
380 set_context = alcGetProcAddress(NULL, "alcSetThreadContext");
381 get_context = alcGetProcAddress(NULL, "alcGetThreadContext");
382 if(!set_context || !get_context)
384 ERR("TLS advertised but functions not found, disabling thread local contexts\n");
385 local_contexts = 0;
388 if(!local_contexts)
390 set_context = alcMakeContextCurrent;
391 get_context = alcGetCurrentContext;
393 else
395 EnterALSection = EnterALSectionTLS;
396 LeaveALSection = LeaveALSectionTLS;
399 return TRUE;
403 static void EnterALSectionTLS(ALCcontext *ctx)
405 if(LIKELY(ctx == TlsGetValue(TlsThreadPtr)))
406 return;
408 if(LIKELY(set_context(ctx) != ALC_FALSE))
409 TlsSetValue(TlsThreadPtr, ctx);
410 else
412 ERR("Couldn't set current context!!\n");
413 checkALCError(alcGetContextsDevice(ctx));
416 static void LeaveALSectionTLS(void)
420 static void EnterALSectionGlob(ALCcontext *ctx)
422 EnterCriticalSection(&openal_crst);
423 if(UNLIKELY(alcMakeContextCurrent(ctx) == ALC_FALSE))
425 ERR("Couldn't set current context!!\n");
426 checkALCError(alcGetContextsDevice(ctx));
429 static void LeaveALSectionGlob(void)
431 LeaveCriticalSection(&openal_crst);
435 static const char *get_device_id(LPCGUID pGuid)
437 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuid))
438 return "DSDEVID_DefaultPlayback";
439 if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuid))
440 return "DSDEVID_DefaultVoicePlayback";
441 if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuid))
442 return "DSDEVID_DefaultCapture";
443 if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuid))
444 return "DSDEVID_DefaultVoiceCapture";
445 return debugstr_guid(pGuid);
448 #ifdef _MSC_VER
449 const CLSID CLSID_MMDeviceEnumerator = {
450 0xBCDE0395,
451 0xE52F, 0x467C,
452 { 0x8E, 0x3D, 0xC4, 0x57, 0x92, 0x91, 0x69, 0x2E }
455 const IID IID_IMMDeviceEnumerator = {
456 0xA95664D2,
457 0x9614, 0x4F35,
458 { 0xA7, 0x46, 0xDE, 0x8D, 0xB6, 0x36, 0x17, 0xE6 }
460 #endif
462 static HRESULT get_mmdevenum(IMMDeviceEnumerator **devenum)
464 HRESULT hr, init_hr;
466 init_hr = CoInitialize(NULL);
468 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
469 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)devenum);
470 if(FAILED(hr))
472 if(SUCCEEDED(init_hr))
473 CoUninitialize();
474 *devenum = NULL;
475 ERR("CoCreateInstance failed: %08lx\n", hr);
476 return hr;
479 return init_hr;
482 static void release_mmdevenum(IMMDeviceEnumerator *devenum, HRESULT init_hr)
484 IMMDeviceEnumerator_Release(devenum);
485 if(SUCCEEDED(init_hr))
486 CoUninitialize();
489 static HRESULT get_mmdevice_guid(IMMDevice *device, IPropertyStore *ps, GUID *guid)
491 PROPVARIANT pv;
492 HRESULT hr;
494 if(ps)
495 IPropertyStore_AddRef(ps);
496 else
498 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
499 if(FAILED(hr))
501 WARN("OpenPropertyStore failed: %08lx\n", hr);
502 return hr;
506 PropVariantInit(&pv);
508 hr = IPropertyStore_GetValue(ps, &PKEY_AudioEndpoint_GUID, &pv);
509 if(FAILED(hr))
511 IPropertyStore_Release(ps);
512 WARN("GetValue(GUID) failed: %08lx\n", hr);
513 return hr;
516 CLSIDFromString(pv.pwszVal, guid);
518 PropVariantClear(&pv);
519 IPropertyStore_Release(ps);
521 return S_OK;
525 static BOOL send_device(IMMDevice *device, EDataFlow flow, DeviceList *devlist, PRVTENUMCALLBACK cb, void *user)
527 IPropertyStore *ps;
528 PROPVARIANT pv;
529 BOOL keep_going;
530 size_t dev_count;
531 HRESULT hr;
532 GUID guid;
534 PropVariantInit(&pv);
536 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
537 if(FAILED(hr))
539 WARN("OpenPropertyStore failed: %08lx\n", hr);
540 return TRUE;
543 hr = get_mmdevice_guid(device, ps, &guid);
544 if(FAILED(hr) || (devlist->Count > 0 && IsEqualGUID(&devlist->Guids[0], &guid)))
546 IPropertyStore_Release(ps);
547 return TRUE;
550 hr = IPropertyStore_GetValue(ps, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
551 if(FAILED(hr))
553 IPropertyStore_Release(ps);
554 WARN("GetValue(FriendlyName) failed: %08lx\n", hr);
555 return TRUE;
558 dev_count = devlist->Count++;
559 devlist->Guids[dev_count] = guid;
561 keep_going = FALSE;
562 if(cb)
564 TRACE("Calling back with %s - %ls\n", debugstr_guid(&devlist->Guids[dev_count]),
565 pv.pwszVal);
566 keep_going = cb(flow, &devlist->Guids[dev_count], pv.pwszVal, aldriver_name, user);
569 PropVariantClear(&pv);
570 IPropertyStore_Release(ps);
572 return keep_going;
575 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device)
577 IMMDeviceEnumerator *devenum;
578 IMMDeviceCollection *coll;
579 UINT count, i;
580 HRESULT hr, init_hr;
582 *device = NULL;
584 init_hr = get_mmdevenum(&devenum);
585 if(!devenum) return init_hr;
587 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow, DEVICE_STATE_ACTIVE, &coll);
588 if(FAILED(hr))
590 WARN("EnumAudioEndpoints failed: %08lx\n", hr);
591 release_mmdevenum(devenum, init_hr);
592 return hr;
595 hr = IMMDeviceCollection_GetCount(coll, &count);
596 if(FAILED(hr))
598 IMMDeviceCollection_Release(coll);
599 release_mmdevenum(devenum, init_hr);
600 WARN("GetCount failed: %08lx\n", hr);
601 return hr;
604 for(i = 0; i < count;++i)
606 GUID guid;
608 hr = IMMDeviceCollection_Item(coll, i, device);
609 if(FAILED(hr)) continue;
611 hr = get_mmdevice_guid(*device, NULL, &guid);
612 if(SUCCEEDED(hr) && IsEqualGUID(&guid, tgt))
614 IMMDeviceCollection_Release(coll);
615 IMMDeviceEnumerator_Release(devenum);
616 return init_hr;
619 IMMDevice_Release(*device);
620 *device = NULL;
623 WARN("No device with GUID %s found!\n", debugstr_guid(tgt));
625 IMMDeviceCollection_Release(coll);
626 release_mmdevenum(devenum, init_hr);
628 return DSERR_INVALIDPARAM;
631 void release_mmdevice(IMMDevice *device, HRESULT init_hr)
633 IMMDevice_Release(device);
634 if(SUCCEEDED(init_hr))
635 CoUninitialize();
638 /* S_FALSE means the callback returned FALSE at some point
639 * S_OK means the callback always returned TRUE */
640 HRESULT enumerate_mmdevices(EDataFlow flow, PRVTENUMCALLBACK cb, void *user)
642 static const WCHAR primary_desc[] = L"Primary Sound Driver";
644 IMMDeviceEnumerator *devenum;
645 IMMDeviceCollection *coll;
646 IMMDevice *device;
647 DeviceList *devlist;
648 UINT count, i;
649 BOOL keep_going;
650 HRESULT hr, init_hr;
652 init_hr = get_mmdevenum(&devenum);
653 if(!devenum) return init_hr;
655 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow, DEVICE_STATE_ACTIVE, &coll);
656 if(FAILED(hr))
658 release_mmdevenum(devenum, init_hr);
659 WARN("EnumAudioEndpoints failed: %08lx\n", hr);
660 return DS_OK;
663 hr = IMMDeviceCollection_GetCount(coll, &count);
664 if(FAILED(hr))
666 IMMDeviceCollection_Release(coll);
667 release_mmdevenum(devenum, init_hr);
668 WARN("GetCount failed: %08lx\n", hr);
669 return DS_OK;
672 if(count == 0)
674 IMMDeviceCollection_Release(coll);
675 release_mmdevenum(devenum, init_hr);
676 return DS_OK;
679 devlist = (flow==eCapture) ? &CaptureDevices : &PlaybackDevices;
681 HeapFree(GetProcessHeap(), 0, devlist->Guids);
682 devlist->Count = 0;
683 devlist->Guids = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
684 sizeof(devlist->Guids[0])*count);
686 TRACE("Calling back with NULL (%ls)\n", primary_desc);
687 keep_going = cb(flow, NULL, primary_desc, L"", user);
689 /* always send the default device first */
690 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow, eMultimedia, &device);
691 if(SUCCEEDED(hr))
693 if(!keep_going) cb = NULL;
694 keep_going = send_device(device, flow, devlist, cb, user);
695 IMMDevice_Release(device);
698 for(i = 0;i < count;++i)
700 if(!keep_going)
701 cb = NULL;
703 hr = IMMDeviceCollection_Item(coll, i, &device);
704 if(FAILED(hr))
706 WARN("Item failed: %08lx\n", hr);
707 continue;
710 keep_going = send_device(device, flow, devlist, cb, user);
712 IMMDevice_Release(device);
714 IMMDeviceCollection_Release(coll);
716 release_mmdevenum(devenum, init_hr);
718 return keep_going ? S_OK : S_FALSE;
721 /***************************************************************************
722 * GetDeviceID [DSOUND.9]
724 * Retrieves unique identifier of default device specified
726 * PARAMS
727 * pGuidSrc [I] Address of device GUID.
728 * pGuidDest [O] Address to receive unique device GUID.
730 * RETURNS
731 * Success: DS_OK
732 * Failure: DSERR_INVALIDPARAM
734 * NOTES
735 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
736 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
737 * DSDEVID_DefaultVoiceCapture.
738 * Returns pGuidSrc if pGuidSrc is a valid device or the device
739 * GUID for the specified constants.
741 HRESULT WINAPI DSOAL_GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
743 IMMDeviceEnumerator *devenum;
744 HRESULT hr, init_hr;
745 IMMDevice *device;
746 EDataFlow flow;
747 ERole role;
749 TRACE("(%s, %p)\n", get_device_id(pGuidSrc), pGuidDest);
751 if(!pGuidSrc || !pGuidDest)
752 return DSERR_INVALIDPARAM;
754 flow = eRender;
755 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuidSrc))
756 role = eMultimedia;
757 else if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuidSrc))
758 role = eCommunications;
759 else
761 flow = eCapture;
762 if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuidSrc))
763 role = eMultimedia;
764 else if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuidSrc))
765 role = eCommunications;
766 else
768 *pGuidDest = *pGuidSrc;
769 return DS_OK;
773 init_hr = get_mmdevenum(&devenum);
774 if(!devenum) return init_hr;
776 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow, role, &device);
777 if(FAILED(hr))
779 WARN("GetDefaultAudioEndpoint failed: %08lx\n", hr);
780 release_mmdevenum(devenum, init_hr);
781 return DSERR_NODRIVER;
784 hr = get_mmdevice_guid(device, NULL, pGuidDest);
785 IMMDevice_Release(device);
787 release_mmdevenum(devenum, init_hr);
789 return hr;
793 struct morecontextW {
794 LPDSENUMCALLBACKW callW;
795 LPVOID data;
798 static BOOL CALLBACK w_callback(EDataFlow flow, LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
800 struct morecontextW *context = data;
801 (void)flow;
803 return context->callW(guid, descW, modW, context->data);
806 struct morecontextA {
807 LPDSENUMCALLBACKA callA;
808 LPVOID data;
811 static BOOL CALLBACK w_to_a_callback(EDataFlow flow, LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
813 struct morecontextA *context = data;
814 char *descA, *modA;
815 int dlen, mlen;
816 BOOL ret;
817 (void)flow;
819 dlen = WideCharToMultiByte(CP_ACP, 0, descW, -1, NULL, 0, NULL, NULL);
820 mlen = WideCharToMultiByte(CP_ACP, 0, modW, -1, NULL, 0, NULL, NULL);
821 if(dlen < 0 || mlen < 0) return FALSE;
823 descA = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dlen+mlen+2);
824 if(!descA) return FALSE;
825 modA = descA + dlen+1;
827 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, dlen, NULL, NULL);
828 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, mlen, NULL, NULL);
830 ret = context->callA(guid, descA, modA, context->data);
832 HeapFree(GetProcessHeap(), 0, descA);
833 return ret;
836 /***************************************************************************
837 * DirectSoundEnumerateA [DSOUND.2]
839 * Enumerate all DirectSound drivers installed in the system
841 * PARAMS
842 * lpDSEnumCallback [I] Address of callback function.
843 * lpContext [I] Address of user defined context passed to callback function.
845 * RETURNS
846 * Success: DS_OK
847 * Failure: DSERR_INVALIDPARAM
849 HRESULT WINAPI DSOAL_DirectSoundEnumerateA(
850 LPDSENUMCALLBACKA lpDSEnumCallback,
851 LPVOID lpContext)
853 struct morecontextA ctx;
854 HRESULT hr;
856 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
858 if(lpDSEnumCallback == NULL)
860 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
861 return DSERR_INVALIDPARAM;
864 ctx.callA = lpDSEnumCallback;
865 ctx.data = lpContext;
867 hr = enumerate_mmdevices(eRender, w_to_a_callback, &ctx);
868 return SUCCEEDED(hr) ? DS_OK : hr;
872 /***************************************************************************
873 * DirectSoundEnumerateW [DSOUND.3]
875 * Enumerate all DirectSound drivers installed in the system
877 * PARAMS
878 * lpDSEnumCallback [I] Address of callback function.
879 * lpContext [I] Address of user defined context passed to callback function.
881 * RETURNS
882 * Success: DS_OK
883 * Failure: DSERR_INVALIDPARAM
885 HRESULT WINAPI DSOAL_DirectSoundEnumerateW(
886 LPDSENUMCALLBACKW lpDSEnumCallback,
887 LPVOID lpContext )
889 struct morecontextW ctx;
890 HRESULT hr;
892 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
894 if(lpDSEnumCallback == NULL)
896 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
897 return DSERR_INVALIDPARAM;
900 ctx.callW = lpDSEnumCallback;
901 ctx.data = lpContext;
903 hr = enumerate_mmdevices(eRender, w_callback, &ctx);
904 return SUCCEEDED(hr) ? DS_OK : hr;
907 /***************************************************************************
908 * DirectSoundCaptureEnumerateA [DSOUND.7]
910 * Enumerate all DirectSound drivers installed in the system.
912 * PARAMS
913 * lpDSEnumCallback [I] Address of callback function.
914 * lpContext [I] Address of user defined context passed to callback function.
916 * RETURNS
917 * Success: DS_OK
918 * Failure: DSERR_INVALIDPARAM
920 HRESULT WINAPI DSOAL_DirectSoundCaptureEnumerateA(
921 LPDSENUMCALLBACKA lpDSEnumCallback,
922 LPVOID lpContext)
924 struct morecontextA ctx;
925 HRESULT hr;
927 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
929 if(lpDSEnumCallback == NULL)
931 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
932 return DSERR_INVALIDPARAM;
935 ctx.callA = lpDSEnumCallback;
936 ctx.data = lpContext;
938 hr = enumerate_mmdevices(eCapture, w_to_a_callback, &ctx);
939 return SUCCEEDED(hr) ? DS_OK : hr;
942 /***************************************************************************
943 * DirectSoundCaptureEnumerateW [DSOUND.8]
945 * Enumerate all DirectSound drivers installed in the system.
947 * PARAMS
948 * lpDSEnumCallback [I] Address of callback function.
949 * lpContext [I] Address of user defined context passed to callback function.
951 * RETURNS
952 * Success: DS_OK
953 * Failure: DSERR_INVALIDPARAM
955 HRESULT WINAPI DSOAL_DirectSoundCaptureEnumerateW(
956 LPDSENUMCALLBACKW lpDSEnumCallback,
957 LPVOID lpContext)
959 struct morecontextW ctx;
960 HRESULT hr;
962 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
964 if(lpDSEnumCallback == NULL)
966 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
967 return DSERR_INVALIDPARAM;
970 ctx.callW = lpDSEnumCallback;
971 ctx.data = lpContext;
973 hr = enumerate_mmdevices(eCapture, w_callback, &ctx);
974 return SUCCEEDED(hr) ? DS_OK : hr;
977 /*******************************************************************************
978 * DirectSoundCreate (DSOUND.1)
980 * Creates and initializes a DirectSound interface.
982 * PARAMS
983 * lpcGUID [I] Address of the GUID that identifies the sound device.
984 * ppDS [O] Address of a variable to receive the interface pointer.
985 * pUnkOuter [I] Must be NULL.
987 * RETURNS
988 * Success: DS_OK
989 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
990 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
992 HRESULT WINAPI
993 DSOAL_DirectSoundCreate(LPCGUID lpcGUID, IDirectSound **ppDS, IUnknown *pUnkOuter)
995 HRESULT hr;
996 void *pDS;
998 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
1000 if (ppDS == NULL) {
1001 WARN("invalid parameter: ppDS == NULL\n");
1002 return DSERR_INVALIDPARAM;
1004 *ppDS = NULL;
1006 if (pUnkOuter != NULL) {
1007 WARN("invalid parameter: pUnkOuter != NULL\n");
1008 return DSERR_INVALIDPARAM;
1011 hr = DSOUND_Create(&IID_IDirectSound, &pDS);
1012 if(SUCCEEDED(hr))
1014 *ppDS = pDS;
1015 hr = IDirectSound_Initialize(*ppDS, lpcGUID);
1016 if(FAILED(hr))
1018 IDirectSound_Release(*ppDS);
1019 *ppDS = NULL;
1023 return hr;
1026 /*******************************************************************************
1027 * DirectSoundCreate8 (DSOUND.11)
1029 * Creates and initializes a DirectSound8 interface.
1031 * PARAMS
1032 * lpcGUID [I] Address of the GUID that identifies the sound device.
1033 * ppDS [O] Address of a variable to receive the interface pointer.
1034 * pUnkOuter [I] Must be NULL.
1036 * RETURNS
1037 * Success: DS_OK
1038 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
1039 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
1041 HRESULT WINAPI
1042 DSOAL_DirectSoundCreate8(LPCGUID lpcGUID, IDirectSound8 **ppDS, IUnknown *pUnkOuter)
1044 HRESULT hr;
1045 void *pDS;
1047 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
1049 if (ppDS == NULL) {
1050 WARN("invalid parameter: ppDS == NULL\n");
1051 return DSERR_INVALIDPARAM;
1053 *ppDS = NULL;
1055 if (pUnkOuter != NULL) {
1056 WARN("invalid parameter: pUnkOuter != NULL\n");
1057 return DSERR_INVALIDPARAM;
1060 hr = DSOUND_Create8(&IID_IDirectSound8, &pDS);
1061 if(SUCCEEDED(hr))
1063 *ppDS = pDS;
1064 hr = IDirectSound8_Initialize(*ppDS, lpcGUID);
1065 if(FAILED(hr))
1067 IDirectSound8_Release(*ppDS);
1068 *ppDS = NULL;
1072 return hr;
1075 /***************************************************************************
1076 * DirectSoundCaptureCreate [DSOUND.6]
1078 * Create and initialize a DirectSoundCapture interface.
1080 * PARAMS
1081 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1082 * lplpDSC [O] Address of a variable to receive the interface pointer.
1083 * pUnkOuter [I] Must be NULL.
1085 * RETURNS
1086 * Success: DS_OK
1087 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1088 * DSERR_OUTOFMEMORY
1090 * NOTES
1091 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1092 * or NULL for the default device or DSDEVID_DefaultCapture or
1093 * DSDEVID_DefaultVoiceCapture.
1095 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1097 HRESULT WINAPI
1098 DSOAL_DirectSoundCaptureCreate(LPCGUID lpcGUID, IDirectSoundCapture **ppDSC, IUnknown *pUnkOuter)
1100 HRESULT hr;
1101 void *pDSC;
1103 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
1105 if(pUnkOuter)
1107 WARN("invalid parameter: pUnkOuter != NULL\n");
1108 return DSERR_NOAGGREGATION;
1111 if(ppDSC == NULL)
1113 WARN("invalid parameter: ppDSC == NULL\n");
1114 return DSERR_INVALIDPARAM;
1116 *ppDSC = NULL;
1118 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, &pDSC);
1119 if(SUCCEEDED(hr))
1121 *ppDSC = pDSC;
1122 hr = IDirectSoundCapture_Initialize(*ppDSC, lpcGUID);
1123 if(FAILED(hr))
1125 IDirectSoundCapture_Release(*ppDSC);
1126 *ppDSC = NULL;
1130 return hr;
1133 /***************************************************************************
1134 * DirectSoundCaptureCreate8 [DSOUND.12]
1136 * Create and initialize a DirectSoundCapture interface.
1138 * PARAMS
1139 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1140 * lplpDSC [O] Address of a variable to receive the interface pointer.
1141 * pUnkOuter [I] Must be NULL.
1143 * RETURNS
1144 * Success: DS_OK
1145 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1146 * DSERR_OUTOFMEMORY
1148 * NOTES
1149 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1150 * or NULL for the default device or DSDEVID_DefaultCapture or
1151 * DSDEVID_DefaultVoiceCapture.
1153 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1155 HRESULT WINAPI
1156 DSOAL_DirectSoundCaptureCreate8(LPCGUID lpcGUID, IDirectSoundCapture8 **ppDSC8, IUnknown *pUnkOuter)
1158 HRESULT hr;
1159 void *pDSC8;
1161 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
1163 if(pUnkOuter)
1165 WARN("invalid parameter: pUnkOuter != NULL\n");
1166 return DSERR_NOAGGREGATION;
1169 if(ppDSC8 == NULL)
1171 WARN("invalid parameter: ppDSC8 == NULL\n");
1172 return DSERR_INVALIDPARAM;
1174 *ppDSC8 = NULL;
1176 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture, &pDSC8);
1177 if(SUCCEEDED(hr))
1179 *ppDSC8 = pDSC8;
1180 hr = IDirectSoundCapture_Initialize(*ppDSC8, lpcGUID);
1181 if(FAILED(hr))
1183 IDirectSoundCapture_Release(*ppDSC8);
1184 *ppDSC8 = NULL;
1188 return hr;
1191 /*******************************************************************************
1192 * DirectSound ClassFactory
1195 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
1197 typedef struct {
1198 IClassFactory IClassFactory_iface;
1199 LONG ref;
1200 REFCLSID rclsid;
1201 FnCreateInstance pfnCreateInstance;
1202 } IClassFactoryImpl;
1204 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
1206 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
1209 static HRESULT WINAPI DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
1211 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1212 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
1213 if (ppobj == NULL)
1214 return E_POINTER;
1215 if (IsEqualIID(riid, &IID_IUnknown) ||
1216 IsEqualIID(riid, &IID_IClassFactory))
1218 *ppobj = iface;
1219 IUnknown_AddRef(iface);
1220 return S_OK;
1222 *ppobj = NULL;
1223 return E_NOINTERFACE;
1226 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
1228 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1229 ULONG ref = InterlockedIncrement(&(This->ref));
1230 TRACE("(%p) ref %lu\n", iface, ref);
1231 return ref;
1234 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
1236 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1237 ULONG ref = InterlockedDecrement(&(This->ref));
1238 TRACE("(%p) ref %lu\n", iface, ref);
1239 /* static class, won't be freed */
1240 return ref;
1243 static HRESULT WINAPI DSCF_CreateInstance(
1244 LPCLASSFACTORY iface,
1245 LPUNKNOWN pOuter,
1246 REFIID riid,
1247 LPVOID *ppobj)
1249 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1250 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
1252 if (pOuter)
1253 return CLASS_E_NOAGGREGATION;
1255 if (ppobj == NULL) {
1256 WARN("invalid parameter\n");
1257 return DSERR_INVALIDPARAM;
1259 *ppobj = NULL;
1260 return This->pfnCreateInstance(riid, ppobj);
1263 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
1265 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1266 FIXME("(%p, %d) stub!\n", This, dolock);
1267 return S_OK;
1270 static const IClassFactoryVtbl DSCF_Vtbl = {
1271 DSCF_QueryInterface,
1272 DSCF_AddRef,
1273 DSCF_Release,
1274 DSCF_CreateInstance,
1275 DSCF_LockServer
1278 static IClassFactoryImpl DSOUND_CF[] = {
1279 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound, DSOUND_Create },
1280 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound8, DSOUND_Create8 },
1281 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
1282 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
1283 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
1284 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
1285 { {NULL}, 0, NULL, NULL }
1288 /*******************************************************************************
1289 * DllGetClassObject [DSOUND.@]
1290 * Retrieves class object from a DLL object
1292 * NOTES
1293 * Docs say returns STDAPI
1295 * PARAMS
1296 * rclsid [I] CLSID for the class object
1297 * riid [I] Reference to identifier of interface for class object
1298 * ppv [O] Address of variable to receive interface pointer for riid
1300 * RETURNS
1301 * Success: S_OK
1302 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
1303 * E_UNEXPECTED
1305 HRESULT WINAPI DSOAL_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1307 int i = 0;
1308 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1310 if (ppv == NULL) {
1311 WARN("invalid parameter\n");
1312 return E_INVALIDARG;
1315 *ppv = NULL;
1317 if (!IsEqualIID(riid, &IID_IClassFactory) &&
1318 !IsEqualIID(riid, &IID_IUnknown)) {
1319 WARN("no interface for %s\n", debugstr_guid(riid));
1320 return E_NOINTERFACE;
1323 while (NULL != DSOUND_CF[i].rclsid) {
1324 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
1325 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
1326 *ppv = &DSOUND_CF[i].IClassFactory_iface;
1327 return S_OK;
1329 i++;
1332 WARN("No class found for %s\n", debugstr_guid(rclsid));
1333 return CLASS_E_CLASSNOTAVAILABLE;
1337 /*******************************************************************************
1338 * DllCanUnloadNow [DSOUND.4]
1339 * Determines whether the DLL is in use.
1341 * RETURNS
1342 * Success: S_OK
1343 * Failure: S_FALSE
1345 HRESULT WINAPI DSOAL_DllCanUnloadNow(void)
1347 FIXME("(void): stub\n");
1348 return S_FALSE;
1351 /***********************************************************************
1352 * DllMain (DSOUND.init)
1354 DECLSPEC_EXPORT BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
1356 const WCHAR *wstr;
1358 TRACE("(%p, %lu, %p)\n", hInstDLL, fdwReason, lpvReserved);
1360 switch(fdwReason)
1362 case DLL_PROCESS_ATTACH:
1363 LogFile = stderr;
1364 if((wstr=_wgetenv(L"DSOAL_LOGFILE")) != NULL && wstr[0] != 0)
1366 FILE *f = _wfopen(wstr, L"wt");
1367 if(!f) ERR("Failed to open log file %ls\n", wstr);
1368 else LogFile = f;
1371 if(!load_libopenal())
1372 return FALSE;
1373 TlsThreadPtr = TlsAlloc();
1374 InitializeCriticalSection(&openal_crst);
1375 /* Increase refcount on dsound by 1 */
1376 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
1377 break;
1379 case DLL_THREAD_ATTACH:
1380 break;
1382 case DLL_THREAD_DETACH:
1383 break;
1385 case DLL_PROCESS_DETACH:
1386 HeapFree(GetProcessHeap(), 0, PlaybackDevices.Guids);
1387 PlaybackDevices.Guids = NULL;
1388 PlaybackDevices.Count = 0;
1389 HeapFree(GetProcessHeap(), 0, CaptureDevices.Guids);
1390 CaptureDevices.Guids = NULL;
1391 CaptureDevices.Count = 0;
1393 if(openal_handle)
1394 FreeLibrary(openal_handle);
1395 TlsFree(TlsThreadPtr);
1396 DeleteCriticalSection(&openal_crst);
1397 if(LogFile != stderr)
1398 fclose(LogFile);
1399 LogFile = stderr;
1400 break;
1402 return TRUE;
1405 #ifdef __WINESRC__
1406 /***********************************************************************
1407 * DllRegisterServer (DSOUND.@)
1409 HRESULT WINAPI DllRegisterServer(void)
1411 return __wine_register_resources(instance);
1414 /***********************************************************************
1415 * DllUnregisterServer (DSOUND.@)
1417 HRESULT WINAPI DllUnregisterServer(void)
1419 return __wine_unregister_resources(instance);
1421 #endif