Another fix attempt for 64-bit MSVC
[dsound-openal.git] / dsound_main.c
blob0188fa1406ba05aa9eb9949dfb9668ac94189489
1 /* DirectSound
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * Most thread locking is complete. There may be a few race
22 * conditions still lurking.
24 * TODO:
25 * Implement SetCooperativeLevel properly (need to address focus issues)
26 * Implement DirectSound3DBuffers (stubs in place)
27 * Use hardware 3D support if available
28 * Add critical section locking inside Release and AddRef methods
29 * Handle static buffers - put those in hardware, non-static not in hardware
30 * Hardware DuplicateSoundBuffer
31 * Proper volume calculation for 3d buffers
32 * Remove DS_HEL_FRAGS and use mixer fragment length for it
35 #define CONST_VTABLE
36 #define INITGUID
37 #include <stdarg.h>
38 #include <string.h>
40 #include <windows.h>
41 #include <dsound.h>
42 #include <mmsystem.h>
43 #include <mmdeviceapi.h>
44 #include <devpropdef.h>
46 #include "dsound_private.h"
47 #include "eax-presets.h"
49 #ifndef DECLSPEC_EXPORT
50 #ifdef _WIN32
51 #define DECLSPEC_EXPORT __declspec(dllexport)
52 #else
53 #define DECLSPEC_EXPORT
54 #endif
55 #endif
58 int LogLevel = 1;
59 FILE *LogFile;
62 typedef struct DeviceList {
63 GUID *Guids;
64 size_t Count;
65 } DeviceList;
66 static DeviceList PlaybackDevices = { NULL, 0 };
67 static DeviceList CaptureDevices = { NULL, 0 };
69 const WCHAR aldriver_name[] = L"dsoal-aldrv.dll";
72 const EAXREVERBPROPERTIES EnvironmentDefaults[EAX_ENVIRONMENT_UNDEFINED] = {
73 REVERB_PRESET_GENERIC,
74 REVERB_PRESET_PADDEDCELL,
75 REVERB_PRESET_ROOM,
76 REVERB_PRESET_BATHROOM,
77 REVERB_PRESET_LIVINGROOM,
78 REVERB_PRESET_STONEROOM,
79 REVERB_PRESET_AUDITORIUM,
80 REVERB_PRESET_CONCERTHALL,
81 REVERB_PRESET_CAVE,
82 REVERB_PRESET_ARENA,
83 REVERB_PRESET_HANGAR,
84 REVERB_PRESET_CARPETEDHALLWAY,
85 REVERB_PRESET_HALLWAY,
86 REVERB_PRESET_STONECORRIDOR,
87 REVERB_PRESET_ALLEY,
88 REVERB_PRESET_FOREST,
89 REVERB_PRESET_CITY,
90 REVERB_PRESET_MOUNTAINS,
91 REVERB_PRESET_QUARRY,
92 REVERB_PRESET_PLAIN,
93 REVERB_PRESET_PARKINGLOT,
94 REVERB_PRESET_SEWERPIPE,
95 REVERB_PRESET_UNDERWATER,
96 REVERB_PRESET_DRUGGED,
97 REVERB_PRESET_DIZZY,
98 REVERB_PRESET_PSYCHOTIC
101 CRITICAL_SECTION openal_crst;
103 int openal_loaded = 0;
104 static HANDLE openal_handle = NULL;
105 LPALCCREATECONTEXT palcCreateContext = NULL;
106 LPALCMAKECONTEXTCURRENT palcMakeContextCurrent = NULL;
107 LPALCPROCESSCONTEXT palcProcessContext = NULL;
108 LPALCSUSPENDCONTEXT palcSuspendContext = NULL;
109 LPALCDESTROYCONTEXT palcDestroyContext = NULL;
110 LPALCGETCURRENTCONTEXT palcGetCurrentContext = NULL;
111 LPALCGETCONTEXTSDEVICE palcGetContextsDevice = NULL;
112 LPALCOPENDEVICE palcOpenDevice = NULL;
113 LPALCCLOSEDEVICE palcCloseDevice = NULL;
114 LPALCGETERROR palcGetError = NULL;
115 LPALCISEXTENSIONPRESENT palcIsExtensionPresent = NULL;
116 LPALCGETPROCADDRESS palcGetProcAddress = NULL;
117 LPALCGETENUMVALUE palcGetEnumValue = NULL;
118 LPALCGETSTRING palcGetString = NULL;
119 LPALCGETINTEGERV palcGetIntegerv = NULL;
120 LPALCCAPTUREOPENDEVICE palcCaptureOpenDevice = NULL;
121 LPALCCAPTURECLOSEDEVICE palcCaptureCloseDevice = NULL;
122 LPALCCAPTURESTART palcCaptureStart = NULL;
123 LPALCCAPTURESTOP palcCaptureStop = NULL;
124 LPALCCAPTURESAMPLES palcCaptureSamples = NULL;
125 LPALENABLE palEnable = NULL;
126 LPALDISABLE palDisable = NULL;
127 LPALISENABLED palIsEnabled = NULL;
128 LPALGETSTRING palGetString = NULL;
129 LPALGETBOOLEANV palGetBooleanv = NULL;
130 LPALGETINTEGERV palGetIntegerv = NULL;
131 LPALGETFLOATV palGetFloatv = NULL;
132 LPALGETDOUBLEV palGetDoublev = NULL;
133 LPALGETBOOLEAN palGetBoolean = NULL;
134 LPALGETINTEGER palGetInteger = NULL;
135 LPALGETFLOAT palGetFloat = NULL;
136 LPALGETDOUBLE palGetDouble = NULL;
137 LPALGETERROR palGetError = NULL;
138 LPALISEXTENSIONPRESENT palIsExtensionPresent = NULL;
139 LPALGETPROCADDRESS palGetProcAddress = NULL;
140 LPALGETENUMVALUE palGetEnumValue = NULL;
141 LPALLISTENERF palListenerf = NULL;
142 LPALLISTENER3F palListener3f = NULL;
143 LPALLISTENERFV palListenerfv = NULL;
144 LPALLISTENERI palListeneri = NULL;
145 LPALLISTENER3I palListener3i = NULL;
146 LPALLISTENERIV palListeneriv = NULL;
147 LPALGETLISTENERF palGetListenerf = NULL;
148 LPALGETLISTENER3F palGetListener3f = NULL;
149 LPALGETLISTENERFV palGetListenerfv = NULL;
150 LPALGETLISTENERI palGetListeneri = NULL;
151 LPALGETLISTENER3I palGetListener3i = NULL;
152 LPALGETLISTENERIV palGetListeneriv = NULL;
153 LPALGENSOURCES palGenSources = NULL;
154 LPALDELETESOURCES palDeleteSources = NULL;
155 LPALISSOURCE palIsSource = NULL;
156 LPALSOURCEF palSourcef = NULL;
157 LPALSOURCE3F palSource3f = NULL;
158 LPALSOURCEFV palSourcefv = NULL;
159 LPALSOURCEI palSourcei = NULL;
160 LPALSOURCE3I palSource3i = NULL;
161 LPALSOURCEIV palSourceiv = NULL;
162 LPALGETSOURCEF palGetSourcef = NULL;
163 LPALGETSOURCE3F palGetSource3f = NULL;
164 LPALGETSOURCEFV palGetSourcefv = NULL;
165 LPALGETSOURCEI palGetSourcei = NULL;
166 LPALGETSOURCE3I palGetSource3i = NULL;
167 LPALGETSOURCEIV palGetSourceiv = NULL;
168 LPALSOURCEPLAYV palSourcePlayv = NULL;
169 LPALSOURCESTOPV palSourceStopv = NULL;
170 LPALSOURCEREWINDV palSourceRewindv = NULL;
171 LPALSOURCEPAUSEV palSourcePausev = NULL;
172 LPALSOURCEPLAY palSourcePlay = NULL;
173 LPALSOURCESTOP palSourceStop = NULL;
174 LPALSOURCEREWIND palSourceRewind = NULL;
175 LPALSOURCEPAUSE palSourcePause = NULL;
176 LPALSOURCEQUEUEBUFFERS palSourceQueueBuffers = NULL;
177 LPALSOURCEUNQUEUEBUFFERS palSourceUnqueueBuffers = NULL;
178 LPALGENBUFFERS palGenBuffers = NULL;
179 LPALDELETEBUFFERS palDeleteBuffers = NULL;
180 LPALISBUFFER palIsBuffer = NULL;
181 LPALBUFFERF palBufferf = NULL;
182 LPALBUFFER3F palBuffer3f = NULL;
183 LPALBUFFERFV palBufferfv = NULL;
184 LPALBUFFERI palBufferi = NULL;
185 LPALBUFFER3I palBuffer3i = NULL;
186 LPALBUFFERIV palBufferiv = NULL;
187 LPALGETBUFFERF palGetBufferf = NULL;
188 LPALGETBUFFER3F palGetBuffer3f = NULL;
189 LPALGETBUFFERFV palGetBufferfv = NULL;
190 LPALGETBUFFERI palGetBufferi = NULL;
191 LPALGETBUFFER3I palGetBuffer3i = NULL;
192 LPALGETBUFFERIV palGetBufferiv = NULL;
193 LPALBUFFERDATA palBufferData = NULL;
194 LPALDOPPLERFACTOR palDopplerFactor = NULL;
195 LPALDOPPLERVELOCITY palDopplerVelocity = NULL;
196 LPALDISTANCEMODEL palDistanceModel = NULL;
197 LPALSPEEDOFSOUND palSpeedOfSound = NULL;
199 LPALGENFILTERS palGenFilters = NULL;
200 LPALDELETEFILTERS palDeleteFilters = NULL;
201 LPALFILTERI palFilteri = NULL;
202 LPALFILTERF palFilterf = NULL;
203 LPALGENEFFECTS palGenEffects = NULL;
204 LPALDELETEEFFECTS palDeleteEffects = NULL;
205 LPALEFFECTI palEffecti = NULL;
206 LPALEFFECTF palEffectf = NULL;
207 LPALEFFECTFV palEffectfv = NULL;
208 LPALGENAUXILIARYEFFECTSLOTS palGenAuxiliaryEffectSlots = NULL;
209 LPALDELETEAUXILIARYEFFECTSLOTS palDeleteAuxiliaryEffectSlots = NULL;
210 LPALAUXILIARYEFFECTSLOTI palAuxiliaryEffectSloti = NULL;
211 LPALAUXILIARYEFFECTSLOTF palAuxiliaryEffectSlotf = NULL;
212 LPALDEFERUPDATESSOFT palDeferUpdatesSOFT = NULL;
213 LPALPROCESSUPDATESSOFT palProcessUpdatesSOFT = NULL;
214 LPALBUFFERSTORAGESOFT palBufferStorageSOFT = NULL;
215 LPALMAPBUFFERSOFT palMapBufferSOFT = NULL;
216 LPALUNMAPBUFFERSOFT palUnmapBufferSOFT = NULL;
217 LPALFLUSHMAPPEDBUFFERSOFT palFlushMappedBufferSOFT = NULL;
219 LPALCMAKECONTEXTCURRENT set_context;
220 LPALCGETCURRENTCONTEXT get_context;
221 BOOL local_contexts;
224 static void AL_APIENTRY wrap_DeferUpdates(void)
225 { alcSuspendContext(alcGetCurrentContext()); }
226 static void AL_APIENTRY wrap_ProcessUpdates(void)
227 { alcProcessContext(alcGetCurrentContext()); }
229 static void EnterALSectionTLS(ALCcontext *ctx);
230 static void LeaveALSectionTLS(void);
231 static void EnterALSectionGlob(ALCcontext *ctx);
232 static void LeaveALSectionGlob(void);
234 DWORD TlsThreadPtr;
235 void (*EnterALSection)(ALCcontext *ctx) = EnterALSectionGlob;
236 void (*LeaveALSection)(void) = LeaveALSectionGlob;
239 static BOOL load_libopenal(void)
241 BOOL failed = FALSE;
242 const char *str;
244 str = getenv("DSOAL_LOGLEVEL");
245 if(str && *str)
246 LogLevel = atoi(str);
248 openal_handle = LoadLibraryW(aldriver_name);
249 if(!openal_handle)
251 ERR("Couldn't load %ls: %lu\n", aldriver_name, GetLastError());
252 return FALSE;
255 #define LOAD_FUNCPTR(f) do { \
256 union { void *ptr; FARPROC *proc; } func = { &p##f }; \
257 if((*func.proc = GetProcAddress(openal_handle, #f)) == NULL) \
259 ERR("Couldn't lookup %s in %ls\n", #f, aldriver_name); \
260 failed = TRUE; \
262 } while(0)
264 LOAD_FUNCPTR(alcCreateContext);
265 LOAD_FUNCPTR(alcMakeContextCurrent);
266 LOAD_FUNCPTR(alcProcessContext);
267 LOAD_FUNCPTR(alcSuspendContext);
268 LOAD_FUNCPTR(alcDestroyContext);
269 LOAD_FUNCPTR(alcGetCurrentContext);
270 LOAD_FUNCPTR(alcGetContextsDevice);
271 LOAD_FUNCPTR(alcOpenDevice);
272 LOAD_FUNCPTR(alcCloseDevice);
273 LOAD_FUNCPTR(alcGetError);
274 LOAD_FUNCPTR(alcIsExtensionPresent);
275 LOAD_FUNCPTR(alcGetProcAddress);
276 LOAD_FUNCPTR(alcGetEnumValue);
277 LOAD_FUNCPTR(alcGetString);
278 LOAD_FUNCPTR(alcGetIntegerv);
279 LOAD_FUNCPTR(alcCaptureOpenDevice);
280 LOAD_FUNCPTR(alcCaptureCloseDevice);
281 LOAD_FUNCPTR(alcCaptureStart);
282 LOAD_FUNCPTR(alcCaptureStop);
283 LOAD_FUNCPTR(alcCaptureSamples);
284 LOAD_FUNCPTR(alEnable);
285 LOAD_FUNCPTR(alDisable);
286 LOAD_FUNCPTR(alIsEnabled);
287 LOAD_FUNCPTR(alGetString);
288 LOAD_FUNCPTR(alGetBooleanv);
289 LOAD_FUNCPTR(alGetIntegerv);
290 LOAD_FUNCPTR(alGetFloatv);
291 LOAD_FUNCPTR(alGetDoublev);
292 LOAD_FUNCPTR(alGetBoolean);
293 LOAD_FUNCPTR(alGetInteger);
294 LOAD_FUNCPTR(alGetFloat);
295 LOAD_FUNCPTR(alGetDouble);
296 LOAD_FUNCPTR(alGetError);
297 LOAD_FUNCPTR(alIsExtensionPresent);
298 LOAD_FUNCPTR(alGetProcAddress);
299 LOAD_FUNCPTR(alGetEnumValue);
300 LOAD_FUNCPTR(alListenerf);
301 LOAD_FUNCPTR(alListener3f);
302 LOAD_FUNCPTR(alListenerfv);
303 LOAD_FUNCPTR(alListeneri);
304 LOAD_FUNCPTR(alListener3i);
305 LOAD_FUNCPTR(alListeneriv);
306 LOAD_FUNCPTR(alGetListenerf);
307 LOAD_FUNCPTR(alGetListener3f);
308 LOAD_FUNCPTR(alGetListenerfv);
309 LOAD_FUNCPTR(alGetListeneri);
310 LOAD_FUNCPTR(alGetListener3i);
311 LOAD_FUNCPTR(alGetListeneriv);
312 LOAD_FUNCPTR(alGenSources);
313 LOAD_FUNCPTR(alDeleteSources);
314 LOAD_FUNCPTR(alIsSource);
315 LOAD_FUNCPTR(alSourcef);
316 LOAD_FUNCPTR(alSource3f);
317 LOAD_FUNCPTR(alSourcefv);
318 LOAD_FUNCPTR(alSourcei);
319 LOAD_FUNCPTR(alSource3i);
320 LOAD_FUNCPTR(alSourceiv);
321 LOAD_FUNCPTR(alGetSourcef);
322 LOAD_FUNCPTR(alGetSource3f);
323 LOAD_FUNCPTR(alGetSourcefv);
324 LOAD_FUNCPTR(alGetSourcei);
325 LOAD_FUNCPTR(alGetSource3i);
326 LOAD_FUNCPTR(alGetSourceiv);
327 LOAD_FUNCPTR(alSourcePlayv);
328 LOAD_FUNCPTR(alSourceStopv);
329 LOAD_FUNCPTR(alSourceRewindv);
330 LOAD_FUNCPTR(alSourcePausev);
331 LOAD_FUNCPTR(alSourcePlay);
332 LOAD_FUNCPTR(alSourceStop);
333 LOAD_FUNCPTR(alSourceRewind);
334 LOAD_FUNCPTR(alSourcePause);
335 LOAD_FUNCPTR(alSourceQueueBuffers);
336 LOAD_FUNCPTR(alSourceUnqueueBuffers);
337 LOAD_FUNCPTR(alGenBuffers);
338 LOAD_FUNCPTR(alDeleteBuffers);
339 LOAD_FUNCPTR(alIsBuffer);
340 LOAD_FUNCPTR(alBufferf);
341 LOAD_FUNCPTR(alBuffer3f);
342 LOAD_FUNCPTR(alBufferfv);
343 LOAD_FUNCPTR(alBufferi);
344 LOAD_FUNCPTR(alBuffer3i);
345 LOAD_FUNCPTR(alBufferiv);
346 LOAD_FUNCPTR(alGetBufferf);
347 LOAD_FUNCPTR(alGetBuffer3f);
348 LOAD_FUNCPTR(alGetBufferfv);
349 LOAD_FUNCPTR(alGetBufferi);
350 LOAD_FUNCPTR(alGetBuffer3i);
351 LOAD_FUNCPTR(alGetBufferiv);
352 LOAD_FUNCPTR(alBufferData);
353 LOAD_FUNCPTR(alDopplerFactor);
354 LOAD_FUNCPTR(alDopplerVelocity);
355 LOAD_FUNCPTR(alDistanceModel);
356 LOAD_FUNCPTR(alSpeedOfSound);
357 #undef LOAD_FUNCPTR
358 if (failed)
360 WARN("Unloading %ls\n", aldriver_name);
361 if (openal_handle != NULL)
362 FreeLibrary(openal_handle);
363 openal_handle = NULL;
364 return FALSE;
367 openal_loaded = 1;
368 TRACE("Loaded %ls\n", aldriver_name);
370 #define LOAD_FUNCPTR(f) p##f = alcGetProcAddress(NULL, #f)
371 LOAD_FUNCPTR(alGenFilters);
372 LOAD_FUNCPTR(alDeleteFilters);
373 LOAD_FUNCPTR(alFilteri);
374 LOAD_FUNCPTR(alFilterf);
375 LOAD_FUNCPTR(alGenEffects);
376 LOAD_FUNCPTR(alDeleteEffects);
377 LOAD_FUNCPTR(alEffecti);
378 LOAD_FUNCPTR(alEffectf);
379 LOAD_FUNCPTR(alEffectfv);
380 LOAD_FUNCPTR(alGenAuxiliaryEffectSlots);
381 LOAD_FUNCPTR(alDeleteAuxiliaryEffectSlots);
382 LOAD_FUNCPTR(alAuxiliaryEffectSloti);
383 LOAD_FUNCPTR(alAuxiliaryEffectSlotf);
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 #ifdef _MSC_VER
471 const CLSID CLSID_MMDeviceEnumerator = {
472 0xBCDE0395,
473 0xE52F, 0x467C,
474 { 0x8E, 0x3D, 0xC4, 0x57, 0x92, 0x91, 0x69, 0x2E }
477 const IID IID_IMMDeviceEnumerator = {
478 0xA95664D2,
479 0x9614, 0x4F35,
480 { 0xA7, 0x46, 0xDE, 0x8D, 0xB6, 0x36, 0x17, 0xE6 }
482 #endif
484 static HRESULT get_mmdevenum(IMMDeviceEnumerator **devenum)
486 HRESULT hr, init_hr;
488 init_hr = CoInitialize(NULL);
490 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
491 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)devenum);
492 if(FAILED(hr))
494 if(SUCCEEDED(init_hr))
495 CoUninitialize();
496 *devenum = NULL;
497 ERR("CoCreateInstance failed: %08lx\n", hr);
498 return hr;
501 return init_hr;
504 static void release_mmdevenum(IMMDeviceEnumerator *devenum, HRESULT init_hr)
506 IMMDeviceEnumerator_Release(devenum);
507 if(SUCCEEDED(init_hr))
508 CoUninitialize();
511 static HRESULT get_mmdevice_guid(IMMDevice *device, IPropertyStore *ps, GUID *guid)
513 PROPVARIANT pv;
514 HRESULT hr;
516 if(ps)
517 IPropertyStore_AddRef(ps);
518 else
520 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
521 if(FAILED(hr))
523 WARN("OpenPropertyStore failed: %08lx\n", hr);
524 return hr;
528 PropVariantInit(&pv);
530 hr = IPropertyStore_GetValue(ps, &PKEY_AudioEndpoint_GUID, &pv);
531 if(FAILED(hr))
533 IPropertyStore_Release(ps);
534 WARN("GetValue(GUID) failed: %08lx\n", hr);
535 return hr;
538 CLSIDFromString(pv.pwszVal, guid);
540 PropVariantClear(&pv);
541 IPropertyStore_Release(ps);
543 return S_OK;
547 static BOOL send_device(IMMDevice *device, EDataFlow flow, DeviceList *devlist, PRVTENUMCALLBACK cb, void *user)
549 IPropertyStore *ps;
550 PROPVARIANT pv;
551 BOOL keep_going;
552 size_t dev_count;
553 HRESULT hr;
554 GUID guid;
556 PropVariantInit(&pv);
558 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
559 if(FAILED(hr))
561 WARN("OpenPropertyStore failed: %08lx\n", hr);
562 return TRUE;
565 hr = get_mmdevice_guid(device, ps, &guid);
566 if(FAILED(hr) || (devlist->Count > 0 && IsEqualGUID(&devlist->Guids[0], &guid)))
568 IPropertyStore_Release(ps);
569 return TRUE;
572 hr = IPropertyStore_GetValue(ps, (const PROPERTYKEY*)&DEVPKEY_Device_FriendlyName, &pv);
573 if(FAILED(hr))
575 IPropertyStore_Release(ps);
576 WARN("GetValue(FriendlyName) failed: %08lx\n", hr);
577 return TRUE;
580 dev_count = devlist->Count++;
581 devlist->Guids[dev_count] = guid;
583 keep_going = FALSE;
584 if(cb)
586 TRACE("Calling back with %s - %ls\n", debugstr_guid(&devlist->Guids[dev_count]),
587 pv.pwszVal);
588 keep_going = cb(flow, &devlist->Guids[dev_count], pv.pwszVal, aldriver_name, user);
591 PropVariantClear(&pv);
592 IPropertyStore_Release(ps);
594 return keep_going;
597 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device)
599 IMMDeviceEnumerator *devenum;
600 IMMDeviceCollection *coll;
601 UINT count, i;
602 HRESULT hr, init_hr;
604 init_hr = get_mmdevenum(&devenum);
605 if(!devenum) return init_hr;
607 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow, DEVICE_STATE_ACTIVE, &coll);
608 if(FAILED(hr))
610 WARN("EnumAudioEndpoints failed: %08lx\n", hr);
611 release_mmdevenum(devenum, init_hr);
612 return hr;
615 hr = IMMDeviceCollection_GetCount(coll, &count);
616 if(FAILED(hr))
618 IMMDeviceCollection_Release(coll);
619 release_mmdevenum(devenum, init_hr);
620 WARN("GetCount failed: %08lx\n", hr);
621 return hr;
624 for(i = 0; i < count;++i)
626 GUID guid;
628 hr = IMMDeviceCollection_Item(coll, i, device);
629 if(FAILED(hr)) continue;
631 hr = get_mmdevice_guid(*device, NULL, &guid);
632 if(FAILED(hr))
634 IMMDevice_Release(*device);
635 continue;
638 if(IsEqualGUID(&guid, tgt))
640 IMMDeviceCollection_Release(coll);
641 release_mmdevenum(devenum, init_hr);
642 return DS_OK;
645 IMMDevice_Release(*device);
648 WARN("No device with GUID %s found!\n", debugstr_guid(tgt));
650 IMMDeviceCollection_Release(coll);
651 release_mmdevenum(devenum, init_hr);
653 return DSERR_INVALIDPARAM;
656 /* S_FALSE means the callback returned FALSE at some point
657 * S_OK means the callback always returned TRUE */
658 HRESULT enumerate_mmdevices(EDataFlow flow, PRVTENUMCALLBACK cb, void *user)
660 static const WCHAR primary_desc[] = L"Primary Sound Driver";
662 IMMDeviceEnumerator *devenum;
663 IMMDeviceCollection *coll;
664 IMMDevice *device;
665 DeviceList *devlist;
666 UINT count, i;
667 BOOL keep_going;
668 HRESULT hr, init_hr;
670 init_hr = get_mmdevenum(&devenum);
671 if(!devenum) return init_hr;
673 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow, DEVICE_STATE_ACTIVE, &coll);
674 if(FAILED(hr))
676 release_mmdevenum(devenum, init_hr);
677 WARN("EnumAudioEndpoints failed: %08lx\n", hr);
678 return DS_OK;
681 hr = IMMDeviceCollection_GetCount(coll, &count);
682 if(FAILED(hr))
684 IMMDeviceCollection_Release(coll);
685 release_mmdevenum(devenum, init_hr);
686 WARN("GetCount failed: %08lx\n", hr);
687 return DS_OK;
690 if(count == 0)
692 IMMDeviceCollection_Release(coll);
693 release_mmdevenum(devenum, init_hr);
694 return DS_OK;
697 devlist = (flow==eCapture) ? &CaptureDevices : &PlaybackDevices;
699 HeapFree(GetProcessHeap(), 0, devlist->Guids);
700 devlist->Count = 0;
701 devlist->Guids = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
702 sizeof(devlist->Guids[0])*count);
704 TRACE("Calling back with NULL (%ls)\n", primary_desc);
705 keep_going = cb(flow, NULL, primary_desc, L"", user);
707 /* always send the default device first */
708 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow, eMultimedia, &device);
709 if(SUCCEEDED(hr))
711 if(!keep_going) cb = NULL;
712 keep_going = send_device(device, flow, devlist, cb, user);
713 IMMDevice_Release(device);
716 for(i = 0;i < count;++i)
718 if(!keep_going)
719 cb = NULL;
721 hr = IMMDeviceCollection_Item(coll, i, &device);
722 if(FAILED(hr))
724 WARN("Item failed: %08lx\n", hr);
725 continue;
728 keep_going = send_device(device, flow, devlist, cb, user);
730 IMMDevice_Release(device);
732 IMMDeviceCollection_Release(coll);
734 release_mmdevenum(devenum, init_hr);
736 return keep_going ? S_OK : S_FALSE;
739 /***************************************************************************
740 * GetDeviceID [DSOUND.9]
742 * Retrieves unique identifier of default device specified
744 * PARAMS
745 * pGuidSrc [I] Address of device GUID.
746 * pGuidDest [O] Address to receive unique device GUID.
748 * RETURNS
749 * Success: DS_OK
750 * Failure: DSERR_INVALIDPARAM
752 * NOTES
753 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
754 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
755 * DSDEVID_DefaultVoiceCapture.
756 * Returns pGuidSrc if pGuidSrc is a valid device or the device
757 * GUID for the specified constants.
759 HRESULT WINAPI DSOAL_GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
761 IMMDeviceEnumerator *devenum;
762 HRESULT hr, init_hr;
763 IMMDevice *device;
764 EDataFlow flow;
765 ERole role;
767 TRACE("(%s, %p)\n", get_device_id(pGuidSrc), pGuidDest);
769 if(!pGuidSrc || !pGuidDest)
770 return DSERR_INVALIDPARAM;
772 flow = eRender;
773 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuidSrc))
774 role = eMultimedia;
775 else if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuidSrc))
776 role = eCommunications;
777 else
779 flow = eCapture;
780 if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuidSrc))
781 role = eMultimedia;
782 else if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuidSrc))
783 role = eCommunications;
784 else
786 *pGuidDest = *pGuidSrc;
787 return DS_OK;
791 init_hr = get_mmdevenum(&devenum);
792 if(!devenum) return init_hr;
794 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow, role, &device);
795 if(FAILED(hr))
797 WARN("GetDefaultAudioEndpoint failed: %08lx\n", hr);
798 release_mmdevenum(devenum, init_hr);
799 return DSERR_NODRIVER;
802 hr = get_mmdevice_guid(device, NULL, pGuidDest);
803 IMMDevice_Release(device);
805 release_mmdevenum(devenum, init_hr);
807 return hr;
811 struct morecontextW {
812 LPDSENUMCALLBACKW callW;
813 LPVOID data;
816 static BOOL CALLBACK w_callback(EDataFlow flow, LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
818 struct morecontextW *context = data;
819 (void)flow;
821 return context->callW(guid, descW, modW, context->data);
824 struct morecontextA {
825 LPDSENUMCALLBACKA callA;
826 LPVOID data;
829 static BOOL CALLBACK w_to_a_callback(EDataFlow flow, LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
831 struct morecontextA *context = data;
832 char *descA, *modA;
833 int dlen, mlen;
834 BOOL ret;
835 (void)flow;
837 dlen = WideCharToMultiByte(CP_ACP, 0, descW, -1, NULL, 0, NULL, NULL);
838 mlen = WideCharToMultiByte(CP_ACP, 0, modW, -1, NULL, 0, NULL, NULL);
839 if(dlen < 0 || mlen < 0) return FALSE;
841 descA = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dlen+mlen+2);
842 if(!descA) return FALSE;
843 modA = descA + dlen+1;
845 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, dlen, NULL, NULL);
846 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, mlen, NULL, NULL);
848 ret = context->callA(guid, descA, modA, context->data);
850 HeapFree(GetProcessHeap(), 0, descA);
851 return ret;
854 /***************************************************************************
855 * DirectSoundEnumerateA [DSOUND.2]
857 * Enumerate all DirectSound drivers installed in the system
859 * PARAMS
860 * lpDSEnumCallback [I] Address of callback function.
861 * lpContext [I] Address of user defined context passed to callback function.
863 * RETURNS
864 * Success: DS_OK
865 * Failure: DSERR_INVALIDPARAM
867 HRESULT WINAPI DSOAL_DirectSoundEnumerateA(
868 LPDSENUMCALLBACKA lpDSEnumCallback,
869 LPVOID lpContext)
871 struct morecontextA ctx;
872 HRESULT hr;
874 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
876 if(lpDSEnumCallback == NULL)
878 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
879 return DSERR_INVALIDPARAM;
882 ctx.callA = lpDSEnumCallback;
883 ctx.data = lpContext;
885 hr = enumerate_mmdevices(eRender, w_to_a_callback, &ctx);
886 return SUCCEEDED(hr) ? DS_OK : hr;
890 /***************************************************************************
891 * DirectSoundEnumerateW [DSOUND.3]
893 * Enumerate all DirectSound drivers installed in the system
895 * PARAMS
896 * lpDSEnumCallback [I] Address of callback function.
897 * lpContext [I] Address of user defined context passed to callback function.
899 * RETURNS
900 * Success: DS_OK
901 * Failure: DSERR_INVALIDPARAM
903 HRESULT WINAPI DSOAL_DirectSoundEnumerateW(
904 LPDSENUMCALLBACKW lpDSEnumCallback,
905 LPVOID lpContext )
907 struct morecontextW ctx;
908 HRESULT hr;
910 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
912 if(lpDSEnumCallback == NULL)
914 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
915 return DSERR_INVALIDPARAM;
918 ctx.callW = lpDSEnumCallback;
919 ctx.data = lpContext;
921 hr = enumerate_mmdevices(eRender, w_callback, &ctx);
922 return SUCCEEDED(hr) ? DS_OK : hr;
925 /***************************************************************************
926 * DirectSoundCaptureEnumerateA [DSOUND.7]
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 HRESULT WINAPI DSOAL_DirectSoundCaptureEnumerateA(
939 LPDSENUMCALLBACKA lpDSEnumCallback,
940 LPVOID lpContext)
942 struct morecontextA ctx;
943 HRESULT hr;
945 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
947 if(lpDSEnumCallback == NULL)
949 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
950 return DSERR_INVALIDPARAM;
953 ctx.callA = lpDSEnumCallback;
954 ctx.data = lpContext;
956 hr = enumerate_mmdevices(eCapture, w_to_a_callback, &ctx);
957 return SUCCEEDED(hr) ? DS_OK : hr;
960 /***************************************************************************
961 * DirectSoundCaptureEnumerateW [DSOUND.8]
963 * Enumerate all DirectSound drivers installed in the system.
965 * PARAMS
966 * lpDSEnumCallback [I] Address of callback function.
967 * lpContext [I] Address of user defined context passed to callback function.
969 * RETURNS
970 * Success: DS_OK
971 * Failure: DSERR_INVALIDPARAM
973 HRESULT WINAPI DSOAL_DirectSoundCaptureEnumerateW(
974 LPDSENUMCALLBACKW lpDSEnumCallback,
975 LPVOID lpContext)
977 struct morecontextW ctx;
978 HRESULT hr;
980 TRACE("(%p, %p)\n", lpDSEnumCallback, lpContext);
982 if(lpDSEnumCallback == NULL)
984 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
985 return DSERR_INVALIDPARAM;
988 ctx.callW = lpDSEnumCallback;
989 ctx.data = lpContext;
991 hr = enumerate_mmdevices(eCapture, w_callback, &ctx);
992 return SUCCEEDED(hr) ? DS_OK : hr;
995 /*******************************************************************************
996 * DirectSoundCreate (DSOUND.1)
998 * Creates and initializes a DirectSound interface.
1000 * PARAMS
1001 * lpcGUID [I] Address of the GUID that identifies the sound device.
1002 * ppDS [O] Address of a variable to receive the interface pointer.
1003 * pUnkOuter [I] Must be NULL.
1005 * RETURNS
1006 * Success: DS_OK
1007 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
1008 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
1010 HRESULT WINAPI
1011 DSOAL_DirectSoundCreate(LPCGUID lpcGUID, IDirectSound **ppDS, IUnknown *pUnkOuter)
1013 HRESULT hr;
1014 void *pDS;
1016 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
1018 if (ppDS == NULL) {
1019 WARN("invalid parameter: ppDS == NULL\n");
1020 return DSERR_INVALIDPARAM;
1022 *ppDS = NULL;
1024 if (pUnkOuter != NULL) {
1025 WARN("invalid parameter: pUnkOuter != NULL\n");
1026 return DSERR_INVALIDPARAM;
1029 hr = DSOUND_Create(&IID_IDirectSound, &pDS);
1030 if(SUCCEEDED(hr))
1032 *ppDS = pDS;
1033 hr = IDirectSound_Initialize(*ppDS, lpcGUID);
1034 if(FAILED(hr))
1036 IDirectSound_Release(*ppDS);
1037 *ppDS = NULL;
1041 return hr;
1044 /*******************************************************************************
1045 * DirectSoundCreate8 (DSOUND.11)
1047 * Creates and initializes a DirectSound8 interface.
1049 * PARAMS
1050 * lpcGUID [I] Address of the GUID that identifies the sound device.
1051 * ppDS [O] Address of a variable to receive the interface pointer.
1052 * pUnkOuter [I] Must be NULL.
1054 * RETURNS
1055 * Success: DS_OK
1056 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
1057 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
1059 HRESULT WINAPI
1060 DSOAL_DirectSoundCreate8(LPCGUID lpcGUID, IDirectSound8 **ppDS, IUnknown *pUnkOuter)
1062 HRESULT hr;
1063 void *pDS;
1065 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
1067 if (ppDS == NULL) {
1068 WARN("invalid parameter: ppDS == NULL\n");
1069 return DSERR_INVALIDPARAM;
1071 *ppDS = NULL;
1073 if (pUnkOuter != NULL) {
1074 WARN("invalid parameter: pUnkOuter != NULL\n");
1075 return DSERR_INVALIDPARAM;
1078 hr = DSOUND_Create8(&IID_IDirectSound8, &pDS);
1079 if(SUCCEEDED(hr))
1081 *ppDS = pDS;
1082 hr = IDirectSound8_Initialize(*ppDS, lpcGUID);
1083 if(FAILED(hr))
1085 IDirectSound8_Release(*ppDS);
1086 *ppDS = NULL;
1090 return hr;
1093 /***************************************************************************
1094 * DirectSoundCaptureCreate [DSOUND.6]
1096 * Create and initialize a DirectSoundCapture interface.
1098 * PARAMS
1099 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1100 * lplpDSC [O] Address of a variable to receive the interface pointer.
1101 * pUnkOuter [I] Must be NULL.
1103 * RETURNS
1104 * Success: DS_OK
1105 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1106 * DSERR_OUTOFMEMORY
1108 * NOTES
1109 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1110 * or NULL for the default device or DSDEVID_DefaultCapture or
1111 * DSDEVID_DefaultVoiceCapture.
1113 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1115 HRESULT WINAPI
1116 DSOAL_DirectSoundCaptureCreate(LPCGUID lpcGUID, IDirectSoundCapture **ppDSC, IUnknown *pUnkOuter)
1118 HRESULT hr;
1119 void *pDSC;
1121 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
1123 if(pUnkOuter)
1125 WARN("invalid parameter: pUnkOuter != NULL\n");
1126 return DSERR_NOAGGREGATION;
1129 if(ppDSC == NULL)
1131 WARN("invalid parameter: ppDSC == NULL\n");
1132 return DSERR_INVALIDPARAM;
1134 *ppDSC = NULL;
1136 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, &pDSC);
1137 if(SUCCEEDED(hr))
1139 *ppDSC = pDSC;
1140 hr = IDirectSoundCapture_Initialize(*ppDSC, lpcGUID);
1141 if(FAILED(hr))
1143 IDirectSoundCapture_Release(*ppDSC);
1144 *ppDSC = NULL;
1148 return hr;
1151 /***************************************************************************
1152 * DirectSoundCaptureCreate8 [DSOUND.12]
1154 * Create and initialize a DirectSoundCapture interface.
1156 * PARAMS
1157 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
1158 * lplpDSC [O] Address of a variable to receive the interface pointer.
1159 * pUnkOuter [I] Must be NULL.
1161 * RETURNS
1162 * Success: DS_OK
1163 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
1164 * DSERR_OUTOFMEMORY
1166 * NOTES
1167 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
1168 * or NULL for the default device or DSDEVID_DefaultCapture or
1169 * DSDEVID_DefaultVoiceCapture.
1171 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
1173 HRESULT WINAPI
1174 DSOAL_DirectSoundCaptureCreate8(LPCGUID lpcGUID, IDirectSoundCapture8 **ppDSC8, IUnknown *pUnkOuter)
1176 HRESULT hr;
1177 void *pDSC8;
1179 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
1181 if(pUnkOuter)
1183 WARN("invalid parameter: pUnkOuter != NULL\n");
1184 return DSERR_NOAGGREGATION;
1187 if(ppDSC8 == NULL)
1189 WARN("invalid parameter: ppDSC8 == NULL\n");
1190 return DSERR_INVALIDPARAM;
1192 *ppDSC8 = NULL;
1194 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture, &pDSC8);
1195 if(SUCCEEDED(hr))
1197 *ppDSC8 = pDSC8;
1198 hr = IDirectSoundCapture_Initialize(*ppDSC8, lpcGUID);
1199 if(FAILED(hr))
1201 IDirectSoundCapture_Release(*ppDSC8);
1202 *ppDSC8 = NULL;
1206 return hr;
1209 /*******************************************************************************
1210 * DirectSound ClassFactory
1213 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
1215 typedef struct {
1216 IClassFactory IClassFactory_iface;
1217 LONG ref;
1218 REFCLSID rclsid;
1219 FnCreateInstance pfnCreateInstance;
1220 } IClassFactoryImpl;
1222 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
1224 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
1227 static HRESULT WINAPI DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
1229 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1230 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
1231 if (ppobj == NULL)
1232 return E_POINTER;
1233 if (IsEqualIID(riid, &IID_IUnknown) ||
1234 IsEqualIID(riid, &IID_IClassFactory))
1236 *ppobj = iface;
1237 IUnknown_AddRef(iface);
1238 return S_OK;
1240 *ppobj = NULL;
1241 return E_NOINTERFACE;
1244 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
1246 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1247 ULONG ref = InterlockedIncrement(&(This->ref));
1248 TRACE("(%p) ref %lu\n", iface, ref);
1249 return ref;
1252 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
1254 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1255 ULONG ref = InterlockedDecrement(&(This->ref));
1256 TRACE("(%p) ref %lu\n", iface, ref);
1257 /* static class, won't be freed */
1258 return ref;
1261 static HRESULT WINAPI DSCF_CreateInstance(
1262 LPCLASSFACTORY iface,
1263 LPUNKNOWN pOuter,
1264 REFIID riid,
1265 LPVOID *ppobj)
1267 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1268 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
1270 if (pOuter)
1271 return CLASS_E_NOAGGREGATION;
1273 if (ppobj == NULL) {
1274 WARN("invalid parameter\n");
1275 return DSERR_INVALIDPARAM;
1277 *ppobj = NULL;
1278 return This->pfnCreateInstance(riid, ppobj);
1281 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
1283 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1284 FIXME("(%p, %d) stub!\n", This, dolock);
1285 return S_OK;
1288 static const IClassFactoryVtbl DSCF_Vtbl = {
1289 DSCF_QueryInterface,
1290 DSCF_AddRef,
1291 DSCF_Release,
1292 DSCF_CreateInstance,
1293 DSCF_LockServer
1296 static IClassFactoryImpl DSOUND_CF[] = {
1297 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound, DSOUND_Create },
1298 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound8, DSOUND_Create8 },
1299 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
1300 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
1301 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
1302 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
1303 { {NULL}, 0, NULL, NULL }
1306 /*******************************************************************************
1307 * DllGetClassObject [DSOUND.@]
1308 * Retrieves class object from a DLL object
1310 * NOTES
1311 * Docs say returns STDAPI
1313 * PARAMS
1314 * rclsid [I] CLSID for the class object
1315 * riid [I] Reference to identifier of interface for class object
1316 * ppv [O] Address of variable to receive interface pointer for riid
1318 * RETURNS
1319 * Success: S_OK
1320 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
1321 * E_UNEXPECTED
1323 HRESULT WINAPI DSOAL_DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1325 int i = 0;
1326 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1328 if (ppv == NULL) {
1329 WARN("invalid parameter\n");
1330 return E_INVALIDARG;
1333 *ppv = NULL;
1335 if (!IsEqualIID(riid, &IID_IClassFactory) &&
1336 !IsEqualIID(riid, &IID_IUnknown)) {
1337 WARN("no interface for %s\n", debugstr_guid(riid));
1338 return E_NOINTERFACE;
1341 while (NULL != DSOUND_CF[i].rclsid) {
1342 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
1343 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
1344 *ppv = &DSOUND_CF[i].IClassFactory_iface;
1345 return S_OK;
1347 i++;
1350 WARN("No class found for %s\n", debugstr_guid(rclsid));
1351 return CLASS_E_CLASSNOTAVAILABLE;
1355 /*******************************************************************************
1356 * DllCanUnloadNow [DSOUND.4]
1357 * Determines whether the DLL is in use.
1359 * RETURNS
1360 * Success: S_OK
1361 * Failure: S_FALSE
1363 HRESULT WINAPI DSOAL_DllCanUnloadNow(void)
1365 FIXME("(void): stub\n");
1366 return S_FALSE;
1369 /***********************************************************************
1370 * DllMain (DSOUND.init)
1372 DECLSPEC_EXPORT BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
1374 const WCHAR *wstr;
1376 TRACE("(%p, %lu, %p)\n", hInstDLL, fdwReason, lpvReserved);
1378 switch(fdwReason)
1380 case DLL_PROCESS_ATTACH:
1381 LogFile = stderr;
1382 if((wstr=_wgetenv(L"DSOAL_LOGFILE")) != NULL && wstr[0] != 0)
1384 FILE *f = _wfopen(wstr, L"wt");
1385 if(!f) ERR("Failed to open log file %ls\n", wstr);
1386 else LogFile = f;
1389 if(!load_libopenal())
1390 return FALSE;
1391 TlsThreadPtr = TlsAlloc();
1392 InitializeCriticalSection(&openal_crst);
1393 /* Increase refcount on dsound by 1 */
1394 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
1395 break;
1397 case DLL_THREAD_ATTACH:
1398 break;
1400 case DLL_THREAD_DETACH:
1401 if(local_contexts)
1402 set_context(NULL);
1403 break;
1405 case DLL_PROCESS_DETACH:
1406 HeapFree(GetProcessHeap(), 0, PlaybackDevices.Guids);
1407 PlaybackDevices.Guids = NULL;
1408 PlaybackDevices.Count = 0;
1409 HeapFree(GetProcessHeap(), 0, CaptureDevices.Guids);
1410 CaptureDevices.Guids = NULL;
1411 CaptureDevices.Count = 0;
1413 if(openal_handle)
1414 FreeLibrary(openal_handle);
1415 TlsFree(TlsThreadPtr);
1416 DeleteCriticalSection(&openal_crst);
1417 if(LogFile != stderr)
1418 fclose(LogFile);
1419 LogFile = stderr;
1420 break;
1422 return TRUE;
1425 #ifdef __WINESRC__
1426 /***********************************************************************
1427 * DllRegisterServer (DSOUND.@)
1429 HRESULT WINAPI DllRegisterServer(void)
1431 return __wine_register_resources(instance);
1434 /***********************************************************************
1435 * DllUnregisterServer (DSOUND.@)
1437 HRESULT WINAPI DllUnregisterServer(void)
1439 return __wine_unregister_resources(instance);
1441 #endif