Track FXSlot targets per-buffer
[dsound-openal.git] / dsound_main.c
blob3f3efe7b70cb92540c2d4c24d6e28aaf8b6b8fbd
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 static GUID *EnumeratedDevices = NULL;
63 static size_t EnumeratedDeviceCount = 0;
65 const WCHAR aldriver_name[] = L"dsoal-aldrv.dll";
68 const EAXREVERBPROPERTIES EnvironmentDefaults[EAX_ENVIRONMENT_UNDEFINED] = {
69 REVERB_PRESET_GENERIC,
70 REVERB_PRESET_PADDEDCELL,
71 REVERB_PRESET_ROOM,
72 REVERB_PRESET_BATHROOM,
73 REVERB_PRESET_LIVINGROOM,
74 REVERB_PRESET_STONEROOM,
75 REVERB_PRESET_AUDITORIUM,
76 REVERB_PRESET_CONCERTHALL,
77 REVERB_PRESET_CAVE,
78 REVERB_PRESET_ARENA,
79 REVERB_PRESET_HANGAR,
80 REVERB_PRESET_CARPETEDHALLWAY,
81 REVERB_PRESET_HALLWAY,
82 REVERB_PRESET_STONECORRIDOR,
83 REVERB_PRESET_ALLEY,
84 REVERB_PRESET_FOREST,
85 REVERB_PRESET_CITY,
86 REVERB_PRESET_MOUNTAINS,
87 REVERB_PRESET_QUARRY,
88 REVERB_PRESET_PLAIN,
89 REVERB_PRESET_PARKINGLOT,
90 REVERB_PRESET_SEWERPIPE,
91 REVERB_PRESET_UNDERWATER,
92 REVERB_PRESET_DRUGGED,
93 REVERB_PRESET_DIZZY,
94 REVERB_PRESET_PSYCHOTIC
97 static CRITICAL_SECTION_DEBUG openal_crst_debug =
99 0, 0, &openal_crst,
100 { &openal_crst_debug.ProcessLocksList,
101 &openal_crst_debug.ProcessLocksList },
102 0, 0, { (DWORD_PTR)(__FILE__ ": openal_crst_debug") }
104 CRITICAL_SECTION openal_crst = { &openal_crst_debug, -1, 0, 0, 0, 0 };
106 int openal_loaded = 0;
107 static HANDLE openal_handle = NULL;
108 LPALCCREATECONTEXT palcCreateContext = NULL;
109 LPALCMAKECONTEXTCURRENT palcMakeContextCurrent = NULL;
110 LPALCPROCESSCONTEXT palcProcessContext = NULL;
111 LPALCSUSPENDCONTEXT palcSuspendContext = NULL;
112 LPALCDESTROYCONTEXT palcDestroyContext = NULL;
113 LPALCGETCURRENTCONTEXT palcGetCurrentContext = NULL;
114 LPALCGETCONTEXTSDEVICE palcGetContextsDevice = NULL;
115 LPALCOPENDEVICE palcOpenDevice = NULL;
116 LPALCCLOSEDEVICE palcCloseDevice = NULL;
117 LPALCGETERROR palcGetError = NULL;
118 LPALCISEXTENSIONPRESENT palcIsExtensionPresent = NULL;
119 LPALCGETPROCADDRESS palcGetProcAddress = NULL;
120 LPALCGETENUMVALUE palcGetEnumValue = NULL;
121 LPALCGETSTRING palcGetString = NULL;
122 LPALCGETINTEGERV palcGetIntegerv = NULL;
123 LPALCCAPTUREOPENDEVICE palcCaptureOpenDevice = NULL;
124 LPALCCAPTURECLOSEDEVICE palcCaptureCloseDevice = NULL;
125 LPALCCAPTURESTART palcCaptureStart = NULL;
126 LPALCCAPTURESTOP palcCaptureStop = NULL;
127 LPALCCAPTURESAMPLES palcCaptureSamples = NULL;
128 LPALENABLE palEnable = NULL;
129 LPALDISABLE palDisable = NULL;
130 LPALISENABLED palIsEnabled = NULL;
131 LPALGETSTRING palGetString = NULL;
132 LPALGETBOOLEANV palGetBooleanv = NULL;
133 LPALGETINTEGERV palGetIntegerv = NULL;
134 LPALGETFLOATV palGetFloatv = NULL;
135 LPALGETDOUBLEV palGetDoublev = NULL;
136 LPALGETBOOLEAN palGetBoolean = NULL;
137 LPALGETINTEGER palGetInteger = NULL;
138 LPALGETFLOAT palGetFloat = NULL;
139 LPALGETDOUBLE palGetDouble = NULL;
140 LPALGETERROR palGetError = NULL;
141 LPALISEXTENSIONPRESENT palIsExtensionPresent = NULL;
142 LPALGETPROCADDRESS palGetProcAddress = NULL;
143 LPALGETENUMVALUE palGetEnumValue = NULL;
144 LPALLISTENERF palListenerf = NULL;
145 LPALLISTENER3F palListener3f = NULL;
146 LPALLISTENERFV palListenerfv = NULL;
147 LPALLISTENERI palListeneri = NULL;
148 LPALLISTENER3I palListener3i = NULL;
149 LPALLISTENERIV palListeneriv = NULL;
150 LPALGETLISTENERF palGetListenerf = NULL;
151 LPALGETLISTENER3F palGetListener3f = NULL;
152 LPALGETLISTENERFV palGetListenerfv = NULL;
153 LPALGETLISTENERI palGetListeneri = NULL;
154 LPALGETLISTENER3I palGetListener3i = NULL;
155 LPALGETLISTENERIV palGetListeneriv = NULL;
156 LPALGENSOURCES palGenSources = NULL;
157 LPALDELETESOURCES palDeleteSources = NULL;
158 LPALISSOURCE palIsSource = NULL;
159 LPALSOURCEF palSourcef = NULL;
160 LPALSOURCE3F palSource3f = NULL;
161 LPALSOURCEFV palSourcefv = NULL;
162 LPALSOURCEI palSourcei = NULL;
163 LPALSOURCE3I palSource3i = NULL;
164 LPALSOURCEIV palSourceiv = NULL;
165 LPALGETSOURCEF palGetSourcef = NULL;
166 LPALGETSOURCE3F palGetSource3f = NULL;
167 LPALGETSOURCEFV palGetSourcefv = NULL;
168 LPALGETSOURCEI palGetSourcei = NULL;
169 LPALGETSOURCE3I palGetSource3i = NULL;
170 LPALGETSOURCEIV palGetSourceiv = NULL;
171 LPALSOURCEPLAYV palSourcePlayv = NULL;
172 LPALSOURCESTOPV palSourceStopv = NULL;
173 LPALSOURCEREWINDV palSourceRewindv = NULL;
174 LPALSOURCEPAUSEV palSourcePausev = NULL;
175 LPALSOURCEPLAY palSourcePlay = NULL;
176 LPALSOURCESTOP palSourceStop = NULL;
177 LPALSOURCEREWIND palSourceRewind = NULL;
178 LPALSOURCEPAUSE palSourcePause = NULL;
179 LPALSOURCEQUEUEBUFFERS palSourceQueueBuffers = NULL;
180 LPALSOURCEUNQUEUEBUFFERS palSourceUnqueueBuffers = NULL;
181 LPALGENBUFFERS palGenBuffers = NULL;
182 LPALDELETEBUFFERS palDeleteBuffers = NULL;
183 LPALISBUFFER palIsBuffer = NULL;
184 LPALBUFFERF palBufferf = NULL;
185 LPALBUFFER3F palBuffer3f = NULL;
186 LPALBUFFERFV palBufferfv = NULL;
187 LPALBUFFERI palBufferi = NULL;
188 LPALBUFFER3I palBuffer3i = NULL;
189 LPALBUFFERIV palBufferiv = NULL;
190 LPALGETBUFFERF palGetBufferf = NULL;
191 LPALGETBUFFER3F palGetBuffer3f = NULL;
192 LPALGETBUFFERFV palGetBufferfv = NULL;
193 LPALGETBUFFERI palGetBufferi = NULL;
194 LPALGETBUFFER3I palGetBuffer3i = NULL;
195 LPALGETBUFFERIV palGetBufferiv = NULL;
196 LPALBUFFERDATA palBufferData = NULL;
197 LPALDOPPLERFACTOR palDopplerFactor = NULL;
198 LPALDOPPLERVELOCITY palDopplerVelocity = NULL;
199 LPALDISTANCEMODEL palDistanceModel = NULL;
200 LPALSPEEDOFSOUND palSpeedOfSound = NULL;
202 LPALGENFILTERS palGenFilters = NULL;
203 LPALDELETEFILTERS palDeleteFilters = NULL;
204 LPALFILTERI palFilteri = NULL;
205 LPALFILTERF palFilterf = NULL;
206 LPALGENEFFECTS palGenEffects = NULL;
207 LPALDELETEEFFECTS palDeleteEffects = NULL;
208 LPALEFFECTI palEffecti = NULL;
209 LPALEFFECTF palEffectf = NULL;
210 LPALEFFECTFV palEffectfv = NULL;
211 LPALGENAUXILIARYEFFECTSLOTS palGenAuxiliaryEffectSlots = NULL;
212 LPALDELETEAUXILIARYEFFECTSLOTS palDeleteAuxiliaryEffectSlots = NULL;
213 LPALAUXILIARYEFFECTSLOTI palAuxiliaryEffectSloti = NULL;
214 LPALAUXILIARYEFFECTSLOTF palAuxiliaryEffectSlotf = NULL;
215 LPALDEFERUPDATESSOFT palDeferUpdatesSOFT = NULL;
216 LPALPROCESSUPDATESSOFT palProcessUpdatesSOFT = NULL;
217 LPALBUFFERSTORAGESOFT palBufferStorageSOFT = NULL;
218 LPALMAPBUFFERSOFT palMapBufferSOFT = NULL;
219 LPALUNMAPBUFFERSOFT palUnmapBufferSOFT = NULL;
220 LPALFLUSHMAPPEDBUFFERSOFT palFlushMappedBufferSOFT = NULL;
222 LPALCMAKECONTEXTCURRENT set_context;
223 LPALCGETCURRENTCONTEXT get_context;
224 BOOL local_contexts;
227 static void AL_APIENTRY wrap_DeferUpdates(void)
228 { alcSuspendContext(alcGetCurrentContext()); }
229 static void AL_APIENTRY wrap_ProcessUpdates(void)
230 { alcProcessContext(alcGetCurrentContext()); }
232 static void EnterALSectionTLS(ALCcontext *ctx);
233 static void LeaveALSectionTLS(void);
234 static void EnterALSectionGlob(ALCcontext *ctx);
235 static void LeaveALSectionGlob(void);
237 DWORD TlsThreadPtr;
238 void (*EnterALSection)(ALCcontext *ctx) = EnterALSectionGlob;
239 void (*LeaveALSection)(void) = LeaveALSectionGlob;
242 static BOOL load_libopenal(void)
244 BOOL failed = FALSE;
245 const char *str;
247 str = getenv("DSOAL_LOGLEVEL");
248 if(str && *str)
249 LogLevel = atoi(str);
251 openal_handle = LoadLibraryW(aldriver_name);
252 if(!openal_handle)
254 ERR("Couldn't load %ls: %lu\n", aldriver_name, GetLastError());
255 return FALSE;
258 #define LOAD_FUNCPTR(f) do { \
259 if((*((void**)&p##f) = GetProcAddress(openal_handle, #f)) == NULL) \
261 ERR("Couldn't lookup %s in %ls\n", #f, aldriver_name); \
262 failed = TRUE; \
264 } while(0)
266 LOAD_FUNCPTR(alcCreateContext);
267 LOAD_FUNCPTR(alcMakeContextCurrent);
268 LOAD_FUNCPTR(alcProcessContext);
269 LOAD_FUNCPTR(alcSuspendContext);
270 LOAD_FUNCPTR(alcDestroyContext);
271 LOAD_FUNCPTR(alcGetCurrentContext);
272 LOAD_FUNCPTR(alcGetContextsDevice);
273 LOAD_FUNCPTR(alcOpenDevice);
274 LOAD_FUNCPTR(alcCloseDevice);
275 LOAD_FUNCPTR(alcGetError);
276 LOAD_FUNCPTR(alcIsExtensionPresent);
277 LOAD_FUNCPTR(alcGetProcAddress);
278 LOAD_FUNCPTR(alcGetEnumValue);
279 LOAD_FUNCPTR(alcGetString);
280 LOAD_FUNCPTR(alcGetIntegerv);
281 LOAD_FUNCPTR(alcCaptureOpenDevice);
282 LOAD_FUNCPTR(alcCaptureCloseDevice);
283 LOAD_FUNCPTR(alcCaptureStart);
284 LOAD_FUNCPTR(alcCaptureStop);
285 LOAD_FUNCPTR(alcCaptureSamples);
286 LOAD_FUNCPTR(alEnable);
287 LOAD_FUNCPTR(alDisable);
288 LOAD_FUNCPTR(alIsEnabled);
289 LOAD_FUNCPTR(alGetString);
290 LOAD_FUNCPTR(alGetBooleanv);
291 LOAD_FUNCPTR(alGetIntegerv);
292 LOAD_FUNCPTR(alGetFloatv);
293 LOAD_FUNCPTR(alGetDoublev);
294 LOAD_FUNCPTR(alGetBoolean);
295 LOAD_FUNCPTR(alGetInteger);
296 LOAD_FUNCPTR(alGetFloat);
297 LOAD_FUNCPTR(alGetDouble);
298 LOAD_FUNCPTR(alGetError);
299 LOAD_FUNCPTR(alIsExtensionPresent);
300 LOAD_FUNCPTR(alGetProcAddress);
301 LOAD_FUNCPTR(alGetEnumValue);
302 LOAD_FUNCPTR(alListenerf);
303 LOAD_FUNCPTR(alListener3f);
304 LOAD_FUNCPTR(alListenerfv);
305 LOAD_FUNCPTR(alListeneri);
306 LOAD_FUNCPTR(alListener3i);
307 LOAD_FUNCPTR(alListeneriv);
308 LOAD_FUNCPTR(alGetListenerf);
309 LOAD_FUNCPTR(alGetListener3f);
310 LOAD_FUNCPTR(alGetListenerfv);
311 LOAD_FUNCPTR(alGetListeneri);
312 LOAD_FUNCPTR(alGetListener3i);
313 LOAD_FUNCPTR(alGetListeneriv);
314 LOAD_FUNCPTR(alGenSources);
315 LOAD_FUNCPTR(alDeleteSources);
316 LOAD_FUNCPTR(alIsSource);
317 LOAD_FUNCPTR(alSourcef);
318 LOAD_FUNCPTR(alSource3f);
319 LOAD_FUNCPTR(alSourcefv);
320 LOAD_FUNCPTR(alSourcei);
321 LOAD_FUNCPTR(alSource3i);
322 LOAD_FUNCPTR(alSourceiv);
323 LOAD_FUNCPTR(alGetSourcef);
324 LOAD_FUNCPTR(alGetSource3f);
325 LOAD_FUNCPTR(alGetSourcefv);
326 LOAD_FUNCPTR(alGetSourcei);
327 LOAD_FUNCPTR(alGetSource3i);
328 LOAD_FUNCPTR(alGetSourceiv);
329 LOAD_FUNCPTR(alSourcePlayv);
330 LOAD_FUNCPTR(alSourceStopv);
331 LOAD_FUNCPTR(alSourceRewindv);
332 LOAD_FUNCPTR(alSourcePausev);
333 LOAD_FUNCPTR(alSourcePlay);
334 LOAD_FUNCPTR(alSourceStop);
335 LOAD_FUNCPTR(alSourceRewind);
336 LOAD_FUNCPTR(alSourcePause);
337 LOAD_FUNCPTR(alSourceQueueBuffers);
338 LOAD_FUNCPTR(alSourceUnqueueBuffers);
339 LOAD_FUNCPTR(alGenBuffers);
340 LOAD_FUNCPTR(alDeleteBuffers);
341 LOAD_FUNCPTR(alIsBuffer);
342 LOAD_FUNCPTR(alBufferf);
343 LOAD_FUNCPTR(alBuffer3f);
344 LOAD_FUNCPTR(alBufferfv);
345 LOAD_FUNCPTR(alBufferi);
346 LOAD_FUNCPTR(alBuffer3i);
347 LOAD_FUNCPTR(alBufferiv);
348 LOAD_FUNCPTR(alGetBufferf);
349 LOAD_FUNCPTR(alGetBuffer3f);
350 LOAD_FUNCPTR(alGetBufferfv);
351 LOAD_FUNCPTR(alGetBufferi);
352 LOAD_FUNCPTR(alGetBuffer3i);
353 LOAD_FUNCPTR(alGetBufferiv);
354 LOAD_FUNCPTR(alBufferData);
355 LOAD_FUNCPTR(alDopplerFactor);
356 LOAD_FUNCPTR(alDopplerVelocity);
357 LOAD_FUNCPTR(alDistanceModel);
358 LOAD_FUNCPTR(alSpeedOfSound);
359 #undef LOAD_FUNCPTR
360 if (failed)
362 WARN("Unloading %ls\n", aldriver_name);
363 if (openal_handle != NULL)
364 FreeLibrary(openal_handle);
365 openal_handle = NULL;
366 return FALSE;
369 openal_loaded = 1;
370 TRACE("Loaded %ls\n", aldriver_name);
372 #define LOAD_FUNCPTR(f) *((void**)&p##f) = alcGetProcAddress(NULL, #f)
373 LOAD_FUNCPTR(alGenFilters);
374 LOAD_FUNCPTR(alDeleteFilters);
375 LOAD_FUNCPTR(alFilteri);
376 LOAD_FUNCPTR(alFilterf);
377 LOAD_FUNCPTR(alGenEffects);
378 LOAD_FUNCPTR(alDeleteEffects);
379 LOAD_FUNCPTR(alEffecti);
380 LOAD_FUNCPTR(alEffectf);
381 LOAD_FUNCPTR(alEffectfv);
382 LOAD_FUNCPTR(alGenAuxiliaryEffectSlots);
383 LOAD_FUNCPTR(alDeleteAuxiliaryEffectSlots);
384 LOAD_FUNCPTR(alAuxiliaryEffectSloti);
385 LOAD_FUNCPTR(alAuxiliaryEffectSlotf);
386 LOAD_FUNCPTR(alDeferUpdatesSOFT);
387 LOAD_FUNCPTR(alProcessUpdatesSOFT);
388 LOAD_FUNCPTR(alBufferStorageSOFT);
389 LOAD_FUNCPTR(alMapBufferSOFT);
390 LOAD_FUNCPTR(alUnmapBufferSOFT);
391 LOAD_FUNCPTR(alFlushMappedBufferSOFT);
392 #undef LOAD_FUNCPTR
393 if(!palDeferUpdatesSOFT || !palProcessUpdatesSOFT)
395 palDeferUpdatesSOFT = wrap_DeferUpdates;
396 palProcessUpdatesSOFT = wrap_ProcessUpdates;
399 local_contexts = alcIsExtensionPresent(NULL, "ALC_EXT_thread_local_context");
400 if(local_contexts)
402 TRACE("Found ALC_EXT_thread_local_context\n");
404 set_context = alcGetProcAddress(NULL, "alcSetThreadContext");
405 get_context = alcGetProcAddress(NULL, "alcGetThreadContext");
406 if(!set_context || !get_context)
408 ERR("TLS advertised but functions not found, disabling thread local contexts\n");
409 local_contexts = 0;
412 if(!local_contexts)
414 set_context = alcMakeContextCurrent;
415 get_context = alcGetCurrentContext;
417 else
419 EnterALSection = EnterALSectionTLS;
420 LeaveALSection = LeaveALSectionTLS;
423 return TRUE;
427 static void EnterALSectionTLS(ALCcontext *ctx)
429 if(LIKELY(ctx == TlsGetValue(TlsThreadPtr)))
430 return;
432 if(LIKELY(set_context(ctx) != ALC_FALSE))
433 TlsSetValue(TlsThreadPtr, ctx);
434 else
436 ERR("Couldn't set current context!!\n");
437 checkALCError(alcGetContextsDevice(ctx));
440 static void LeaveALSectionTLS(void)
444 static void EnterALSectionGlob(ALCcontext *ctx)
446 EnterCriticalSection(&openal_crst);
447 if(UNLIKELY(alcMakeContextCurrent(ctx) == ALC_FALSE))
449 ERR("Couldn't set current context!!\n");
450 checkALCError(alcGetContextsDevice(ctx));
453 static void LeaveALSectionGlob(void)
455 LeaveCriticalSection(&openal_crst);
459 static const char *get_device_id(LPCGUID pGuid)
461 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuid))
462 return "DSDEVID_DefaultPlayback";
463 if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuid))
464 return "DSDEVID_DefaultVoicePlayback";
465 if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuid))
466 return "DSDEVID_DefaultCapture";
467 if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuid))
468 return "DSDEVID_DefaultVoiceCapture";
469 return debugstr_guid(pGuid);
472 static HRESULT get_mmdevenum(IMMDeviceEnumerator **devenum)
474 HRESULT hr, init_hr;
476 init_hr = CoInitialize(NULL);
478 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
479 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)devenum);
480 if(FAILED(hr))
482 if(SUCCEEDED(init_hr))
483 CoUninitialize();
484 *devenum = NULL;
485 ERR("CoCreateInstance failed: %08lx\n", hr);
486 return hr;
489 return init_hr;
492 static void release_mmdevenum(IMMDeviceEnumerator *devenum, HRESULT init_hr)
494 IMMDeviceEnumerator_Release(devenum);
495 if(SUCCEEDED(init_hr))
496 CoUninitialize();
499 static HRESULT get_mmdevice_guid(IMMDevice *device, IPropertyStore *ps, GUID *guid)
501 PROPVARIANT pv;
502 HRESULT hr;
504 if(ps)
505 IPropertyStore_AddRef(ps);
506 else
508 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
509 if(FAILED(hr))
511 WARN("OpenPropertyStore failed: %08lx\n", hr);
512 return hr;
516 PropVariantInit(&pv);
518 hr = IPropertyStore_GetValue(ps, &PKEY_AudioEndpoint_GUID, &pv);
519 if(FAILED(hr))
521 IPropertyStore_Release(ps);
522 WARN("GetValue(GUID) failed: %08lx\n", hr);
523 return hr;
526 CLSIDFromString(pv.pwszVal, guid);
528 PropVariantClear(&pv);
529 IPropertyStore_Release(ps);
531 return S_OK;
535 static BOOL send_device(IMMDevice *device, const GUID *defguid, EDataFlow flow, PRVTENUMCALLBACK cb, void *user)
537 IPropertyStore *ps;
538 PROPVARIANT pv;
539 BOOL keep_going;
540 size_t dev_count;
541 HRESULT hr;
542 GUID guid;
544 PropVariantInit(&pv);
546 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
547 if(FAILED(hr))
549 WARN("OpenPropertyStore failed: %08lx\n", hr);
550 return TRUE;
553 hr = get_mmdevice_guid(device, ps, &guid);
554 if(FAILED(hr) || (defguid && IsEqualGUID(defguid, &guid)))
556 IPropertyStore_Release(ps);
557 return TRUE;
560 hr = IPropertyStore_GetValue(ps, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
561 if(FAILED(hr))
563 IPropertyStore_Release(ps);
564 WARN("GetValue(FriendlyName) failed: %08lx\n", hr);
565 return TRUE;
568 dev_count = EnumeratedDeviceCount++;
569 EnumeratedDevices[dev_count] = guid;
571 keep_going = FALSE;
572 if(cb)
574 TRACE("Calling back with %s - %ls\n", debugstr_guid(&EnumeratedDevices[dev_count]),
575 pv.pwszVal);
576 keep_going = cb(flow, &EnumeratedDevices[dev_count], pv.pwszVal, aldriver_name, user);
579 PropVariantClear(&pv);
580 IPropertyStore_Release(ps);
582 return keep_going;
585 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device)
587 IMMDeviceEnumerator *devenum;
588 IMMDeviceCollection *coll;
589 UINT count, i;
590 HRESULT hr, init_hr;
592 init_hr = get_mmdevenum(&devenum);
593 if(!devenum) return init_hr;
595 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow, DEVICE_STATE_ACTIVE, &coll);
596 if(FAILED(hr))
598 WARN("EnumAudioEndpoints failed: %08lx\n", hr);
599 release_mmdevenum(devenum, init_hr);
600 return hr;
603 hr = IMMDeviceCollection_GetCount(coll, &count);
604 if(FAILED(hr))
606 IMMDeviceCollection_Release(coll);
607 release_mmdevenum(devenum, init_hr);
608 WARN("GetCount failed: %08lx\n", hr);
609 return hr;
612 for(i = 0; i < count;++i)
614 GUID guid;
616 hr = IMMDeviceCollection_Item(coll, i, device);
617 if(FAILED(hr)) continue;
619 hr = get_mmdevice_guid(*device, NULL, &guid);
620 if(FAILED(hr))
622 IMMDevice_Release(*device);
623 continue;
626 if(IsEqualGUID(&guid, tgt))
628 IMMDeviceCollection_Release(coll);
629 release_mmdevenum(devenum, init_hr);
630 return DS_OK;
633 IMMDevice_Release(*device);
636 WARN("No device with GUID %s found!\n", debugstr_guid(tgt));
638 IMMDeviceCollection_Release(coll);
639 release_mmdevenum(devenum, init_hr);
641 return DSERR_INVALIDPARAM;
644 /* S_FALSE means the callback returned FALSE at some point
645 * S_OK means the callback always returned TRUE */
646 HRESULT enumerate_mmdevices(EDataFlow flow, PRVTENUMCALLBACK cb, void *user)
648 static const WCHAR primary_desc[] = L"Primary Sound Driver";
650 IMMDeviceEnumerator *devenum;
651 IMMDeviceCollection *coll;
652 IMMDevice *device;
653 GUID defguid = GUID_NULL;
654 UINT count, i;
655 BOOL keep_going;
656 HRESULT hr, init_hr;
658 init_hr = get_mmdevenum(&devenum);
659 if(!devenum) return init_hr;
661 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow, DEVICE_STATE_ACTIVE, &coll);
662 if(FAILED(hr))
664 release_mmdevenum(devenum, init_hr);
665 WARN("EnumAudioEndpoints failed: %08lx\n", hr);
666 return DS_OK;
669 hr = IMMDeviceCollection_GetCount(coll, &count);
670 if(FAILED(hr))
672 IMMDeviceCollection_Release(coll);
673 release_mmdevenum(devenum, init_hr);
674 WARN("GetCount failed: %08lx\n", hr);
675 return DS_OK;
678 if(count == 0)
680 IMMDeviceCollection_Release(coll);
681 release_mmdevenum(devenum, init_hr);
682 return DS_OK;
685 HeapFree(GetProcessHeap(), 0, EnumeratedDevices);
686 EnumeratedDeviceCount = 0;
687 EnumeratedDevices = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
688 sizeof(EnumeratedDevices[0])*count);
690 TRACE("Calling back with NULL (%ls)\n", primary_desc);
691 keep_going = cb(flow, NULL, primary_desc, L"", user);
693 /* always send the default device first */
694 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow, eMultimedia, &device);
695 if(SUCCEEDED(hr))
697 if(!keep_going) cb = NULL;
698 keep_going = send_device(device, NULL, flow, cb, user);
699 defguid = EnumeratedDevices[0];
700 IMMDevice_Release(device);
703 for(i = 0;i < count;++i)
705 if(!keep_going)
706 cb = NULL;
708 hr = IMMDeviceCollection_Item(coll, i, &device);
709 if(FAILED(hr))
711 WARN("Item failed: %08lx\n", hr);
712 continue;
715 keep_going = send_device(device, &defguid, flow, cb, user);
717 IMMDevice_Release(device);
719 IMMDeviceCollection_Release(coll);
721 release_mmdevenum(devenum, init_hr);
723 return keep_going ? S_OK : S_FALSE;
726 /***************************************************************************
727 * GetDeviceID [DSOUND.9]
729 * Retrieves unique identifier of default device specified
731 * PARAMS
732 * pGuidSrc [I] Address of device GUID.
733 * pGuidDest [O] Address to receive unique device GUID.
735 * RETURNS
736 * Success: DS_OK
737 * Failure: DSERR_INVALIDPARAM
739 * NOTES
740 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
741 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
742 * DSDEVID_DefaultVoiceCapture.
743 * Returns pGuidSrc if pGuidSrc is a valid device or the device
744 * GUID for the specified constants.
746 DECLSPEC_EXPORT HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
748 IMMDeviceEnumerator *devenum;
749 EDataFlow flow = (EDataFlow)-1;
750 ERole role = (ERole)-1;
751 HRESULT hr, init_hr;
753 TRACE("(%s, %p)\n", get_device_id(pGuidSrc), pGuidDest);
755 if(!pGuidSrc || !pGuidDest)
756 return DSERR_INVALIDPARAM;
758 init_hr = get_mmdevenum(&devenum);
759 if(!devenum) return init_hr;
761 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuidSrc)){
762 role = eMultimedia;
763 flow = eRender;
764 }else if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuidSrc)){
765 role = eCommunications;
766 flow = eRender;
767 }else if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuidSrc)){
768 role = eMultimedia;
769 flow = eCapture;
770 }else if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuidSrc)){
771 role = eCommunications;
772 flow = eCapture;
775 if(role != (ERole)-1 && flow != (EDataFlow)-1)
777 IMMDevice *device;
779 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow, role, &device);
780 if(FAILED(hr))
782 WARN("GetDefaultAudioEndpoint failed: %08lx\n", hr);
783 release_mmdevenum(devenum, init_hr);
784 return DSERR_NODRIVER;
787 hr = get_mmdevice_guid(device, NULL, pGuidDest);
788 IMMDevice_Release(device);
790 release_mmdevenum(devenum, init_hr);
792 return hr;
795 release_mmdevenum(devenum, init_hr);
797 *pGuidDest = *pGuidSrc;
799 return DS_OK;
803 struct morecontextW {
804 LPDSENUMCALLBACKW callW;
805 LPVOID data;
808 static BOOL CALLBACK w_callback(EDataFlow flow, LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
810 struct morecontextW *context = data;
811 (void)flow;
813 return context->callW(guid, descW, modW, context->data);
816 struct morecontextA {
817 LPDSENUMCALLBACKA callA;
818 LPVOID data;
821 static BOOL CALLBACK w_to_a_callback(EDataFlow flow, LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
823 struct morecontextA *context = data;
824 char *descA, *modA;
825 int dlen, mlen;
826 BOOL ret;
827 (void)flow;
829 dlen = WideCharToMultiByte(CP_ACP, 0, descW, -1, NULL, 0, NULL, NULL);
830 mlen = WideCharToMultiByte(CP_ACP, 0, modW, -1, NULL, 0, NULL, NULL);
831 if(dlen < 0 || mlen < 0) return FALSE;
833 descA = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dlen+mlen+2);
834 if(!descA) return FALSE;
835 modA = descA + dlen+1;
837 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, dlen, NULL, NULL);
838 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, mlen, NULL, NULL);
840 ret = context->callA(guid, descA, modA, context->data);
842 HeapFree(GetProcessHeap(), 0, descA);
843 return ret;
846 /***************************************************************************
847 * DirectSoundEnumerateA [DSOUND.2]
849 * Enumerate all DirectSound drivers installed in the system
851 * PARAMS
852 * lpDSEnumCallback [I] Address of callback function.
853 * lpContext [I] Address of user defined context passed to callback function.
855 * RETURNS
856 * Success: DS_OK
857 * Failure: DSERR_INVALIDPARAM
859 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundEnumerateA(
860 LPDSENUMCALLBACKA lpDSEnumCallback,
861 LPVOID lpContext)
863 struct morecontextA ctx;
864 HRESULT hr;
866 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
868 if(lpDSEnumCallback == NULL)
870 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
871 return DSERR_INVALIDPARAM;
874 ctx.callA = lpDSEnumCallback;
875 ctx.data = lpContext;
877 hr = enumerate_mmdevices(eRender, w_to_a_callback, &ctx);
878 return SUCCEEDED(hr) ? DS_OK : hr;
882 /***************************************************************************
883 * DirectSoundEnumerateW [DSOUND.3]
885 * Enumerate all DirectSound drivers installed in the system
887 * PARAMS
888 * lpDSEnumCallback [I] Address of callback function.
889 * lpContext [I] Address of user defined context passed to callback function.
891 * RETURNS
892 * Success: DS_OK
893 * Failure: DSERR_INVALIDPARAM
895 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundEnumerateW(
896 LPDSENUMCALLBACKW lpDSEnumCallback,
897 LPVOID lpContext )
899 struct morecontextW ctx;
900 HRESULT hr;
902 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
904 if(lpDSEnumCallback == NULL)
906 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
907 return DSERR_INVALIDPARAM;
910 ctx.callW = lpDSEnumCallback;
911 ctx.data = lpContext;
913 hr = enumerate_mmdevices(eRender, w_callback, &ctx);
914 return SUCCEEDED(hr) ? DS_OK : hr;
917 /***************************************************************************
918 * DirectSoundCaptureEnumerateA [DSOUND.7]
920 * Enumerate all DirectSound drivers installed in the system.
922 * PARAMS
923 * lpDSEnumCallback [I] Address of callback function.
924 * lpContext [I] Address of user defined context passed to callback function.
926 * RETURNS
927 * Success: DS_OK
928 * Failure: DSERR_INVALIDPARAM
930 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundCaptureEnumerateA(
931 LPDSENUMCALLBACKA lpDSEnumCallback,
932 LPVOID lpContext)
934 struct morecontextA ctx;
935 HRESULT hr;
937 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
939 if(lpDSEnumCallback == NULL)
941 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
942 return DSERR_INVALIDPARAM;
945 ctx.callA = lpDSEnumCallback;
946 ctx.data = lpContext;
948 hr = enumerate_mmdevices(eCapture, w_to_a_callback, &ctx);
949 return SUCCEEDED(hr) ? DS_OK : hr;
952 /***************************************************************************
953 * DirectSoundCaptureEnumerateW [DSOUND.8]
955 * Enumerate all DirectSound drivers installed in the system.
957 * PARAMS
958 * lpDSEnumCallback [I] Address of callback function.
959 * lpContext [I] Address of user defined context passed to callback function.
961 * RETURNS
962 * Success: DS_OK
963 * Failure: DSERR_INVALIDPARAM
965 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundCaptureEnumerateW(
966 LPDSENUMCALLBACKW lpDSEnumCallback,
967 LPVOID lpContext)
969 struct morecontextW ctx;
970 HRESULT hr;
972 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
974 if(lpDSEnumCallback == NULL)
976 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
977 return DSERR_INVALIDPARAM;
980 ctx.callW = lpDSEnumCallback;
981 ctx.data = lpContext;
983 hr = enumerate_mmdevices(eCapture, w_callback, &ctx);
984 return SUCCEEDED(hr) ? DS_OK : hr;
987 /*******************************************************************************
988 * DirectSoundCreate (DSOUND.1)
990 * Creates and initializes a DirectSound interface.
992 * PARAMS
993 * lpcGUID [I] Address of the GUID that identifies the sound device.
994 * ppDS [O] Address of a variable to receive the interface pointer.
995 * pUnkOuter [I] Must be NULL.
997 * RETURNS
998 * Success: DS_OK
999 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
1000 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
1002 DECLSPEC_EXPORT HRESULT WINAPI
1003 DirectSoundCreate(LPCGUID lpcGUID, IDirectSound **ppDS, IUnknown *pUnkOuter)
1005 HRESULT hr;
1006 void *pDS;
1008 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
1010 if (ppDS == NULL) {
1011 WARN("invalid parameter: ppDS == NULL\n");
1012 return DSERR_INVALIDPARAM;
1014 *ppDS = NULL;
1016 if (pUnkOuter != NULL) {
1017 WARN("invalid parameter: pUnkOuter != NULL\n");
1018 return DSERR_INVALIDPARAM;
1021 hr = DSOUND_Create(&IID_IDirectSound, &pDS);
1022 if(SUCCEEDED(hr))
1024 *ppDS = pDS;
1025 hr = IDirectSound_Initialize(*ppDS, lpcGUID);
1026 if(FAILED(hr))
1028 IDirectSound_Release(*ppDS);
1029 *ppDS = NULL;
1033 return hr;
1036 /*******************************************************************************
1037 * DirectSoundCreate8 (DSOUND.11)
1039 * Creates and initializes a DirectSound8 interface.
1041 * PARAMS
1042 * lpcGUID [I] Address of the GUID that identifies the sound device.
1043 * ppDS [O] Address of a variable to receive the interface pointer.
1044 * pUnkOuter [I] Must be NULL.
1046 * RETURNS
1047 * Success: DS_OK
1048 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
1049 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
1051 DECLSPEC_EXPORT HRESULT WINAPI
1052 DirectSoundCreate8(LPCGUID lpcGUID, IDirectSound8 **ppDS, IUnknown *pUnkOuter)
1054 HRESULT hr;
1055 void *pDS;
1057 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
1059 if (ppDS == NULL) {
1060 WARN("invalid parameter: ppDS == NULL\n");
1061 return DSERR_INVALIDPARAM;
1063 *ppDS = NULL;
1065 if (pUnkOuter != NULL) {
1066 WARN("invalid parameter: pUnkOuter != NULL\n");
1067 return DSERR_INVALIDPARAM;
1070 hr = DSOUND_Create8(&IID_IDirectSound8, &pDS);
1071 if(SUCCEEDED(hr))
1073 *ppDS = pDS;
1074 hr = IDirectSound8_Initialize(*ppDS, lpcGUID);
1075 if(FAILED(hr))
1077 IDirectSound8_Release(*ppDS);
1078 *ppDS = NULL;
1082 return hr;
1085 /***************************************************************************
1086 * DirectSoundCaptureCreate [DSOUND.6]
1088 * Create and initialize a DirectSoundCapture interface.
1090 * PARAMS
1091 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1092 * lplpDSC [O] Address of a variable to receive the interface pointer.
1093 * pUnkOuter [I] Must be NULL.
1095 * RETURNS
1096 * Success: DS_OK
1097 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1098 * DSERR_OUTOFMEMORY
1100 * NOTES
1101 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1102 * or NULL for the default device or DSDEVID_DefaultCapture or
1103 * DSDEVID_DefaultVoiceCapture.
1105 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1107 DECLSPEC_EXPORT HRESULT WINAPI
1108 DirectSoundCaptureCreate(LPCGUID lpcGUID, IDirectSoundCapture **ppDSC, IUnknown *pUnkOuter)
1110 HRESULT hr;
1111 void *pDSC;
1113 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
1115 if(pUnkOuter)
1117 WARN("invalid parameter: pUnkOuter != NULL\n");
1118 return DSERR_NOAGGREGATION;
1121 if(ppDSC == NULL)
1123 WARN("invalid parameter: ppDSC == NULL\n");
1124 return DSERR_INVALIDPARAM;
1126 *ppDSC = NULL;
1128 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, &pDSC);
1129 if(SUCCEEDED(hr))
1131 *ppDSC = pDSC;
1132 hr = IDirectSoundCapture_Initialize(*ppDSC, lpcGUID);
1133 if(FAILED(hr))
1135 IDirectSoundCapture_Release(*ppDSC);
1136 *ppDSC = NULL;
1140 return hr;
1143 /***************************************************************************
1144 * DirectSoundCaptureCreate8 [DSOUND.12]
1146 * Create and initialize a DirectSoundCapture interface.
1148 * PARAMS
1149 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1150 * lplpDSC [O] Address of a variable to receive the interface pointer.
1151 * pUnkOuter [I] Must be NULL.
1153 * RETURNS
1154 * Success: DS_OK
1155 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1156 * DSERR_OUTOFMEMORY
1158 * NOTES
1159 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1160 * or NULL for the default device or DSDEVID_DefaultCapture or
1161 * DSDEVID_DefaultVoiceCapture.
1163 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1165 DECLSPEC_EXPORT HRESULT WINAPI
1166 DirectSoundCaptureCreate8(LPCGUID lpcGUID, IDirectSoundCapture8 **ppDSC8, IUnknown *pUnkOuter)
1168 HRESULT hr;
1169 void *pDSC8;
1171 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
1173 if(pUnkOuter)
1175 WARN("invalid parameter: pUnkOuter != NULL\n");
1176 return DSERR_NOAGGREGATION;
1179 if(ppDSC8 == NULL)
1181 WARN("invalid parameter: ppDSC8 == NULL\n");
1182 return DSERR_INVALIDPARAM;
1184 *ppDSC8 = NULL;
1186 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture, &pDSC8);
1187 if(SUCCEEDED(hr))
1189 *ppDSC8 = pDSC8;
1190 hr = IDirectSoundCapture_Initialize(*ppDSC8, lpcGUID);
1191 if(FAILED(hr))
1193 IDirectSoundCapture_Release(*ppDSC8);
1194 *ppDSC8 = NULL;
1198 return hr;
1201 /*******************************************************************************
1202 * DirectSound ClassFactory
1205 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
1207 typedef struct {
1208 IClassFactory IClassFactory_iface;
1209 LONG ref;
1210 REFCLSID rclsid;
1211 FnCreateInstance pfnCreateInstance;
1212 } IClassFactoryImpl;
1214 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
1216 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
1219 static HRESULT WINAPI DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
1221 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1222 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
1223 if (ppobj == NULL)
1224 return E_POINTER;
1225 if (IsEqualIID(riid, &IID_IUnknown) ||
1226 IsEqualIID(riid, &IID_IClassFactory))
1228 *ppobj = iface;
1229 IUnknown_AddRef(iface);
1230 return S_OK;
1232 *ppobj = NULL;
1233 return E_NOINTERFACE;
1236 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
1238 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1239 ULONG ref = InterlockedIncrement(&(This->ref));
1240 TRACE("(%p) ref %lu\n", iface, ref);
1241 return ref;
1244 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
1246 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1247 ULONG ref = InterlockedDecrement(&(This->ref));
1248 TRACE("(%p) ref %lu\n", iface, ref);
1249 /* static class, won't be freed */
1250 return ref;
1253 static HRESULT WINAPI DSCF_CreateInstance(
1254 LPCLASSFACTORY iface,
1255 LPUNKNOWN pOuter,
1256 REFIID riid,
1257 LPVOID *ppobj)
1259 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1260 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
1262 if (pOuter)
1263 return CLASS_E_NOAGGREGATION;
1265 if (ppobj == NULL) {
1266 WARN("invalid parameter\n");
1267 return DSERR_INVALIDPARAM;
1269 *ppobj = NULL;
1270 return This->pfnCreateInstance(riid, ppobj);
1273 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
1275 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1276 FIXME("(%p, %d) stub!\n", This, dolock);
1277 return S_OK;
1280 static const IClassFactoryVtbl DSCF_Vtbl = {
1281 DSCF_QueryInterface,
1282 DSCF_AddRef,
1283 DSCF_Release,
1284 DSCF_CreateInstance,
1285 DSCF_LockServer
1288 static IClassFactoryImpl DSOUND_CF[] = {
1289 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound, DSOUND_Create },
1290 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound8, DSOUND_Create8 },
1291 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
1292 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
1293 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
1294 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
1295 { {NULL}, 0, NULL, NULL }
1298 /*******************************************************************************
1299 * DllGetClassObject [DSOUND.@]
1300 * Retrieves class object from a DLL object
1302 * NOTES
1303 * Docs say returns STDAPI
1305 * PARAMS
1306 * rclsid [I] CLSID for the class object
1307 * riid [I] Reference to identifier of interface for class object
1308 * ppv [O] Address of variable to receive interface pointer for riid
1310 * RETURNS
1311 * Success: S_OK
1312 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
1313 * E_UNEXPECTED
1315 DECLSPEC_EXPORT HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1317 int i = 0;
1318 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1320 if (ppv == NULL) {
1321 WARN("invalid parameter\n");
1322 return E_INVALIDARG;
1325 *ppv = NULL;
1327 if (!IsEqualIID(riid, &IID_IClassFactory) &&
1328 !IsEqualIID(riid, &IID_IUnknown)) {
1329 WARN("no interface for %s\n", debugstr_guid(riid));
1330 return E_NOINTERFACE;
1333 while (NULL != DSOUND_CF[i].rclsid) {
1334 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
1335 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
1336 *ppv = &DSOUND_CF[i].IClassFactory_iface;
1337 return S_OK;
1339 i++;
1342 WARN("No class found for %s\n", debugstr_guid(rclsid));
1343 return CLASS_E_CLASSNOTAVAILABLE;
1347 /*******************************************************************************
1348 * DllCanUnloadNow [DSOUND.4]
1349 * Determines whether the DLL is in use.
1351 * RETURNS
1352 * Success: S_OK
1353 * Failure: S_FALSE
1355 DECLSPEC_EXPORT HRESULT WINAPI DllCanUnloadNow(void)
1357 FIXME("(void): stub\n");
1358 return S_FALSE;
1361 /***********************************************************************
1362 * DllMain (DSOUND.init)
1364 DECLSPEC_EXPORT BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
1366 const WCHAR *wstr;
1368 TRACE("(%p, %lu, %p)\n", hInstDLL, fdwReason, lpvReserved);
1370 switch(fdwReason)
1372 case DLL_PROCESS_ATTACH:
1373 LogFile = stderr;
1374 if((wstr=_wgetenv(L"DSOAL_LOGFILE")) != NULL && wstr[0] != 0)
1376 FILE *f = _wfopen(wstr, L"wt");
1377 if(!f) ERR("Failed to open log file %ls\n", wstr);
1378 else LogFile = f;
1381 if(!load_libopenal())
1382 return FALSE;
1383 TlsThreadPtr = TlsAlloc();
1384 /* Increase refcount on dsound by 1 */
1385 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
1386 break;
1388 case DLL_THREAD_ATTACH:
1389 break;
1391 case DLL_THREAD_DETACH:
1392 if(local_contexts)
1393 set_context(NULL);
1394 break;
1396 case DLL_PROCESS_DETACH:
1397 HeapFree(GetProcessHeap(), 0, EnumeratedDevices);
1398 EnumeratedDevices = NULL;
1399 EnumeratedDeviceCount = 0;
1401 if(openal_handle)
1402 FreeLibrary(openal_handle);
1403 TlsFree(TlsThreadPtr);
1404 if(LogFile != stderr)
1405 fclose(LogFile);
1406 LogFile = stderr;
1407 break;
1409 return TRUE;
1412 #ifdef __WINESRC__
1413 /***********************************************************************
1414 * DllRegisterServer (DSOUND.@)
1416 HRESULT WINAPI DllRegisterServer(void)
1418 return __wine_register_resources(instance);
1421 /***********************************************************************
1422 * DllUnregisterServer (DSOUND.@)
1424 HRESULT WINAPI DllUnregisterServer(void)
1426 return __wine_unregister_resources(instance);
1428 #endif