Protect the primary buffer when modifying it during secondary buffer destruction
[dsound-openal.git] / dsound_main.c
blob2f438f91876ba77865feffc773d2c28def77de28
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 #include <stdarg.h>
37 #ifdef __WINESRC__
39 #include "wine/library.h"
41 #define COBJMACROS
42 #define NONAMELESSSTRUCT
43 #define NONAMELESSUNION
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winuser.h"
47 #include "winnls.h"
48 #include "winreg.h"
49 #include "mmsystem.h"
50 #include "winternl.h"
51 #include "mmddk.h"
52 #include "wine/debug.h"
53 #include "dsound.h"
54 #include "dsconf.h"
55 #include "ks.h"
56 #include "initguid.h"
57 #include "ksmedia.h"
58 #include "rpcproxy.h"
60 #include "dsound_private.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
64 #else
66 #define WINVER 0x0600
67 #include <windows.h>
68 #include <dsound.h>
70 #include "dsound_private.h"
72 #ifndef CP_UNIXCP
73 #define CP_UNIXCP 65010
74 #endif
76 DEFINE_GUID(CLSID_DirectSoundPrivate,0x11ab3ec0,0x25ec,0x11d1,0xa4,0xd8,0x00,0xc0,0x4f,0xc2,0x8a,0xca);
78 int LogLevel = 1;
80 /* From dlls/winecrt0/register.c */
81 static HRESULT __wine_register_resources(HMODULE module, const CLSID *clsid)
83 (void)module;
84 (void)clsid;
85 return E_FAIL;
87 static HRESULT __wine_unregister_resources(HMODULE module, const CLSID *clsid)
89 (void)module;
90 (void)clsid;
91 return E_FAIL;
93 #endif
95 static HINSTANCE instance;
97 const GUID DSOUND_renderer_guid = { 0xbd6dd71a, 0x3deb, 0x11d1, { 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 } };
98 const GUID DSOUND_capture_guid = { 0xbd6dd71b, 0x3deb, 0x11d1, { 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 } };
100 const IID DSPROPSETID_EAX20_ListenerProperties = {
101 0x0306a6a8, 0xb224, 0x11d2, { 0x99, 0xe5, 0x00, 0x00, 0xe8, 0xd8, 0xc7, 0x22 }
104 const EAXLISTENERPROPERTIES EnvironmentDefaults[EAX_ENVIRONMENT_COUNT] =
106 REVERB_PRESET_GENERIC,
107 REVERB_PRESET_PADDEDCELL,
108 REVERB_PRESET_ROOM,
109 REVERB_PRESET_BATHROOM,
110 REVERB_PRESET_LIVINGROOM,
111 REVERB_PRESET_STONEROOM,
112 REVERB_PRESET_AUDITORIUM,
113 REVERB_PRESET_CONCERTHALL,
114 REVERB_PRESET_CAVE,
115 REVERB_PRESET_ARENA,
116 REVERB_PRESET_HANGAR,
117 REVERB_PRESET_CARPETEDHALLWAY,
118 REVERB_PRESET_HALLWAY,
119 REVERB_PRESET_STONECORRIDOR,
120 REVERB_PRESET_ALLEY,
121 REVERB_PRESET_FOREST,
122 REVERB_PRESET_CITY,
123 REVERB_PRESET_MOUNTAINS,
124 REVERB_PRESET_QUARRY,
125 REVERB_PRESET_PLAIN,
126 REVERB_PRESET_PARKINGLOT,
127 REVERB_PRESET_SEWERPIPE,
128 REVERB_PRESET_UNDERWATER,
129 REVERB_PRESET_DRUGGED,
130 REVERB_PRESET_DIZZY,
131 REVERB_PRESET_PSYCHOTIC
134 static CRITICAL_SECTION_DEBUG openal_crst_debug =
136 0, 0, &openal_crst,
137 { &openal_crst_debug.ProcessLocksList,
138 &openal_crst_debug.ProcessLocksList },
139 0, 0, { (DWORD_PTR)(__FILE__ ": openal_crst_debug") }
141 CRITICAL_SECTION openal_crst = { &openal_crst_debug, -1, 0, 0, 0, 0 };
143 int openal_loaded = 0;
144 #ifdef SONAME_LIBOPENAL
145 static void *openal_handle = NULL;
146 LPALCCREATECONTEXT palcCreateContext = NULL;
147 LPALCMAKECONTEXTCURRENT palcMakeContextCurrent = NULL;
148 LPALCPROCESSCONTEXT palcProcessContext = NULL;
149 LPALCSUSPENDCONTEXT palcSuspendContext = NULL;
150 LPALCDESTROYCONTEXT palcDestroyContext = NULL;
151 LPALCGETCURRENTCONTEXT palcGetCurrentContext = NULL;
152 LPALCGETCONTEXTSDEVICE palcGetContextsDevice = NULL;
153 LPALCOPENDEVICE palcOpenDevice = NULL;
154 LPALCCLOSEDEVICE palcCloseDevice = NULL;
155 LPALCGETERROR palcGetError = NULL;
156 LPALCISEXTENSIONPRESENT palcIsExtensionPresent = NULL;
157 LPALCGETPROCADDRESS palcGetProcAddress = NULL;
158 LPALCGETENUMVALUE palcGetEnumValue = NULL;
159 LPALCGETSTRING palcGetString = NULL;
160 LPALCGETINTEGERV palcGetIntegerv = NULL;
161 LPALCCAPTUREOPENDEVICE palcCaptureOpenDevice = NULL;
162 LPALCCAPTURECLOSEDEVICE palcCaptureCloseDevice = NULL;
163 LPALCCAPTURESTART palcCaptureStart = NULL;
164 LPALCCAPTURESTOP palcCaptureStop = NULL;
165 LPALCCAPTURESAMPLES palcCaptureSamples = NULL;
166 LPALENABLE palEnable = NULL;
167 LPALDISABLE palDisable = NULL;
168 LPALISENABLED palIsEnabled = NULL;
169 LPALGETSTRING palGetString = NULL;
170 LPALGETBOOLEANV palGetBooleanv = NULL;
171 LPALGETINTEGERV palGetIntegerv = NULL;
172 LPALGETFLOATV palGetFloatv = NULL;
173 LPALGETDOUBLEV palGetDoublev = NULL;
174 LPALGETBOOLEAN palGetBoolean = NULL;
175 LPALGETINTEGER palGetInteger = NULL;
176 LPALGETFLOAT palGetFloat = NULL;
177 LPALGETDOUBLE palGetDouble = NULL;
178 LPALGETERROR palGetError = NULL;
179 LPALISEXTENSIONPRESENT palIsExtensionPresent = NULL;
180 LPALGETPROCADDRESS palGetProcAddress = NULL;
181 LPALGETENUMVALUE palGetEnumValue = NULL;
182 LPALLISTENERF palListenerf = NULL;
183 LPALLISTENER3F palListener3f = NULL;
184 LPALLISTENERFV palListenerfv = NULL;
185 LPALLISTENERI palListeneri = NULL;
186 LPALLISTENER3I palListener3i = NULL;
187 LPALLISTENERIV palListeneriv = NULL;
188 LPALGETLISTENERF palGetListenerf = NULL;
189 LPALGETLISTENER3F palGetListener3f = NULL;
190 LPALGETLISTENERFV palGetListenerfv = NULL;
191 LPALGETLISTENERI palGetListeneri = NULL;
192 LPALGETLISTENER3I palGetListener3i = NULL;
193 LPALGETLISTENERIV palGetListeneriv = NULL;
194 LPALGENSOURCES palGenSources = NULL;
195 LPALDELETESOURCES palDeleteSources = NULL;
196 LPALISSOURCE palIsSource = NULL;
197 LPALSOURCEF palSourcef = NULL;
198 LPALSOURCE3F palSource3f = NULL;
199 LPALSOURCEFV palSourcefv = NULL;
200 LPALSOURCEI palSourcei = NULL;
201 LPALSOURCE3I palSource3i = NULL;
202 LPALSOURCEIV palSourceiv = NULL;
203 LPALGETSOURCEF palGetSourcef = NULL;
204 LPALGETSOURCE3F palGetSource3f = NULL;
205 LPALGETSOURCEFV palGetSourcefv = NULL;
206 LPALGETSOURCEI palGetSourcei = NULL;
207 LPALGETSOURCE3I palGetSource3i = NULL;
208 LPALGETSOURCEIV palGetSourceiv = NULL;
209 LPALSOURCEPLAYV palSourcePlayv = NULL;
210 LPALSOURCESTOPV palSourceStopv = NULL;
211 LPALSOURCEREWINDV palSourceRewindv = NULL;
212 LPALSOURCEPAUSEV palSourcePausev = NULL;
213 LPALSOURCEPLAY palSourcePlay = NULL;
214 LPALSOURCESTOP palSourceStop = NULL;
215 LPALSOURCEREWIND palSourceRewind = NULL;
216 LPALSOURCEPAUSE palSourcePause = NULL;
217 LPALSOURCEQUEUEBUFFERS palSourceQueueBuffers = NULL;
218 LPALSOURCEUNQUEUEBUFFERS palSourceUnqueueBuffers = NULL;
219 LPALGENBUFFERS palGenBuffers = NULL;
220 LPALDELETEBUFFERS palDeleteBuffers = NULL;
221 LPALISBUFFER palIsBuffer = NULL;
222 LPALBUFFERF palBufferf = NULL;
223 LPALBUFFER3F palBuffer3f = NULL;
224 LPALBUFFERFV palBufferfv = NULL;
225 LPALBUFFERI palBufferi = NULL;
226 LPALBUFFER3I palBuffer3i = NULL;
227 LPALBUFFERIV palBufferiv = NULL;
228 LPALGETBUFFERF palGetBufferf = NULL;
229 LPALGETBUFFER3F palGetBuffer3f = NULL;
230 LPALGETBUFFERFV palGetBufferfv = NULL;
231 LPALGETBUFFERI palGetBufferi = NULL;
232 LPALGETBUFFER3I palGetBuffer3i = NULL;
233 LPALGETBUFFERIV palGetBufferiv = NULL;
234 LPALBUFFERDATA palBufferData = NULL;
235 LPALDOPPLERFACTOR palDopplerFactor = NULL;
236 LPALDOPPLERVELOCITY palDopplerVelocity = NULL;
237 LPALDISTANCEMODEL palDistanceModel = NULL;
238 LPALSPEEDOFSOUND palSpeedOfSound = NULL;
239 #endif
241 LPALCMAKECONTEXTCURRENT set_context;
242 LPALCGETCURRENTCONTEXT get_context;
243 BOOL local_contexts;
245 static void load_libopenal(void)
247 #ifndef __WINESRC__
248 const char *str = getenv("DSOAL_LOGLEVEL");
249 if(str && *str)
250 LogLevel = atoi(str);
251 #elif defined(SONAME_LIBOPENAL)
252 BOOL failed = FALSE;
253 char error[128];
255 openal_handle = wine_dlopen(SONAME_LIBOPENAL, RTLD_NOW, error, sizeof(error));
256 if(!openal_handle)
257 ERR("Couldn't load " SONAME_LIBOPENAL ": %s\n", error);
258 #define LOAD_FUNCPTR(f) \
259 if((p##f = wine_dlsym(openal_handle, #f, NULL, 0)) == NULL) { \
260 ERR("Couldn't lookup %s in " SONAME_LIBOPENAL "\n", #f); \
261 failed = TRUE; \
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 openal\n");
361 if (openal_handle != RTLD_DEFAULT)
362 wine_dlclose(openal_handle, NULL, 0);
363 openal_handle = NULL;
365 else
366 #endif
368 openal_loaded = 1;
370 local_contexts = alcIsExtensionPresent(NULL, "ALC_EXT_thread_local_context");
371 if(local_contexts)
373 set_context = alcGetProcAddress(NULL, "alcSetThreadContext");
374 get_context = alcGetProcAddress(NULL, "alcGetThreadContext");
375 if(!set_context || !get_context)
377 ERR("TLS advertised but functions not found, disabling thread local context\n");
378 local_contexts = 0;
381 if(!local_contexts)
383 set_context = alcMakeContextCurrent;
384 get_context = alcGetCurrentContext;
389 const ALCchar *DSOUND_getdevicestrings(void)
391 const ALCchar *str = NULL;
392 int multiple;
394 multiple = alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT");
395 if (multiple)
396 str = alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER);
397 else
398 str = alcGetString(NULL, ALC_DEVICE_SPECIFIER);
399 return str;
402 const ALCchar *DSOUND_getcapturedevicestrings(void)
404 const ALCchar *str = NULL;
405 str = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
406 return str;
409 /***************************************************************************
410 * GetDeviceID [DSOUND.9]
412 * Retrieves unique identifier of default device specified
414 * PARAMS
415 * pGuidSrc [I] Address of device GUID.
416 * pGuidDest [O] Address to receive unique device GUID.
418 * RETURNS
419 * Success: DS_OK
420 * Failure: DSERR_INVALIDPARAM
422 * NOTES
423 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
424 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
425 * DSDEVID_DefaultVoiceCapture.
426 * Returns pGuidSrc if pGuidSrc is a valid device or the device
427 * GUID for the specified constants.
429 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
431 TRACE("(%s,%p)\n", debugstr_guid(pGuidSrc),pGuidDest);
433 if ( pGuidSrc == NULL) {
434 WARN("invalid parameter: pGuidSrc == NULL\n");
435 return DSERR_INVALIDPARAM;
438 if ( pGuidDest == NULL ) {
439 WARN("invalid parameter: pGuidDest == NULL\n");
440 return DSERR_INVALIDPARAM;
443 if ( IsEqualGUID( &DSDEVID_DefaultPlayback, pGuidSrc ) ||
444 IsEqualGUID( &DSDEVID_DefaultVoicePlayback, pGuidSrc ) ) {
445 *pGuidDest = DSOUND_renderer_guid;
446 TRACE("returns %s\n", debugstr_guid(pGuidDest));
447 return DS_OK;
450 if ( IsEqualGUID( &DSDEVID_DefaultCapture, pGuidSrc ) ||
451 IsEqualGUID( &DSDEVID_DefaultVoiceCapture, pGuidSrc ) ) {
452 *pGuidDest = DSOUND_capture_guid;
453 TRACE("returns %s\n", debugstr_guid(pGuidDest));
454 return DS_OK;
457 *pGuidDest = *pGuidSrc;
458 TRACE("returns %s\n", debugstr_guid(pGuidDest));
460 return DS_OK;
463 struct morecontext
465 LPDSENUMCALLBACKA callA;
466 LPVOID data;
469 static BOOL CALLBACK a_to_w_callback(LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
471 struct morecontext *context = data;
472 char descA[MAXPNAMELEN], modA[MAXPNAMELEN];
474 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, sizeof(descA), NULL, NULL);
475 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, sizeof(modA), NULL, NULL);
477 return context->callA(guid, descA, modA, context->data);
480 /***************************************************************************
481 * DirectSoundEnumerateA [DSOUND.2]
483 * Enumerate all DirectSound drivers installed in the system
485 * PARAMS
486 * lpDSEnumCallback [I] Address of callback function.
487 * lpContext [I] Address of user defined context passed to callback function.
489 * RETURNS
490 * Success: DS_OK
491 * Failure: DSERR_INVALIDPARAM
493 HRESULT WINAPI DirectSoundEnumerateA(
494 LPDSENUMCALLBACKA lpDSEnumCallback,
495 LPVOID lpContext)
497 struct morecontext context;
499 if (lpDSEnumCallback == NULL) {
500 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
501 return DSERR_INVALIDPARAM;
504 context.callA = lpDSEnumCallback;
505 context.data = lpContext;
507 return DirectSoundEnumerateW(a_to_w_callback, &context);
510 /***************************************************************************
511 * DirectSoundEnumerateW [DSOUND.3]
513 * Enumerate all DirectSound drivers installed in the system
515 * PARAMS
516 * lpDSEnumCallback [I] Address of callback function.
517 * lpContext [I] Address of user defined context passed to callback function.
519 * RETURNS
520 * Success: DS_OK
521 * Failure: DSERR_INVALIDPARAM
523 HRESULT WINAPI DirectSoundEnumerateW(
524 LPDSENUMCALLBACKW lpDSEnumCallback,
525 LPVOID lpContext )
527 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
528 lpDSEnumCallback, lpContext);
530 if (lpDSEnumCallback == NULL) {
531 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
532 return DSERR_INVALIDPARAM;
534 else if (openal_loaded)
536 GUID guid;
537 WCHAR wDesc[MAXPNAMELEN];
538 WCHAR wName[MAXPNAMELEN];
539 const ALCchar *devstr;
540 static const WCHAR empty[] = { 0 };
541 EnterCriticalSection(&openal_crst);
542 devstr = DSOUND_getdevicestrings();
543 if (!devstr || !*devstr)
544 goto out;
546 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
547 "Primary Sound Driver","",lpContext);
548 MultiByteToWideChar( CP_ACP, 0, "Primary Sound Driver", -1,
549 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
550 if (lpDSEnumCallback(NULL, wDesc, empty, lpContext) == FALSE)
551 goto out;
553 guid = DSOUND_renderer_guid;
554 do {
555 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
556 debugstr_guid(&guid),devstr,"wineal.drv",lpContext);
557 MultiByteToWideChar( CP_UNIXCP, 0, devstr, -1,
558 wDesc, sizeof(wDesc)/sizeof(WCHAR)-1 );
559 wDesc[sizeof(wDesc)/sizeof(WCHAR)-1] = 0;
560 MultiByteToWideChar( CP_ACP, 0, "wineal.drv", -1,
561 wName, sizeof(wName)/sizeof(WCHAR)-1 );
562 if (lpDSEnumCallback(&guid, wDesc, wName, lpContext) == FALSE)
563 goto out;
564 guid.Data4[7]++;
565 devstr += strlen(devstr)+1;
566 } while (*devstr);
568 out:
569 LeaveCriticalSection(&openal_crst);
571 else
573 ERR("Attempting to enumerate sound cards without OpenAL support\n");
574 ERR("Please recompile wine with OpenAL for sound to work\n");
576 return DS_OK;
579 /***************************************************************************
580 * DirectSoundCaptureEnumerateA [DSOUND.7]
582 * Enumerate all DirectSound drivers installed in the system.
584 * PARAMS
585 * lpDSEnumCallback [I] Address of callback function.
586 * lpContext [I] Address of user defined context passed to callback function.
588 * RETURNS
589 * Success: DS_OK
590 * Failure: DSERR_INVALIDPARAM
592 HRESULT WINAPI DirectSoundCaptureEnumerateA(
593 LPDSENUMCALLBACKA lpDSEnumCallback,
594 LPVOID lpContext)
596 struct morecontext context;
598 if (lpDSEnumCallback == NULL) {
599 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
600 return DSERR_INVALIDPARAM;
603 context.callA = lpDSEnumCallback;
604 context.data = lpContext;
606 return DirectSoundCaptureEnumerateW(a_to_w_callback, &context);
609 /***************************************************************************
610 * DirectSoundCaptureEnumerateW [DSOUND.8]
612 * Enumerate all DirectSound drivers installed in the system.
614 * PARAMS
615 * lpDSEnumCallback [I] Address of callback function.
616 * lpContext [I] Address of user defined context passed to callback function.
618 * RETURNS
619 * Success: DS_OK
620 * Failure: DSERR_INVALIDPARAM
622 HRESULT WINAPI
623 DirectSoundCaptureEnumerateW(
624 LPDSENUMCALLBACKW lpDSEnumCallback,
625 LPVOID lpContext)
627 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
629 if (lpDSEnumCallback == NULL) {
630 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
631 return DSERR_INVALIDPARAM;
633 else if (openal_loaded)
635 GUID guid;
636 WCHAR wDesc[MAXPNAMELEN];
637 WCHAR wName[MAXPNAMELEN];
638 const ALCchar *devstr;
639 static const WCHAR empty[] = { 0 };
641 EnterCriticalSection(&openal_crst);
642 devstr = DSOUND_getcapturedevicestrings();
643 if (!devstr || !*devstr)
644 goto out;
646 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
647 "Primary Sound Capture Driver","",lpContext);
648 MultiByteToWideChar( CP_ACP, 0, "Primary Sound Driver", -1,
649 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
650 if (lpDSEnumCallback(NULL, wDesc, empty, lpContext) == FALSE)
651 goto out;
653 guid = DSOUND_capture_guid;
654 do {
655 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
656 debugstr_guid(&guid),devstr,"wineal.drv",lpContext);
657 MultiByteToWideChar( CP_UNIXCP, 0, devstr, -1,
658 wDesc, sizeof(wDesc)/sizeof(WCHAR)-1 );
659 wDesc[sizeof(wDesc)/sizeof(WCHAR)-1] = 0;
660 MultiByteToWideChar( CP_ACP, 0, "wineal.drv", -1,
661 wName, sizeof(wName)/sizeof(WCHAR)-1 );
662 if (lpDSEnumCallback(&guid, wDesc, wName, lpContext) == FALSE)
663 goto out;
664 guid.Data4[7]++;
665 devstr += strlen(devstr)+1;
666 } while (*devstr);
668 out:
669 LeaveCriticalSection(&openal_crst);
671 else
673 ERR("Attempting to enumerate sound cards without OpenAL support\n");
674 ERR("Please recompile wine with OpenAL for sound to work\n");
676 return DS_OK;
679 /*******************************************************************************
680 * DirectSoundCreate (DSOUND.1)
682 * Creates and initializes a DirectSound interface.
684 * PARAMS
685 * lpcGUID [I] Address of the GUID that identifies the sound device.
686 * ppDS [O] Address of a variable to receive the interface pointer.
687 * pUnkOuter [I] Must be NULL.
689 * RETURNS
690 * Success: DS_OK
691 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
692 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
694 HRESULT WINAPI DirectSoundCreate(
695 LPCGUID lpcGUID,
696 LPDIRECTSOUND *ppDS,
697 IUnknown *pUnkOuter)
699 HRESULT hr;
700 LPDIRECTSOUND pDS;
702 TRACE("(%s,%p,%p)\n",debugstr_guid(lpcGUID),ppDS,pUnkOuter);
704 if (ppDS == NULL) {
705 WARN("invalid parameter: ppDS == NULL\n");
706 return DSERR_INVALIDPARAM;
709 if (pUnkOuter != NULL) {
710 WARN("invalid parameter: pUnkOuter != NULL\n");
711 *ppDS = 0;
712 return DSERR_INVALIDPARAM;
715 hr = DSOUND_Create(&IID_IDirectSound, (void **)&pDS);
716 if (hr == DS_OK) {
717 hr = IDirectSound_Initialize(pDS, lpcGUID);
718 if (hr != DS_OK) {
719 if (hr != DSERR_ALREADYINITIALIZED) {
720 IDirectSound_Release(pDS);
721 pDS = 0;
722 } else
723 hr = DS_OK;
727 *ppDS = pDS;
729 return hr;
732 /*******************************************************************************
733 * DirectSoundCreate8 (DSOUND.11)
735 * Creates and initializes a DirectSound8 interface.
737 * PARAMS
738 * lpcGUID [I] Address of the GUID that identifies the sound device.
739 * ppDS [O] Address of a variable to receive the interface pointer.
740 * pUnkOuter [I] Must be NULL.
742 * RETURNS
743 * Success: DS_OK
744 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
745 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
747 HRESULT WINAPI DirectSoundCreate8(
748 LPCGUID lpcGUID,
749 LPDIRECTSOUND8 *ppDS,
750 IUnknown *pUnkOuter)
752 HRESULT hr;
753 LPDIRECTSOUND8 pDS;
755 TRACE("(%s,%p,%p)\n",debugstr_guid(lpcGUID),ppDS,pUnkOuter);
757 if (ppDS == NULL) {
758 WARN("invalid parameter: ppDS == NULL\n");
759 return DSERR_INVALIDPARAM;
762 if (pUnkOuter != NULL) {
763 WARN("invalid parameter: pUnkOuter != NULL\n");
764 *ppDS = 0;
765 return DSERR_INVALIDPARAM;
768 hr = DSOUND_Create8(&IID_IDirectSound8, (void**)&pDS);
769 if (hr == DS_OK) {
770 hr = IDirectSound8_Initialize(pDS, lpcGUID);
771 if (hr != DS_OK) {
772 if (hr != DSERR_ALREADYINITIALIZED) {
773 IDirectSound8_Release(pDS);
774 pDS = 0;
775 } else
776 hr = DS_OK;
780 *ppDS = pDS;
782 return hr;
785 /***************************************************************************
786 * DirectSoundCaptureCreate [DSOUND.6]
788 * Create and initialize a DirectSoundCapture interface.
790 * PARAMS
791 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
792 * lplpDSC [O] Address of a variable to receive the interface pointer.
793 * pUnkOuter [I] Must be NULL.
795 * RETURNS
796 * Success: DS_OK
797 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
798 * DSERR_OUTOFMEMORY
800 * NOTES
801 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
802 * or NULL for the default device or DSDEVID_DefaultCapture or
803 * DSDEVID_DefaultVoiceCapture.
805 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
807 HRESULT WINAPI DirectSoundCaptureCreate(
808 LPCGUID lpcGUID,
809 IDirectSoundCapture **ppDSC,
810 IUnknown *pUnkOuter)
812 void *pDSC;
813 HRESULT hr;
815 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
817 if(ppDSC == NULL)
819 WARN("invalid parameter: ppDSC == NULL\n");
820 return DSERR_INVALIDPARAM;
822 *ppDSC = NULL;
824 if(pUnkOuter)
826 WARN("invalid parameter: pUnkOuter != NULL\n");
827 return DSERR_NOAGGREGATION;
830 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, &pDSC);
831 if(hr == DS_OK)
833 *ppDSC = pDSC;
834 hr = IDirectSoundCapture_Initialize(*ppDSC, lpcGUID);
835 if(hr != DS_OK)
837 IDirectSoundCapture_Release(*ppDSC);
838 *ppDSC = 0;
842 return hr;
845 /***************************************************************************
846 * DirectSoundCaptureCreate8 [DSOUND.12]
848 * Create and initialize a DirectSoundCapture interface.
850 * PARAMS
851 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
852 * lplpDSC [O] Address of a variable to receive the interface pointer.
853 * pUnkOuter [I] Must be NULL.
855 * RETURNS
856 * Success: DS_OK
857 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
858 * DSERR_OUTOFMEMORY
860 * NOTES
861 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
862 * or NULL for the default device or DSDEVID_DefaultCapture or
863 * DSDEVID_DefaultVoiceCapture.
865 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
867 HRESULT WINAPI DirectSoundCaptureCreate8(
868 LPCGUID lpcGUID,
869 IDirectSoundCapture8 **ppDSC8,
870 IUnknown *pUnkOuter)
872 void *pDSC8;
873 HRESULT hr;
875 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
877 if(ppDSC8 == NULL)
879 WARN("invalid parameter: ppDSC8 == NULL\n");
880 return DSERR_INVALIDPARAM;
882 *ppDSC8 = NULL;
884 if(pUnkOuter)
886 WARN("invalid parameter: pUnkOuter != NULL\n");
887 return DSERR_NOAGGREGATION;
890 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture8, &pDSC8);
891 if(hr == DS_OK)
893 *ppDSC8 = pDSC8;
894 hr = IDirectSoundCapture_Initialize(*ppDSC8, lpcGUID);
895 if(hr != DS_OK)
897 IDirectSoundCapture_Release(*ppDSC8);
898 *ppDSC8 = NULL;
902 return hr;
905 /*******************************************************************************
906 * DirectSound ClassFactory
909 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
911 typedef struct {
912 IClassFactory IClassFactory_iface;
913 LONG ref;
914 REFCLSID rclsid;
915 FnCreateInstance pfnCreateInstance;
916 } IClassFactoryImpl;
918 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
920 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
923 static HRESULT WINAPI DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
925 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
926 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
927 if (ppobj == NULL)
928 return E_POINTER;
929 if (IsEqualIID(riid, &IID_IUnknown) ||
930 IsEqualIID(riid, &IID_IClassFactory))
932 *ppobj = iface;
933 IUnknown_AddRef(iface);
934 return S_OK;
936 *ppobj = NULL;
937 return E_NOINTERFACE;
940 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
942 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
943 ULONG ref = InterlockedIncrement(&(This->ref));
944 TRACE("(%p) ref was %d\n", This, ref - 1);
945 return ref;
948 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
950 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
951 ULONG ref = InterlockedDecrement(&(This->ref));
952 TRACE("(%p) ref was %d\n", This, ref + 1);
953 /* static class, won't be freed */
954 return ref;
957 static HRESULT WINAPI DSCF_CreateInstance(
958 LPCLASSFACTORY iface,
959 LPUNKNOWN pOuter,
960 REFIID riid,
961 LPVOID *ppobj)
963 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
964 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
966 if (pOuter)
967 return CLASS_E_NOAGGREGATION;
969 if (ppobj == NULL) {
970 WARN("invalid parameter\n");
971 return DSERR_INVALIDPARAM;
973 *ppobj = NULL;
974 return This->pfnCreateInstance(riid, ppobj);
977 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
979 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
980 FIXME("(%p, %d) stub!\n", This, dolock);
981 return S_OK;
984 static const IClassFactoryVtbl DSCF_Vtbl = {
985 DSCF_QueryInterface,
986 DSCF_AddRef,
987 DSCF_Release,
988 DSCF_CreateInstance,
989 DSCF_LockServer
992 static IClassFactoryImpl DSOUND_CF[] = {
993 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSound, DSOUND_Create },
994 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSound8, DSOUND_Create8 },
995 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
996 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
997 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
998 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
999 { {NULL}, 0, NULL, NULL }
1002 /*******************************************************************************
1003 * DllGetClassObject [DSOUND.@]
1004 * Retrieves class object from a DLL object
1006 * NOTES
1007 * Docs say returns STDAPI
1009 * PARAMS
1010 * rclsid [I] CLSID for the class object
1011 * riid [I] Reference to identifier of interface for class object
1012 * ppv [O] Address of variable to receive interface pointer for riid
1014 * RETURNS
1015 * Success: S_OK
1016 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
1017 * E_UNEXPECTED
1019 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1021 int i = 0;
1022 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1024 if (ppv == NULL) {
1025 WARN("invalid parameter\n");
1026 return E_INVALIDARG;
1029 *ppv = NULL;
1031 if (!IsEqualIID(riid, &IID_IClassFactory) &&
1032 !IsEqualIID(riid, &IID_IUnknown)) {
1033 WARN("no interface for %s\n", debugstr_guid(riid));
1034 return E_NOINTERFACE;
1037 while (NULL != DSOUND_CF[i].rclsid) {
1038 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
1039 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
1040 *ppv = &DSOUND_CF[i];
1041 return S_OK;
1043 i++;
1046 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
1047 debugstr_guid(riid), ppv);
1048 return CLASS_E_CLASSNOTAVAILABLE;
1052 /*******************************************************************************
1053 * DllCanUnloadNow [DSOUND.4]
1054 * Determines whether the DLL is in use.
1056 * RETURNS
1057 * Success: S_OK
1058 * Failure: S_FALSE
1060 HRESULT WINAPI DllCanUnloadNow(void)
1062 FIXME("(void): stub\n");
1063 return S_FALSE;
1066 /***********************************************************************
1067 * DllMain (DSOUND.init)
1069 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
1071 TRACE("(%p, %d, %p)\n", hInstDLL, fdwReason, lpvReserved);
1073 switch(fdwReason)
1075 case DLL_PROCESS_ATTACH:
1076 TRACE("DLL_PROCESS_ATTACH\n");
1077 instance = hInstDLL;
1078 load_libopenal();
1079 DisableThreadLibraryCalls(hInstDLL);
1080 /* Increase refcount on dsound by 1 */
1081 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
1082 break;
1083 case DLL_PROCESS_DETACH:
1084 TRACE("DLL_PROCESS_DETACH\n");
1085 #ifdef SONAME_LIBOPENAL
1086 if(openal_handle)
1087 wine_dlclose(openal_handle, NULL, 0);
1088 #endif /*SONAME_LIBOPENAL*/
1089 break;
1090 default:
1091 TRACE("UNKNOWN REASON\n");
1092 break;
1094 return TRUE;
1097 /***********************************************************************
1098 * DllRegisterServer (DSOUND.@)
1100 HRESULT WINAPI DllRegisterServer(void)
1102 return __wine_register_resources(instance, NULL);
1105 /***********************************************************************
1106 * DllUnregisterServer (DSOUND.@)
1108 HRESULT WINAPI DllUnregisterServer(void)
1110 return __wine_unregister_resources(instance, NULL);