Don't bother with an alternate mB-to-gain converter function
[dsound-openal.git] / dsound_main.c
blob8b7632a251a11a8f8863618ef36a0682df6942d9
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 #include <stdarg.h>
37 #include <string.h>
39 #include <windows.h>
40 #include <dsound.h>
41 #include <mmsystem.h>
42 #include <mmdeviceapi.h>
43 #include <devpropdef.h>
45 #include "dsound_private.h"
47 #ifndef DECLSPEC_EXPORT
48 #ifdef _WIN32
49 #define DECLSPEC_EXPORT __declspec(dllexport)
50 #else
51 #define DECLSPEC_EXPORT
52 #endif
53 #endif
55 DEFINE_DEVPROPKEY(DEVPKEY_Device_FriendlyName, 0xa45c254e,0xdf1c,0x4efd,0x80,0x20,0x67,0xd1,0x46,0xa8,0x50,0xe0, 14);
56 DEFINE_GUID(CLSID_DirectSoundPrivate,0x11ab3ec0,0x25ec,0x11d1,0xa4,0xd8,0x00,0xc0,0x4f,0xc2,0x8a,0xca);
58 int LogLevel = 1;
59 FILE *LogFile;
62 const WCHAR wine_vxd_drv[] = L"winemm.vxd";
64 const IID DSPROPSETID_EAX20_ListenerProperties = {
65 0x0306a6a8, 0xb224, 0x11d2, { 0x99, 0xe5, 0x00, 0x00, 0xe8, 0xd8, 0xc7, 0x22 }
68 const IID DSPROPSETID_EAX20_BufferProperties = {
69 0x0306a6a7, 0xb224, 0x11d2, { 0x99, 0xe5, 0x00, 0x00, 0xe8, 0xd8, 0xc7, 0x22 }
72 const EAXLISTENERPROPERTIES EnvironmentDefaults[EAX_ENVIRONMENT_COUNT] =
74 REVERB_PRESET_GENERIC,
75 REVERB_PRESET_PADDEDCELL,
76 REVERB_PRESET_ROOM,
77 REVERB_PRESET_BATHROOM,
78 REVERB_PRESET_LIVINGROOM,
79 REVERB_PRESET_STONEROOM,
80 REVERB_PRESET_AUDITORIUM,
81 REVERB_PRESET_CONCERTHALL,
82 REVERB_PRESET_CAVE,
83 REVERB_PRESET_ARENA,
84 REVERB_PRESET_HANGAR,
85 REVERB_PRESET_CARPETEDHALLWAY,
86 REVERB_PRESET_HALLWAY,
87 REVERB_PRESET_STONECORRIDOR,
88 REVERB_PRESET_ALLEY,
89 REVERB_PRESET_FOREST,
90 REVERB_PRESET_CITY,
91 REVERB_PRESET_MOUNTAINS,
92 REVERB_PRESET_QUARRY,
93 REVERB_PRESET_PLAIN,
94 REVERB_PRESET_PARKINGLOT,
95 REVERB_PRESET_SEWERPIPE,
96 REVERB_PRESET_UNDERWATER,
97 REVERB_PRESET_DRUGGED,
98 REVERB_PRESET_DIZZY,
99 REVERB_PRESET_PSYCHOTIC
102 static CRITICAL_SECTION_DEBUG openal_crst_debug =
104 0, 0, &openal_crst,
105 { &openal_crst_debug.ProcessLocksList,
106 &openal_crst_debug.ProcessLocksList },
107 0, 0, { (DWORD_PTR)(__FILE__ ": openal_crst_debug") }
109 CRITICAL_SECTION openal_crst = { &openal_crst_debug, -1, 0, 0, 0, 0 };
111 int openal_loaded = 0;
112 static HANDLE openal_handle = NULL;
113 LPALCCREATECONTEXT palcCreateContext = NULL;
114 LPALCMAKECONTEXTCURRENT palcMakeContextCurrent = NULL;
115 LPALCPROCESSCONTEXT palcProcessContext = NULL;
116 LPALCSUSPENDCONTEXT palcSuspendContext = NULL;
117 LPALCDESTROYCONTEXT palcDestroyContext = NULL;
118 LPALCGETCURRENTCONTEXT palcGetCurrentContext = NULL;
119 LPALCGETCONTEXTSDEVICE palcGetContextsDevice = NULL;
120 LPALCOPENDEVICE palcOpenDevice = NULL;
121 LPALCCLOSEDEVICE palcCloseDevice = NULL;
122 LPALCGETERROR palcGetError = NULL;
123 LPALCISEXTENSIONPRESENT palcIsExtensionPresent = NULL;
124 LPALCGETPROCADDRESS palcGetProcAddress = NULL;
125 LPALCGETENUMVALUE palcGetEnumValue = NULL;
126 LPALCGETSTRING palcGetString = NULL;
127 LPALCGETINTEGERV palcGetIntegerv = NULL;
128 LPALCCAPTUREOPENDEVICE palcCaptureOpenDevice = NULL;
129 LPALCCAPTURECLOSEDEVICE palcCaptureCloseDevice = NULL;
130 LPALCCAPTURESTART palcCaptureStart = NULL;
131 LPALCCAPTURESTOP palcCaptureStop = NULL;
132 LPALCCAPTURESAMPLES palcCaptureSamples = NULL;
133 LPALENABLE palEnable = NULL;
134 LPALDISABLE palDisable = NULL;
135 LPALISENABLED palIsEnabled = NULL;
136 LPALGETSTRING palGetString = NULL;
137 LPALGETBOOLEANV palGetBooleanv = NULL;
138 LPALGETINTEGERV palGetIntegerv = NULL;
139 LPALGETFLOATV palGetFloatv = NULL;
140 LPALGETDOUBLEV palGetDoublev = NULL;
141 LPALGETBOOLEAN palGetBoolean = NULL;
142 LPALGETINTEGER palGetInteger = NULL;
143 LPALGETFLOAT palGetFloat = NULL;
144 LPALGETDOUBLE palGetDouble = NULL;
145 LPALGETERROR palGetError = NULL;
146 LPALISEXTENSIONPRESENT palIsExtensionPresent = NULL;
147 LPALGETPROCADDRESS palGetProcAddress = NULL;
148 LPALGETENUMVALUE palGetEnumValue = NULL;
149 LPALLISTENERF palListenerf = NULL;
150 LPALLISTENER3F palListener3f = NULL;
151 LPALLISTENERFV palListenerfv = NULL;
152 LPALLISTENERI palListeneri = NULL;
153 LPALLISTENER3I palListener3i = NULL;
154 LPALLISTENERIV palListeneriv = NULL;
155 LPALGETLISTENERF palGetListenerf = NULL;
156 LPALGETLISTENER3F palGetListener3f = NULL;
157 LPALGETLISTENERFV palGetListenerfv = NULL;
158 LPALGETLISTENERI palGetListeneri = NULL;
159 LPALGETLISTENER3I palGetListener3i = NULL;
160 LPALGETLISTENERIV palGetListeneriv = NULL;
161 LPALGENSOURCES palGenSources = NULL;
162 LPALDELETESOURCES palDeleteSources = NULL;
163 LPALISSOURCE palIsSource = NULL;
164 LPALSOURCEF palSourcef = NULL;
165 LPALSOURCE3F palSource3f = NULL;
166 LPALSOURCEFV palSourcefv = NULL;
167 LPALSOURCEI palSourcei = NULL;
168 LPALSOURCE3I palSource3i = NULL;
169 LPALSOURCEIV palSourceiv = NULL;
170 LPALGETSOURCEF palGetSourcef = NULL;
171 LPALGETSOURCE3F palGetSource3f = NULL;
172 LPALGETSOURCEFV palGetSourcefv = NULL;
173 LPALGETSOURCEI palGetSourcei = NULL;
174 LPALGETSOURCE3I palGetSource3i = NULL;
175 LPALGETSOURCEIV palGetSourceiv = NULL;
176 LPALSOURCEPLAYV palSourcePlayv = NULL;
177 LPALSOURCESTOPV palSourceStopv = NULL;
178 LPALSOURCEREWINDV palSourceRewindv = NULL;
179 LPALSOURCEPAUSEV palSourcePausev = NULL;
180 LPALSOURCEPLAY palSourcePlay = NULL;
181 LPALSOURCESTOP palSourceStop = NULL;
182 LPALSOURCEREWIND palSourceRewind = NULL;
183 LPALSOURCEPAUSE palSourcePause = NULL;
184 LPALSOURCEQUEUEBUFFERS palSourceQueueBuffers = NULL;
185 LPALSOURCEUNQUEUEBUFFERS palSourceUnqueueBuffers = NULL;
186 LPALGENBUFFERS palGenBuffers = NULL;
187 LPALDELETEBUFFERS palDeleteBuffers = NULL;
188 LPALISBUFFER palIsBuffer = NULL;
189 LPALBUFFERF palBufferf = NULL;
190 LPALBUFFER3F palBuffer3f = NULL;
191 LPALBUFFERFV palBufferfv = NULL;
192 LPALBUFFERI palBufferi = NULL;
193 LPALBUFFER3I palBuffer3i = NULL;
194 LPALBUFFERIV palBufferiv = NULL;
195 LPALGETBUFFERF palGetBufferf = NULL;
196 LPALGETBUFFER3F palGetBuffer3f = NULL;
197 LPALGETBUFFERFV palGetBufferfv = NULL;
198 LPALGETBUFFERI palGetBufferi = NULL;
199 LPALGETBUFFER3I palGetBuffer3i = NULL;
200 LPALGETBUFFERIV palGetBufferiv = NULL;
201 LPALBUFFERDATA palBufferData = NULL;
202 LPALDOPPLERFACTOR palDopplerFactor = NULL;
203 LPALDOPPLERVELOCITY palDopplerVelocity = NULL;
204 LPALDISTANCEMODEL palDistanceModel = NULL;
205 LPALSPEEDOFSOUND palSpeedOfSound = NULL;
207 LPALGENFILTERS palGenFilters = (void*)0xdeadbee0;//NULL;
208 LPALDELETEFILTERS palDeleteFilters = (void*)0xdeadbee1;//NULL;
209 LPALFILTERI palFilteri = (void*)0xdeadbee2;//NULL;
210 LPALFILTERF palFilterf = (void*)0xdeadbee3;//NULL;
211 LPALGENEFFECTS palGenEffects = (void*)0xdeadbee4;//NULL;
212 LPALDELETEEFFECTS palDeleteEffects = (void*)0xdeadbee5;//NULL;
213 LPALEFFECTI palEffecti = (void*)0xdeadbee6;//NULL;
214 LPALEFFECTF palEffectf = (void*)0xdeadbee7;//NULL;
215 LPALGENAUXILIARYEFFECTSLOTS palGenAuxiliaryEffectSlots = (void*)0xdeadbee8;//NULL;
216 LPALDELETEAUXILIARYEFFECTSLOTS palDeleteAuxiliaryEffectSlots = (void*)0xdeadbee9;//NULL;
217 LPALAUXILIARYEFFECTSLOTI palAuxiliaryEffectSloti = (void*)0xdeadbeea;//NULL;
218 LPALDEFERUPDATESSOFT palDeferUpdatesSOFT = (void*)0xdeadbeeb;//NULL;
219 LPALPROCESSUPDATESSOFT palProcessUpdatesSOFT = (void*)0xdeadbeec;//NULL;
220 LPALBUFFERSTORAGESOFT palBufferStorageSOFT = (void*)0xdeadbeed;//NULL;
221 LPALMAPBUFFERSOFT palMapBufferSOFT = (void*)0xdeadbeee;//NULL;
222 LPALUNMAPBUFFERSOFT palUnmapBufferSOFT = (void*)0xdeadbeef;//NULL;
223 LPALFLUSHMAPPEDBUFFERSOFT palFlushMappedBufferSOFT = (void*)0xdeadbef0;//NULL;
225 LPALCMAKECONTEXTCURRENT set_context;
226 LPALCGETCURRENTCONTEXT get_context;
227 BOOL local_contexts;
230 static void AL_APIENTRY wrap_DeferUpdates(void)
231 { alcSuspendContext(alcGetCurrentContext()); }
232 static void AL_APIENTRY wrap_ProcessUpdates(void)
233 { alcProcessContext(alcGetCurrentContext()); }
235 static void EnterALSectionTLS(ALCcontext *ctx);
236 static void LeaveALSectionTLS(void);
237 static void EnterALSectionGlob(ALCcontext *ctx);
238 static void LeaveALSectionGlob(void);
240 DWORD TlsThreadPtr;
241 void (*EnterALSection)(ALCcontext *ctx) = EnterALSectionGlob;
242 void (*LeaveALSection)(void) = LeaveALSectionGlob;
245 static BOOL load_libopenal(void)
247 const char libname[] = "dsoal-aldrv.dll";
248 BOOL failed = FALSE;
249 const char *str;
251 str = getenv("DSOAL_LOGLEVEL");
252 if(str && *str)
253 LogLevel = atoi(str);
255 openal_handle = LoadLibraryA(libname);
256 if(!openal_handle)
258 ERR("Couldn't load %s: %lu\n", libname, GetLastError());
259 return FALSE;
262 #define LOAD_FUNCPTR(f) do { \
263 if((*((void**)&p##f) = GetProcAddress(openal_handle, #f)) == NULL) \
265 ERR("Couldn't lookup %s in %s\n", #f, libname); \
266 failed = TRUE; \
268 } while(0)
270 LOAD_FUNCPTR(alcCreateContext);
271 LOAD_FUNCPTR(alcMakeContextCurrent);
272 LOAD_FUNCPTR(alcProcessContext);
273 LOAD_FUNCPTR(alcSuspendContext);
274 LOAD_FUNCPTR(alcDestroyContext);
275 LOAD_FUNCPTR(alcGetCurrentContext);
276 LOAD_FUNCPTR(alcGetContextsDevice);
277 LOAD_FUNCPTR(alcOpenDevice);
278 LOAD_FUNCPTR(alcCloseDevice);
279 LOAD_FUNCPTR(alcGetError);
280 LOAD_FUNCPTR(alcIsExtensionPresent);
281 LOAD_FUNCPTR(alcGetProcAddress);
282 LOAD_FUNCPTR(alcGetEnumValue);
283 LOAD_FUNCPTR(alcGetString);
284 LOAD_FUNCPTR(alcGetIntegerv);
285 LOAD_FUNCPTR(alcCaptureOpenDevice);
286 LOAD_FUNCPTR(alcCaptureCloseDevice);
287 LOAD_FUNCPTR(alcCaptureStart);
288 LOAD_FUNCPTR(alcCaptureStop);
289 LOAD_FUNCPTR(alcCaptureSamples);
290 LOAD_FUNCPTR(alEnable);
291 LOAD_FUNCPTR(alDisable);
292 LOAD_FUNCPTR(alIsEnabled);
293 LOAD_FUNCPTR(alGetString);
294 LOAD_FUNCPTR(alGetBooleanv);
295 LOAD_FUNCPTR(alGetIntegerv);
296 LOAD_FUNCPTR(alGetFloatv);
297 LOAD_FUNCPTR(alGetDoublev);
298 LOAD_FUNCPTR(alGetBoolean);
299 LOAD_FUNCPTR(alGetInteger);
300 LOAD_FUNCPTR(alGetFloat);
301 LOAD_FUNCPTR(alGetDouble);
302 LOAD_FUNCPTR(alGetError);
303 LOAD_FUNCPTR(alIsExtensionPresent);
304 LOAD_FUNCPTR(alGetProcAddress);
305 LOAD_FUNCPTR(alGetEnumValue);
306 LOAD_FUNCPTR(alListenerf);
307 LOAD_FUNCPTR(alListener3f);
308 LOAD_FUNCPTR(alListenerfv);
309 LOAD_FUNCPTR(alListeneri);
310 LOAD_FUNCPTR(alListener3i);
311 LOAD_FUNCPTR(alListeneriv);
312 LOAD_FUNCPTR(alGetListenerf);
313 LOAD_FUNCPTR(alGetListener3f);
314 LOAD_FUNCPTR(alGetListenerfv);
315 LOAD_FUNCPTR(alGetListeneri);
316 LOAD_FUNCPTR(alGetListener3i);
317 LOAD_FUNCPTR(alGetListeneriv);
318 LOAD_FUNCPTR(alGenSources);
319 LOAD_FUNCPTR(alDeleteSources);
320 LOAD_FUNCPTR(alIsSource);
321 LOAD_FUNCPTR(alSourcef);
322 LOAD_FUNCPTR(alSource3f);
323 LOAD_FUNCPTR(alSourcefv);
324 LOAD_FUNCPTR(alSourcei);
325 LOAD_FUNCPTR(alSource3i);
326 LOAD_FUNCPTR(alSourceiv);
327 LOAD_FUNCPTR(alGetSourcef);
328 LOAD_FUNCPTR(alGetSource3f);
329 LOAD_FUNCPTR(alGetSourcefv);
330 LOAD_FUNCPTR(alGetSourcei);
331 LOAD_FUNCPTR(alGetSource3i);
332 LOAD_FUNCPTR(alGetSourceiv);
333 LOAD_FUNCPTR(alSourcePlayv);
334 LOAD_FUNCPTR(alSourceStopv);
335 LOAD_FUNCPTR(alSourceRewindv);
336 LOAD_FUNCPTR(alSourcePausev);
337 LOAD_FUNCPTR(alSourcePlay);
338 LOAD_FUNCPTR(alSourceStop);
339 LOAD_FUNCPTR(alSourceRewind);
340 LOAD_FUNCPTR(alSourcePause);
341 LOAD_FUNCPTR(alSourceQueueBuffers);
342 LOAD_FUNCPTR(alSourceUnqueueBuffers);
343 LOAD_FUNCPTR(alGenBuffers);
344 LOAD_FUNCPTR(alDeleteBuffers);
345 LOAD_FUNCPTR(alIsBuffer);
346 LOAD_FUNCPTR(alBufferf);
347 LOAD_FUNCPTR(alBuffer3f);
348 LOAD_FUNCPTR(alBufferfv);
349 LOAD_FUNCPTR(alBufferi);
350 LOAD_FUNCPTR(alBuffer3i);
351 LOAD_FUNCPTR(alBufferiv);
352 LOAD_FUNCPTR(alGetBufferf);
353 LOAD_FUNCPTR(alGetBuffer3f);
354 LOAD_FUNCPTR(alGetBufferfv);
355 LOAD_FUNCPTR(alGetBufferi);
356 LOAD_FUNCPTR(alGetBuffer3i);
357 LOAD_FUNCPTR(alGetBufferiv);
358 LOAD_FUNCPTR(alBufferData);
359 LOAD_FUNCPTR(alDopplerFactor);
360 LOAD_FUNCPTR(alDopplerVelocity);
361 LOAD_FUNCPTR(alDistanceModel);
362 LOAD_FUNCPTR(alSpeedOfSound);
363 #undef LOAD_FUNCPTR
364 if (failed)
366 WARN("Unloading %s\n", libname);
367 if (openal_handle != NULL)
368 FreeLibrary(openal_handle);
369 openal_handle = NULL;
370 return FALSE;
373 openal_loaded = 1;
374 TRACE("Loaded %s\n", libname);
376 #define LOAD_FUNCPTR(f) *((void**)&p##f) = alcGetProcAddress(NULL, #f)
377 LOAD_FUNCPTR(alGenFilters);
378 LOAD_FUNCPTR(alDeleteFilters);
379 LOAD_FUNCPTR(alFilteri);
380 LOAD_FUNCPTR(alFilterf);
381 LOAD_FUNCPTR(alGenEffects);
382 LOAD_FUNCPTR(alDeleteEffects);
383 LOAD_FUNCPTR(alEffecti);
384 LOAD_FUNCPTR(alEffectf);
385 LOAD_FUNCPTR(alGenAuxiliaryEffectSlots);
386 LOAD_FUNCPTR(alDeleteAuxiliaryEffectSlots);
387 LOAD_FUNCPTR(alAuxiliaryEffectSloti);
388 LOAD_FUNCPTR(alDeferUpdatesSOFT);
389 LOAD_FUNCPTR(alProcessUpdatesSOFT);
390 LOAD_FUNCPTR(alBufferStorageSOFT);
391 LOAD_FUNCPTR(alMapBufferSOFT);
392 LOAD_FUNCPTR(alUnmapBufferSOFT);
393 LOAD_FUNCPTR(alFlushMappedBufferSOFT);
394 #undef LOAD_FUNCPTR
395 if(!palDeferUpdatesSOFT || !palProcessUpdatesSOFT)
397 palDeferUpdatesSOFT = wrap_DeferUpdates;
398 palProcessUpdatesSOFT = wrap_ProcessUpdates;
401 local_contexts = alcIsExtensionPresent(NULL, "ALC_EXT_thread_local_context");
402 if(local_contexts)
404 TRACE("Found ALC_EXT_thread_local_context\n");
406 set_context = alcGetProcAddress(NULL, "alcSetThreadContext");
407 get_context = alcGetProcAddress(NULL, "alcGetThreadContext");
408 if(!set_context || !get_context)
410 ERR("TLS advertised but functions not found, disabling thread local contexts\n");
411 local_contexts = 0;
414 if(!local_contexts)
416 set_context = alcMakeContextCurrent;
417 get_context = alcGetCurrentContext;
419 else
421 EnterALSection = EnterALSectionTLS;
422 LeaveALSection = LeaveALSectionTLS;
425 return TRUE;
429 static void EnterALSectionTLS(ALCcontext *ctx)
431 if(LIKELY(ctx == TlsGetValue(TlsThreadPtr)))
432 return;
434 if(LIKELY(set_context(ctx) != ALC_FALSE))
435 TlsSetValue(TlsThreadPtr, ctx);
436 else
438 ERR("Couldn't set current context!!\n");
439 checkALCError(alcGetContextsDevice(ctx));
442 static void LeaveALSectionTLS(void)
446 static void EnterALSectionGlob(ALCcontext *ctx)
448 EnterCriticalSection(&openal_crst);
449 if(UNLIKELY(alcMakeContextCurrent(ctx) == ALC_FALSE))
451 ERR("Couldn't set current context!!\n");
452 checkALCError(alcGetContextsDevice(ctx));
455 static void LeaveALSectionGlob(void)
457 LeaveCriticalSection(&openal_crst);
461 static const char *get_device_id(LPCGUID pGuid)
463 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuid))
464 return "DSDEVID_DefaultPlayback";
465 if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuid))
466 return "DSDEVID_DefaultVoicePlayback";
467 if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuid))
468 return "DSDEVID_DefaultCapture";
469 if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuid))
470 return "DSDEVID_DefaultVoiceCapture";
471 return debugstr_guid(pGuid);
474 static HRESULT get_mmdevenum(IMMDeviceEnumerator **devenum)
476 HRESULT hr, init_hr;
478 init_hr = CoInitialize(NULL);
480 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
481 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)devenum);
482 if(FAILED(hr))
484 if(SUCCEEDED(init_hr))
485 CoUninitialize();
486 *devenum = NULL;
487 ERR("CoCreateInstance failed: %08lx\n", hr);
488 return hr;
491 return init_hr;
494 static void release_mmdevenum(IMMDeviceEnumerator *devenum, HRESULT init_hr)
496 IMMDeviceEnumerator_Release(devenum);
497 if(SUCCEEDED(init_hr))
498 CoUninitialize();
501 static HRESULT get_mmdevice_guid(IMMDevice *device, IPropertyStore *ps, GUID *guid)
503 PROPVARIANT pv;
504 HRESULT hr;
506 if(ps)
507 IPropertyStore_AddRef(ps);
508 else
510 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
511 if(FAILED(hr))
513 WARN("OpenPropertyStore failed: %08lx\n", hr);
514 return hr;
518 PropVariantInit(&pv);
520 hr = IPropertyStore_GetValue(ps, &PKEY_AudioEndpoint_GUID, &pv);
521 if(FAILED(hr))
523 IPropertyStore_Release(ps);
524 WARN("GetValue(GUID) failed: %08lx\n", hr);
525 return hr;
528 CLSIDFromString(pv.pwszVal, guid);
530 PropVariantClear(&pv);
531 IPropertyStore_Release(ps);
533 return S_OK;
537 static BOOL send_device(IMMDevice *device, LPDSENUMCALLBACKW cb, void *user)
539 IPropertyStore *ps;
540 PROPVARIANT pv;
541 BOOL keep_going;
542 HRESULT hr;
543 GUID guid;
545 PropVariantInit(&pv);
547 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
548 if(FAILED(hr))
550 WARN("OpenPropertyStore failed: %08lx\n", hr);
551 return TRUE;
554 hr = get_mmdevice_guid(device, ps, &guid);
555 if(FAILED(hr))
557 IPropertyStore_Release(ps);
558 return TRUE;
561 hr = IPropertyStore_GetValue(ps, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
562 if(FAILED(hr))
564 IPropertyStore_Release(ps);
565 WARN("GetValue(FriendlyName) failed: %08lx\n", hr);
566 return TRUE;
569 TRACE("Calling back with %s (%ls)\n", debugstr_guid(&guid), pv.pwszVal);
571 keep_going = cb(&guid, pv.pwszVal, wine_vxd_drv, user);
573 PropVariantClear(&pv);
574 IPropertyStore_Release(ps);
576 return keep_going;
579 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device)
581 IMMDeviceEnumerator *devenum;
582 IMMDeviceCollection *coll;
583 UINT count, i;
584 HRESULT hr, init_hr;
586 init_hr = get_mmdevenum(&devenum);
587 if(!devenum) return init_hr;
589 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow, DEVICE_STATE_ACTIVE, &coll);
590 if(FAILED(hr))
592 WARN("EnumAudioEndpoints failed: %08lx\n", hr);
593 release_mmdevenum(devenum, init_hr);
594 return hr;
597 hr = IMMDeviceCollection_GetCount(coll, &count);
598 if(FAILED(hr))
600 IMMDeviceCollection_Release(coll);
601 release_mmdevenum(devenum, init_hr);
602 WARN("GetCount failed: %08lx\n", hr);
603 return hr;
606 for(i = 0; i < count;++i)
608 GUID guid;
610 hr = IMMDeviceCollection_Item(coll, i, device);
611 if(FAILED(hr)) continue;
613 hr = get_mmdevice_guid(*device, NULL, &guid);
614 if(FAILED(hr))
616 IMMDevice_Release(*device);
617 continue;
620 if(IsEqualGUID(&guid, tgt))
622 IMMDeviceCollection_Release(coll);
623 release_mmdevenum(devenum, init_hr);
624 return DS_OK;
627 IMMDevice_Release(*device);
630 WARN("No device with GUID %s found!\n", debugstr_guid(tgt));
632 IMMDeviceCollection_Release(coll);
633 release_mmdevenum(devenum, init_hr);
635 return DSERR_INVALIDPARAM;
638 /* S_FALSE means the callback returned FALSE at some point
639 * S_OK means the callback always returned TRUE */
640 HRESULT enumerate_mmdevices(EDataFlow flow, LPDSENUMCALLBACKW cb, void *user)
642 static const WCHAR primary_desc[] = L"Primary Sound Driver";
644 IMMDeviceEnumerator *devenum;
645 IMMDeviceCollection *coll;
646 IMMDevice *defdev = NULL;
647 UINT count, i, n;
648 BOOL keep_going;
649 HRESULT hr, init_hr;
651 init_hr = get_mmdevenum(&devenum);
652 if(!devenum) return init_hr;
654 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow, DEVICE_STATE_ACTIVE, &coll);
655 if(FAILED(hr))
657 release_mmdevenum(devenum, init_hr);
658 WARN("EnumAudioEndpoints failed: %08lx\n", hr);
659 return DS_OK;
662 hr = IMMDeviceCollection_GetCount(coll, &count);
663 if(FAILED(hr))
665 IMMDeviceCollection_Release(coll);
666 release_mmdevenum(devenum, init_hr);
667 WARN("GetCount failed: %08lx\n", hr);
668 return DS_OK;
671 if(count == 0)
673 release_mmdevenum(devenum, init_hr);
674 return DS_OK;
677 TRACE("Calling back with NULL (%ls)\n", primary_desc);
678 keep_going = cb(NULL, primary_desc, L"", user);
680 /* always send the default device first */
681 if(keep_going)
683 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow, eMultimedia, &defdev);
684 if(FAILED(hr))
686 defdev = NULL;
687 n = 0;
689 else
691 keep_going = send_device(defdev, cb, user);
692 n = 1;
696 for(i = 0;keep_going && i < count;++i)
698 IMMDevice *device;
700 hr = IMMDeviceCollection_Item(coll, i, &device);
701 if(FAILED(hr))
703 WARN("Item failed: %08lx\n", hr);
704 continue;
707 if(device != defdev)
709 keep_going = send_device(device, cb, user);
710 ++n;
713 IMMDevice_Release(device);
716 if(defdev)
717 IMMDevice_Release(defdev);
718 IMMDeviceCollection_Release(coll);
720 release_mmdevenum(devenum, init_hr);
722 return keep_going ? S_OK : S_FALSE;
725 /***************************************************************************
726 * GetDeviceID [DSOUND.9]
728 * Retrieves unique identifier of default device specified
730 * PARAMS
731 * pGuidSrc [I] Address of device GUID.
732 * pGuidDest [O] Address to receive unique device GUID.
734 * RETURNS
735 * Success: DS_OK
736 * Failure: DSERR_INVALIDPARAM
738 * NOTES
739 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
740 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
741 * DSDEVID_DefaultVoiceCapture.
742 * Returns pGuidSrc if pGuidSrc is a valid device or the device
743 * GUID for the specified constants.
745 DECLSPEC_EXPORT HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
747 IMMDeviceEnumerator *devenum;
748 EDataFlow flow = (EDataFlow)-1;
749 ERole role = (ERole)-1;
750 HRESULT hr, init_hr;
752 TRACE("(%s, %p)\n", get_device_id(pGuidSrc), pGuidDest);
754 if(!pGuidSrc || !pGuidDest)
755 return DSERR_INVALIDPARAM;
757 init_hr = get_mmdevenum(&devenum);
758 if(!devenum) return init_hr;
760 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuidSrc)){
761 role = eMultimedia;
762 flow = eRender;
763 }else if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuidSrc)){
764 role = eCommunications;
765 flow = eRender;
766 }else if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuidSrc)){
767 role = eMultimedia;
768 flow = eCapture;
769 }else if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuidSrc)){
770 role = eCommunications;
771 flow = eCapture;
774 if(role != (ERole)-1 && flow != (EDataFlow)-1)
776 IMMDevice *device;
778 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow, role, &device);
779 if(FAILED(hr))
781 WARN("GetDefaultAudioEndpoint failed: %08lx\n", hr);
782 release_mmdevenum(devenum, init_hr);
783 return DSERR_NODRIVER;
786 hr = get_mmdevice_guid(device, NULL, pGuidDest);
787 IMMDevice_Release(device);
789 release_mmdevenum(devenum, init_hr);
791 return hr;
794 release_mmdevenum(devenum, init_hr);
796 *pGuidDest = *pGuidSrc;
798 return DS_OK;
802 struct morecontext {
803 LPDSENUMCALLBACKA callA;
804 LPVOID data;
807 static BOOL CALLBACK w_to_a_callback(LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
809 struct morecontext *context = data;
810 char *descA, *modA;
811 int dlen, mlen;
812 BOOL ret;
814 dlen = WideCharToMultiByte(CP_ACP, 0, descW, -1, NULL, 0, NULL, NULL);
815 mlen = WideCharToMultiByte(CP_ACP, 0, modW, -1, NULL, 0, NULL, NULL);
816 if(dlen < 0 || mlen < 0) return FALSE;
818 descA = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dlen+mlen+2);
819 if(!descA) return FALSE;
820 modA = descA + dlen+1;
822 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, dlen, NULL, NULL);
823 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, mlen, NULL, NULL);
825 ret = context->callA(guid, descA, modA, context->data);
827 HeapFree(GetProcessHeap(), 0, descA);
828 return ret;
831 /***************************************************************************
832 * DirectSoundEnumerateA [DSOUND.2]
834 * Enumerate all DirectSound drivers installed in the system
836 * PARAMS
837 * lpDSEnumCallback [I] Address of callback function.
838 * lpContext [I] Address of user defined context passed to callback function.
840 * RETURNS
841 * Success: DS_OK
842 * Failure: DSERR_INVALIDPARAM
844 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundEnumerateA(
845 LPDSENUMCALLBACKA lpDSEnumCallback,
846 LPVOID lpContext)
848 struct morecontext context;
850 if(lpDSEnumCallback == NULL)
852 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
853 return DSERR_INVALIDPARAM;
856 context.callA = lpDSEnumCallback;
857 context.data = lpContext;
859 return DirectSoundEnumerateW(w_to_a_callback, &context);
863 /***************************************************************************
864 * DirectSoundEnumerateW [DSOUND.3]
866 * Enumerate all DirectSound drivers installed in the system
868 * PARAMS
869 * lpDSEnumCallback [I] Address of callback function.
870 * lpContext [I] Address of user defined context passed to callback function.
872 * RETURNS
873 * Success: DS_OK
874 * Failure: DSERR_INVALIDPARAM
876 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundEnumerateW(
877 LPDSENUMCALLBACKW lpDSEnumCallback,
878 LPVOID lpContext )
880 HRESULT hr;
882 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
884 if(lpDSEnumCallback == NULL)
886 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
887 return DSERR_INVALIDPARAM;
890 hr = enumerate_mmdevices(eRender, lpDSEnumCallback, lpContext);
891 return SUCCEEDED(hr) ? DS_OK : hr;
894 /***************************************************************************
895 * DirectSoundCaptureEnumerateA [DSOUND.7]
897 * Enumerate all DirectSound drivers installed in the system.
899 * PARAMS
900 * lpDSEnumCallback [I] Address of callback function.
901 * lpContext [I] Address of user defined context passed to callback function.
903 * RETURNS
904 * Success: DS_OK
905 * Failure: DSERR_INVALIDPARAM
907 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundCaptureEnumerateA(
908 LPDSENUMCALLBACKA lpDSEnumCallback,
909 LPVOID lpContext)
911 struct morecontext context;
913 if(lpDSEnumCallback == NULL)
915 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
916 return DSERR_INVALIDPARAM;
919 context.callA = lpDSEnumCallback;
920 context.data = lpContext;
922 return DirectSoundCaptureEnumerateW(w_to_a_callback, &context);
925 /***************************************************************************
926 * DirectSoundCaptureEnumerateW [DSOUND.8]
928 * Enumerate all DirectSound drivers installed in the system.
930 * PARAMS
931 * lpDSEnumCallback [I] Address of callback function.
932 * lpContext [I] Address of user defined context passed to callback function.
934 * RETURNS
935 * Success: DS_OK
936 * Failure: DSERR_INVALIDPARAM
938 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundCaptureEnumerateW(
939 LPDSENUMCALLBACKW lpDSEnumCallback,
940 LPVOID lpContext)
942 HRESULT hr;
944 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext );
946 if(lpDSEnumCallback == NULL)
948 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
949 return DSERR_INVALIDPARAM;
952 hr = enumerate_mmdevices(eCapture, lpDSEnumCallback, lpContext);
953 return SUCCEEDED(hr) ? DS_OK : hr;
956 /*******************************************************************************
957 * DirectSoundCreate (DSOUND.1)
959 * Creates and initializes a DirectSound interface.
961 * PARAMS
962 * lpcGUID [I] Address of the GUID that identifies the sound device.
963 * ppDS [O] Address of a variable to receive the interface pointer.
964 * pUnkOuter [I] Must be NULL.
966 * RETURNS
967 * Success: DS_OK
968 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
969 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
971 DECLSPEC_EXPORT HRESULT WINAPI
972 DirectSoundCreate(LPCGUID lpcGUID, IDirectSound **ppDS, IUnknown *pUnkOuter)
974 HRESULT hr;
975 void *pDS;
977 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
979 if (ppDS == NULL) {
980 WARN("invalid parameter: ppDS == NULL\n");
981 return DSERR_INVALIDPARAM;
983 *ppDS = NULL;
985 if (pUnkOuter != NULL) {
986 WARN("invalid parameter: pUnkOuter != NULL\n");
987 return DSERR_INVALIDPARAM;
990 hr = DSOUND_Create(&IID_IDirectSound, &pDS);
991 if(SUCCEEDED(hr))
993 *ppDS = pDS;
994 hr = IDirectSound_Initialize(*ppDS, lpcGUID);
995 if(FAILED(hr))
997 IDirectSound_Release(*ppDS);
998 *ppDS = NULL;
1002 return hr;
1005 /*******************************************************************************
1006 * DirectSoundCreate8 (DSOUND.11)
1008 * Creates and initializes a DirectSound8 interface.
1010 * PARAMS
1011 * lpcGUID [I] Address of the GUID that identifies the sound device.
1012 * ppDS [O] Address of a variable to receive the interface pointer.
1013 * pUnkOuter [I] Must be NULL.
1015 * RETURNS
1016 * Success: DS_OK
1017 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
1018 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
1020 DECLSPEC_EXPORT HRESULT WINAPI
1021 DirectSoundCreate8(LPCGUID lpcGUID, IDirectSound8 **ppDS, IUnknown *pUnkOuter)
1023 HRESULT hr;
1024 void *pDS;
1026 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
1028 if (ppDS == NULL) {
1029 WARN("invalid parameter: ppDS == NULL\n");
1030 return DSERR_INVALIDPARAM;
1032 *ppDS = NULL;
1034 if (pUnkOuter != NULL) {
1035 WARN("invalid parameter: pUnkOuter != NULL\n");
1036 return DSERR_INVALIDPARAM;
1039 hr = DSOUND_Create8(&IID_IDirectSound8, &pDS);
1040 if(SUCCEEDED(hr))
1042 *ppDS = pDS;
1043 hr = IDirectSound8_Initialize(*ppDS, lpcGUID);
1044 if(FAILED(hr))
1046 IDirectSound8_Release(*ppDS);
1047 *ppDS = NULL;
1051 return hr;
1054 /***************************************************************************
1055 * DirectSoundCaptureCreate [DSOUND.6]
1057 * Create and initialize a DirectSoundCapture interface.
1059 * PARAMS
1060 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1061 * lplpDSC [O] Address of a variable to receive the interface pointer.
1062 * pUnkOuter [I] Must be NULL.
1064 * RETURNS
1065 * Success: DS_OK
1066 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1067 * DSERR_OUTOFMEMORY
1069 * NOTES
1070 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1071 * or NULL for the default device or DSDEVID_DefaultCapture or
1072 * DSDEVID_DefaultVoiceCapture.
1074 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1076 DECLSPEC_EXPORT HRESULT WINAPI
1077 DirectSoundCaptureCreate(LPCGUID lpcGUID, IDirectSoundCapture **ppDSC, IUnknown *pUnkOuter)
1079 HRESULT hr;
1080 void *pDSC;
1082 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
1084 if(pUnkOuter)
1086 WARN("invalid parameter: pUnkOuter != NULL\n");
1087 return DSERR_NOAGGREGATION;
1090 if(ppDSC == NULL)
1092 WARN("invalid parameter: ppDSC == NULL\n");
1093 return DSERR_INVALIDPARAM;
1095 *ppDSC = NULL;
1097 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, &pDSC);
1098 if(SUCCEEDED(hr))
1100 *ppDSC = pDSC;
1101 hr = IDirectSoundCapture_Initialize(*ppDSC, lpcGUID);
1102 if(FAILED(hr))
1104 IDirectSoundCapture_Release(*ppDSC);
1105 *ppDSC = NULL;
1109 return hr;
1112 /***************************************************************************
1113 * DirectSoundCaptureCreate8 [DSOUND.12]
1115 * Create and initialize a DirectSoundCapture interface.
1117 * PARAMS
1118 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1119 * lplpDSC [O] Address of a variable to receive the interface pointer.
1120 * pUnkOuter [I] Must be NULL.
1122 * RETURNS
1123 * Success: DS_OK
1124 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1125 * DSERR_OUTOFMEMORY
1127 * NOTES
1128 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1129 * or NULL for the default device or DSDEVID_DefaultCapture or
1130 * DSDEVID_DefaultVoiceCapture.
1132 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1134 DECLSPEC_EXPORT HRESULT WINAPI
1135 DirectSoundCaptureCreate8(LPCGUID lpcGUID, IDirectSoundCapture8 **ppDSC8, IUnknown *pUnkOuter)
1137 HRESULT hr;
1138 void *pDSC8;
1140 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
1142 if(pUnkOuter)
1144 WARN("invalid parameter: pUnkOuter != NULL\n");
1145 return DSERR_NOAGGREGATION;
1148 if(ppDSC8 == NULL)
1150 WARN("invalid parameter: ppDSC8 == NULL\n");
1151 return DSERR_INVALIDPARAM;
1153 *ppDSC8 = NULL;
1155 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture, &pDSC8);
1156 if(SUCCEEDED(hr))
1158 *ppDSC8 = pDSC8;
1159 hr = IDirectSoundCapture_Initialize(*ppDSC8, lpcGUID);
1160 if(FAILED(hr))
1162 IDirectSoundCapture_Release(*ppDSC8);
1163 *ppDSC8 = NULL;
1167 return hr;
1170 /*******************************************************************************
1171 * DirectSound ClassFactory
1174 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
1176 typedef struct {
1177 IClassFactory IClassFactory_iface;
1178 LONG ref;
1179 REFCLSID rclsid;
1180 FnCreateInstance pfnCreateInstance;
1181 } IClassFactoryImpl;
1183 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
1185 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
1188 static HRESULT WINAPI DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
1190 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1191 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
1192 if (ppobj == NULL)
1193 return E_POINTER;
1194 if (IsEqualIID(riid, &IID_IUnknown) ||
1195 IsEqualIID(riid, &IID_IClassFactory))
1197 *ppobj = iface;
1198 IUnknown_AddRef(iface);
1199 return S_OK;
1201 *ppobj = NULL;
1202 return E_NOINTERFACE;
1205 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
1207 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1208 ULONG ref = InterlockedIncrement(&(This->ref));
1209 TRACE("(%p) ref was %lu\n", This, ref - 1);
1210 return ref;
1213 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
1215 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1216 ULONG ref = InterlockedDecrement(&(This->ref));
1217 TRACE("(%p) ref was %lu\n", This, ref + 1);
1218 /* static class, won't be freed */
1219 return ref;
1222 static HRESULT WINAPI DSCF_CreateInstance(
1223 LPCLASSFACTORY iface,
1224 LPUNKNOWN pOuter,
1225 REFIID riid,
1226 LPVOID *ppobj)
1228 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1229 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
1231 if (pOuter)
1232 return CLASS_E_NOAGGREGATION;
1234 if (ppobj == NULL) {
1235 WARN("invalid parameter\n");
1236 return DSERR_INVALIDPARAM;
1238 *ppobj = NULL;
1239 return This->pfnCreateInstance(riid, ppobj);
1242 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
1244 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1245 FIXME("(%p, %d) stub!\n", This, dolock);
1246 return S_OK;
1249 static const IClassFactoryVtbl DSCF_Vtbl = {
1250 DSCF_QueryInterface,
1251 DSCF_AddRef,
1252 DSCF_Release,
1253 DSCF_CreateInstance,
1254 DSCF_LockServer
1257 static IClassFactoryImpl DSOUND_CF[] = {
1258 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound, DSOUND_Create },
1259 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound8, DSOUND_Create8 },
1260 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
1261 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
1262 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
1263 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
1264 { {NULL}, 0, NULL, NULL }
1267 /*******************************************************************************
1268 * DllGetClassObject [DSOUND.@]
1269 * Retrieves class object from a DLL object
1271 * NOTES
1272 * Docs say returns STDAPI
1274 * PARAMS
1275 * rclsid [I] CLSID for the class object
1276 * riid [I] Reference to identifier of interface for class object
1277 * ppv [O] Address of variable to receive interface pointer for riid
1279 * RETURNS
1280 * Success: S_OK
1281 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
1282 * E_UNEXPECTED
1284 DECLSPEC_EXPORT HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1286 int i = 0;
1287 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1289 if (ppv == NULL) {
1290 WARN("invalid parameter\n");
1291 return E_INVALIDARG;
1294 *ppv = NULL;
1296 if (!IsEqualIID(riid, &IID_IClassFactory) &&
1297 !IsEqualIID(riid, &IID_IUnknown)) {
1298 WARN("no interface for %s\n", debugstr_guid(riid));
1299 return E_NOINTERFACE;
1302 while (NULL != DSOUND_CF[i].rclsid) {
1303 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
1304 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
1305 *ppv = &DSOUND_CF[i];
1306 return S_OK;
1308 i++;
1311 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
1312 debugstr_guid(riid), ppv);
1313 return CLASS_E_CLASSNOTAVAILABLE;
1317 /*******************************************************************************
1318 * DllCanUnloadNow [DSOUND.4]
1319 * Determines whether the DLL is in use.
1321 * RETURNS
1322 * Success: S_OK
1323 * Failure: S_FALSE
1325 DECLSPEC_EXPORT HRESULT WINAPI DllCanUnloadNow(void)
1327 FIXME("(void): stub\n");
1328 return S_FALSE;
1331 /***********************************************************************
1332 * DllMain (DSOUND.init)
1334 DECLSPEC_EXPORT BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
1336 const WCHAR *wstr;
1338 TRACE("(%p, %lu, %p)\n", hInstDLL, fdwReason, lpvReserved);
1340 switch(fdwReason)
1342 case DLL_PROCESS_ATTACH:
1343 LogFile = stderr;
1344 if((wstr=_wgetenv(L"DSOAL_LOGFILE")) != NULL && wstr[0] != 0)
1346 FILE *f = _wfopen(wstr, L"wt");
1347 if(!f) ERR("Failed to open log file %ls\n", wstr);
1348 else LogFile = f;
1351 TRACE("DLL_PROCESS_ATTACH\n");
1352 if(!load_libopenal())
1353 return FALSE;
1354 TlsThreadPtr = TlsAlloc();
1355 /* Increase refcount on dsound by 1 */
1356 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
1357 break;
1359 case DLL_THREAD_ATTACH:
1360 TRACE("DLL_THREAD_ATTACH\n");
1361 break;
1363 case DLL_THREAD_DETACH:
1364 TRACE("DLL_THREAD_DETACH\n");
1365 if(local_contexts)
1366 set_context(NULL);
1367 break;
1369 case DLL_PROCESS_DETACH:
1370 TRACE("DLL_PROCESS_DETACH\n");
1371 if(openal_handle)
1372 FreeLibrary(openal_handle);
1373 TlsFree(TlsThreadPtr);
1374 if(LogFile != stderr)
1375 fclose(LogFile);
1376 LogFile = stderr;
1377 break;
1379 default:
1380 TRACE("UNKNOWN REASON\n");
1381 break;
1383 return TRUE;
1386 #ifdef __WINESRC__
1387 /***********************************************************************
1388 * DllRegisterServer (DSOUND.@)
1390 HRESULT WINAPI DllRegisterServer(void)
1392 return __wine_register_resources(instance);
1395 /***********************************************************************
1396 * DllUnregisterServer (DSOUND.@)
1398 HRESULT WINAPI DllUnregisterServer(void)
1400 return __wine_unregister_resources(instance);
1402 #endif