Avoid calling through the vtable when initializing known objects
[dsound-openal.git] / dsound_main.c
blob141cec053e93a7caca28c3b1d60a0c3385e7c538
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>
43 #include "dsound_private.h"
45 #ifndef DECLSPEC_EXPORT
46 #ifdef _WIN32
47 #define DECLSPEC_EXPORT __declspec(dllexport)
48 #else
49 #define DECLSPEC_EXPORT
50 #endif
51 #endif
53 DEFINE_GUID(CLSID_DirectSoundPrivate,0x11ab3ec0,0x25ec,0x11d1,0xa4,0xd8,0x00,0xc0,0x4f,0xc2,0x8a,0xca);
55 int LogLevel = 1;
58 const GUID DSOUND_renderer_guid = { 0xbd6dd71a, 0x3deb, 0x11d1, { 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 } };
59 const GUID DSOUND_capture_guid = { 0xbd6dd71b, 0x3deb, 0x11d1, { 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 } };
61 const IID DSPROPSETID_EAX20_ListenerProperties = {
62 0x0306a6a8, 0xb224, 0x11d2, { 0x99, 0xe5, 0x00, 0x00, 0xe8, 0xd8, 0xc7, 0x22 }
65 const IID DSPROPSETID_EAX20_BufferProperties = {
66 0x0306a6a7, 0xb224, 0x11d2, { 0x99, 0xe5, 0x00, 0x00, 0xe8, 0xd8, 0xc7, 0x22 }
69 const EAXLISTENERPROPERTIES EnvironmentDefaults[EAX_ENVIRONMENT_COUNT] =
71 REVERB_PRESET_GENERIC,
72 REVERB_PRESET_PADDEDCELL,
73 REVERB_PRESET_ROOM,
74 REVERB_PRESET_BATHROOM,
75 REVERB_PRESET_LIVINGROOM,
76 REVERB_PRESET_STONEROOM,
77 REVERB_PRESET_AUDITORIUM,
78 REVERB_PRESET_CONCERTHALL,
79 REVERB_PRESET_CAVE,
80 REVERB_PRESET_ARENA,
81 REVERB_PRESET_HANGAR,
82 REVERB_PRESET_CARPETEDHALLWAY,
83 REVERB_PRESET_HALLWAY,
84 REVERB_PRESET_STONECORRIDOR,
85 REVERB_PRESET_ALLEY,
86 REVERB_PRESET_FOREST,
87 REVERB_PRESET_CITY,
88 REVERB_PRESET_MOUNTAINS,
89 REVERB_PRESET_QUARRY,
90 REVERB_PRESET_PLAIN,
91 REVERB_PRESET_PARKINGLOT,
92 REVERB_PRESET_SEWERPIPE,
93 REVERB_PRESET_UNDERWATER,
94 REVERB_PRESET_DRUGGED,
95 REVERB_PRESET_DIZZY,
96 REVERB_PRESET_PSYCHOTIC
99 static CRITICAL_SECTION_DEBUG openal_crst_debug =
101 0, 0, &openal_crst,
102 { &openal_crst_debug.ProcessLocksList,
103 &openal_crst_debug.ProcessLocksList },
104 0, 0, { (DWORD_PTR)(__FILE__ ": openal_crst_debug") }
106 CRITICAL_SECTION openal_crst = { &openal_crst_debug, -1, 0, 0, 0, 0 };
108 int openal_loaded = 0;
109 static HANDLE openal_handle = NULL;
110 LPALCCREATECONTEXT palcCreateContext = NULL;
111 LPALCMAKECONTEXTCURRENT palcMakeContextCurrent = NULL;
112 LPALCPROCESSCONTEXT palcProcessContext = NULL;
113 LPALCSUSPENDCONTEXT palcSuspendContext = NULL;
114 LPALCDESTROYCONTEXT palcDestroyContext = NULL;
115 LPALCGETCURRENTCONTEXT palcGetCurrentContext = NULL;
116 LPALCGETCONTEXTSDEVICE palcGetContextsDevice = NULL;
117 LPALCOPENDEVICE palcOpenDevice = NULL;
118 LPALCCLOSEDEVICE palcCloseDevice = NULL;
119 LPALCGETERROR palcGetError = NULL;
120 LPALCISEXTENSIONPRESENT palcIsExtensionPresent = NULL;
121 LPALCGETPROCADDRESS palcGetProcAddress = NULL;
122 LPALCGETENUMVALUE palcGetEnumValue = NULL;
123 LPALCGETSTRING palcGetString = NULL;
124 LPALCGETINTEGERV palcGetIntegerv = NULL;
125 LPALCCAPTUREOPENDEVICE palcCaptureOpenDevice = NULL;
126 LPALCCAPTURECLOSEDEVICE palcCaptureCloseDevice = NULL;
127 LPALCCAPTURESTART palcCaptureStart = NULL;
128 LPALCCAPTURESTOP palcCaptureStop = NULL;
129 LPALCCAPTURESAMPLES palcCaptureSamples = NULL;
130 LPALENABLE palEnable = NULL;
131 LPALDISABLE palDisable = NULL;
132 LPALISENABLED palIsEnabled = NULL;
133 LPALGETSTRING palGetString = NULL;
134 LPALGETBOOLEANV palGetBooleanv = NULL;
135 LPALGETINTEGERV palGetIntegerv = NULL;
136 LPALGETFLOATV palGetFloatv = NULL;
137 LPALGETDOUBLEV palGetDoublev = NULL;
138 LPALGETBOOLEAN palGetBoolean = NULL;
139 LPALGETINTEGER palGetInteger = NULL;
140 LPALGETFLOAT palGetFloat = NULL;
141 LPALGETDOUBLE palGetDouble = NULL;
142 LPALGETERROR palGetError = NULL;
143 LPALISEXTENSIONPRESENT palIsExtensionPresent = NULL;
144 LPALGETPROCADDRESS palGetProcAddress = NULL;
145 LPALGETENUMVALUE palGetEnumValue = NULL;
146 LPALLISTENERF palListenerf = NULL;
147 LPALLISTENER3F palListener3f = NULL;
148 LPALLISTENERFV palListenerfv = NULL;
149 LPALLISTENERI palListeneri = NULL;
150 LPALLISTENER3I palListener3i = NULL;
151 LPALLISTENERIV palListeneriv = NULL;
152 LPALGETLISTENERF palGetListenerf = NULL;
153 LPALGETLISTENER3F palGetListener3f = NULL;
154 LPALGETLISTENERFV palGetListenerfv = NULL;
155 LPALGETLISTENERI palGetListeneri = NULL;
156 LPALGETLISTENER3I palGetListener3i = NULL;
157 LPALGETLISTENERIV palGetListeneriv = NULL;
158 LPALGENSOURCES palGenSources = NULL;
159 LPALDELETESOURCES palDeleteSources = NULL;
160 LPALISSOURCE palIsSource = NULL;
161 LPALSOURCEF palSourcef = NULL;
162 LPALSOURCE3F palSource3f = NULL;
163 LPALSOURCEFV palSourcefv = NULL;
164 LPALSOURCEI palSourcei = NULL;
165 LPALSOURCE3I palSource3i = NULL;
166 LPALSOURCEIV palSourceiv = NULL;
167 LPALGETSOURCEF palGetSourcef = NULL;
168 LPALGETSOURCE3F palGetSource3f = NULL;
169 LPALGETSOURCEFV palGetSourcefv = NULL;
170 LPALGETSOURCEI palGetSourcei = NULL;
171 LPALGETSOURCE3I palGetSource3i = NULL;
172 LPALGETSOURCEIV palGetSourceiv = NULL;
173 LPALSOURCEPLAYV palSourcePlayv = NULL;
174 LPALSOURCESTOPV palSourceStopv = NULL;
175 LPALSOURCEREWINDV palSourceRewindv = NULL;
176 LPALSOURCEPAUSEV palSourcePausev = NULL;
177 LPALSOURCEPLAY palSourcePlay = NULL;
178 LPALSOURCESTOP palSourceStop = NULL;
179 LPALSOURCEREWIND palSourceRewind = NULL;
180 LPALSOURCEPAUSE palSourcePause = NULL;
181 LPALSOURCEQUEUEBUFFERS palSourceQueueBuffers = NULL;
182 LPALSOURCEUNQUEUEBUFFERS palSourceUnqueueBuffers = NULL;
183 LPALGENBUFFERS palGenBuffers = NULL;
184 LPALDELETEBUFFERS palDeleteBuffers = NULL;
185 LPALISBUFFER palIsBuffer = NULL;
186 LPALBUFFERF palBufferf = NULL;
187 LPALBUFFER3F palBuffer3f = NULL;
188 LPALBUFFERFV palBufferfv = NULL;
189 LPALBUFFERI palBufferi = NULL;
190 LPALBUFFER3I palBuffer3i = NULL;
191 LPALBUFFERIV palBufferiv = NULL;
192 LPALGETBUFFERF palGetBufferf = NULL;
193 LPALGETBUFFER3F palGetBuffer3f = NULL;
194 LPALGETBUFFERFV palGetBufferfv = NULL;
195 LPALGETBUFFERI palGetBufferi = NULL;
196 LPALGETBUFFER3I palGetBuffer3i = NULL;
197 LPALGETBUFFERIV palGetBufferiv = NULL;
198 LPALBUFFERDATA palBufferData = NULL;
199 LPALDOPPLERFACTOR palDopplerFactor = NULL;
200 LPALDOPPLERVELOCITY palDopplerVelocity = NULL;
201 LPALDISTANCEMODEL palDistanceModel = NULL;
202 LPALSPEEDOFSOUND palSpeedOfSound = NULL;
204 LPALCMAKECONTEXTCURRENT set_context;
205 LPALCGETCURRENTCONTEXT get_context;
206 BOOL local_contexts;
208 static void EnterALSectionTLS(ALCcontext *ctx);
209 static void LeaveALSectionTLS(void);
210 static void EnterALSectionGlob(ALCcontext *ctx);
211 static void LeaveALSectionGlob(void);
213 DWORD TlsThreadPtr;
214 void (*EnterALSection)(ALCcontext *ctx) = EnterALSectionGlob;
215 void (*LeaveALSection)(void) = LeaveALSectionGlob;
218 static BOOL load_libopenal(void)
220 const char libname[] = "dsoal-aldrv.dll";
221 BOOL failed = FALSE;
222 const char *str;
224 str = getenv("DSOAL_LOGLEVEL");
225 if(str && *str)
226 LogLevel = atoi(str);
228 openal_handle = LoadLibraryA(libname);
229 if(!openal_handle)
231 ERR("Couldn't load %s: %lu\n", libname, GetLastError());
232 return FALSE;
235 #define LOAD_FUNCPTR(f) do { \
236 if((*((void**)&p##f) = GetProcAddress(openal_handle, #f)) == NULL) \
238 ERR("Couldn't lookup %s in %s\n", #f, libname); \
239 failed = TRUE; \
241 } while(0)
243 LOAD_FUNCPTR(alcCreateContext);
244 LOAD_FUNCPTR(alcMakeContextCurrent);
245 LOAD_FUNCPTR(alcProcessContext);
246 LOAD_FUNCPTR(alcSuspendContext);
247 LOAD_FUNCPTR(alcDestroyContext);
248 LOAD_FUNCPTR(alcGetCurrentContext);
249 LOAD_FUNCPTR(alcGetContextsDevice);
250 LOAD_FUNCPTR(alcOpenDevice);
251 LOAD_FUNCPTR(alcCloseDevice);
252 LOAD_FUNCPTR(alcGetError);
253 LOAD_FUNCPTR(alcIsExtensionPresent);
254 LOAD_FUNCPTR(alcGetProcAddress);
255 LOAD_FUNCPTR(alcGetEnumValue);
256 LOAD_FUNCPTR(alcGetString);
257 LOAD_FUNCPTR(alcGetIntegerv);
258 LOAD_FUNCPTR(alcCaptureOpenDevice);
259 LOAD_FUNCPTR(alcCaptureCloseDevice);
260 LOAD_FUNCPTR(alcCaptureStart);
261 LOAD_FUNCPTR(alcCaptureStop);
262 LOAD_FUNCPTR(alcCaptureSamples);
263 LOAD_FUNCPTR(alEnable);
264 LOAD_FUNCPTR(alDisable);
265 LOAD_FUNCPTR(alIsEnabled);
266 LOAD_FUNCPTR(alGetString);
267 LOAD_FUNCPTR(alGetBooleanv);
268 LOAD_FUNCPTR(alGetIntegerv);
269 LOAD_FUNCPTR(alGetFloatv);
270 LOAD_FUNCPTR(alGetDoublev);
271 LOAD_FUNCPTR(alGetBoolean);
272 LOAD_FUNCPTR(alGetInteger);
273 LOAD_FUNCPTR(alGetFloat);
274 LOAD_FUNCPTR(alGetDouble);
275 LOAD_FUNCPTR(alGetError);
276 LOAD_FUNCPTR(alIsExtensionPresent);
277 LOAD_FUNCPTR(alGetProcAddress);
278 LOAD_FUNCPTR(alGetEnumValue);
279 LOAD_FUNCPTR(alListenerf);
280 LOAD_FUNCPTR(alListener3f);
281 LOAD_FUNCPTR(alListenerfv);
282 LOAD_FUNCPTR(alListeneri);
283 LOAD_FUNCPTR(alListener3i);
284 LOAD_FUNCPTR(alListeneriv);
285 LOAD_FUNCPTR(alGetListenerf);
286 LOAD_FUNCPTR(alGetListener3f);
287 LOAD_FUNCPTR(alGetListenerfv);
288 LOAD_FUNCPTR(alGetListeneri);
289 LOAD_FUNCPTR(alGetListener3i);
290 LOAD_FUNCPTR(alGetListeneriv);
291 LOAD_FUNCPTR(alGenSources);
292 LOAD_FUNCPTR(alDeleteSources);
293 LOAD_FUNCPTR(alIsSource);
294 LOAD_FUNCPTR(alSourcef);
295 LOAD_FUNCPTR(alSource3f);
296 LOAD_FUNCPTR(alSourcefv);
297 LOAD_FUNCPTR(alSourcei);
298 LOAD_FUNCPTR(alSource3i);
299 LOAD_FUNCPTR(alSourceiv);
300 LOAD_FUNCPTR(alGetSourcef);
301 LOAD_FUNCPTR(alGetSource3f);
302 LOAD_FUNCPTR(alGetSourcefv);
303 LOAD_FUNCPTR(alGetSourcei);
304 LOAD_FUNCPTR(alGetSource3i);
305 LOAD_FUNCPTR(alGetSourceiv);
306 LOAD_FUNCPTR(alSourcePlayv);
307 LOAD_FUNCPTR(alSourceStopv);
308 LOAD_FUNCPTR(alSourceRewindv);
309 LOAD_FUNCPTR(alSourcePausev);
310 LOAD_FUNCPTR(alSourcePlay);
311 LOAD_FUNCPTR(alSourceStop);
312 LOAD_FUNCPTR(alSourceRewind);
313 LOAD_FUNCPTR(alSourcePause);
314 LOAD_FUNCPTR(alSourceQueueBuffers);
315 LOAD_FUNCPTR(alSourceUnqueueBuffers);
316 LOAD_FUNCPTR(alGenBuffers);
317 LOAD_FUNCPTR(alDeleteBuffers);
318 LOAD_FUNCPTR(alIsBuffer);
319 LOAD_FUNCPTR(alBufferf);
320 LOAD_FUNCPTR(alBuffer3f);
321 LOAD_FUNCPTR(alBufferfv);
322 LOAD_FUNCPTR(alBufferi);
323 LOAD_FUNCPTR(alBuffer3i);
324 LOAD_FUNCPTR(alBufferiv);
325 LOAD_FUNCPTR(alGetBufferf);
326 LOAD_FUNCPTR(alGetBuffer3f);
327 LOAD_FUNCPTR(alGetBufferfv);
328 LOAD_FUNCPTR(alGetBufferi);
329 LOAD_FUNCPTR(alGetBuffer3i);
330 LOAD_FUNCPTR(alGetBufferiv);
331 LOAD_FUNCPTR(alBufferData);
332 LOAD_FUNCPTR(alDopplerFactor);
333 LOAD_FUNCPTR(alDopplerVelocity);
334 LOAD_FUNCPTR(alDistanceModel);
335 LOAD_FUNCPTR(alSpeedOfSound);
336 #undef LOAD_FUNCPTR
337 if (failed)
339 WARN("Unloading %s\n", libname);
340 if (openal_handle != NULL)
341 FreeLibrary(openal_handle);
342 openal_handle = NULL;
343 return FALSE;
346 openal_loaded = 1;
347 TRACE("Loaded %s\n", libname);
349 local_contexts = alcIsExtensionPresent(NULL, "ALC_EXT_thread_local_context");
350 if(local_contexts)
352 TRACE("Found ALC_EXT_thread_local_context\n");
354 set_context = alcGetProcAddress(NULL, "alcSetThreadContext");
355 get_context = alcGetProcAddress(NULL, "alcGetThreadContext");
356 if(!set_context || !get_context)
358 ERR("TLS advertised but functions not found, disabling thread local contexts\n");
359 local_contexts = 0;
362 if(!local_contexts)
364 set_context = alcMakeContextCurrent;
365 get_context = alcGetCurrentContext;
367 else
369 EnterALSection = EnterALSectionTLS;
370 LeaveALSection = LeaveALSectionTLS;
373 return TRUE;
377 static void EnterALSectionTLS(ALCcontext *ctx)
379 if(LIKELY(ctx == TlsGetValue(TlsThreadPtr)))
380 return;
382 if(LIKELY(set_context(ctx) != ALC_FALSE))
383 TlsSetValue(TlsThreadPtr, ctx);
384 else
386 ERR("Couldn't set current context!!\n");
387 checkALCError(alcGetContextsDevice(ctx));
390 static void LeaveALSectionTLS(void)
394 static void EnterALSectionGlob(ALCcontext *ctx)
396 EnterCriticalSection(&openal_crst);
397 if(UNLIKELY(alcMakeContextCurrent(ctx) == ALC_FALSE))
399 ERR("Couldn't set current context!!\n");
400 checkALCError(alcGetContextsDevice(ctx));
403 static void LeaveALSectionGlob(void)
405 LeaveCriticalSection(&openal_crst);
409 const ALCchar *DSOUND_getdevicestrings(void)
411 if(alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT"))
412 return alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER);
413 return alcGetString(NULL, ALC_DEVICE_SPECIFIER);
416 const ALCchar *DSOUND_getcapturedevicestrings(void)
418 return alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
421 /***************************************************************************
422 * GetDeviceID [DSOUND.9]
424 * Retrieves unique identifier of default device specified
426 * PARAMS
427 * pGuidSrc [I] Address of device GUID.
428 * pGuidDest [O] Address to receive unique device GUID.
430 * RETURNS
431 * Success: DS_OK
432 * Failure: DSERR_INVALIDPARAM
434 * NOTES
435 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
436 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
437 * DSDEVID_DefaultVoiceCapture.
438 * Returns pGuidSrc if pGuidSrc is a valid device or the device
439 * GUID for the specified constants.
441 DECLSPEC_EXPORT HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
443 TRACE("(%s, %p)\n", debugstr_guid(pGuidSrc), pGuidDest);
445 if(pGuidSrc == NULL)
447 WARN("invalid parameter: pGuidSrc == NULL\n");
448 return DSERR_INVALIDPARAM;
451 if(pGuidDest == NULL)
453 WARN("invalid parameter: pGuidDest == NULL\n");
454 return DSERR_INVALIDPARAM;
457 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuidSrc) ||
458 IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuidSrc))
459 *pGuidDest = DSOUND_renderer_guid;
460 else if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuidSrc) ||
461 IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuidSrc))
462 *pGuidDest = DSOUND_capture_guid;
463 else
464 *pGuidDest = *pGuidSrc;
466 TRACE("returns %s\n", debugstr_guid(pGuidDest));
467 return DS_OK;
470 struct ConvAcontext
472 LPDSENUMCALLBACKA callA;
473 LPVOID data;
475 CHAR *str;
476 int strlen;
479 static BOOL CALLBACK w2a_callback(LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
481 struct ConvAcontext *context = data;
482 int desclen, modlen;
483 CHAR *descA, *modA;
485 desclen = WideCharToMultiByte(CP_ACP, 0, descW, -1, NULL, 0, NULL, NULL);
486 modlen = WideCharToMultiByte(CP_ACP, 0, modW, -1, NULL, 0, NULL, NULL);
488 if(desclen+modlen > context->strlen)
490 CHAR *newstr = realloc(context->str, desclen+modlen+2);
491 if(!newstr) return FALSE;
493 context->str = newstr;
494 context->strlen = desclen+modlen;
497 descA = context->str;
498 modA = context->str + desclen+1;
500 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, desclen, NULL, NULL);
501 descA[desclen] = 0;
502 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, modlen, NULL, NULL);
503 modA[modlen] = 0;
505 return context->callA(guid, descA, modA, context->data);
508 /***************************************************************************
509 * DirectSoundEnumerateA [DSOUND.2]
511 * Enumerate all DirectSound drivers installed in the system
513 * PARAMS
514 * lpDSEnumCallback [I] Address of callback function.
515 * lpContext [I] Address of user defined context passed to callback function.
517 * RETURNS
518 * Success: DS_OK
519 * Failure: DSERR_INVALIDPARAM
521 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundEnumerateA(
522 LPDSENUMCALLBACKA lpDSEnumCallback,
523 LPVOID lpContext)
525 struct ConvAcontext context;
526 HRESULT ret;
528 TRACE("lpDSEnumCallback = %p, lpContext = %p\n", lpDSEnumCallback, lpContext);
530 if(lpDSEnumCallback == NULL) {
531 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
532 return DSERR_INVALIDPARAM;
535 context.callA = lpDSEnumCallback;
536 context.data = lpContext;
537 context.str = NULL;
538 context.strlen = 0;
540 ret = DirectSoundEnumerateW(w2a_callback, &context);
542 free(context.str);
543 return ret;
546 /***************************************************************************
547 * DirectSoundEnumerateW [DSOUND.3]
549 * Enumerate all DirectSound drivers installed in the system
551 * PARAMS
552 * lpDSEnumCallback [I] Address of callback function.
553 * lpContext [I] Address of user defined context passed to callback function.
555 * RETURNS
556 * Success: DS_OK
557 * Failure: DSERR_INVALIDPARAM
559 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundEnumerateW(
560 LPDSENUMCALLBACKW lpDSEnumCallback,
561 LPVOID lpContext )
563 TRACE("lpDSEnumCallback = %p, lpContext = %p\n", lpDSEnumCallback, lpContext);
565 if(!lpDSEnumCallback)
567 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
568 return DSERR_INVALIDPARAM;
571 if(!openal_loaded)
573 ERR("Attempting to enumerate sound cards without OpenAL support\n");
574 ERR("Please recompile wine with OpenAL for sound to work\n");
576 else
578 WCHAR *devwstr = NULL;
579 int devwstrlen = 0;
580 const ALCchar *devstr;
581 GUID guid;
583 EnterCriticalSection(&openal_crst);
584 devstr = DSOUND_getdevicestrings();
585 if(!devstr || !*devstr)
586 goto out;
588 TRACE("calling lpDSEnumCallback(NULL,\"%ls\",\"%ls\",%p)\n",
589 L"Primary Sound Driver", L"", lpContext);
590 if(lpDSEnumCallback(NULL, L"Primary Sound Driver", L"", lpContext) == FALSE)
591 goto out;
593 guid = DSOUND_renderer_guid;
594 do {
595 int len;
597 len = MultiByteToWideChar(CP_UTF8, 0, devstr, -1, NULL, 0);
598 if(len <= 0) break;
600 if(len > devwstrlen)
602 WCHAR *str = realloc(devwstr, (len+1)*sizeof(WCHAR));
603 if(!str) break;
605 devwstr = str;
606 devwstrlen = len;
608 MultiByteToWideChar(CP_UTF8, 0, devstr, -1, devwstr, len);
609 devwstr[len] = 0;
611 TRACE("calling lpDSEnumCallback(%s,\"%ls\",\"%ls\",%p)\n",
612 debugstr_guid(&guid), devwstr, L"wineal.drv", lpContext);
613 if(lpDSEnumCallback(&guid, devwstr, L"wineal.drv", lpContext) == FALSE)
614 goto out;
616 guid.Data4[7]++;
617 devstr += strlen(devstr)+1;
618 } while(*devstr);
619 out:
620 LeaveCriticalSection(&openal_crst);
621 free(devwstr);
623 return DS_OK;
626 /***************************************************************************
627 * DirectSoundCaptureEnumerateA [DSOUND.7]
629 * Enumerate all DirectSound drivers installed in the system.
631 * PARAMS
632 * lpDSEnumCallback [I] Address of callback function.
633 * lpContext [I] Address of user defined context passed to callback function.
635 * RETURNS
636 * Success: DS_OK
637 * Failure: DSERR_INVALIDPARAM
639 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundCaptureEnumerateA(
640 LPDSENUMCALLBACKA lpDSEnumCallback,
641 LPVOID lpContext)
643 struct ConvAcontext context;
644 HRESULT ret;
646 TRACE("lpDSEnumCallback = %p, lpContext = %p\n", lpDSEnumCallback, lpContext);
648 if(lpDSEnumCallback == NULL) {
649 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
650 return DSERR_INVALIDPARAM;
653 context.callA = lpDSEnumCallback;
654 context.data = lpContext;
655 context.str = NULL;
656 context.strlen = 0;
658 ret = DirectSoundCaptureEnumerateW(w2a_callback, &context);
660 free(context.str);
661 return ret;
664 /***************************************************************************
665 * DirectSoundCaptureEnumerateW [DSOUND.8]
667 * Enumerate all DirectSound drivers installed in the system.
669 * PARAMS
670 * lpDSEnumCallback [I] Address of callback function.
671 * lpContext [I] Address of user defined context passed to callback function.
673 * RETURNS
674 * Success: DS_OK
675 * Failure: DSERR_INVALIDPARAM
677 DECLSPEC_EXPORT HRESULT WINAPI DirectSoundCaptureEnumerateW(
678 LPDSENUMCALLBACKW lpDSEnumCallback,
679 LPVOID lpContext)
681 TRACE("lpDSEnumCallback = %p, lpContext = %p\n", lpDSEnumCallback, lpContext);
683 if(!lpDSEnumCallback)
685 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
686 return DSERR_INVALIDPARAM;
689 if(!openal_loaded)
691 ERR("Attempting to enumerate sound cards without OpenAL support\n");
692 ERR("Please recompile wine with OpenAL for sound to work\n");
694 else
696 WCHAR *devwstr = NULL;
697 int devwstrlen = 0;
698 const ALCchar *devstr;
699 GUID guid;
701 EnterCriticalSection(&openal_crst);
702 devstr = DSOUND_getcapturedevicestrings();
703 if(!devstr || !*devstr)
704 goto out;
706 TRACE("calling lpDSEnumCallback(NULL,\"%ls\",\"%ls\",%p)\n",
707 L"Primary Sound Driver", L"", lpContext);
708 if(lpDSEnumCallback(NULL, L"Primary Sound Driver", L"", lpContext) == FALSE)
709 goto out;
711 guid = DSOUND_capture_guid;
712 do {
713 int len;
715 len = MultiByteToWideChar(CP_UTF8, 0, devstr, -1, NULL, 0);
716 if(len <= 0) break;
718 if(len > devwstrlen)
720 WCHAR *str = realloc(devwstr, (len+1)*sizeof(WCHAR));
721 if(!str) break;
723 devwstr = str;
724 devwstrlen = len;
726 MultiByteToWideChar(CP_UTF8, 0, devstr, -1, devwstr, len);
727 devwstr[len] = 0;
729 TRACE("calling lpDSEnumCallback(%s,\"%ls\",\"%ls\",%p)\n",
730 debugstr_guid(&guid), devwstr, L"wineal.drv", lpContext);
731 if(lpDSEnumCallback(&guid, devwstr, L"wineal.drv", lpContext) == FALSE)
732 goto out;
734 guid.Data4[7]++;
735 devstr += strlen(devstr)+1;
736 } while(*devstr);
737 out:
738 LeaveCriticalSection(&openal_crst);
739 free(devwstr);
742 return DS_OK;
745 /*******************************************************************************
746 * DirectSoundCreate (DSOUND.1)
748 * Creates and initializes a DirectSound interface.
750 * PARAMS
751 * lpcGUID [I] Address of the GUID that identifies the sound device.
752 * ppDS [O] Address of a variable to receive the interface pointer.
753 * pUnkOuter [I] Must be NULL.
755 * RETURNS
756 * Success: DS_OK
757 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
758 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
760 DECLSPEC_EXPORT HRESULT WINAPI
761 DirectSoundCreate(LPCGUID lpcGUID, IDirectSound **ppDS, IUnknown *pUnkOuter)
763 HRESULT hr;
764 void *pDS;
766 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
768 if (ppDS == NULL) {
769 WARN("invalid parameter: ppDS == NULL\n");
770 return DSERR_INVALIDPARAM;
772 *ppDS = NULL;
774 if (pUnkOuter != NULL) {
775 WARN("invalid parameter: pUnkOuter != NULL\n");
776 return DSERR_INVALIDPARAM;
779 hr = DSOUND_Create(&IID_IDirectSound, &pDS);
780 if(SUCCEEDED(hr))
782 *ppDS = pDS;
783 hr = IDirectSound_Initialize(*ppDS, lpcGUID);
784 if(FAILED(hr))
786 IDirectSound_Release(*ppDS);
787 *ppDS = NULL;
791 return hr;
794 /*******************************************************************************
795 * DirectSoundCreate8 (DSOUND.11)
797 * Creates and initializes a DirectSound8 interface.
799 * PARAMS
800 * lpcGUID [I] Address of the GUID that identifies the sound device.
801 * ppDS [O] Address of a variable to receive the interface pointer.
802 * pUnkOuter [I] Must be NULL.
804 * RETURNS
805 * Success: DS_OK
806 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
807 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
809 DECLSPEC_EXPORT HRESULT WINAPI
810 DirectSoundCreate8(LPCGUID lpcGUID, IDirectSound8 **ppDS, IUnknown *pUnkOuter)
812 HRESULT hr;
813 void *pDS;
815 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
817 if (ppDS == NULL) {
818 WARN("invalid parameter: ppDS == NULL\n");
819 return DSERR_INVALIDPARAM;
821 *ppDS = NULL;
823 if (pUnkOuter != NULL) {
824 WARN("invalid parameter: pUnkOuter != NULL\n");
825 return DSERR_INVALIDPARAM;
828 hr = DSOUND_Create8(&IID_IDirectSound8, &pDS);
829 if(SUCCEEDED(hr))
831 *ppDS = pDS;
832 hr = IDirectSound8_Initialize(*ppDS, lpcGUID);
833 if(FAILED(hr))
835 IDirectSound8_Release(*ppDS);
836 *ppDS = NULL;
840 return hr;
843 /***************************************************************************
844 * DirectSoundCaptureCreate [DSOUND.6]
846 * Create and initialize a DirectSoundCapture interface.
848 * PARAMS
849 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
850 * lplpDSC [O] Address of a variable to receive the interface pointer.
851 * pUnkOuter [I] Must be NULL.
853 * RETURNS
854 * Success: DS_OK
855 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
856 * DSERR_OUTOFMEMORY
858 * NOTES
859 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
860 * or NULL for the default device or DSDEVID_DefaultCapture or
861 * DSDEVID_DefaultVoiceCapture.
863 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
865 DECLSPEC_EXPORT HRESULT WINAPI
866 DirectSoundCaptureCreate(LPCGUID lpcGUID, IDirectSoundCapture **ppDSC, IUnknown *pUnkOuter)
868 HRESULT hr;
869 void *pDSC;
871 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
873 if(ppDSC == NULL)
875 WARN("invalid parameter: ppDSC == NULL\n");
876 return DSERR_INVALIDPARAM;
878 *ppDSC = NULL;
880 if(pUnkOuter)
882 WARN("invalid parameter: pUnkOuter != NULL\n");
883 return DSERR_NOAGGREGATION;
886 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, &pDSC);
887 if(SUCCEEDED(hr))
889 *ppDSC = pDSC;
890 hr = IDirectSoundCapture_Initialize(*ppDSC, lpcGUID);
891 if(FAILED(hr))
893 IDirectSoundCapture_Release(*ppDSC);
894 *ppDSC = NULL;
898 return hr;
901 /***************************************************************************
902 * DirectSoundCaptureCreate8 [DSOUND.12]
904 * Create and initialize a DirectSoundCapture interface.
906 * PARAMS
907 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
908 * lplpDSC [O] Address of a variable to receive the interface pointer.
909 * pUnkOuter [I] Must be NULL.
911 * RETURNS
912 * Success: DS_OK
913 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
914 * DSERR_OUTOFMEMORY
916 * NOTES
917 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
918 * or NULL for the default device or DSDEVID_DefaultCapture or
919 * DSDEVID_DefaultVoiceCapture.
921 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
923 DECLSPEC_EXPORT HRESULT WINAPI
924 DirectSoundCaptureCreate8(LPCGUID lpcGUID, IDirectSoundCapture8 **ppDSC8, IUnknown *pUnkOuter)
926 HRESULT hr;
927 void *pDSC8;
929 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
931 if(ppDSC8 == NULL)
933 WARN("invalid parameter: ppDSC8 == NULL\n");
934 return DSERR_INVALIDPARAM;
936 *ppDSC8 = NULL;
938 if(pUnkOuter)
940 WARN("invalid parameter: pUnkOuter != NULL\n");
941 return DSERR_NOAGGREGATION;
944 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture, &pDSC8);
945 if(SUCCEEDED(hr))
947 *ppDSC8 = pDSC8;
948 hr = IDirectSoundCapture_Initialize(*ppDSC8, lpcGUID);
949 if(FAILED(hr))
951 IDirectSoundCapture_Release(*ppDSC8);
952 *ppDSC8 = NULL;
956 return hr;
959 /*******************************************************************************
960 * DirectSound ClassFactory
963 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
965 typedef struct {
966 IClassFactory IClassFactory_iface;
967 LONG ref;
968 REFCLSID rclsid;
969 FnCreateInstance pfnCreateInstance;
970 } IClassFactoryImpl;
972 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
974 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
977 static HRESULT WINAPI DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
979 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
980 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
981 if (ppobj == NULL)
982 return E_POINTER;
983 if (IsEqualIID(riid, &IID_IUnknown) ||
984 IsEqualIID(riid, &IID_IClassFactory))
986 *ppobj = iface;
987 IUnknown_AddRef(iface);
988 return S_OK;
990 *ppobj = NULL;
991 return E_NOINTERFACE;
994 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
996 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
997 ULONG ref = InterlockedIncrement(&(This->ref));
998 TRACE("(%p) ref was %lu\n", This, ref - 1);
999 return ref;
1002 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
1004 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1005 ULONG ref = InterlockedDecrement(&(This->ref));
1006 TRACE("(%p) ref was %lu\n", This, ref + 1);
1007 /* static class, won't be freed */
1008 return ref;
1011 static HRESULT WINAPI DSCF_CreateInstance(
1012 LPCLASSFACTORY iface,
1013 LPUNKNOWN pOuter,
1014 REFIID riid,
1015 LPVOID *ppobj)
1017 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1018 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
1020 if (pOuter)
1021 return CLASS_E_NOAGGREGATION;
1023 if (ppobj == NULL) {
1024 WARN("invalid parameter\n");
1025 return DSERR_INVALIDPARAM;
1027 *ppobj = NULL;
1028 return This->pfnCreateInstance(riid, ppobj);
1031 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
1033 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
1034 FIXME("(%p, %d) stub!\n", This, dolock);
1035 return S_OK;
1038 static const IClassFactoryVtbl DSCF_Vtbl = {
1039 DSCF_QueryInterface,
1040 DSCF_AddRef,
1041 DSCF_Release,
1042 DSCF_CreateInstance,
1043 DSCF_LockServer
1046 static IClassFactoryImpl DSOUND_CF[] = {
1047 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound, DSOUND_Create },
1048 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound8, DSOUND_Create8 },
1049 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
1050 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
1051 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
1052 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
1053 { {NULL}, 0, NULL, NULL }
1056 /*******************************************************************************
1057 * DllGetClassObject [DSOUND.@]
1058 * Retrieves class object from a DLL object
1060 * NOTES
1061 * Docs say returns STDAPI
1063 * PARAMS
1064 * rclsid [I] CLSID for the class object
1065 * riid [I] Reference to identifier of interface for class object
1066 * ppv [O] Address of variable to receive interface pointer for riid
1068 * RETURNS
1069 * Success: S_OK
1070 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
1071 * E_UNEXPECTED
1073 DECLSPEC_EXPORT HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1075 int i = 0;
1076 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1078 if (ppv == NULL) {
1079 WARN("invalid parameter\n");
1080 return E_INVALIDARG;
1083 *ppv = NULL;
1085 if (!IsEqualIID(riid, &IID_IClassFactory) &&
1086 !IsEqualIID(riid, &IID_IUnknown)) {
1087 WARN("no interface for %s\n", debugstr_guid(riid));
1088 return E_NOINTERFACE;
1091 while (NULL != DSOUND_CF[i].rclsid) {
1092 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
1093 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
1094 *ppv = &DSOUND_CF[i];
1095 return S_OK;
1097 i++;
1100 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
1101 debugstr_guid(riid), ppv);
1102 return CLASS_E_CLASSNOTAVAILABLE;
1106 /*******************************************************************************
1107 * DllCanUnloadNow [DSOUND.4]
1108 * Determines whether the DLL is in use.
1110 * RETURNS
1111 * Success: S_OK
1112 * Failure: S_FALSE
1114 DECLSPEC_EXPORT HRESULT WINAPI DllCanUnloadNow(void)
1116 FIXME("(void): stub\n");
1117 return S_FALSE;
1120 /***********************************************************************
1121 * DllMain (DSOUND.init)
1123 DECLSPEC_EXPORT BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
1125 TRACE("(%p, %lu, %p)\n", hInstDLL, fdwReason, lpvReserved);
1127 switch(fdwReason)
1129 case DLL_PROCESS_ATTACH:
1130 TRACE("DLL_PROCESS_ATTACH\n");
1131 if(!load_libopenal())
1132 return FALSE;
1133 TlsThreadPtr = TlsAlloc();
1134 /* Increase refcount on dsound by 1 */
1135 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
1136 break;
1138 case DLL_THREAD_ATTACH:
1139 TRACE("DLL_THREAD_ATTACH\n");
1140 break;
1142 case DLL_THREAD_DETACH:
1143 TRACE("DLL_THREAD_DETACH\n");
1144 if(local_contexts)
1145 set_context(NULL);
1146 break;
1148 case DLL_PROCESS_DETACH:
1149 TRACE("DLL_PROCESS_DETACH\n");
1150 if(openal_handle)
1151 FreeLibrary(openal_handle);
1152 TlsFree(TlsThreadPtr);
1153 break;
1155 default:
1156 TRACE("UNKNOWN REASON\n");
1157 break;
1159 return TRUE;
1162 #ifdef __WINESRC__
1163 /***********************************************************************
1164 * DllRegisterServer (DSOUND.@)
1166 HRESULT WINAPI DllRegisterServer(void)
1168 return __wine_register_resources(instance);
1171 /***********************************************************************
1172 * DllUnregisterServer (DSOUND.@)
1174 HRESULT WINAPI DllUnregisterServer(void)
1176 return __wine_unregister_resources(instance);
1178 #endif