Merge pull request #2 from Mechami/master
[dsound-openal.git] / dsound_main.c
blob4edd3c012c281599a052c6e4472733d7b77a2964
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 static CRITICAL_SECTION_DEBUG openal_crst_debug =
103 0, 0, &openal_crst,
104 { &openal_crst_debug.ProcessLocksList,
105 &openal_crst_debug.ProcessLocksList },
106 0, 0, { (DWORD_PTR)(__FILE__ ": openal_crst_debug") }
108 CRITICAL_SECTION openal_crst = { &openal_crst_debug, -1, 0, 0, 0, 0 };
110 int openal_loaded = 0;
111 static HANDLE openal_handle = NULL;
112 LPALCCREATECONTEXT palcCreateContext = NULL;
113 LPALCMAKECONTEXTCURRENT palcMakeContextCurrent = NULL;
114 LPALCPROCESSCONTEXT palcProcessContext = NULL;
115 LPALCSUSPENDCONTEXT palcSuspendContext = NULL;
116 LPALCDESTROYCONTEXT palcDestroyContext = NULL;
117 LPALCGETCURRENTCONTEXT palcGetCurrentContext = NULL;
118 LPALCGETCONTEXTSDEVICE palcGetContextsDevice = NULL;
119 LPALCOPENDEVICE palcOpenDevice = NULL;
120 LPALCCLOSEDEVICE palcCloseDevice = NULL;
121 LPALCGETERROR palcGetError = NULL;
122 LPALCISEXTENSIONPRESENT palcIsExtensionPresent = NULL;
123 LPALCGETPROCADDRESS palcGetProcAddress = NULL;
124 LPALCGETENUMVALUE palcGetEnumValue = NULL;
125 LPALCGETSTRING palcGetString = NULL;
126 LPALCGETINTEGERV palcGetIntegerv = NULL;
127 LPALCCAPTUREOPENDEVICE palcCaptureOpenDevice = NULL;
128 LPALCCAPTURECLOSEDEVICE palcCaptureCloseDevice = NULL;
129 LPALCCAPTURESTART palcCaptureStart = NULL;
130 LPALCCAPTURESTOP palcCaptureStop = NULL;
131 LPALCCAPTURESAMPLES palcCaptureSamples = NULL;
132 LPALENABLE palEnable = NULL;
133 LPALDISABLE palDisable = NULL;
134 LPALISENABLED palIsEnabled = NULL;
135 LPALGETSTRING palGetString = NULL;
136 LPALGETBOOLEANV palGetBooleanv = NULL;
137 LPALGETINTEGERV palGetIntegerv = NULL;
138 LPALGETFLOATV palGetFloatv = NULL;
139 LPALGETDOUBLEV palGetDoublev = NULL;
140 LPALGETBOOLEAN palGetBoolean = NULL;
141 LPALGETINTEGER palGetInteger = NULL;
142 LPALGETFLOAT palGetFloat = NULL;
143 LPALGETDOUBLE palGetDouble = NULL;
144 LPALGETERROR palGetError = NULL;
145 LPALISEXTENSIONPRESENT palIsExtensionPresent = NULL;
146 LPALGETPROCADDRESS palGetProcAddress = NULL;
147 LPALGETENUMVALUE palGetEnumValue = NULL;
148 LPALLISTENERF palListenerf = NULL;
149 LPALLISTENER3F palListener3f = NULL;
150 LPALLISTENERFV palListenerfv = NULL;
151 LPALLISTENERI palListeneri = NULL;
152 LPALLISTENER3I palListener3i = NULL;
153 LPALLISTENERIV palListeneriv = NULL;
154 LPALGETLISTENERF palGetListenerf = NULL;
155 LPALGETLISTENER3F palGetListener3f = NULL;
156 LPALGETLISTENERFV palGetListenerfv = NULL;
157 LPALGETLISTENERI palGetListeneri = NULL;
158 LPALGETLISTENER3I palGetListener3i = NULL;
159 LPALGETLISTENERIV palGetListeneriv = NULL;
160 LPALGENSOURCES palGenSources = NULL;
161 LPALDELETESOURCES palDeleteSources = NULL;
162 LPALISSOURCE palIsSource = NULL;
163 LPALSOURCEF palSourcef = NULL;
164 LPALSOURCE3F palSource3f = NULL;
165 LPALSOURCEFV palSourcefv = NULL;
166 LPALSOURCEI palSourcei = NULL;
167 LPALSOURCE3I palSource3i = NULL;
168 LPALSOURCEIV palSourceiv = NULL;
169 LPALGETSOURCEF palGetSourcef = NULL;
170 LPALGETSOURCE3F palGetSource3f = NULL;
171 LPALGETSOURCEFV palGetSourcefv = NULL;
172 LPALGETSOURCEI palGetSourcei = NULL;
173 LPALGETSOURCE3I palGetSource3i = NULL;
174 LPALGETSOURCEIV palGetSourceiv = NULL;
175 LPALSOURCEPLAYV palSourcePlayv = NULL;
176 LPALSOURCESTOPV palSourceStopv = NULL;
177 LPALSOURCEREWINDV palSourceRewindv = NULL;
178 LPALSOURCEPAUSEV palSourcePausev = NULL;
179 LPALSOURCEPLAY palSourcePlay = NULL;
180 LPALSOURCESTOP palSourceStop = NULL;
181 LPALSOURCEREWIND palSourceRewind = NULL;
182 LPALSOURCEPAUSE palSourcePause = NULL;
183 LPALSOURCEQUEUEBUFFERS palSourceQueueBuffers = NULL;
184 LPALSOURCEUNQUEUEBUFFERS palSourceUnqueueBuffers = NULL;
185 LPALGENBUFFERS palGenBuffers = NULL;
186 LPALDELETEBUFFERS palDeleteBuffers = NULL;
187 LPALISBUFFER palIsBuffer = NULL;
188 LPALBUFFERF palBufferf = NULL;
189 LPALBUFFER3F palBuffer3f = NULL;
190 LPALBUFFERFV palBufferfv = NULL;
191 LPALBUFFERI palBufferi = NULL;
192 LPALBUFFER3I palBuffer3i = NULL;
193 LPALBUFFERIV palBufferiv = NULL;
194 LPALGETBUFFERF palGetBufferf = NULL;
195 LPALGETBUFFER3F palGetBuffer3f = NULL;
196 LPALGETBUFFERFV palGetBufferfv = NULL;
197 LPALGETBUFFERI palGetBufferi = NULL;
198 LPALGETBUFFER3I palGetBuffer3i = NULL;
199 LPALGETBUFFERIV palGetBufferiv = NULL;
200 LPALBUFFERDATA palBufferData = NULL;
201 LPALDOPPLERFACTOR palDopplerFactor = NULL;
202 LPALDOPPLERVELOCITY palDopplerVelocity = NULL;
203 LPALDISTANCEMODEL palDistanceModel = NULL;
204 LPALSPEEDOFSOUND palSpeedOfSound = NULL;
206 LPALGENFILTERS palGenFilters = NULL;
207 LPALDELETEFILTERS palDeleteFilters = NULL;
208 LPALFILTERI palFilteri = NULL;
209 LPALFILTERF palFilterf = NULL;
210 LPALGENEFFECTS palGenEffects = NULL;
211 LPALDELETEEFFECTS palDeleteEffects = NULL;
212 LPALEFFECTI palEffecti = NULL;
213 LPALEFFECTF palEffectf = NULL;
214 LPALEFFECTFV palEffectfv = NULL;
215 LPALGENAUXILIARYEFFECTSLOTS palGenAuxiliaryEffectSlots = NULL;
216 LPALDELETEAUXILIARYEFFECTSLOTS palDeleteAuxiliaryEffectSlots = NULL;
217 LPALAUXILIARYEFFECTSLOTI palAuxiliaryEffectSloti = NULL;
218 LPALAUXILIARYEFFECTSLOTF palAuxiliaryEffectSlotf = NULL;
219 LPALDEFERUPDATESSOFT palDeferUpdatesSOFT = NULL;
220 LPALPROCESSUPDATESSOFT palProcessUpdatesSOFT = NULL;
221 LPALBUFFERSTORAGESOFT palBufferStorageSOFT = NULL;
222 LPALMAPBUFFERSOFT palMapBufferSOFT = NULL;
223 LPALUNMAPBUFFERSOFT palUnmapBufferSOFT = NULL;
224 LPALFLUSHMAPPEDBUFFERSOFT palFlushMappedBufferSOFT = NULL;
226 LPALCMAKECONTEXTCURRENT set_context;
227 LPALCGETCURRENTCONTEXT get_context;
228 BOOL local_contexts;
231 static void AL_APIENTRY wrap_DeferUpdates(void)
232 { alcSuspendContext(alcGetCurrentContext()); }
233 static void AL_APIENTRY wrap_ProcessUpdates(void)
234 { alcProcessContext(alcGetCurrentContext()); }
236 static void EnterALSectionTLS(ALCcontext *ctx);
237 static void LeaveALSectionTLS(void);
238 static void EnterALSectionGlob(ALCcontext *ctx);
239 static void LeaveALSectionGlob(void);
241 DWORD TlsThreadPtr;
242 void (*EnterALSection)(ALCcontext *ctx) = EnterALSectionGlob;
243 void (*LeaveALSection)(void) = LeaveALSectionGlob;
246 static BOOL load_libopenal(void)
248 BOOL failed = FALSE;
249 const char *str;
251 str = getenv("DSOAL_LOGLEVEL");
252 if(str && *str)
253 LogLevel = atoi(str);
255 openal_handle = LoadLibraryW(aldriver_name);
256 if(!openal_handle)
258 ERR("Couldn't load %ls: %lu\n", aldriver_name, GetLastError());
259 return FALSE;
262 #define LOAD_FUNCPTR(f) do { \
263 union { void *ptr; FARPROC *proc; } func = { &p##f }; \
264 if((*func.proc = GetProcAddress(openal_handle, #f)) == NULL) \
266 ERR("Couldn't lookup %s in %ls\n", #f, aldriver_name); \
267 failed = TRUE; \
269 } while(0)
271 LOAD_FUNCPTR(alcCreateContext);
272 LOAD_FUNCPTR(alcMakeContextCurrent);
273 LOAD_FUNCPTR(alcProcessContext);
274 LOAD_FUNCPTR(alcSuspendContext);
275 LOAD_FUNCPTR(alcDestroyContext);
276 LOAD_FUNCPTR(alcGetCurrentContext);
277 LOAD_FUNCPTR(alcGetContextsDevice);
278 LOAD_FUNCPTR(alcOpenDevice);
279 LOAD_FUNCPTR(alcCloseDevice);
280 LOAD_FUNCPTR(alcGetError);
281 LOAD_FUNCPTR(alcIsExtensionPresent);
282 LOAD_FUNCPTR(alcGetProcAddress);
283 LOAD_FUNCPTR(alcGetEnumValue);
284 LOAD_FUNCPTR(alcGetString);
285 LOAD_FUNCPTR(alcGetIntegerv);
286 LOAD_FUNCPTR(alcCaptureOpenDevice);
287 LOAD_FUNCPTR(alcCaptureCloseDevice);
288 LOAD_FUNCPTR(alcCaptureStart);
289 LOAD_FUNCPTR(alcCaptureStop);
290 LOAD_FUNCPTR(alcCaptureSamples);
291 LOAD_FUNCPTR(alEnable);
292 LOAD_FUNCPTR(alDisable);
293 LOAD_FUNCPTR(alIsEnabled);
294 LOAD_FUNCPTR(alGetString);
295 LOAD_FUNCPTR(alGetBooleanv);
296 LOAD_FUNCPTR(alGetIntegerv);
297 LOAD_FUNCPTR(alGetFloatv);
298 LOAD_FUNCPTR(alGetDoublev);
299 LOAD_FUNCPTR(alGetBoolean);
300 LOAD_FUNCPTR(alGetInteger);
301 LOAD_FUNCPTR(alGetFloat);
302 LOAD_FUNCPTR(alGetDouble);
303 LOAD_FUNCPTR(alGetError);
304 LOAD_FUNCPTR(alIsExtensionPresent);
305 LOAD_FUNCPTR(alGetProcAddress);
306 LOAD_FUNCPTR(alGetEnumValue);
307 LOAD_FUNCPTR(alListenerf);
308 LOAD_FUNCPTR(alListener3f);
309 LOAD_FUNCPTR(alListenerfv);
310 LOAD_FUNCPTR(alListeneri);
311 LOAD_FUNCPTR(alListener3i);
312 LOAD_FUNCPTR(alListeneriv);
313 LOAD_FUNCPTR(alGetListenerf);
314 LOAD_FUNCPTR(alGetListener3f);
315 LOAD_FUNCPTR(alGetListenerfv);
316 LOAD_FUNCPTR(alGetListeneri);
317 LOAD_FUNCPTR(alGetListener3i);
318 LOAD_FUNCPTR(alGetListeneriv);
319 LOAD_FUNCPTR(alGenSources);
320 LOAD_FUNCPTR(alDeleteSources);
321 LOAD_FUNCPTR(alIsSource);
322 LOAD_FUNCPTR(alSourcef);
323 LOAD_FUNCPTR(alSource3f);
324 LOAD_FUNCPTR(alSourcefv);
325 LOAD_FUNCPTR(alSourcei);
326 LOAD_FUNCPTR(alSource3i);
327 LOAD_FUNCPTR(alSourceiv);
328 LOAD_FUNCPTR(alGetSourcef);
329 LOAD_FUNCPTR(alGetSource3f);
330 LOAD_FUNCPTR(alGetSourcefv);
331 LOAD_FUNCPTR(alGetSourcei);
332 LOAD_FUNCPTR(alGetSource3i);
333 LOAD_FUNCPTR(alGetSourceiv);
334 LOAD_FUNCPTR(alSourcePlayv);
335 LOAD_FUNCPTR(alSourceStopv);
336 LOAD_FUNCPTR(alSourceRewindv);
337 LOAD_FUNCPTR(alSourcePausev);
338 LOAD_FUNCPTR(alSourcePlay);
339 LOAD_FUNCPTR(alSourceStop);
340 LOAD_FUNCPTR(alSourceRewind);
341 LOAD_FUNCPTR(alSourcePause);
342 LOAD_FUNCPTR(alSourceQueueBuffers);
343 LOAD_FUNCPTR(alSourceUnqueueBuffers);
344 LOAD_FUNCPTR(alGenBuffers);
345 LOAD_FUNCPTR(alDeleteBuffers);
346 LOAD_FUNCPTR(alIsBuffer);
347 LOAD_FUNCPTR(alBufferf);
348 LOAD_FUNCPTR(alBuffer3f);
349 LOAD_FUNCPTR(alBufferfv);
350 LOAD_FUNCPTR(alBufferi);
351 LOAD_FUNCPTR(alBuffer3i);
352 LOAD_FUNCPTR(alBufferiv);
353 LOAD_FUNCPTR(alGetBufferf);
354 LOAD_FUNCPTR(alGetBuffer3f);
355 LOAD_FUNCPTR(alGetBufferfv);
356 LOAD_FUNCPTR(alGetBufferi);
357 LOAD_FUNCPTR(alGetBuffer3i);
358 LOAD_FUNCPTR(alGetBufferiv);
359 LOAD_FUNCPTR(alBufferData);
360 LOAD_FUNCPTR(alDopplerFactor);
361 LOAD_FUNCPTR(alDopplerVelocity);
362 LOAD_FUNCPTR(alDistanceModel);
363 LOAD_FUNCPTR(alSpeedOfSound);
364 #undef LOAD_FUNCPTR
365 if (failed)
367 WARN("Unloading %ls\n", aldriver_name);
368 if (openal_handle != NULL)
369 FreeLibrary(openal_handle);
370 openal_handle = NULL;
371 return FALSE;
374 openal_loaded = 1;
375 TRACE("Loaded %ls\n", aldriver_name);
377 #define LOAD_FUNCPTR(f) p##f = alcGetProcAddress(NULL, #f)
378 LOAD_FUNCPTR(alGenFilters);
379 LOAD_FUNCPTR(alDeleteFilters);
380 LOAD_FUNCPTR(alFilteri);
381 LOAD_FUNCPTR(alFilterf);
382 LOAD_FUNCPTR(alGenEffects);
383 LOAD_FUNCPTR(alDeleteEffects);
384 LOAD_FUNCPTR(alEffecti);
385 LOAD_FUNCPTR(alEffectf);
386 LOAD_FUNCPTR(alEffectfv);
387 LOAD_FUNCPTR(alGenAuxiliaryEffectSlots);
388 LOAD_FUNCPTR(alDeleteAuxiliaryEffectSlots);
389 LOAD_FUNCPTR(alAuxiliaryEffectSloti);
390 LOAD_FUNCPTR(alAuxiliaryEffectSlotf);
391 LOAD_FUNCPTR(alDeferUpdatesSOFT);
392 LOAD_FUNCPTR(alProcessUpdatesSOFT);
393 LOAD_FUNCPTR(alBufferStorageSOFT);
394 LOAD_FUNCPTR(alMapBufferSOFT);
395 LOAD_FUNCPTR(alUnmapBufferSOFT);
396 LOAD_FUNCPTR(alFlushMappedBufferSOFT);
397 #undef LOAD_FUNCPTR
398 if(!palDeferUpdatesSOFT || !palProcessUpdatesSOFT)
400 palDeferUpdatesSOFT = wrap_DeferUpdates;
401 palProcessUpdatesSOFT = wrap_ProcessUpdates;
404 local_contexts = alcIsExtensionPresent(NULL, "ALC_EXT_thread_local_context");
405 if(local_contexts)
407 TRACE("Found ALC_EXT_thread_local_context\n");
409 set_context = alcGetProcAddress(NULL, "alcSetThreadContext");
410 get_context = alcGetProcAddress(NULL, "alcGetThreadContext");
411 if(!set_context || !get_context)
413 ERR("TLS advertised but functions not found, disabling thread local contexts\n");
414 local_contexts = 0;
417 if(!local_contexts)
419 set_context = alcMakeContextCurrent;
420 get_context = alcGetCurrentContext;
422 else
424 EnterALSection = EnterALSectionTLS;
425 LeaveALSection = LeaveALSectionTLS;
428 return TRUE;
432 static void EnterALSectionTLS(ALCcontext *ctx)
434 if(LIKELY(ctx == TlsGetValue(TlsThreadPtr)))
435 return;
437 if(LIKELY(set_context(ctx) != ALC_FALSE))
438 TlsSetValue(TlsThreadPtr, ctx);
439 else
441 ERR("Couldn't set current context!!\n");
442 checkALCError(alcGetContextsDevice(ctx));
445 static void LeaveALSectionTLS(void)
449 static void EnterALSectionGlob(ALCcontext *ctx)
451 EnterCriticalSection(&openal_crst);
452 if(UNLIKELY(alcMakeContextCurrent(ctx) == ALC_FALSE))
454 ERR("Couldn't set current context!!\n");
455 checkALCError(alcGetContextsDevice(ctx));
458 static void LeaveALSectionGlob(void)
460 LeaveCriticalSection(&openal_crst);
464 static const char *get_device_id(LPCGUID pGuid)
466 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuid))
467 return "DSDEVID_DefaultPlayback";
468 if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuid))
469 return "DSDEVID_DefaultVoicePlayback";
470 if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuid))
471 return "DSDEVID_DefaultCapture";
472 if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuid))
473 return "DSDEVID_DefaultVoiceCapture";
474 return debugstr_guid(pGuid);
477 #ifdef _MSC_VER
478 const CLSID CLSID_MMDeviceEnumerator = {
479 0xBCDE0395,
480 0xE52F, 0x467C,
481 { 0x8E, 0x3D, 0xC4, 0x57, 0x92, 0x91, 0x69, 0x2E }
484 const IID IID_IMMDeviceEnumerator = {
485 0xA95664D2,
486 0x9614, 0x4F35,
487 { 0xA7, 0x46, 0xDE, 0x8D, 0xB6, 0x36, 0x17, 0xE6 }
489 #endif
491 static HRESULT get_mmdevenum(IMMDeviceEnumerator **devenum)
493 HRESULT hr, init_hr;
495 init_hr = CoInitialize(NULL);
497 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
498 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)devenum);
499 if(FAILED(hr))
501 if(SUCCEEDED(init_hr))
502 CoUninitialize();
503 *devenum = NULL;
504 ERR("CoCreateInstance failed: %08lx\n", hr);
505 return hr;
508 return init_hr;
511 static void release_mmdevenum(IMMDeviceEnumerator *devenum, HRESULT init_hr)
513 IMMDeviceEnumerator_Release(devenum);
514 if(SUCCEEDED(init_hr))
515 CoUninitialize();
518 static HRESULT get_mmdevice_guid(IMMDevice *device, IPropertyStore *ps, GUID *guid)
520 PROPVARIANT pv;
521 HRESULT hr;
523 if(ps)
524 IPropertyStore_AddRef(ps);
525 else
527 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
528 if(FAILED(hr))
530 WARN("OpenPropertyStore failed: %08lx\n", hr);
531 return hr;
535 PropVariantInit(&pv);
537 hr = IPropertyStore_GetValue(ps, &PKEY_AudioEndpoint_GUID, &pv);
538 if(FAILED(hr))
540 IPropertyStore_Release(ps);
541 WARN("GetValue(GUID) failed: %08lx\n", hr);
542 return hr;
545 CLSIDFromString(pv.pwszVal, guid);
547 PropVariantClear(&pv);
548 IPropertyStore_Release(ps);
550 return S_OK;
554 static BOOL send_device(IMMDevice *device, EDataFlow flow, DeviceList *devlist, PRVTENUMCALLBACK cb, void *user)
556 IPropertyStore *ps;
557 PROPVARIANT pv;
558 BOOL keep_going;
559 size_t dev_count;
560 HRESULT hr;
561 GUID guid;
563 PropVariantInit(&pv);
565 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
566 if(FAILED(hr))
568 WARN("OpenPropertyStore failed: %08lx\n", hr);
569 return TRUE;
572 hr = get_mmdevice_guid(device, ps, &guid);
573 if(FAILED(hr) || (devlist->Count > 0 && IsEqualGUID(&devlist->Guids[0], &guid)))
575 IPropertyStore_Release(ps);
576 return TRUE;
579 hr = IPropertyStore_GetValue(ps, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
580 if(FAILED(hr))
582 IPropertyStore_Release(ps);
583 WARN("GetValue(FriendlyName) failed: %08lx\n", hr);
584 return TRUE;
587 dev_count = devlist->Count++;
588 devlist->Guids[dev_count] = guid;
590 keep_going = FALSE;
591 if(cb)
593 TRACE("Calling back with %s - %ls\n", debugstr_guid(&devlist->Guids[dev_count]),
594 pv.pwszVal);
595 keep_going = cb(flow, &devlist->Guids[dev_count], pv.pwszVal, aldriver_name, user);
598 PropVariantClear(&pv);
599 IPropertyStore_Release(ps);
601 return keep_going;
604 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device)
606 IMMDeviceEnumerator *devenum;
607 IMMDeviceCollection *coll;
608 UINT count, i;
609 HRESULT hr, init_hr;
611 init_hr = get_mmdevenum(&devenum);
612 if(!devenum) return init_hr;
614 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow, DEVICE_STATE_ACTIVE, &coll);
615 if(FAILED(hr))
617 WARN("EnumAudioEndpoints failed: %08lx\n", hr);
618 release_mmdevenum(devenum, init_hr);
619 return hr;
622 hr = IMMDeviceCollection_GetCount(coll, &count);
623 if(FAILED(hr))
625 IMMDeviceCollection_Release(coll);
626 release_mmdevenum(devenum, init_hr);
627 WARN("GetCount failed: %08lx\n", hr);
628 return hr;
631 for(i = 0; i < count;++i)
633 GUID guid;
635 hr = IMMDeviceCollection_Item(coll, i, device);
636 if(FAILED(hr)) continue;
638 hr = get_mmdevice_guid(*device, NULL, &guid);
639 if(FAILED(hr))
641 IMMDevice_Release(*device);
642 continue;
645 if(IsEqualGUID(&guid, tgt))
647 IMMDeviceCollection_Release(coll);
648 release_mmdevenum(devenum, init_hr);
649 return DS_OK;
652 IMMDevice_Release(*device);
655 WARN("No device with GUID %s found!\n", debugstr_guid(tgt));
657 IMMDeviceCollection_Release(coll);
658 release_mmdevenum(devenum, init_hr);
660 return DSERR_INVALIDPARAM;
663 /* S_FALSE means the callback returned FALSE at some point
664 * S_OK means the callback always returned TRUE */
665 HRESULT enumerate_mmdevices(EDataFlow flow, PRVTENUMCALLBACK cb, void *user)
667 static const WCHAR primary_desc[] = L"Primary Sound Driver";
669 IMMDeviceEnumerator *devenum;
670 IMMDeviceCollection *coll;
671 IMMDevice *device;
672 DeviceList *devlist;
673 UINT count, i;
674 BOOL keep_going;
675 HRESULT hr, init_hr;
677 init_hr = get_mmdevenum(&devenum);
678 if(!devenum) return init_hr;
680 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow, DEVICE_STATE_ACTIVE, &coll);
681 if(FAILED(hr))
683 release_mmdevenum(devenum, init_hr);
684 WARN("EnumAudioEndpoints failed: %08lx\n", hr);
685 return DS_OK;
688 hr = IMMDeviceCollection_GetCount(coll, &count);
689 if(FAILED(hr))
691 IMMDeviceCollection_Release(coll);
692 release_mmdevenum(devenum, init_hr);
693 WARN("GetCount failed: %08lx\n", hr);
694 return DS_OK;
697 if(count == 0)
699 IMMDeviceCollection_Release(coll);
700 release_mmdevenum(devenum, init_hr);
701 return DS_OK;
704 devlist = (flow==eCapture) ? &CaptureDevices : &PlaybackDevices;
706 HeapFree(GetProcessHeap(), 0, devlist->Guids);
707 devlist->Count = 0;
708 devlist->Guids = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
709 sizeof(devlist->Guids[0])*count);
711 TRACE("Calling back with NULL (%ls)\n", primary_desc);
712 keep_going = cb(flow, NULL, primary_desc, L"", user);
714 /* always send the default device first */
715 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow, eMultimedia, &device);
716 if(SUCCEEDED(hr))
718 if(!keep_going) cb = NULL;
719 keep_going = send_device(device, flow, devlist, cb, user);
720 IMMDevice_Release(device);
723 for(i = 0;i < count;++i)
725 if(!keep_going)
726 cb = NULL;
728 hr = IMMDeviceCollection_Item(coll, i, &device);
729 if(FAILED(hr))
731 WARN("Item failed: %08lx\n", hr);
732 continue;
735 keep_going = send_device(device, flow, devlist, cb, user);
737 IMMDevice_Release(device);
739 IMMDeviceCollection_Release(coll);
741 release_mmdevenum(devenum, init_hr);
743 return keep_going ? S_OK : S_FALSE;
746 /***************************************************************************
747 * GetDeviceID [DSOUND.9]
749 * Retrieves unique identifier of default device specified
751 * PARAMS
752 * pGuidSrc [I] Address of device GUID.
753 * pGuidDest [O] Address to receive unique device GUID.
755 * RETURNS
756 * Success: DS_OK
757 * Failure: DSERR_INVALIDPARAM
759 * NOTES
760 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
761 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
762 * DSDEVID_DefaultVoiceCapture.
763 * Returns pGuidSrc if pGuidSrc is a valid device or the device
764 * GUID for the specified constants.
766 HRESULT WINAPI DSOAL_GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
768 IMMDeviceEnumerator *devenum;
769 HRESULT hr, init_hr;
770 IMMDevice *device;
771 EDataFlow flow;
772 ERole role;
774 TRACE("(%s, %p)\n", get_device_id(pGuidSrc), pGuidDest);
776 if(!pGuidSrc || !pGuidDest)
777 return DSERR_INVALIDPARAM;
779 flow = eRender;
780 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuidSrc))
781 role = eMultimedia;
782 else if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuidSrc))
783 role = eCommunications;
784 else
786 flow = eCapture;
787 if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuidSrc))
788 role = eMultimedia;
789 else if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuidSrc))
790 role = eCommunications;
791 else
793 *pGuidDest = *pGuidSrc;
794 return DS_OK;
798 init_hr = get_mmdevenum(&devenum);
799 if(!devenum) return init_hr;
801 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow, role, &device);
802 if(FAILED(hr))
804 WARN("GetDefaultAudioEndpoint failed: %08lx\n", hr);
805 release_mmdevenum(devenum, init_hr);
806 return DSERR_NODRIVER;
809 hr = get_mmdevice_guid(device, NULL, pGuidDest);
810 IMMDevice_Release(device);
812 release_mmdevenum(devenum, init_hr);
814 return hr;
818 struct morecontextW {
819 LPDSENUMCALLBACKW callW;
820 LPVOID data;
823 static BOOL CALLBACK w_callback(EDataFlow flow, LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
825 struct morecontextW *context = data;
826 (void)flow;
828 return context->callW(guid, descW, modW, context->data);
831 struct morecontextA {
832 LPDSENUMCALLBACKA callA;
833 LPVOID data;
836 static BOOL CALLBACK w_to_a_callback(EDataFlow flow, LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
838 struct morecontextA *context = data;
839 char *descA, *modA;
840 int dlen, mlen;
841 BOOL ret;
842 (void)flow;
844 dlen = WideCharToMultiByte(CP_ACP, 0, descW, -1, NULL, 0, NULL, NULL);
845 mlen = WideCharToMultiByte(CP_ACP, 0, modW, -1, NULL, 0, NULL, NULL);
846 if(dlen < 0 || mlen < 0) return FALSE;
848 descA = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dlen+mlen+2);
849 if(!descA) return FALSE;
850 modA = descA + dlen+1;
852 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, dlen, NULL, NULL);
853 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, mlen, NULL, NULL);
855 ret = context->callA(guid, descA, modA, context->data);
857 HeapFree(GetProcessHeap(), 0, descA);
858 return ret;
861 /***************************************************************************
862 * DirectSoundEnumerateA [DSOUND.2]
864 * Enumerate all DirectSound drivers installed in the system
866 * PARAMS
867 * lpDSEnumCallback [I] Address of callback function.
868 * lpContext [I] Address of user defined context passed to callback function.
870 * RETURNS
871 * Success: DS_OK
872 * Failure: DSERR_INVALIDPARAM
874 HRESULT WINAPI DSOAL_DirectSoundEnumerateA(
875 LPDSENUMCALLBACKA lpDSEnumCallback,
876 LPVOID lpContext)
878 struct morecontextA ctx;
879 HRESULT hr;
881 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
883 if(lpDSEnumCallback == NULL)
885 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
886 return DSERR_INVALIDPARAM;
889 ctx.callA = lpDSEnumCallback;
890 ctx.data = lpContext;
892 hr = enumerate_mmdevices(eRender, w_to_a_callback, &ctx);
893 return SUCCEEDED(hr) ? DS_OK : hr;
897 /***************************************************************************
898 * DirectSoundEnumerateW [DSOUND.3]
900 * Enumerate all DirectSound drivers installed in the system
902 * PARAMS
903 * lpDSEnumCallback [I] Address of callback function.
904 * lpContext [I] Address of user defined context passed to callback function.
906 * RETURNS
907 * Success: DS_OK
908 * Failure: DSERR_INVALIDPARAM
910 HRESULT WINAPI DSOAL_DirectSoundEnumerateW(
911 LPDSENUMCALLBACKW lpDSEnumCallback,
912 LPVOID lpContext )
914 struct morecontextW ctx;
915 HRESULT hr;
917 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
919 if(lpDSEnumCallback == NULL)
921 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
922 return DSERR_INVALIDPARAM;
925 ctx.callW = lpDSEnumCallback;
926 ctx.data = lpContext;
928 hr = enumerate_mmdevices(eRender, w_callback, &ctx);
929 return SUCCEEDED(hr) ? DS_OK : hr;
932 /***************************************************************************
933 * DirectSoundCaptureEnumerateA [DSOUND.7]
935 * Enumerate all DirectSound drivers installed in the system.
937 * PARAMS
938 * lpDSEnumCallback [I] Address of callback function.
939 * lpContext [I] Address of user defined context passed to callback function.
941 * RETURNS
942 * Success: DS_OK
943 * Failure: DSERR_INVALIDPARAM
945 HRESULT WINAPI DSOAL_DirectSoundCaptureEnumerateA(
946 LPDSENUMCALLBACKA lpDSEnumCallback,
947 LPVOID lpContext)
949 struct morecontextA ctx;
950 HRESULT hr;
952 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
954 if(lpDSEnumCallback == NULL)
956 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
957 return DSERR_INVALIDPARAM;
960 ctx.callA = lpDSEnumCallback;
961 ctx.data = lpContext;
963 hr = enumerate_mmdevices(eCapture, w_to_a_callback, &ctx);
964 return SUCCEEDED(hr) ? DS_OK : hr;
967 /***************************************************************************
968 * DirectSoundCaptureEnumerateW [DSOUND.8]
970 * Enumerate all DirectSound drivers installed in the system.
972 * PARAMS
973 * lpDSEnumCallback [I] Address of callback function.
974 * lpContext [I] Address of user defined context passed to callback function.
976 * RETURNS
977 * Success: DS_OK
978 * Failure: DSERR_INVALIDPARAM
980 HRESULT WINAPI DSOAL_DirectSoundCaptureEnumerateW(
981 LPDSENUMCALLBACKW lpDSEnumCallback,
982 LPVOID lpContext)
984 struct morecontextW ctx;
985 HRESULT hr;
987 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
989 if(lpDSEnumCallback == NULL)
991 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
992 return DSERR_INVALIDPARAM;
995 ctx.callW = lpDSEnumCallback;
996 ctx.data = lpContext;
998 hr = enumerate_mmdevices(eCapture, w_callback, &ctx);
999 return SUCCEEDED(hr) ? DS_OK : hr;
1002 /*******************************************************************************
1003 * DirectSoundCreate (DSOUND.1)
1005 * Creates and initializes a DirectSound interface.
1007 * PARAMS
1008 * lpcGUID [I] Address of the GUID that identifies the sound device.
1009 * ppDS [O] Address of a variable to receive the interface pointer.
1010 * pUnkOuter [I] Must be NULL.
1012 * RETURNS
1013 * Success: DS_OK
1014 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
1015 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
1017 HRESULT WINAPI
1018 DSOAL_DirectSoundCreate(LPCGUID lpcGUID, IDirectSound **ppDS, IUnknown *pUnkOuter)
1020 HRESULT hr;
1021 void *pDS;
1023 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
1025 if (ppDS == NULL) {
1026 WARN("invalid parameter: ppDS == NULL\n");
1027 return DSERR_INVALIDPARAM;
1029 *ppDS = NULL;
1031 if (pUnkOuter != NULL) {
1032 WARN("invalid parameter: pUnkOuter != NULL\n");
1033 return DSERR_INVALIDPARAM;
1036 hr = DSOUND_Create(&IID_IDirectSound, &pDS);
1037 if(SUCCEEDED(hr))
1039 *ppDS = pDS;
1040 hr = IDirectSound_Initialize(*ppDS, lpcGUID);
1041 if(FAILED(hr))
1043 IDirectSound_Release(*ppDS);
1044 *ppDS = NULL;
1048 return hr;
1051 /*******************************************************************************
1052 * DirectSoundCreate8 (DSOUND.11)
1054 * Creates and initializes a DirectSound8 interface.
1056 * PARAMS
1057 * lpcGUID [I] Address of the GUID that identifies the sound device.
1058 * ppDS [O] Address of a variable to receive the interface pointer.
1059 * pUnkOuter [I] Must be NULL.
1061 * RETURNS
1062 * Success: DS_OK
1063 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
1064 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
1066 HRESULT WINAPI
1067 DSOAL_DirectSoundCreate8(LPCGUID lpcGUID, IDirectSound8 **ppDS, IUnknown *pUnkOuter)
1069 HRESULT hr;
1070 void *pDS;
1072 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
1074 if (ppDS == NULL) {
1075 WARN("invalid parameter: ppDS == NULL\n");
1076 return DSERR_INVALIDPARAM;
1078 *ppDS = NULL;
1080 if (pUnkOuter != NULL) {
1081 WARN("invalid parameter: pUnkOuter != NULL\n");
1082 return DSERR_INVALIDPARAM;
1085 hr = DSOUND_Create8(&IID_IDirectSound8, &pDS);
1086 if(SUCCEEDED(hr))
1088 *ppDS = pDS;
1089 hr = IDirectSound8_Initialize(*ppDS, lpcGUID);
1090 if(FAILED(hr))
1092 IDirectSound8_Release(*ppDS);
1093 *ppDS = NULL;
1097 return hr;
1100 /***************************************************************************
1101 * DirectSoundCaptureCreate [DSOUND.6]
1103 * Create and initialize a DirectSoundCapture interface.
1105 * PARAMS
1106 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1107 * lplpDSC [O] Address of a variable to receive the interface pointer.
1108 * pUnkOuter [I] Must be NULL.
1110 * RETURNS
1111 * Success: DS_OK
1112 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1113 * DSERR_OUTOFMEMORY
1115 * NOTES
1116 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1117 * or NULL for the default device or DSDEVID_DefaultCapture or
1118 * DSDEVID_DefaultVoiceCapture.
1120 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1122 HRESULT WINAPI
1123 DSOAL_DirectSoundCaptureCreate(LPCGUID lpcGUID, IDirectSoundCapture **ppDSC, IUnknown *pUnkOuter)
1125 HRESULT hr;
1126 void *pDSC;
1128 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
1130 if(pUnkOuter)
1132 WARN("invalid parameter: pUnkOuter != NULL\n");
1133 return DSERR_NOAGGREGATION;
1136 if(ppDSC == NULL)
1138 WARN("invalid parameter: ppDSC == NULL\n");
1139 return DSERR_INVALIDPARAM;
1141 *ppDSC = NULL;
1143 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, &pDSC);
1144 if(SUCCEEDED(hr))
1146 *ppDSC = pDSC;
1147 hr = IDirectSoundCapture_Initialize(*ppDSC, lpcGUID);
1148 if(FAILED(hr))
1150 IDirectSoundCapture_Release(*ppDSC);
1151 *ppDSC = NULL;
1155 return hr;
1158 /***************************************************************************
1159 * DirectSoundCaptureCreate8 [DSOUND.12]
1161 * Create and initialize a DirectSoundCapture interface.
1163 * PARAMS
1164 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1165 * lplpDSC [O] Address of a variable to receive the interface pointer.
1166 * pUnkOuter [I] Must be NULL.
1168 * RETURNS
1169 * Success: DS_OK
1170 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1171 * DSERR_OUTOFMEMORY
1173 * NOTES
1174 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1175 * or NULL for the default device or DSDEVID_DefaultCapture or
1176 * DSDEVID_DefaultVoiceCapture.
1178 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1180 HRESULT WINAPI
1181 DSOAL_DirectSoundCaptureCreate8(LPCGUID lpcGUID, IDirectSoundCapture8 **ppDSC8, IUnknown *pUnkOuter)
1183 HRESULT hr;
1184 void *pDSC8;
1186 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
1188 if(pUnkOuter)
1190 WARN("invalid parameter: pUnkOuter != NULL\n");
1191 return DSERR_NOAGGREGATION;
1194 if(ppDSC8 == NULL)
1196 WARN("invalid parameter: ppDSC8 == NULL\n");
1197 return DSERR_INVALIDPARAM;
1199 *ppDSC8 = NULL;
1201 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture, &pDSC8);
1202 if(SUCCEEDED(hr))
1204 *ppDSC8 = pDSC8;
1205 hr = IDirectSoundCapture_Initialize(*ppDSC8, lpcGUID);
1206 if(FAILED(hr))
1208 IDirectSoundCapture_Release(*ppDSC8);
1209 *ppDSC8 = NULL;
1213 return hr;
1216 /*******************************************************************************
1217 * DirectSound ClassFactory
1220 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
1222 typedef struct {
1223 IClassFactory IClassFactory_iface;
1224 LONG ref;
1225 REFCLSID rclsid;
1226 FnCreateInstance pfnCreateInstance;
1227 } IClassFactoryImpl;
1229 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
1231 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
1234 static HRESULT WINAPI DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
1236 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1237 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
1238 if (ppobj == NULL)
1239 return E_POINTER;
1240 if (IsEqualIID(riid, &IID_IUnknown) ||
1241 IsEqualIID(riid, &IID_IClassFactory))
1243 *ppobj = iface;
1244 IUnknown_AddRef(iface);
1245 return S_OK;
1247 *ppobj = NULL;
1248 return E_NOINTERFACE;
1251 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
1253 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1254 ULONG ref = InterlockedIncrement(&(This->ref));
1255 TRACE("(%p) ref %lu\n", iface, ref);
1256 return ref;
1259 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
1261 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1262 ULONG ref = InterlockedDecrement(&(This->ref));
1263 TRACE("(%p) ref %lu\n", iface, ref);
1264 /* static class, won't be freed */
1265 return ref;
1268 static HRESULT WINAPI DSCF_CreateInstance(
1269 LPCLASSFACTORY iface,
1270 LPUNKNOWN pOuter,
1271 REFIID riid,
1272 LPVOID *ppobj)
1274 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1275 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
1277 if (pOuter)
1278 return CLASS_E_NOAGGREGATION;
1280 if (ppobj == NULL) {
1281 WARN("invalid parameter\n");
1282 return DSERR_INVALIDPARAM;
1284 *ppobj = NULL;
1285 return This->pfnCreateInstance(riid, ppobj);
1288 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
1290 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1291 FIXME("(%p, %d) stub!\n", This, dolock);
1292 return S_OK;
1295 static const IClassFactoryVtbl DSCF_Vtbl = {
1296 DSCF_QueryInterface,
1297 DSCF_AddRef,
1298 DSCF_Release,
1299 DSCF_CreateInstance,
1300 DSCF_LockServer
1303 static IClassFactoryImpl DSOUND_CF[] = {
1304 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound, DSOUND_Create },
1305 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound8, DSOUND_Create8 },
1306 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
1307 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
1308 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
1309 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
1310 { {NULL}, 0, NULL, NULL }
1313 /*******************************************************************************
1314 * DllGetClassObject [DSOUND.@]
1315 * Retrieves class object from a DLL object
1317 * NOTES
1318 * Docs say returns STDAPI
1320 * PARAMS
1321 * rclsid [I] CLSID for the class object
1322 * riid [I] Reference to identifier of interface for class object
1323 * ppv [O] Address of variable to receive interface pointer for riid
1325 * RETURNS
1326 * Success: S_OK
1327 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
1328 * E_UNEXPECTED
1330 HRESULT WINAPI DSOAL_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1332 int i = 0;
1333 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1335 if (ppv == NULL) {
1336 WARN("invalid parameter\n");
1337 return E_INVALIDARG;
1340 *ppv = NULL;
1342 if (!IsEqualIID(riid, &IID_IClassFactory) &&
1343 !IsEqualIID(riid, &IID_IUnknown)) {
1344 WARN("no interface for %s\n", debugstr_guid(riid));
1345 return E_NOINTERFACE;
1348 while (NULL != DSOUND_CF[i].rclsid) {
1349 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
1350 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
1351 *ppv = &DSOUND_CF[i].IClassFactory_iface;
1352 return S_OK;
1354 i++;
1357 WARN("No class found for %s\n", debugstr_guid(rclsid));
1358 return CLASS_E_CLASSNOTAVAILABLE;
1362 /*******************************************************************************
1363 * DllCanUnloadNow [DSOUND.4]
1364 * Determines whether the DLL is in use.
1366 * RETURNS
1367 * Success: S_OK
1368 * Failure: S_FALSE
1370 HRESULT WINAPI DSOAL_DllCanUnloadNow(void)
1372 FIXME("(void): stub\n");
1373 return S_FALSE;
1376 /***********************************************************************
1377 * DllMain (DSOUND.init)
1379 DECLSPEC_EXPORT BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
1381 const WCHAR *wstr;
1383 TRACE("(%p, %lu, %p)\n", hInstDLL, fdwReason, lpvReserved);
1385 switch(fdwReason)
1387 case DLL_PROCESS_ATTACH:
1388 LogFile = stderr;
1389 if((wstr=_wgetenv(L"DSOAL_LOGFILE")) != NULL && wstr[0] != 0)
1391 FILE *f = _wfopen(wstr, L"wt");
1392 if(!f) ERR("Failed to open log file %ls\n", wstr);
1393 else LogFile = f;
1396 if(!load_libopenal())
1397 return FALSE;
1398 TlsThreadPtr = TlsAlloc();
1399 /* Increase refcount on dsound by 1 */
1400 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
1401 break;
1403 case DLL_THREAD_ATTACH:
1404 break;
1406 case DLL_THREAD_DETACH:
1407 if(local_contexts)
1408 set_context(NULL);
1409 break;
1411 case DLL_PROCESS_DETACH:
1412 HeapFree(GetProcessHeap(), 0, PlaybackDevices.Guids);
1413 PlaybackDevices.Guids = NULL;
1414 PlaybackDevices.Count = 0;
1415 HeapFree(GetProcessHeap(), 0, CaptureDevices.Guids);
1416 CaptureDevices.Guids = NULL;
1417 CaptureDevices.Count = 0;
1419 if(openal_handle)
1420 FreeLibrary(openal_handle);
1421 TlsFree(TlsThreadPtr);
1422 if(LogFile != stderr)
1423 fclose(LogFile);
1424 LogFile = stderr;
1425 break;
1427 return TRUE;
1430 #ifdef __WINESRC__
1431 /***********************************************************************
1432 * DllRegisterServer (DSOUND.@)
1434 HRESULT WINAPI DllRegisterServer(void)
1436 return __wine_register_resources(instance);
1439 /***********************************************************************
1440 * DllUnregisterServer (DSOUND.@)
1442 HRESULT WINAPI DllUnregisterServer(void)
1444 return __wine_unregister_resources(instance);
1446 #endif