Update the source queue and notification position when stopping
[dsound-openal.git] / dsound_main.c
blobb93c58dad5839a9e3143b8dbdc4236a29ec68bbe
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 EAX30LISTENERPROPERTIES 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 LPALDEFERUPDATESSOFT palDeferUpdatesSOFT = NULL;
215 LPALPROCESSUPDATESSOFT palProcessUpdatesSOFT = NULL;
216 LPALBUFFERSTORAGESOFT palBufferStorageSOFT = NULL;
217 LPALMAPBUFFERSOFT palMapBufferSOFT = NULL;
218 LPALUNMAPBUFFERSOFT palUnmapBufferSOFT = NULL;
219 LPALFLUSHMAPPEDBUFFERSOFT palFlushMappedBufferSOFT = NULL;
221 LPALCMAKECONTEXTCURRENT set_context;
222 LPALCGETCURRENTCONTEXT get_context;
223 BOOL local_contexts;
226 static void AL_APIENTRY wrap_DeferUpdates(void)
227 { alcSuspendContext(alcGetCurrentContext()); }
228 static void AL_APIENTRY wrap_ProcessUpdates(void)
229 { alcProcessContext(alcGetCurrentContext()); }
231 static void EnterALSectionTLS(ALCcontext *ctx);
232 static void LeaveALSectionTLS(void);
233 static void EnterALSectionGlob(ALCcontext *ctx);
234 static void LeaveALSectionGlob(void);
236 DWORD TlsThreadPtr;
237 void (*EnterALSection)(ALCcontext *ctx) = EnterALSectionGlob;
238 void (*LeaveALSection)(void) = LeaveALSectionGlob;
241 static BOOL load_libopenal(void)
243 BOOL failed = FALSE;
244 const char *str;
246 str = getenv("DSOAL_LOGLEVEL");
247 if(str && *str)
248 LogLevel = atoi(str);
250 openal_handle = LoadLibraryW(aldriver_name);
251 if(!openal_handle)
253 ERR("Couldn't load %ls: %lu\n", aldriver_name, GetLastError());
254 return FALSE;
257 #define LOAD_FUNCPTR(f) do { \
258 if((*((void**)&p##f) = GetProcAddress(openal_handle, #f)) == NULL) \
260 ERR("Couldn't lookup %s in %ls\n", #f, aldriver_name); \
261 failed = TRUE; \
263 } while(0)
265 LOAD_FUNCPTR(alcCreateContext);
266 LOAD_FUNCPTR(alcMakeContextCurrent);
267 LOAD_FUNCPTR(alcProcessContext);
268 LOAD_FUNCPTR(alcSuspendContext);
269 LOAD_FUNCPTR(alcDestroyContext);
270 LOAD_FUNCPTR(alcGetCurrentContext);
271 LOAD_FUNCPTR(alcGetContextsDevice);
272 LOAD_FUNCPTR(alcOpenDevice);
273 LOAD_FUNCPTR(alcCloseDevice);
274 LOAD_FUNCPTR(alcGetError);
275 LOAD_FUNCPTR(alcIsExtensionPresent);
276 LOAD_FUNCPTR(alcGetProcAddress);
277 LOAD_FUNCPTR(alcGetEnumValue);
278 LOAD_FUNCPTR(alcGetString);
279 LOAD_FUNCPTR(alcGetIntegerv);
280 LOAD_FUNCPTR(alcCaptureOpenDevice);
281 LOAD_FUNCPTR(alcCaptureCloseDevice);
282 LOAD_FUNCPTR(alcCaptureStart);
283 LOAD_FUNCPTR(alcCaptureStop);
284 LOAD_FUNCPTR(alcCaptureSamples);
285 LOAD_FUNCPTR(alEnable);
286 LOAD_FUNCPTR(alDisable);
287 LOAD_FUNCPTR(alIsEnabled);
288 LOAD_FUNCPTR(alGetString);
289 LOAD_FUNCPTR(alGetBooleanv);
290 LOAD_FUNCPTR(alGetIntegerv);
291 LOAD_FUNCPTR(alGetFloatv);
292 LOAD_FUNCPTR(alGetDoublev);
293 LOAD_FUNCPTR(alGetBoolean);
294 LOAD_FUNCPTR(alGetInteger);
295 LOAD_FUNCPTR(alGetFloat);
296 LOAD_FUNCPTR(alGetDouble);
297 LOAD_FUNCPTR(alGetError);
298 LOAD_FUNCPTR(alIsExtensionPresent);
299 LOAD_FUNCPTR(alGetProcAddress);
300 LOAD_FUNCPTR(alGetEnumValue);
301 LOAD_FUNCPTR(alListenerf);
302 LOAD_FUNCPTR(alListener3f);
303 LOAD_FUNCPTR(alListenerfv);
304 LOAD_FUNCPTR(alListeneri);
305 LOAD_FUNCPTR(alListener3i);
306 LOAD_FUNCPTR(alListeneriv);
307 LOAD_FUNCPTR(alGetListenerf);
308 LOAD_FUNCPTR(alGetListener3f);
309 LOAD_FUNCPTR(alGetListenerfv);
310 LOAD_FUNCPTR(alGetListeneri);
311 LOAD_FUNCPTR(alGetListener3i);
312 LOAD_FUNCPTR(alGetListeneriv);
313 LOAD_FUNCPTR(alGenSources);
314 LOAD_FUNCPTR(alDeleteSources);
315 LOAD_FUNCPTR(alIsSource);
316 LOAD_FUNCPTR(alSourcef);
317 LOAD_FUNCPTR(alSource3f);
318 LOAD_FUNCPTR(alSourcefv);
319 LOAD_FUNCPTR(alSourcei);
320 LOAD_FUNCPTR(alSource3i);
321 LOAD_FUNCPTR(alSourceiv);
322 LOAD_FUNCPTR(alGetSourcef);
323 LOAD_FUNCPTR(alGetSource3f);
324 LOAD_FUNCPTR(alGetSourcefv);
325 LOAD_FUNCPTR(alGetSourcei);
326 LOAD_FUNCPTR(alGetSource3i);
327 LOAD_FUNCPTR(alGetSourceiv);
328 LOAD_FUNCPTR(alSourcePlayv);
329 LOAD_FUNCPTR(alSourceStopv);
330 LOAD_FUNCPTR(alSourceRewindv);
331 LOAD_FUNCPTR(alSourcePausev);
332 LOAD_FUNCPTR(alSourcePlay);
333 LOAD_FUNCPTR(alSourceStop);
334 LOAD_FUNCPTR(alSourceRewind);
335 LOAD_FUNCPTR(alSourcePause);
336 LOAD_FUNCPTR(alSourceQueueBuffers);
337 LOAD_FUNCPTR(alSourceUnqueueBuffers);
338 LOAD_FUNCPTR(alGenBuffers);
339 LOAD_FUNCPTR(alDeleteBuffers);
340 LOAD_FUNCPTR(alIsBuffer);
341 LOAD_FUNCPTR(alBufferf);
342 LOAD_FUNCPTR(alBuffer3f);
343 LOAD_FUNCPTR(alBufferfv);
344 LOAD_FUNCPTR(alBufferi);
345 LOAD_FUNCPTR(alBuffer3i);
346 LOAD_FUNCPTR(alBufferiv);
347 LOAD_FUNCPTR(alGetBufferf);
348 LOAD_FUNCPTR(alGetBuffer3f);
349 LOAD_FUNCPTR(alGetBufferfv);
350 LOAD_FUNCPTR(alGetBufferi);
351 LOAD_FUNCPTR(alGetBuffer3i);
352 LOAD_FUNCPTR(alGetBufferiv);
353 LOAD_FUNCPTR(alBufferData);
354 LOAD_FUNCPTR(alDopplerFactor);
355 LOAD_FUNCPTR(alDopplerVelocity);
356 LOAD_FUNCPTR(alDistanceModel);
357 LOAD_FUNCPTR(alSpeedOfSound);
358 #undef LOAD_FUNCPTR
359 if (failed)
361 WARN("Unloading %ls\n", aldriver_name);
362 if (openal_handle != NULL)
363 FreeLibrary(openal_handle);
364 openal_handle = NULL;
365 return FALSE;
368 openal_loaded = 1;
369 TRACE("Loaded %ls\n", aldriver_name);
371 #define LOAD_FUNCPTR(f) *((void**)&p##f) = alcGetProcAddress(NULL, #f)
372 LOAD_FUNCPTR(alGenFilters);
373 LOAD_FUNCPTR(alDeleteFilters);
374 LOAD_FUNCPTR(alFilteri);
375 LOAD_FUNCPTR(alFilterf);
376 LOAD_FUNCPTR(alGenEffects);
377 LOAD_FUNCPTR(alDeleteEffects);
378 LOAD_FUNCPTR(alEffecti);
379 LOAD_FUNCPTR(alEffectf);
380 LOAD_FUNCPTR(alEffectfv);
381 LOAD_FUNCPTR(alGenAuxiliaryEffectSlots);
382 LOAD_FUNCPTR(alDeleteAuxiliaryEffectSlots);
383 LOAD_FUNCPTR(alAuxiliaryEffectSloti);
384 LOAD_FUNCPTR(alDeferUpdatesSOFT);
385 LOAD_FUNCPTR(alProcessUpdatesSOFT);
386 LOAD_FUNCPTR(alBufferStorageSOFT);
387 LOAD_FUNCPTR(alMapBufferSOFT);
388 LOAD_FUNCPTR(alUnmapBufferSOFT);
389 LOAD_FUNCPTR(alFlushMappedBufferSOFT);
390 #undef LOAD_FUNCPTR
391 if(!palDeferUpdatesSOFT || !palProcessUpdatesSOFT)
393 palDeferUpdatesSOFT = wrap_DeferUpdates;
394 palProcessUpdatesSOFT = wrap_ProcessUpdates;
397 local_contexts = alcIsExtensionPresent(NULL, "ALC_EXT_thread_local_context");
398 if(local_contexts)
400 TRACE("Found ALC_EXT_thread_local_context\n");
402 set_context = alcGetProcAddress(NULL, "alcSetThreadContext");
403 get_context = alcGetProcAddress(NULL, "alcGetThreadContext");
404 if(!set_context || !get_context)
406 ERR("TLS advertised but functions not found, disabling thread local contexts\n");
407 local_contexts = 0;
410 if(!local_contexts)
412 set_context = alcMakeContextCurrent;
413 get_context = alcGetCurrentContext;
415 else
417 EnterALSection = EnterALSectionTLS;
418 LeaveALSection = LeaveALSectionTLS;
421 return TRUE;
425 static void EnterALSectionTLS(ALCcontext *ctx)
427 if(LIKELY(ctx == TlsGetValue(TlsThreadPtr)))
428 return;
430 if(LIKELY(set_context(ctx) != ALC_FALSE))
431 TlsSetValue(TlsThreadPtr, ctx);
432 else
434 ERR("Couldn't set current context!!\n");
435 checkALCError(alcGetContextsDevice(ctx));
438 static void LeaveALSectionTLS(void)
442 static void EnterALSectionGlob(ALCcontext *ctx)
444 EnterCriticalSection(&openal_crst);
445 if(UNLIKELY(alcMakeContextCurrent(ctx) == ALC_FALSE))
447 ERR("Couldn't set current context!!\n");
448 checkALCError(alcGetContextsDevice(ctx));
451 static void LeaveALSectionGlob(void)
453 LeaveCriticalSection(&openal_crst);
457 static const char *get_device_id(LPCGUID pGuid)
459 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuid))
460 return "DSDEVID_DefaultPlayback";
461 if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuid))
462 return "DSDEVID_DefaultVoicePlayback";
463 if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuid))
464 return "DSDEVID_DefaultCapture";
465 if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuid))
466 return "DSDEVID_DefaultVoiceCapture";
467 return debugstr_guid(pGuid);
470 static HRESULT get_mmdevenum(IMMDeviceEnumerator **devenum)
472 HRESULT hr, init_hr;
474 init_hr = CoInitialize(NULL);
476 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
477 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)devenum);
478 if(FAILED(hr))
480 if(SUCCEEDED(init_hr))
481 CoUninitialize();
482 *devenum = NULL;
483 ERR("CoCreateInstance failed: %08lx\n", hr);
484 return hr;
487 return init_hr;
490 static void release_mmdevenum(IMMDeviceEnumerator *devenum, HRESULT init_hr)
492 IMMDeviceEnumerator_Release(devenum);
493 if(SUCCEEDED(init_hr))
494 CoUninitialize();
497 static HRESULT get_mmdevice_guid(IMMDevice *device, IPropertyStore *ps, GUID *guid)
499 PROPVARIANT pv;
500 HRESULT hr;
502 if(ps)
503 IPropertyStore_AddRef(ps);
504 else
506 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
507 if(FAILED(hr))
509 WARN("OpenPropertyStore failed: %08lx\n", hr);
510 return hr;
514 PropVariantInit(&pv);
516 hr = IPropertyStore_GetValue(ps, &PKEY_AudioEndpoint_GUID, &pv);
517 if(FAILED(hr))
519 IPropertyStore_Release(ps);
520 WARN("GetValue(GUID) failed: %08lx\n", hr);
521 return hr;
524 CLSIDFromString(pv.pwszVal, guid);
526 PropVariantClear(&pv);
527 IPropertyStore_Release(ps);
529 return S_OK;
533 static BOOL send_device(IMMDevice *device, const GUID *defguid, EDataFlow flow, PRVTENUMCALLBACK cb, void *user)
535 IPropertyStore *ps;
536 PROPVARIANT pv;
537 BOOL keep_going;
538 size_t dev_count;
539 HRESULT hr;
540 GUID guid;
542 PropVariantInit(&pv);
544 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
545 if(FAILED(hr))
547 WARN("OpenPropertyStore failed: %08lx\n", hr);
548 return TRUE;
551 hr = get_mmdevice_guid(device, ps, &guid);
552 if(FAILED(hr) || (defguid && IsEqualGUID(defguid, &guid)))
554 IPropertyStore_Release(ps);
555 return TRUE;
558 hr = IPropertyStore_GetValue(ps, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
559 if(FAILED(hr))
561 IPropertyStore_Release(ps);
562 WARN("GetValue(FriendlyName) failed: %08lx\n", hr);
563 return TRUE;
566 dev_count = EnumeratedDeviceCount++;
567 EnumeratedDevices[dev_count] = guid;
569 keep_going = FALSE;
570 if(cb)
572 TRACE("Calling back with %s - %ls\n", debugstr_guid(&EnumeratedDevices[dev_count]),
573 pv.pwszVal);
574 keep_going = cb(flow, &EnumeratedDevices[dev_count], pv.pwszVal, aldriver_name, user);
577 PropVariantClear(&pv);
578 IPropertyStore_Release(ps);
580 return keep_going;
583 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device)
585 IMMDeviceEnumerator *devenum;
586 IMMDeviceCollection *coll;
587 UINT count, i;
588 HRESULT hr, init_hr;
590 init_hr = get_mmdevenum(&devenum);
591 if(!devenum) return init_hr;
593 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow, DEVICE_STATE_ACTIVE, &coll);
594 if(FAILED(hr))
596 WARN("EnumAudioEndpoints failed: %08lx\n", hr);
597 release_mmdevenum(devenum, init_hr);
598 return hr;
601 hr = IMMDeviceCollection_GetCount(coll, &count);
602 if(FAILED(hr))
604 IMMDeviceCollection_Release(coll);
605 release_mmdevenum(devenum, init_hr);
606 WARN("GetCount failed: %08lx\n", hr);
607 return hr;
610 for(i = 0; i < count;++i)
612 GUID guid;
614 hr = IMMDeviceCollection_Item(coll, i, device);
615 if(FAILED(hr)) continue;
617 hr = get_mmdevice_guid(*device, NULL, &guid);
618 if(FAILED(hr))
620 IMMDevice_Release(*device);
621 continue;
624 if(IsEqualGUID(&guid, tgt))
626 IMMDeviceCollection_Release(coll);
627 release_mmdevenum(devenum, init_hr);
628 return DS_OK;
631 IMMDevice_Release(*device);
634 WARN("No device with GUID %s found!\n", debugstr_guid(tgt));
636 IMMDeviceCollection_Release(coll);
637 release_mmdevenum(devenum, init_hr);
639 return DSERR_INVALIDPARAM;
642 /* S_FALSE means the callback returned FALSE at some point
643 * S_OK means the callback always returned TRUE */
644 HRESULT enumerate_mmdevices(EDataFlow flow, PRVTENUMCALLBACK cb, void *user)
646 static const WCHAR primary_desc[] = L"Primary Sound Driver";
648 IMMDeviceEnumerator *devenum;
649 IMMDeviceCollection *coll;
650 IMMDevice *device;
651 GUID defguid = GUID_NULL;
652 UINT count, i;
653 BOOL keep_going;
654 HRESULT hr, init_hr;
656 init_hr = get_mmdevenum(&devenum);
657 if(!devenum) return init_hr;
659 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow, DEVICE_STATE_ACTIVE, &coll);
660 if(FAILED(hr))
662 release_mmdevenum(devenum, init_hr);
663 WARN("EnumAudioEndpoints failed: %08lx\n", hr);
664 return DS_OK;
667 hr = IMMDeviceCollection_GetCount(coll, &count);
668 if(FAILED(hr))
670 IMMDeviceCollection_Release(coll);
671 release_mmdevenum(devenum, init_hr);
672 WARN("GetCount failed: %08lx\n", hr);
673 return DS_OK;
676 if(count == 0)
678 IMMDeviceCollection_Release(coll);
679 release_mmdevenum(devenum, init_hr);
680 return DS_OK;
683 HeapFree(GetProcessHeap(), 0, EnumeratedDevices);
684 EnumeratedDeviceCount = 0;
685 EnumeratedDevices = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
686 sizeof(EnumeratedDevices[0])*count);
688 TRACE("Calling back with NULL (%ls)\n", primary_desc);
689 keep_going = cb(flow, NULL, primary_desc, L"", user);
691 /* always send the default device first */
692 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow, eMultimedia, &device);
693 if(SUCCEEDED(hr))
695 if(!keep_going) cb = NULL;
696 keep_going = send_device(device, NULL, flow, cb, user);
697 defguid = EnumeratedDevices[0];
698 IMMDevice_Release(device);
701 for(i = 0;i < count;++i)
703 if(!keep_going)
704 cb = NULL;
706 hr = IMMDeviceCollection_Item(coll, i, &device);
707 if(FAILED(hr))
709 WARN("Item failed: %08lx\n", hr);
710 continue;
713 keep_going = send_device(device, &defguid, flow, cb, user);
715 IMMDevice_Release(device);
717 IMMDeviceCollection_Release(coll);
719 release_mmdevenum(devenum, init_hr);
721 return keep_going ? S_OK : S_FALSE;
724 /***************************************************************************
725 * GetDeviceID [DSOUND.9]
727 * Retrieves unique identifier of default device specified
729 * PARAMS
730 * pGuidSrc [I] Address of device GUID.
731 * pGuidDest [O] Address to receive unique device GUID.
733 * RETURNS
734 * Success: DS_OK
735 * Failure: DSERR_INVALIDPARAM
737 * NOTES
738 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
739 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
740 * DSDEVID_DefaultVoiceCapture.
741 * Returns pGuidSrc if pGuidSrc is a valid device or the device
742 * GUID for the specified constants.
744 DECLSPEC_EXPORT HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
746 IMMDeviceEnumerator *devenum;
747 EDataFlow flow = (EDataFlow)-1;
748 ERole role = (ERole)-1;
749 HRESULT hr, init_hr;
751 TRACE("(%s, %p)\n", get_device_id(pGuidSrc), pGuidDest);
753 if(!pGuidSrc || !pGuidDest)
754 return DSERR_INVALIDPARAM;
756 init_hr = get_mmdevenum(&devenum);
757 if(!devenum) return init_hr;
759 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuidSrc)){
760 role = eMultimedia;
761 flow = eRender;
762 }else if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuidSrc)){
763 role = eCommunications;
764 flow = eRender;
765 }else if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuidSrc)){
766 role = eMultimedia;
767 flow = eCapture;
768 }else if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuidSrc)){
769 role = eCommunications;
770 flow = eCapture;
773 if(role != (ERole)-1 && flow != (EDataFlow)-1)
775 IMMDevice *device;
777 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow, role, &device);
778 if(FAILED(hr))
780 WARN("GetDefaultAudioEndpoint failed: %08lx\n", hr);
781 release_mmdevenum(devenum, init_hr);
782 return DSERR_NODRIVER;
785 hr = get_mmdevice_guid(device, NULL, pGuidDest);
786 IMMDevice_Release(device);
788 release_mmdevenum(devenum, init_hr);
790 return hr;
793 release_mmdevenum(devenum, init_hr);
795 *pGuidDest = *pGuidSrc;
797 return DS_OK;
801 struct morecontextW {
802 LPDSENUMCALLBACKW callW;
803 LPVOID data;
806 static BOOL CALLBACK w_callback(EDataFlow flow, LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
808 struct morecontextW *context = data;
809 (void)flow;
811 return context->callW(guid, descW, modW, context->data);
814 struct morecontextA {
815 LPDSENUMCALLBACKA callA;
816 LPVOID data;
819 static BOOL CALLBACK w_to_a_callback(EDataFlow flow, LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
821 struct morecontextA *context = data;
822 char *descA, *modA;
823 int dlen, mlen;
824 BOOL ret;
825 (void)flow;
827 dlen = WideCharToMultiByte(CP_ACP, 0, descW, -1, NULL, 0, NULL, NULL);
828 mlen = WideCharToMultiByte(CP_ACP, 0, modW, -1, NULL, 0, NULL, NULL);
829 if(dlen < 0 || mlen < 0) return FALSE;
831 descA = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dlen+mlen+2);
832 if(!descA) return FALSE;
833 modA = descA + dlen+1;
835 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, dlen, NULL, NULL);
836 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, mlen, NULL, NULL);
838 ret = context->callA(guid, descA, modA, context->data);
840 HeapFree(GetProcessHeap(), 0, descA);
841 return ret;
844 /***************************************************************************
845 * DirectSoundEnumerateA [DSOUND.2]
847 * Enumerate all DirectSound drivers installed in the system
849 * PARAMS
850 * lpDSEnumCallback [I] Address of callback function.
851 * lpContext [I] Address of user defined context passed to callback function.
853 * RETURNS
854 * Success: DS_OK
855 * Failure: DSERR_INVALIDPARAM
857 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundEnumerateA(
858 LPDSENUMCALLBACKA lpDSEnumCallback,
859 LPVOID lpContext)
861 struct morecontextA ctx;
862 HRESULT hr;
864 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
866 if(lpDSEnumCallback == NULL)
868 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
869 return DSERR_INVALIDPARAM;
872 ctx.callA = lpDSEnumCallback;
873 ctx.data = lpContext;
875 hr = enumerate_mmdevices(eRender, w_to_a_callback, &ctx);
876 return SUCCEEDED(hr) ? DS_OK : hr;
880 /***************************************************************************
881 * DirectSoundEnumerateW [DSOUND.3]
883 * Enumerate all DirectSound drivers installed in the system
885 * PARAMS
886 * lpDSEnumCallback [I] Address of callback function.
887 * lpContext [I] Address of user defined context passed to callback function.
889 * RETURNS
890 * Success: DS_OK
891 * Failure: DSERR_INVALIDPARAM
893 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundEnumerateW(
894 LPDSENUMCALLBACKW lpDSEnumCallback,
895 LPVOID lpContext )
897 struct morecontextW ctx;
898 HRESULT hr;
900 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
902 if(lpDSEnumCallback == NULL)
904 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
905 return DSERR_INVALIDPARAM;
908 ctx.callW = lpDSEnumCallback;
909 ctx.data = lpContext;
911 hr = enumerate_mmdevices(eRender, w_callback, &ctx);
912 return SUCCEEDED(hr) ? DS_OK : hr;
915 /***************************************************************************
916 * DirectSoundCaptureEnumerateA [DSOUND.7]
918 * Enumerate all DirectSound drivers installed in the system.
920 * PARAMS
921 * lpDSEnumCallback [I] Address of callback function.
922 * lpContext [I] Address of user defined context passed to callback function.
924 * RETURNS
925 * Success: DS_OK
926 * Failure: DSERR_INVALIDPARAM
928 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundCaptureEnumerateA(
929 LPDSENUMCALLBACKA lpDSEnumCallback,
930 LPVOID lpContext)
932 struct morecontextA ctx;
933 HRESULT hr;
935 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
937 if(lpDSEnumCallback == NULL)
939 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
940 return DSERR_INVALIDPARAM;
943 ctx.callA = lpDSEnumCallback;
944 ctx.data = lpContext;
946 hr = enumerate_mmdevices(eCapture, w_to_a_callback, &ctx);
947 return SUCCEEDED(hr) ? DS_OK : hr;
950 /***************************************************************************
951 * DirectSoundCaptureEnumerateW [DSOUND.8]
953 * Enumerate all DirectSound drivers installed in the system.
955 * PARAMS
956 * lpDSEnumCallback [I] Address of callback function.
957 * lpContext [I] Address of user defined context passed to callback function.
959 * RETURNS
960 * Success: DS_OK
961 * Failure: DSERR_INVALIDPARAM
963 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundCaptureEnumerateW(
964 LPDSENUMCALLBACKW lpDSEnumCallback,
965 LPVOID lpContext)
967 struct morecontextW ctx;
968 HRESULT hr;
970 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
972 if(lpDSEnumCallback == NULL)
974 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
975 return DSERR_INVALIDPARAM;
978 ctx.callW = lpDSEnumCallback;
979 ctx.data = lpContext;
981 hr = enumerate_mmdevices(eCapture, w_callback, &ctx);
982 return SUCCEEDED(hr) ? DS_OK : hr;
985 /*******************************************************************************
986 * DirectSoundCreate (DSOUND.1)
988 * Creates and initializes a DirectSound interface.
990 * PARAMS
991 * lpcGUID [I] Address of the GUID that identifies the sound device.
992 * ppDS [O] Address of a variable to receive the interface pointer.
993 * pUnkOuter [I] Must be NULL.
995 * RETURNS
996 * Success: DS_OK
997 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
998 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
1000 DECLSPEC_EXPORT HRESULT WINAPI
1001 DirectSoundCreate(LPCGUID lpcGUID, IDirectSound **ppDS, IUnknown *pUnkOuter)
1003 HRESULT hr;
1004 void *pDS;
1006 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
1008 if (ppDS == NULL) {
1009 WARN("invalid parameter: ppDS == NULL\n");
1010 return DSERR_INVALIDPARAM;
1012 *ppDS = NULL;
1014 if (pUnkOuter != NULL) {
1015 WARN("invalid parameter: pUnkOuter != NULL\n");
1016 return DSERR_INVALIDPARAM;
1019 hr = DSOUND_Create(&IID_IDirectSound, &pDS);
1020 if(SUCCEEDED(hr))
1022 *ppDS = pDS;
1023 hr = IDirectSound_Initialize(*ppDS, lpcGUID);
1024 if(FAILED(hr))
1026 IDirectSound_Release(*ppDS);
1027 *ppDS = NULL;
1031 return hr;
1034 /*******************************************************************************
1035 * DirectSoundCreate8 (DSOUND.11)
1037 * Creates and initializes a DirectSound8 interface.
1039 * PARAMS
1040 * lpcGUID [I] Address of the GUID that identifies the sound device.
1041 * ppDS [O] Address of a variable to receive the interface pointer.
1042 * pUnkOuter [I] Must be NULL.
1044 * RETURNS
1045 * Success: DS_OK
1046 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
1047 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
1049 DECLSPEC_EXPORT HRESULT WINAPI
1050 DirectSoundCreate8(LPCGUID lpcGUID, IDirectSound8 **ppDS, IUnknown *pUnkOuter)
1052 HRESULT hr;
1053 void *pDS;
1055 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
1057 if (ppDS == NULL) {
1058 WARN("invalid parameter: ppDS == NULL\n");
1059 return DSERR_INVALIDPARAM;
1061 *ppDS = NULL;
1063 if (pUnkOuter != NULL) {
1064 WARN("invalid parameter: pUnkOuter != NULL\n");
1065 return DSERR_INVALIDPARAM;
1068 hr = DSOUND_Create8(&IID_IDirectSound8, &pDS);
1069 if(SUCCEEDED(hr))
1071 *ppDS = pDS;
1072 hr = IDirectSound8_Initialize(*ppDS, lpcGUID);
1073 if(FAILED(hr))
1075 IDirectSound8_Release(*ppDS);
1076 *ppDS = NULL;
1080 return hr;
1083 /***************************************************************************
1084 * DirectSoundCaptureCreate [DSOUND.6]
1086 * Create and initialize a DirectSoundCapture interface.
1088 * PARAMS
1089 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1090 * lplpDSC [O] Address of a variable to receive the interface pointer.
1091 * pUnkOuter [I] Must be NULL.
1093 * RETURNS
1094 * Success: DS_OK
1095 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1096 * DSERR_OUTOFMEMORY
1098 * NOTES
1099 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1100 * or NULL for the default device or DSDEVID_DefaultCapture or
1101 * DSDEVID_DefaultVoiceCapture.
1103 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1105 DECLSPEC_EXPORT HRESULT WINAPI
1106 DirectSoundCaptureCreate(LPCGUID lpcGUID, IDirectSoundCapture **ppDSC, IUnknown *pUnkOuter)
1108 HRESULT hr;
1109 void *pDSC;
1111 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
1113 if(pUnkOuter)
1115 WARN("invalid parameter: pUnkOuter != NULL\n");
1116 return DSERR_NOAGGREGATION;
1119 if(ppDSC == NULL)
1121 WARN("invalid parameter: ppDSC == NULL\n");
1122 return DSERR_INVALIDPARAM;
1124 *ppDSC = NULL;
1126 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, &pDSC);
1127 if(SUCCEEDED(hr))
1129 *ppDSC = pDSC;
1130 hr = IDirectSoundCapture_Initialize(*ppDSC, lpcGUID);
1131 if(FAILED(hr))
1133 IDirectSoundCapture_Release(*ppDSC);
1134 *ppDSC = NULL;
1138 return hr;
1141 /***************************************************************************
1142 * DirectSoundCaptureCreate8 [DSOUND.12]
1144 * Create and initialize a DirectSoundCapture interface.
1146 * PARAMS
1147 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1148 * lplpDSC [O] Address of a variable to receive the interface pointer.
1149 * pUnkOuter [I] Must be NULL.
1151 * RETURNS
1152 * Success: DS_OK
1153 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1154 * DSERR_OUTOFMEMORY
1156 * NOTES
1157 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1158 * or NULL for the default device or DSDEVID_DefaultCapture or
1159 * DSDEVID_DefaultVoiceCapture.
1161 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1163 DECLSPEC_EXPORT HRESULT WINAPI
1164 DirectSoundCaptureCreate8(LPCGUID lpcGUID, IDirectSoundCapture8 **ppDSC8, IUnknown *pUnkOuter)
1166 HRESULT hr;
1167 void *pDSC8;
1169 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
1171 if(pUnkOuter)
1173 WARN("invalid parameter: pUnkOuter != NULL\n");
1174 return DSERR_NOAGGREGATION;
1177 if(ppDSC8 == NULL)
1179 WARN("invalid parameter: ppDSC8 == NULL\n");
1180 return DSERR_INVALIDPARAM;
1182 *ppDSC8 = NULL;
1184 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture, &pDSC8);
1185 if(SUCCEEDED(hr))
1187 *ppDSC8 = pDSC8;
1188 hr = IDirectSoundCapture_Initialize(*ppDSC8, lpcGUID);
1189 if(FAILED(hr))
1191 IDirectSoundCapture_Release(*ppDSC8);
1192 *ppDSC8 = NULL;
1196 return hr;
1199 /*******************************************************************************
1200 * DirectSound ClassFactory
1203 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
1205 typedef struct {
1206 IClassFactory IClassFactory_iface;
1207 LONG ref;
1208 REFCLSID rclsid;
1209 FnCreateInstance pfnCreateInstance;
1210 } IClassFactoryImpl;
1212 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
1214 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
1217 static HRESULT WINAPI DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
1219 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1220 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
1221 if (ppobj == NULL)
1222 return E_POINTER;
1223 if (IsEqualIID(riid, &IID_IUnknown) ||
1224 IsEqualIID(riid, &IID_IClassFactory))
1226 *ppobj = iface;
1227 IUnknown_AddRef(iface);
1228 return S_OK;
1230 *ppobj = NULL;
1231 return E_NOINTERFACE;
1234 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
1236 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1237 ULONG ref = InterlockedIncrement(&(This->ref));
1238 TRACE("(%p) ref %lu\n", iface, ref);
1239 return ref;
1242 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
1244 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1245 ULONG ref = InterlockedDecrement(&(This->ref));
1246 TRACE("(%p) ref %lu\n", iface, ref);
1247 /* static class, won't be freed */
1248 return ref;
1251 static HRESULT WINAPI DSCF_CreateInstance(
1252 LPCLASSFACTORY iface,
1253 LPUNKNOWN pOuter,
1254 REFIID riid,
1255 LPVOID *ppobj)
1257 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1258 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
1260 if (pOuter)
1261 return CLASS_E_NOAGGREGATION;
1263 if (ppobj == NULL) {
1264 WARN("invalid parameter\n");
1265 return DSERR_INVALIDPARAM;
1267 *ppobj = NULL;
1268 return This->pfnCreateInstance(riid, ppobj);
1271 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
1273 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1274 FIXME("(%p, %d) stub!\n", This, dolock);
1275 return S_OK;
1278 static const IClassFactoryVtbl DSCF_Vtbl = {
1279 DSCF_QueryInterface,
1280 DSCF_AddRef,
1281 DSCF_Release,
1282 DSCF_CreateInstance,
1283 DSCF_LockServer
1286 static IClassFactoryImpl DSOUND_CF[] = {
1287 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound, DSOUND_Create },
1288 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound8, DSOUND_Create8 },
1289 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
1290 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
1291 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
1292 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
1293 { {NULL}, 0, NULL, NULL }
1296 /*******************************************************************************
1297 * DllGetClassObject [DSOUND.@]
1298 * Retrieves class object from a DLL object
1300 * NOTES
1301 * Docs say returns STDAPI
1303 * PARAMS
1304 * rclsid [I] CLSID for the class object
1305 * riid [I] Reference to identifier of interface for class object
1306 * ppv [O] Address of variable to receive interface pointer for riid
1308 * RETURNS
1309 * Success: S_OK
1310 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
1311 * E_UNEXPECTED
1313 DECLSPEC_EXPORT HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1315 int i = 0;
1316 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1318 if (ppv == NULL) {
1319 WARN("invalid parameter\n");
1320 return E_INVALIDARG;
1323 *ppv = NULL;
1325 if (!IsEqualIID(riid, &IID_IClassFactory) &&
1326 !IsEqualIID(riid, &IID_IUnknown)) {
1327 WARN("no interface for %s\n", debugstr_guid(riid));
1328 return E_NOINTERFACE;
1331 while (NULL != DSOUND_CF[i].rclsid) {
1332 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
1333 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
1334 *ppv = &DSOUND_CF[i].IClassFactory_iface;
1335 return S_OK;
1337 i++;
1340 WARN("No class found for %s\n", debugstr_guid(rclsid));
1341 return CLASS_E_CLASSNOTAVAILABLE;
1345 /*******************************************************************************
1346 * DllCanUnloadNow [DSOUND.4]
1347 * Determines whether the DLL is in use.
1349 * RETURNS
1350 * Success: S_OK
1351 * Failure: S_FALSE
1353 DECLSPEC_EXPORT HRESULT WINAPI DllCanUnloadNow(void)
1355 FIXME("(void): stub\n");
1356 return S_FALSE;
1359 /***********************************************************************
1360 * DllMain (DSOUND.init)
1362 DECLSPEC_EXPORT BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
1364 const WCHAR *wstr;
1366 TRACE("(%p, %lu, %p)\n", hInstDLL, fdwReason, lpvReserved);
1368 switch(fdwReason)
1370 case DLL_PROCESS_ATTACH:
1371 LogFile = stderr;
1372 if((wstr=_wgetenv(L"DSOAL_LOGFILE")) != NULL && wstr[0] != 0)
1374 FILE *f = _wfopen(wstr, L"wt");
1375 if(!f) ERR("Failed to open log file %ls\n", wstr);
1376 else LogFile = f;
1379 if(!load_libopenal())
1380 return FALSE;
1381 TlsThreadPtr = TlsAlloc();
1382 /* Increase refcount on dsound by 1 */
1383 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
1384 break;
1386 case DLL_THREAD_ATTACH:
1387 break;
1389 case DLL_THREAD_DETACH:
1390 if(local_contexts)
1391 set_context(NULL);
1392 break;
1394 case DLL_PROCESS_DETACH:
1395 HeapFree(GetProcessHeap(), 0, EnumeratedDevices);
1396 EnumeratedDevices = NULL;
1397 EnumeratedDeviceCount = 0;
1399 if(openal_handle)
1400 FreeLibrary(openal_handle);
1401 TlsFree(TlsThreadPtr);
1402 if(LogFile != stderr)
1403 fclose(LogFile);
1404 LogFile = stderr;
1405 break;
1407 return TRUE;
1410 #ifdef __WINESRC__
1411 /***********************************************************************
1412 * DllRegisterServer (DSOUND.@)
1414 HRESULT WINAPI DllRegisterServer(void)
1416 return __wine_register_resources(instance);
1419 /***********************************************************************
1420 * DllUnregisterServer (DSOUND.@)
1422 HRESULT WINAPI DllUnregisterServer(void)
1424 return __wine_unregister_resources(instance);
1426 #endif