Store source send properties for all possible effect slots
[dsound-openal.git] / dsound_main.c
blob8a19176f77e2ae1b2d1f42c400d4d4333dde3ad3
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 static HRESULT get_mmdevenum(IMMDeviceEnumerator **devenum)
479 HRESULT hr, init_hr;
481 init_hr = CoInitialize(NULL);
483 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
484 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)devenum);
485 if(FAILED(hr))
487 if(SUCCEEDED(init_hr))
488 CoUninitialize();
489 *devenum = NULL;
490 ERR("CoCreateInstance failed: %08lx\n", hr);
491 return hr;
494 return init_hr;
497 static void release_mmdevenum(IMMDeviceEnumerator *devenum, HRESULT init_hr)
499 IMMDeviceEnumerator_Release(devenum);
500 if(SUCCEEDED(init_hr))
501 CoUninitialize();
504 static HRESULT get_mmdevice_guid(IMMDevice *device, IPropertyStore *ps, GUID *guid)
506 PROPVARIANT pv;
507 HRESULT hr;
509 if(ps)
510 IPropertyStore_AddRef(ps);
511 else
513 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
514 if(FAILED(hr))
516 WARN("OpenPropertyStore failed: %08lx\n", hr);
517 return hr;
521 PropVariantInit(&pv);
523 hr = IPropertyStore_GetValue(ps, &PKEY_AudioEndpoint_GUID, &pv);
524 if(FAILED(hr))
526 IPropertyStore_Release(ps);
527 WARN("GetValue(GUID) failed: %08lx\n", hr);
528 return hr;
531 CLSIDFromString(pv.pwszVal, guid);
533 PropVariantClear(&pv);
534 IPropertyStore_Release(ps);
536 return S_OK;
540 static BOOL send_device(IMMDevice *device, EDataFlow flow, DeviceList *devlist, PRVTENUMCALLBACK cb, void *user)
542 IPropertyStore *ps;
543 PROPVARIANT pv;
544 BOOL keep_going;
545 size_t dev_count;
546 HRESULT hr;
547 GUID guid;
549 PropVariantInit(&pv);
551 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
552 if(FAILED(hr))
554 WARN("OpenPropertyStore failed: %08lx\n", hr);
555 return TRUE;
558 hr = get_mmdevice_guid(device, ps, &guid);
559 if(FAILED(hr) || (devlist->Count > 0 && IsEqualGUID(&devlist->Guids[0], &guid)))
561 IPropertyStore_Release(ps);
562 return TRUE;
565 hr = IPropertyStore_GetValue(ps, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
566 if(FAILED(hr))
568 IPropertyStore_Release(ps);
569 WARN("GetValue(FriendlyName) failed: %08lx\n", hr);
570 return TRUE;
573 dev_count = devlist->Count++;
574 devlist->Guids[dev_count] = guid;
576 keep_going = FALSE;
577 if(cb)
579 TRACE("Calling back with %s - %ls\n", debugstr_guid(&devlist->Guids[dev_count]),
580 pv.pwszVal);
581 keep_going = cb(flow, &devlist->Guids[dev_count], pv.pwszVal, aldriver_name, user);
584 PropVariantClear(&pv);
585 IPropertyStore_Release(ps);
587 return keep_going;
590 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device)
592 IMMDeviceEnumerator *devenum;
593 IMMDeviceCollection *coll;
594 UINT count, i;
595 HRESULT hr, init_hr;
597 init_hr = get_mmdevenum(&devenum);
598 if(!devenum) return init_hr;
600 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow, DEVICE_STATE_ACTIVE, &coll);
601 if(FAILED(hr))
603 WARN("EnumAudioEndpoints failed: %08lx\n", hr);
604 release_mmdevenum(devenum, init_hr);
605 return hr;
608 hr = IMMDeviceCollection_GetCount(coll, &count);
609 if(FAILED(hr))
611 IMMDeviceCollection_Release(coll);
612 release_mmdevenum(devenum, init_hr);
613 WARN("GetCount failed: %08lx\n", hr);
614 return hr;
617 for(i = 0; i < count;++i)
619 GUID guid;
621 hr = IMMDeviceCollection_Item(coll, i, device);
622 if(FAILED(hr)) continue;
624 hr = get_mmdevice_guid(*device, NULL, &guid);
625 if(FAILED(hr))
627 IMMDevice_Release(*device);
628 continue;
631 if(IsEqualGUID(&guid, tgt))
633 IMMDeviceCollection_Release(coll);
634 release_mmdevenum(devenum, init_hr);
635 return DS_OK;
638 IMMDevice_Release(*device);
641 WARN("No device with GUID %s found!\n", debugstr_guid(tgt));
643 IMMDeviceCollection_Release(coll);
644 release_mmdevenum(devenum, init_hr);
646 return DSERR_INVALIDPARAM;
649 /* S_FALSE means the callback returned FALSE at some point
650 * S_OK means the callback always returned TRUE */
651 HRESULT enumerate_mmdevices(EDataFlow flow, PRVTENUMCALLBACK cb, void *user)
653 static const WCHAR primary_desc[] = L"Primary Sound Driver";
655 IMMDeviceEnumerator *devenum;
656 IMMDeviceCollection *coll;
657 IMMDevice *device;
658 DeviceList *devlist;
659 UINT count, i;
660 BOOL keep_going;
661 HRESULT hr, init_hr;
663 init_hr = get_mmdevenum(&devenum);
664 if(!devenum) return init_hr;
666 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow, DEVICE_STATE_ACTIVE, &coll);
667 if(FAILED(hr))
669 release_mmdevenum(devenum, init_hr);
670 WARN("EnumAudioEndpoints failed: %08lx\n", hr);
671 return DS_OK;
674 hr = IMMDeviceCollection_GetCount(coll, &count);
675 if(FAILED(hr))
677 IMMDeviceCollection_Release(coll);
678 release_mmdevenum(devenum, init_hr);
679 WARN("GetCount failed: %08lx\n", hr);
680 return DS_OK;
683 if(count == 0)
685 IMMDeviceCollection_Release(coll);
686 release_mmdevenum(devenum, init_hr);
687 return DS_OK;
690 devlist = (flow==eCapture) ? &CaptureDevices : &PlaybackDevices;
692 HeapFree(GetProcessHeap(), 0, devlist->Guids);
693 devlist->Count = 0;
694 devlist->Guids = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
695 sizeof(devlist->Guids[0])*count);
697 TRACE("Calling back with NULL (%ls)\n", primary_desc);
698 keep_going = cb(flow, NULL, primary_desc, L"", user);
700 /* always send the default device first */
701 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow, eMultimedia, &device);
702 if(SUCCEEDED(hr))
704 if(!keep_going) cb = NULL;
705 keep_going = send_device(device, flow, devlist, cb, user);
706 IMMDevice_Release(device);
709 for(i = 0;i < count;++i)
711 if(!keep_going)
712 cb = NULL;
714 hr = IMMDeviceCollection_Item(coll, i, &device);
715 if(FAILED(hr))
717 WARN("Item failed: %08lx\n", hr);
718 continue;
721 keep_going = send_device(device, flow, devlist, cb, user);
723 IMMDevice_Release(device);
725 IMMDeviceCollection_Release(coll);
727 release_mmdevenum(devenum, init_hr);
729 return keep_going ? S_OK : S_FALSE;
732 /***************************************************************************
733 * GetDeviceID [DSOUND.9]
735 * Retrieves unique identifier of default device specified
737 * PARAMS
738 * pGuidSrc [I] Address of device GUID.
739 * pGuidDest [O] Address to receive unique device GUID.
741 * RETURNS
742 * Success: DS_OK
743 * Failure: DSERR_INVALIDPARAM
745 * NOTES
746 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
747 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
748 * DSDEVID_DefaultVoiceCapture.
749 * Returns pGuidSrc if pGuidSrc is a valid device or the device
750 * GUID for the specified constants.
752 DECLSPEC_EXPORT HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
754 IMMDeviceEnumerator *devenum;
755 HRESULT hr, init_hr;
756 IMMDevice *device;
757 EDataFlow flow;
758 ERole role;
760 TRACE("(%s, %p)\n", get_device_id(pGuidSrc), pGuidDest);
762 if(!pGuidSrc || !pGuidDest)
763 return DSERR_INVALIDPARAM;
765 flow = eRender;
766 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuidSrc))
767 role = eMultimedia;
768 else if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuidSrc))
769 role = eCommunications;
770 else
772 flow = eCapture;
773 if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuidSrc))
774 role = eMultimedia;
775 else if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuidSrc))
776 role = eCommunications;
777 else
779 *pGuidDest = *pGuidSrc;
780 return DS_OK;
784 init_hr = get_mmdevenum(&devenum);
785 if(!devenum) return init_hr;
787 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow, role, &device);
788 if(FAILED(hr))
790 WARN("GetDefaultAudioEndpoint failed: %08lx\n", hr);
791 release_mmdevenum(devenum, init_hr);
792 return DSERR_NODRIVER;
795 hr = get_mmdevice_guid(device, NULL, pGuidDest);
796 IMMDevice_Release(device);
798 release_mmdevenum(devenum, init_hr);
800 return hr;
804 struct morecontextW {
805 LPDSENUMCALLBACKW callW;
806 LPVOID data;
809 static BOOL CALLBACK w_callback(EDataFlow flow, LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
811 struct morecontextW *context = data;
812 (void)flow;
814 return context->callW(guid, descW, modW, context->data);
817 struct morecontextA {
818 LPDSENUMCALLBACKA callA;
819 LPVOID data;
822 static BOOL CALLBACK w_to_a_callback(EDataFlow flow, LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
824 struct morecontextA *context = data;
825 char *descA, *modA;
826 int dlen, mlen;
827 BOOL ret;
828 (void)flow;
830 dlen = WideCharToMultiByte(CP_ACP, 0, descW, -1, NULL, 0, NULL, NULL);
831 mlen = WideCharToMultiByte(CP_ACP, 0, modW, -1, NULL, 0, NULL, NULL);
832 if(dlen < 0 || mlen < 0) return FALSE;
834 descA = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dlen+mlen+2);
835 if(!descA) return FALSE;
836 modA = descA + dlen+1;
838 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, dlen, NULL, NULL);
839 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, mlen, NULL, NULL);
841 ret = context->callA(guid, descA, modA, context->data);
843 HeapFree(GetProcessHeap(), 0, descA);
844 return ret;
847 /***************************************************************************
848 * DirectSoundEnumerateA [DSOUND.2]
850 * Enumerate all DirectSound drivers installed in the system
852 * PARAMS
853 * lpDSEnumCallback [I] Address of callback function.
854 * lpContext [I] Address of user defined context passed to callback function.
856 * RETURNS
857 * Success: DS_OK
858 * Failure: DSERR_INVALIDPARAM
860 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundEnumerateA(
861 LPDSENUMCALLBACKA lpDSEnumCallback,
862 LPVOID lpContext)
864 struct morecontextA ctx;
865 HRESULT hr;
867 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
869 if(lpDSEnumCallback == NULL)
871 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
872 return DSERR_INVALIDPARAM;
875 ctx.callA = lpDSEnumCallback;
876 ctx.data = lpContext;
878 hr = enumerate_mmdevices(eRender, w_to_a_callback, &ctx);
879 return SUCCEEDED(hr) ? DS_OK : hr;
883 /***************************************************************************
884 * DirectSoundEnumerateW [DSOUND.3]
886 * Enumerate all DirectSound drivers installed in the system
888 * PARAMS
889 * lpDSEnumCallback [I] Address of callback function.
890 * lpContext [I] Address of user defined context passed to callback function.
892 * RETURNS
893 * Success: DS_OK
894 * Failure: DSERR_INVALIDPARAM
896 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundEnumerateW(
897 LPDSENUMCALLBACKW lpDSEnumCallback,
898 LPVOID lpContext )
900 struct morecontextW ctx;
901 HRESULT hr;
903 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
905 if(lpDSEnumCallback == NULL)
907 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
908 return DSERR_INVALIDPARAM;
911 ctx.callW = lpDSEnumCallback;
912 ctx.data = lpContext;
914 hr = enumerate_mmdevices(eRender, w_callback, &ctx);
915 return SUCCEEDED(hr) ? DS_OK : hr;
918 /***************************************************************************
919 * DirectSoundCaptureEnumerateA [DSOUND.7]
921 * Enumerate all DirectSound drivers installed in the system.
923 * PARAMS
924 * lpDSEnumCallback [I] Address of callback function.
925 * lpContext [I] Address of user defined context passed to callback function.
927 * RETURNS
928 * Success: DS_OK
929 * Failure: DSERR_INVALIDPARAM
931 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundCaptureEnumerateA(
932 LPDSENUMCALLBACKA lpDSEnumCallback,
933 LPVOID lpContext)
935 struct morecontextA ctx;
936 HRESULT hr;
938 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
940 if(lpDSEnumCallback == NULL)
942 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
943 return DSERR_INVALIDPARAM;
946 ctx.callA = lpDSEnumCallback;
947 ctx.data = lpContext;
949 hr = enumerate_mmdevices(eCapture, w_to_a_callback, &ctx);
950 return SUCCEEDED(hr) ? DS_OK : hr;
953 /***************************************************************************
954 * DirectSoundCaptureEnumerateW [DSOUND.8]
956 * Enumerate all DirectSound drivers installed in the system.
958 * PARAMS
959 * lpDSEnumCallback [I] Address of callback function.
960 * lpContext [I] Address of user defined context passed to callback function.
962 * RETURNS
963 * Success: DS_OK
964 * Failure: DSERR_INVALIDPARAM
966 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundCaptureEnumerateW(
967 LPDSENUMCALLBACKW lpDSEnumCallback,
968 LPVOID lpContext)
970 struct morecontextW ctx;
971 HRESULT hr;
973 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
975 if(lpDSEnumCallback == NULL)
977 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
978 return DSERR_INVALIDPARAM;
981 ctx.callW = lpDSEnumCallback;
982 ctx.data = lpContext;
984 hr = enumerate_mmdevices(eCapture, w_callback, &ctx);
985 return SUCCEEDED(hr) ? DS_OK : hr;
988 /*******************************************************************************
989 * DirectSoundCreate (DSOUND.1)
991 * Creates and initializes a DirectSound interface.
993 * PARAMS
994 * lpcGUID [I] Address of the GUID that identifies the sound device.
995 * ppDS [O] Address of a variable to receive the interface pointer.
996 * pUnkOuter [I] Must be NULL.
998 * RETURNS
999 * Success: DS_OK
1000 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
1001 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
1003 DECLSPEC_EXPORT HRESULT WINAPI
1004 DirectSoundCreate(LPCGUID lpcGUID, IDirectSound **ppDS, IUnknown *pUnkOuter)
1006 HRESULT hr;
1007 void *pDS;
1009 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
1011 if (ppDS == NULL) {
1012 WARN("invalid parameter: ppDS == NULL\n");
1013 return DSERR_INVALIDPARAM;
1015 *ppDS = NULL;
1017 if (pUnkOuter != NULL) {
1018 WARN("invalid parameter: pUnkOuter != NULL\n");
1019 return DSERR_INVALIDPARAM;
1022 hr = DSOUND_Create(&IID_IDirectSound, &pDS);
1023 if(SUCCEEDED(hr))
1025 *ppDS = pDS;
1026 hr = IDirectSound_Initialize(*ppDS, lpcGUID);
1027 if(FAILED(hr))
1029 IDirectSound_Release(*ppDS);
1030 *ppDS = NULL;
1034 return hr;
1037 /*******************************************************************************
1038 * DirectSoundCreate8 (DSOUND.11)
1040 * Creates and initializes a DirectSound8 interface.
1042 * PARAMS
1043 * lpcGUID [I] Address of the GUID that identifies the sound device.
1044 * ppDS [O] Address of a variable to receive the interface pointer.
1045 * pUnkOuter [I] Must be NULL.
1047 * RETURNS
1048 * Success: DS_OK
1049 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
1050 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
1052 DECLSPEC_EXPORT HRESULT WINAPI
1053 DirectSoundCreate8(LPCGUID lpcGUID, IDirectSound8 **ppDS, IUnknown *pUnkOuter)
1055 HRESULT hr;
1056 void *pDS;
1058 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
1060 if (ppDS == NULL) {
1061 WARN("invalid parameter: ppDS == NULL\n");
1062 return DSERR_INVALIDPARAM;
1064 *ppDS = NULL;
1066 if (pUnkOuter != NULL) {
1067 WARN("invalid parameter: pUnkOuter != NULL\n");
1068 return DSERR_INVALIDPARAM;
1071 hr = DSOUND_Create8(&IID_IDirectSound8, &pDS);
1072 if(SUCCEEDED(hr))
1074 *ppDS = pDS;
1075 hr = IDirectSound8_Initialize(*ppDS, lpcGUID);
1076 if(FAILED(hr))
1078 IDirectSound8_Release(*ppDS);
1079 *ppDS = NULL;
1083 return hr;
1086 /***************************************************************************
1087 * DirectSoundCaptureCreate [DSOUND.6]
1089 * Create and initialize a DirectSoundCapture interface.
1091 * PARAMS
1092 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1093 * lplpDSC [O] Address of a variable to receive the interface pointer.
1094 * pUnkOuter [I] Must be NULL.
1096 * RETURNS
1097 * Success: DS_OK
1098 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1099 * DSERR_OUTOFMEMORY
1101 * NOTES
1102 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1103 * or NULL for the default device or DSDEVID_DefaultCapture or
1104 * DSDEVID_DefaultVoiceCapture.
1106 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1108 DECLSPEC_EXPORT HRESULT WINAPI
1109 DirectSoundCaptureCreate(LPCGUID lpcGUID, IDirectSoundCapture **ppDSC, IUnknown *pUnkOuter)
1111 HRESULT hr;
1112 void *pDSC;
1114 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
1116 if(pUnkOuter)
1118 WARN("invalid parameter: pUnkOuter != NULL\n");
1119 return DSERR_NOAGGREGATION;
1122 if(ppDSC == NULL)
1124 WARN("invalid parameter: ppDSC == NULL\n");
1125 return DSERR_INVALIDPARAM;
1127 *ppDSC = NULL;
1129 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, &pDSC);
1130 if(SUCCEEDED(hr))
1132 *ppDSC = pDSC;
1133 hr = IDirectSoundCapture_Initialize(*ppDSC, lpcGUID);
1134 if(FAILED(hr))
1136 IDirectSoundCapture_Release(*ppDSC);
1137 *ppDSC = NULL;
1141 return hr;
1144 /***************************************************************************
1145 * DirectSoundCaptureCreate8 [DSOUND.12]
1147 * Create and initialize a DirectSoundCapture interface.
1149 * PARAMS
1150 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1151 * lplpDSC [O] Address of a variable to receive the interface pointer.
1152 * pUnkOuter [I] Must be NULL.
1154 * RETURNS
1155 * Success: DS_OK
1156 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1157 * DSERR_OUTOFMEMORY
1159 * NOTES
1160 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1161 * or NULL for the default device or DSDEVID_DefaultCapture or
1162 * DSDEVID_DefaultVoiceCapture.
1164 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1166 DECLSPEC_EXPORT HRESULT WINAPI
1167 DirectSoundCaptureCreate8(LPCGUID lpcGUID, IDirectSoundCapture8 **ppDSC8, IUnknown *pUnkOuter)
1169 HRESULT hr;
1170 void *pDSC8;
1172 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
1174 if(pUnkOuter)
1176 WARN("invalid parameter: pUnkOuter != NULL\n");
1177 return DSERR_NOAGGREGATION;
1180 if(ppDSC8 == NULL)
1182 WARN("invalid parameter: ppDSC8 == NULL\n");
1183 return DSERR_INVALIDPARAM;
1185 *ppDSC8 = NULL;
1187 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture, &pDSC8);
1188 if(SUCCEEDED(hr))
1190 *ppDSC8 = pDSC8;
1191 hr = IDirectSoundCapture_Initialize(*ppDSC8, lpcGUID);
1192 if(FAILED(hr))
1194 IDirectSoundCapture_Release(*ppDSC8);
1195 *ppDSC8 = NULL;
1199 return hr;
1202 /*******************************************************************************
1203 * DirectSound ClassFactory
1206 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
1208 typedef struct {
1209 IClassFactory IClassFactory_iface;
1210 LONG ref;
1211 REFCLSID rclsid;
1212 FnCreateInstance pfnCreateInstance;
1213 } IClassFactoryImpl;
1215 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
1217 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
1220 static HRESULT WINAPI DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
1222 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1223 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
1224 if (ppobj == NULL)
1225 return E_POINTER;
1226 if (IsEqualIID(riid, &IID_IUnknown) ||
1227 IsEqualIID(riid, &IID_IClassFactory))
1229 *ppobj = iface;
1230 IUnknown_AddRef(iface);
1231 return S_OK;
1233 *ppobj = NULL;
1234 return E_NOINTERFACE;
1237 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
1239 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1240 ULONG ref = InterlockedIncrement(&(This->ref));
1241 TRACE("(%p) ref %lu\n", iface, ref);
1242 return ref;
1245 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
1247 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1248 ULONG ref = InterlockedDecrement(&(This->ref));
1249 TRACE("(%p) ref %lu\n", iface, ref);
1250 /* static class, won't be freed */
1251 return ref;
1254 static HRESULT WINAPI DSCF_CreateInstance(
1255 LPCLASSFACTORY iface,
1256 LPUNKNOWN pOuter,
1257 REFIID riid,
1258 LPVOID *ppobj)
1260 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1261 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
1263 if (pOuter)
1264 return CLASS_E_NOAGGREGATION;
1266 if (ppobj == NULL) {
1267 WARN("invalid parameter\n");
1268 return DSERR_INVALIDPARAM;
1270 *ppobj = NULL;
1271 return This->pfnCreateInstance(riid, ppobj);
1274 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
1276 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1277 FIXME("(%p, %d) stub!\n", This, dolock);
1278 return S_OK;
1281 static const IClassFactoryVtbl DSCF_Vtbl = {
1282 DSCF_QueryInterface,
1283 DSCF_AddRef,
1284 DSCF_Release,
1285 DSCF_CreateInstance,
1286 DSCF_LockServer
1289 static IClassFactoryImpl DSOUND_CF[] = {
1290 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound, DSOUND_Create },
1291 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound8, DSOUND_Create8 },
1292 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
1293 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
1294 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
1295 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
1296 { {NULL}, 0, NULL, NULL }
1299 /*******************************************************************************
1300 * DllGetClassObject [DSOUND.@]
1301 * Retrieves class object from a DLL object
1303 * NOTES
1304 * Docs say returns STDAPI
1306 * PARAMS
1307 * rclsid [I] CLSID for the class object
1308 * riid [I] Reference to identifier of interface for class object
1309 * ppv [O] Address of variable to receive interface pointer for riid
1311 * RETURNS
1312 * Success: S_OK
1313 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
1314 * E_UNEXPECTED
1316 DECLSPEC_EXPORT HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1318 int i = 0;
1319 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1321 if (ppv == NULL) {
1322 WARN("invalid parameter\n");
1323 return E_INVALIDARG;
1326 *ppv = NULL;
1328 if (!IsEqualIID(riid, &IID_IClassFactory) &&
1329 !IsEqualIID(riid, &IID_IUnknown)) {
1330 WARN("no interface for %s\n", debugstr_guid(riid));
1331 return E_NOINTERFACE;
1334 while (NULL != DSOUND_CF[i].rclsid) {
1335 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
1336 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
1337 *ppv = &DSOUND_CF[i].IClassFactory_iface;
1338 return S_OK;
1340 i++;
1343 WARN("No class found for %s\n", debugstr_guid(rclsid));
1344 return CLASS_E_CLASSNOTAVAILABLE;
1348 /*******************************************************************************
1349 * DllCanUnloadNow [DSOUND.4]
1350 * Determines whether the DLL is in use.
1352 * RETURNS
1353 * Success: S_OK
1354 * Failure: S_FALSE
1356 DECLSPEC_EXPORT HRESULT WINAPI DllCanUnloadNow(void)
1358 FIXME("(void): stub\n");
1359 return S_FALSE;
1362 /***********************************************************************
1363 * DllMain (DSOUND.init)
1365 DECLSPEC_EXPORT BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
1367 const WCHAR *wstr;
1369 TRACE("(%p, %lu, %p)\n", hInstDLL, fdwReason, lpvReserved);
1371 switch(fdwReason)
1373 case DLL_PROCESS_ATTACH:
1374 LogFile = stderr;
1375 if((wstr=_wgetenv(L"DSOAL_LOGFILE")) != NULL && wstr[0] != 0)
1377 FILE *f = _wfopen(wstr, L"wt");
1378 if(!f) ERR("Failed to open log file %ls\n", wstr);
1379 else LogFile = f;
1382 if(!load_libopenal())
1383 return FALSE;
1384 TlsThreadPtr = TlsAlloc();
1385 /* Increase refcount on dsound by 1 */
1386 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
1387 break;
1389 case DLL_THREAD_ATTACH:
1390 break;
1392 case DLL_THREAD_DETACH:
1393 if(local_contexts)
1394 set_context(NULL);
1395 break;
1397 case DLL_PROCESS_DETACH:
1398 HeapFree(GetProcessHeap(), 0, PlaybackDevices.Guids);
1399 PlaybackDevices.Guids = NULL;
1400 PlaybackDevices.Count = 0;
1401 HeapFree(GetProcessHeap(), 0, CaptureDevices.Guids);
1402 CaptureDevices.Guids = NULL;
1403 CaptureDevices.Count = 0;
1405 if(openal_handle)
1406 FreeLibrary(openal_handle);
1407 TlsFree(TlsThreadPtr);
1408 if(LogFile != stderr)
1409 fclose(LogFile);
1410 LogFile = stderr;
1411 break;
1413 return TRUE;
1416 #ifdef __WINESRC__
1417 /***********************************************************************
1418 * DllRegisterServer (DSOUND.@)
1420 HRESULT WINAPI DllRegisterServer(void)
1422 return __wine_register_resources(instance);
1425 /***********************************************************************
1426 * DllUnregisterServer (DSOUND.@)
1428 HRESULT WINAPI DllUnregisterServer(void)
1430 return __wine_unregister_resources(instance);
1432 #endif