Initial check-in
[dsound-openal.git] / dsound_main.c
blob642f922c831fb7500e9402a5e3ee0faa2cd02757
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"
59 #include "dsound_private.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
63 #else
65 #define WINVER 0x6100
66 #include <windows.h>
67 #include <dsound.h>
69 #include "dsound_private.h"
71 #ifndef CP_UNIXCP
72 #define CP_UNIXCP 65010
73 #endif
75 DEFINE_GUID(CLSID_DirectSoundPrivate,0x11ab3ec0,0x25ec,0x11d1,0xa4,0xd8,0x00,0xc0,0x4f,0xc2,0x8a,0xca);
77 int LogLevel = 1;
79 #endif
81 const GUID DSOUND_renderer_guid = { 0xbd6dd71a, 0x3deb, 0x11d1, { 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 } };
82 const GUID DSOUND_capture_guid = { 0xbd6dd71b, 0x3deb, 0x11d1, { 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 } };
84 const IID DSPROPSETID_EAX20_ListenerProperties = {
85 0x0306a6a8, 0xb224, 0x11d2, { 0x99, 0xe5, 0x00, 0x00, 0xe8, 0xd8, 0xc7, 0x22 }
88 const EAXLISTENERPROPERTIES EnvironmentDefaults[EAX_ENVIRONMENT_COUNT] =
90 REVERB_PRESET_GENERIC,
91 REVERB_PRESET_PADDEDCELL,
92 REVERB_PRESET_ROOM,
93 REVERB_PRESET_BATHROOM,
94 REVERB_PRESET_LIVINGROOM,
95 REVERB_PRESET_STONEROOM,
96 REVERB_PRESET_AUDITORIUM,
97 REVERB_PRESET_CONCERTHALL,
98 REVERB_PRESET_CAVE,
99 REVERB_PRESET_ARENA,
100 REVERB_PRESET_HANGAR,
101 REVERB_PRESET_CARPETEDHALLWAY,
102 REVERB_PRESET_HALLWAY,
103 REVERB_PRESET_STONECORRIDOR,
104 REVERB_PRESET_ALLEY,
105 REVERB_PRESET_FOREST,
106 REVERB_PRESET_CITY,
107 REVERB_PRESET_MOUNTAINS,
108 REVERB_PRESET_QUARRY,
109 REVERB_PRESET_PLAIN,
110 REVERB_PRESET_PARKINGLOT,
111 REVERB_PRESET_SEWERPIPE,
112 REVERB_PRESET_UNDERWATER,
113 REVERB_PRESET_DRUGGED,
114 REVERB_PRESET_DIZZY,
115 REVERB_PRESET_PSYCHOTIC
118 static CRITICAL_SECTION_DEBUG openal_crst_debug =
120 0, 0, &openal_crst,
121 { &openal_crst_debug.ProcessLocksList,
122 &openal_crst_debug.ProcessLocksList },
123 0, 0, { (DWORD_PTR)(__FILE__ ": openal_crst_debug") }
125 CRITICAL_SECTION openal_crst = { &openal_crst_debug, -1, 0, 0, 0, 0 };
127 int openal_loaded = 0;
128 #ifdef SONAME_LIBOPENAL
129 static void *openal_handle = NULL;
130 LPALCCREATECONTEXT palcCreateContext = NULL;
131 LPALCMAKECONTEXTCURRENT palcMakeContextCurrent = NULL;
132 LPALCPROCESSCONTEXT palcProcessContext = NULL;
133 LPALCSUSPENDCONTEXT palcSuspendContext = NULL;
134 LPALCDESTROYCONTEXT palcDestroyContext = NULL;
135 LPALCGETCURRENTCONTEXT palcGetCurrentContext = NULL;
136 LPALCGETCONTEXTSDEVICE palcGetContextsDevice = NULL;
137 LPALCOPENDEVICE palcOpenDevice = NULL;
138 LPALCCLOSEDEVICE palcCloseDevice = NULL;
139 LPALCGETERROR palcGetError = NULL;
140 LPALCISEXTENSIONPRESENT palcIsExtensionPresent = NULL;
141 LPALCGETPROCADDRESS palcGetProcAddress = NULL;
142 LPALCGETENUMVALUE palcGetEnumValue = NULL;
143 LPALCGETSTRING palcGetString = NULL;
144 LPALCGETINTEGERV palcGetIntegerv = NULL;
145 LPALCCAPTUREOPENDEVICE palcCaptureOpenDevice = NULL;
146 LPALCCAPTURECLOSEDEVICE palcCaptureCloseDevice = NULL;
147 LPALCCAPTURESTART palcCaptureStart = NULL;
148 LPALCCAPTURESTOP palcCaptureStop = NULL;
149 LPALCCAPTURESAMPLES palcCaptureSamples = NULL;
150 LPALENABLE palEnable = NULL;
151 LPALDISABLE palDisable = NULL;
152 LPALISENABLED palIsEnabled = NULL;
153 LPALGETSTRING palGetString = NULL;
154 LPALGETBOOLEANV palGetBooleanv = NULL;
155 LPALGETINTEGERV palGetIntegerv = NULL;
156 LPALGETFLOATV palGetFloatv = NULL;
157 LPALGETDOUBLEV palGetDoublev = NULL;
158 LPALGETBOOLEAN palGetBoolean = NULL;
159 LPALGETINTEGER palGetInteger = NULL;
160 LPALGETFLOAT palGetFloat = NULL;
161 LPALGETDOUBLE palGetDouble = NULL;
162 LPALGETERROR palGetError = NULL;
163 LPALISEXTENSIONPRESENT palIsExtensionPresent = NULL;
164 LPALGETPROCADDRESS palGetProcAddress = NULL;
165 LPALGETENUMVALUE palGetEnumValue = NULL;
166 LPALLISTENERF palListenerf = NULL;
167 LPALLISTENER3F palListener3f = NULL;
168 LPALLISTENERFV palListenerfv = NULL;
169 LPALLISTENERI palListeneri = NULL;
170 LPALLISTENER3I palListener3i = NULL;
171 LPALLISTENERIV palListeneriv = NULL;
172 LPALGETLISTENERF palGetListenerf = NULL;
173 LPALGETLISTENER3F palGetListener3f = NULL;
174 LPALGETLISTENERFV palGetListenerfv = NULL;
175 LPALGETLISTENERI palGetListeneri = NULL;
176 LPALGETLISTENER3I palGetListener3i = NULL;
177 LPALGETLISTENERIV palGetListeneriv = NULL;
178 LPALGENSOURCES palGenSources = NULL;
179 LPALDELETESOURCES palDeleteSources = NULL;
180 LPALISSOURCE palIsSource = NULL;
181 LPALSOURCEF palSourcef = NULL;
182 LPALSOURCE3F palSource3f = NULL;
183 LPALSOURCEFV palSourcefv = NULL;
184 LPALSOURCEI palSourcei = NULL;
185 LPALSOURCE3I palSource3i = NULL;
186 LPALSOURCEIV palSourceiv = NULL;
187 LPALGETSOURCEF palGetSourcef = NULL;
188 LPALGETSOURCE3F palGetSource3f = NULL;
189 LPALGETSOURCEFV palGetSourcefv = NULL;
190 LPALGETSOURCEI palGetSourcei = NULL;
191 LPALGETSOURCE3I palGetSource3i = NULL;
192 LPALGETSOURCEIV palGetSourceiv = NULL;
193 LPALSOURCEPLAYV palSourcePlayv = NULL;
194 LPALSOURCESTOPV palSourceStopv = NULL;
195 LPALSOURCEREWINDV palSourceRewindv = NULL;
196 LPALSOURCEPAUSEV palSourcePausev = NULL;
197 LPALSOURCEPLAY palSourcePlay = NULL;
198 LPALSOURCESTOP palSourceStop = NULL;
199 LPALSOURCEREWIND palSourceRewind = NULL;
200 LPALSOURCEPAUSE palSourcePause = NULL;
201 LPALSOURCEQUEUEBUFFERS palSourceQueueBuffers = NULL;
202 LPALSOURCEUNQUEUEBUFFERS palSourceUnqueueBuffers = NULL;
203 LPALGENBUFFERS palGenBuffers = NULL;
204 LPALDELETEBUFFERS palDeleteBuffers = NULL;
205 LPALISBUFFER palIsBuffer = NULL;
206 LPALBUFFERF palBufferf = NULL;
207 LPALBUFFER3F palBuffer3f = NULL;
208 LPALBUFFERFV palBufferfv = NULL;
209 LPALBUFFERI palBufferi = NULL;
210 LPALBUFFER3I palBuffer3i = NULL;
211 LPALBUFFERIV palBufferiv = NULL;
212 LPALGETBUFFERF palGetBufferf = NULL;
213 LPALGETBUFFER3F palGetBuffer3f = NULL;
214 LPALGETBUFFERFV palGetBufferfv = NULL;
215 LPALGETBUFFERI palGetBufferi = NULL;
216 LPALGETBUFFER3I palGetBuffer3i = NULL;
217 LPALGETBUFFERIV palGetBufferiv = NULL;
218 LPALBUFFERDATA palBufferData = NULL;
219 LPALDOPPLERFACTOR palDopplerFactor = NULL;
220 LPALDOPPLERVELOCITY palDopplerVelocity = NULL;
221 LPALDISTANCEMODEL palDistanceModel = NULL;
222 LPALSPEEDOFSOUND palSpeedOfSound = NULL;
223 #endif
225 LPALCMAKECONTEXTCURRENT set_context;
226 LPALCGETCURRENTCONTEXT get_context;
227 BOOL local_contexts;
229 static void load_libopenal(void)
231 #ifdef WINELESS
232 const char *str = getenv("DSOAL_LOGLEVEL");
233 if(str && *str)
234 LogLevel = atoi(str);
236 if(0) {
238 #elif defined(SONAME_LIBOPENAL)
239 BOOL failed = FALSE;
240 char error[128];
242 openal_handle = wine_dlopen(SONAME_LIBOPENAL, RTLD_NOW, error, sizeof(error));
243 if(!openal_handle)
244 ERR("Couldn't load " SONAME_LIBOPENAL ": %s\n", error);
245 #define LOAD_FUNCPTR(f) \
246 if((p##f = wine_dlsym(openal_handle, #f, NULL, 0)) == NULL) { \
247 ERR("Couldn't lookup %s in " SONAME_LIBOPENAL "\n", #f); \
248 failed = TRUE; \
251 LOAD_FUNCPTR(alcCreateContext);
252 LOAD_FUNCPTR(alcMakeContextCurrent);
253 LOAD_FUNCPTR(alcProcessContext);
254 LOAD_FUNCPTR(alcSuspendContext);
255 LOAD_FUNCPTR(alcDestroyContext);
256 LOAD_FUNCPTR(alcGetCurrentContext);
257 LOAD_FUNCPTR(alcGetContextsDevice);
258 LOAD_FUNCPTR(alcOpenDevice);
259 LOAD_FUNCPTR(alcCloseDevice);
260 LOAD_FUNCPTR(alcGetError);
261 LOAD_FUNCPTR(alcIsExtensionPresent);
262 LOAD_FUNCPTR(alcGetProcAddress);
263 LOAD_FUNCPTR(alcGetEnumValue);
264 LOAD_FUNCPTR(alcGetString);
265 LOAD_FUNCPTR(alcGetIntegerv);
266 LOAD_FUNCPTR(alcCaptureOpenDevice);
267 LOAD_FUNCPTR(alcCaptureCloseDevice);
268 LOAD_FUNCPTR(alcCaptureStart);
269 LOAD_FUNCPTR(alcCaptureStop);
270 LOAD_FUNCPTR(alcCaptureSamples);
271 LOAD_FUNCPTR(alEnable);
272 LOAD_FUNCPTR(alDisable);
273 LOAD_FUNCPTR(alIsEnabled);
274 LOAD_FUNCPTR(alGetString);
275 LOAD_FUNCPTR(alGetBooleanv);
276 LOAD_FUNCPTR(alGetIntegerv);
277 LOAD_FUNCPTR(alGetFloatv);
278 LOAD_FUNCPTR(alGetDoublev);
279 LOAD_FUNCPTR(alGetBoolean);
280 LOAD_FUNCPTR(alGetInteger);
281 LOAD_FUNCPTR(alGetFloat);
282 LOAD_FUNCPTR(alGetDouble);
283 LOAD_FUNCPTR(alGetError);
284 LOAD_FUNCPTR(alIsExtensionPresent);
285 LOAD_FUNCPTR(alGetProcAddress);
286 LOAD_FUNCPTR(alGetEnumValue);
287 LOAD_FUNCPTR(alListenerf);
288 LOAD_FUNCPTR(alListener3f);
289 LOAD_FUNCPTR(alListenerfv);
290 LOAD_FUNCPTR(alListeneri);
291 LOAD_FUNCPTR(alListener3i);
292 LOAD_FUNCPTR(alListeneriv);
293 LOAD_FUNCPTR(alGetListenerf);
294 LOAD_FUNCPTR(alGetListener3f);
295 LOAD_FUNCPTR(alGetListenerfv);
296 LOAD_FUNCPTR(alGetListeneri);
297 LOAD_FUNCPTR(alGetListener3i);
298 LOAD_FUNCPTR(alGetListeneriv);
299 LOAD_FUNCPTR(alGenSources);
300 LOAD_FUNCPTR(alDeleteSources);
301 LOAD_FUNCPTR(alIsSource);
302 LOAD_FUNCPTR(alSourcef);
303 LOAD_FUNCPTR(alSource3f);
304 LOAD_FUNCPTR(alSourcefv);
305 LOAD_FUNCPTR(alSourcei);
306 LOAD_FUNCPTR(alSource3i);
307 LOAD_FUNCPTR(alSourceiv);
308 LOAD_FUNCPTR(alGetSourcef);
309 LOAD_FUNCPTR(alGetSource3f);
310 LOAD_FUNCPTR(alGetSourcefv);
311 LOAD_FUNCPTR(alGetSourcei);
312 LOAD_FUNCPTR(alGetSource3i);
313 LOAD_FUNCPTR(alGetSourceiv);
314 LOAD_FUNCPTR(alSourcePlayv);
315 LOAD_FUNCPTR(alSourceStopv);
316 LOAD_FUNCPTR(alSourceRewindv);
317 LOAD_FUNCPTR(alSourcePausev);
318 LOAD_FUNCPTR(alSourcePlay);
319 LOAD_FUNCPTR(alSourceStop);
320 LOAD_FUNCPTR(alSourceRewind);
321 LOAD_FUNCPTR(alSourcePause);
322 LOAD_FUNCPTR(alSourceQueueBuffers);
323 LOAD_FUNCPTR(alSourceUnqueueBuffers);
324 LOAD_FUNCPTR(alGenBuffers);
325 LOAD_FUNCPTR(alDeleteBuffers);
326 LOAD_FUNCPTR(alIsBuffer);
327 LOAD_FUNCPTR(alBufferf);
328 LOAD_FUNCPTR(alBuffer3f);
329 LOAD_FUNCPTR(alBufferfv);
330 LOAD_FUNCPTR(alBufferi);
331 LOAD_FUNCPTR(alBuffer3i);
332 LOAD_FUNCPTR(alBufferiv);
333 LOAD_FUNCPTR(alGetBufferf);
334 LOAD_FUNCPTR(alGetBuffer3f);
335 LOAD_FUNCPTR(alGetBufferfv);
336 LOAD_FUNCPTR(alGetBufferi);
337 LOAD_FUNCPTR(alGetBuffer3i);
338 LOAD_FUNCPTR(alGetBufferiv);
339 LOAD_FUNCPTR(alBufferData);
340 LOAD_FUNCPTR(alDopplerFactor);
341 LOAD_FUNCPTR(alDopplerVelocity);
342 LOAD_FUNCPTR(alDistanceModel);
343 LOAD_FUNCPTR(alSpeedOfSound);
344 #undef LOAD_FUNCPTR
345 if (failed)
347 WARN("Unloading openal\n");
348 if (openal_handle != RTLD_DEFAULT)
349 wine_dlclose(openal_handle, NULL, 0);
350 openal_handle = NULL;
352 else
353 #endif
355 openal_loaded = 1;
357 local_contexts = alcIsExtensionPresent(NULL, "ALC_EXT_thread_local_context");
358 if(local_contexts)
360 set_context = alcGetProcAddress(NULL, "alcSetThreadContext");
361 get_context = alcGetProcAddress(NULL, "alcGetThreadContext");
362 if(!set_context || !get_context)
364 ERR("TLS advertised but functions not found, disabling thread local context\n");
365 local_contexts = 0;
368 if(!local_contexts)
370 set_context = alcMakeContextCurrent;
371 get_context = alcGetCurrentContext;
376 const ALCchar *DSOUND_getdevicestrings(void)
378 const ALCchar *str = NULL;
379 int multiple;
381 multiple = alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT");
382 if (multiple)
383 str = alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER);
384 else
385 str = alcGetString(NULL, ALC_DEVICE_SPECIFIER);
386 return str;
389 const ALCchar *DSOUND_getcapturedevicestrings(void)
391 const ALCchar *str = NULL;
392 str = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
393 return str;
396 /***************************************************************************
397 * GetDeviceID [DSOUND.9]
399 * Retrieves unique identifier of default device specified
401 * PARAMS
402 * pGuidSrc [I] Address of device GUID.
403 * pGuidDest [O] Address to receive unique device GUID.
405 * RETURNS
406 * Success: DS_OK
407 * Failure: DSERR_INVALIDPARAM
409 * NOTES
410 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
411 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
412 * DSDEVID_DefaultVoiceCapture.
413 * Returns pGuidSrc if pGuidSrc is a valid device or the device
414 * GUID for the specified constants.
416 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
418 TRACE("(%s,%p)\n", debugstr_guid(pGuidSrc),pGuidDest);
420 if ( pGuidSrc == NULL) {
421 WARN("invalid parameter: pGuidSrc == NULL\n");
422 return DSERR_INVALIDPARAM;
425 if ( pGuidDest == NULL ) {
426 WARN("invalid parameter: pGuidDest == NULL\n");
427 return DSERR_INVALIDPARAM;
430 if ( IsEqualGUID( &DSDEVID_DefaultPlayback, pGuidSrc ) ||
431 IsEqualGUID( &DSDEVID_DefaultVoicePlayback, pGuidSrc ) ) {
432 *pGuidDest = DSOUND_renderer_guid;
433 TRACE("returns %s\n", debugstr_guid(pGuidDest));
434 return DS_OK;
437 if ( IsEqualGUID( &DSDEVID_DefaultCapture, pGuidSrc ) ||
438 IsEqualGUID( &DSDEVID_DefaultVoiceCapture, pGuidSrc ) ) {
439 *pGuidDest = DSOUND_capture_guid;
440 TRACE("returns %s\n", debugstr_guid(pGuidDest));
441 return DS_OK;
444 *pGuidDest = *pGuidSrc;
445 TRACE("returns %s\n", debugstr_guid(pGuidDest));
447 return DS_OK;
450 struct morecontext
452 LPDSENUMCALLBACKA callA;
453 LPVOID data;
456 static BOOL CALLBACK a_to_w_callback(LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
458 struct morecontext *context = data;
459 char descA[MAXPNAMELEN], modA[MAXPNAMELEN];
461 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, sizeof(descA), NULL, NULL);
462 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, sizeof(modA), NULL, NULL);
464 return context->callA(guid, descA, modA, context->data);
467 /***************************************************************************
468 * DirectSoundEnumerateA [DSOUND.2]
470 * Enumerate all DirectSound drivers installed in the system
472 * PARAMS
473 * lpDSEnumCallback [I] Address of callback function.
474 * lpContext [I] Address of user defined context passed to callback function.
476 * RETURNS
477 * Success: DS_OK
478 * Failure: DSERR_INVALIDPARAM
480 HRESULT WINAPI DirectSoundEnumerateA(
481 LPDSENUMCALLBACKA lpDSEnumCallback,
482 LPVOID lpContext)
484 struct morecontext context;
486 if (lpDSEnumCallback == NULL) {
487 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
488 return DSERR_INVALIDPARAM;
491 context.callA = lpDSEnumCallback;
492 context.data = lpContext;
494 return DirectSoundEnumerateW(a_to_w_callback, &context);
497 /***************************************************************************
498 * DirectSoundEnumerateW [DSOUND.3]
500 * Enumerate all DirectSound drivers installed in the system
502 * PARAMS
503 * lpDSEnumCallback [I] Address of callback function.
504 * lpContext [I] Address of user defined context passed to callback function.
506 * RETURNS
507 * Success: DS_OK
508 * Failure: DSERR_INVALIDPARAM
510 HRESULT WINAPI DirectSoundEnumerateW(
511 LPDSENUMCALLBACKW lpDSEnumCallback,
512 LPVOID lpContext )
514 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
515 lpDSEnumCallback, lpContext);
517 if (lpDSEnumCallback == NULL) {
518 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
519 return DSERR_INVALIDPARAM;
521 else if (openal_loaded)
523 GUID guid;
524 WCHAR wDesc[MAXPNAMELEN];
525 WCHAR wName[MAXPNAMELEN];
526 const ALCchar *devstr;
527 static const WCHAR empty[] = { 0 };
528 EnterCriticalSection(&openal_crst);
529 devstr = DSOUND_getdevicestrings();
530 if (!devstr || !*devstr)
531 goto out;
533 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
534 "Primary Sound Driver","",lpContext);
535 MultiByteToWideChar( CP_ACP, 0, "Primary Sound Driver", -1,
536 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
537 if (lpDSEnumCallback(NULL, wDesc, empty, lpContext) == FALSE)
538 goto out;
540 guid = DSOUND_renderer_guid;
541 do {
542 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
543 debugstr_guid(&guid),devstr,"wineal.drv",lpContext);
544 MultiByteToWideChar( CP_UNIXCP, 0, devstr, -1,
545 wDesc, sizeof(wDesc)/sizeof(WCHAR)-1 );
546 wDesc[sizeof(wDesc)/sizeof(WCHAR)-1] = 0;
547 MultiByteToWideChar( CP_ACP, 0, "wineal.drv", -1,
548 wName, sizeof(wName)/sizeof(WCHAR)-1 );
549 if (lpDSEnumCallback(&guid, wDesc, wName, lpContext) == FALSE)
550 goto out;
551 guid.Data4[7]++;
552 devstr += strlen(devstr)+1;
553 } while (*devstr);
555 out:
556 LeaveCriticalSection(&openal_crst);
558 else
560 ERR("Attempting to enumerate sound cards without OpenAL support\n");
561 ERR("Please recompile wine with OpenAL for sound to work\n");
563 return DS_OK;
566 /***************************************************************************
567 * DirectSoundCaptureEnumerateA [DSOUND.7]
569 * Enumerate all DirectSound drivers installed in the system.
571 * PARAMS
572 * lpDSEnumCallback [I] Address of callback function.
573 * lpContext [I] Address of user defined context passed to callback function.
575 * RETURNS
576 * Success: DS_OK
577 * Failure: DSERR_INVALIDPARAM
579 HRESULT WINAPI DirectSoundCaptureEnumerateA(
580 LPDSENUMCALLBACKA lpDSEnumCallback,
581 LPVOID lpContext)
583 struct morecontext context;
585 if (lpDSEnumCallback == NULL) {
586 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
587 return DSERR_INVALIDPARAM;
590 context.callA = lpDSEnumCallback;
591 context.data = lpContext;
593 return DirectSoundCaptureEnumerateW(a_to_w_callback, &context);
596 /***************************************************************************
597 * DirectSoundCaptureEnumerateW [DSOUND.8]
599 * Enumerate all DirectSound drivers installed in the system.
601 * PARAMS
602 * lpDSEnumCallback [I] Address of callback function.
603 * lpContext [I] Address of user defined context passed to callback function.
605 * RETURNS
606 * Success: DS_OK
607 * Failure: DSERR_INVALIDPARAM
609 HRESULT WINAPI
610 DirectSoundCaptureEnumerateW(
611 LPDSENUMCALLBACKW lpDSEnumCallback,
612 LPVOID lpContext)
614 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
616 if (lpDSEnumCallback == NULL) {
617 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
618 return DSERR_INVALIDPARAM;
620 else if (openal_loaded)
622 GUID guid;
623 WCHAR wDesc[MAXPNAMELEN];
624 WCHAR wName[MAXPNAMELEN];
625 const ALCchar *devstr;
626 static const WCHAR empty[] = { 0 };
628 EnterCriticalSection(&openal_crst);
629 devstr = DSOUND_getcapturedevicestrings();
630 if (!devstr || !*devstr)
631 goto out;
633 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
634 "Primary Sound Capture Driver","",lpContext);
635 MultiByteToWideChar( CP_ACP, 0, "Primary Sound Driver", -1,
636 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
637 if (lpDSEnumCallback(NULL, wDesc, empty, lpContext) == FALSE)
638 goto out;
640 guid = DSOUND_capture_guid;
641 do {
642 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
643 debugstr_guid(&guid),devstr,"wineal.drv",lpContext);
644 MultiByteToWideChar( CP_UNIXCP, 0, devstr, -1,
645 wDesc, sizeof(wDesc)/sizeof(WCHAR)-1 );
646 wDesc[sizeof(wDesc)/sizeof(WCHAR)-1] = 0;
647 MultiByteToWideChar( CP_ACP, 0, "wineal.drv", -1,
648 wName, sizeof(wName)/sizeof(WCHAR)-1 );
649 if (lpDSEnumCallback(&guid, wDesc, wName, lpContext) == FALSE)
650 goto out;
651 guid.Data4[7]++;
652 devstr += strlen(devstr)+1;
653 } while (*devstr);
655 out:
656 LeaveCriticalSection(&openal_crst);
658 else
660 ERR("Attempting to enumerate sound cards without OpenAL support\n");
661 ERR("Please recompile wine with OpenAL for sound to work\n");
663 return DS_OK;
666 /*******************************************************************************
667 * DirectSoundCreate (DSOUND.1)
669 * Creates and initializes a DirectSound interface.
671 * PARAMS
672 * lpcGUID [I] Address of the GUID that identifies the sound device.
673 * ppDS [O] Address of a variable to receive the interface pointer.
674 * pUnkOuter [I] Must be NULL.
676 * RETURNS
677 * Success: DS_OK
678 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
679 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
681 HRESULT WINAPI DirectSoundCreate(
682 LPCGUID lpcGUID,
683 LPDIRECTSOUND *ppDS,
684 IUnknown *pUnkOuter)
686 HRESULT hr;
687 LPDIRECTSOUND pDS;
689 TRACE("(%s,%p,%p)\n",debugstr_guid(lpcGUID),ppDS,pUnkOuter);
691 if (ppDS == NULL) {
692 WARN("invalid parameter: ppDS == NULL\n");
693 return DSERR_INVALIDPARAM;
696 if (pUnkOuter != NULL) {
697 WARN("invalid parameter: pUnkOuter != NULL\n");
698 *ppDS = 0;
699 return DSERR_INVALIDPARAM;
702 hr = DSOUND_Create(&IID_IDirectSound, (void **)&pDS);
703 if (hr == DS_OK) {
704 hr = IDirectSound_Initialize(pDS, lpcGUID);
705 if (hr != DS_OK) {
706 if (hr != DSERR_ALREADYINITIALIZED) {
707 IDirectSound_Release(pDS);
708 pDS = 0;
709 } else
710 hr = DS_OK;
714 *ppDS = pDS;
716 return hr;
719 /*******************************************************************************
720 * DirectSoundCreate8 (DSOUND.11)
722 * Creates and initializes a DirectSound8 interface.
724 * PARAMS
725 * lpcGUID [I] Address of the GUID that identifies the sound device.
726 * ppDS [O] Address of a variable to receive the interface pointer.
727 * pUnkOuter [I] Must be NULL.
729 * RETURNS
730 * Success: DS_OK
731 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
732 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
734 HRESULT WINAPI DirectSoundCreate8(
735 LPCGUID lpcGUID,
736 LPDIRECTSOUND8 *ppDS,
737 IUnknown *pUnkOuter)
739 HRESULT hr;
740 LPDIRECTSOUND8 pDS;
742 TRACE("(%s,%p,%p)\n",debugstr_guid(lpcGUID),ppDS,pUnkOuter);
744 if (ppDS == NULL) {
745 WARN("invalid parameter: ppDS == NULL\n");
746 return DSERR_INVALIDPARAM;
749 if (pUnkOuter != NULL) {
750 WARN("invalid parameter: pUnkOuter != NULL\n");
751 *ppDS = 0;
752 return DSERR_INVALIDPARAM;
755 hr = DSOUND_Create8(&IID_IDirectSound8, (void**)&pDS);
756 if (hr == DS_OK) {
757 hr = IDirectSound8_Initialize(pDS, lpcGUID);
758 if (hr != DS_OK) {
759 if (hr != DSERR_ALREADYINITIALIZED) {
760 IDirectSound8_Release(pDS);
761 pDS = 0;
762 } else
763 hr = DS_OK;
767 *ppDS = pDS;
769 return hr;
772 /***************************************************************************
773 * DirectSoundCaptureCreate [DSOUND.6]
775 * Create and initialize a DirectSoundCapture interface.
777 * PARAMS
778 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
779 * lplpDSC [O] Address of a variable to receive the interface pointer.
780 * pUnkOuter [I] Must be NULL.
782 * RETURNS
783 * Success: DS_OK
784 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
785 * DSERR_OUTOFMEMORY
787 * NOTES
788 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
789 * or NULL for the default device or DSDEVID_DefaultCapture or
790 * DSDEVID_DefaultVoiceCapture.
792 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
794 HRESULT WINAPI DirectSoundCaptureCreate(
795 LPCGUID lpcGUID,
796 LPDIRECTSOUNDCAPTURE *ppDSC,
797 LPUNKNOWN pUnkOuter)
799 HRESULT hr;
800 LPDIRECTSOUNDCAPTURE pDSC;
801 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
803 if (ppDSC == NULL) {
804 WARN("invalid parameter: ppDSC == NULL\n");
805 return DSERR_INVALIDPARAM;
808 if (pUnkOuter) {
809 WARN("invalid parameter: pUnkOuter != NULL\n");
810 *ppDSC = NULL;
811 return DSERR_NOAGGREGATION;
814 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, &pDSC);
815 if (hr == DS_OK) {
816 hr = IDirectSoundCapture_Initialize(pDSC, lpcGUID);
817 if (hr != DS_OK) {
818 IDirectSoundCapture_Release(pDSC);
819 pDSC = 0;
823 *ppDSC = pDSC;
825 return hr;
828 /***************************************************************************
829 * DirectSoundCaptureCreate8 [DSOUND.12]
831 * Create and initialize a DirectSoundCapture interface.
833 * PARAMS
834 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
835 * lplpDSC [O] Address of a variable to receive the interface pointer.
836 * pUnkOuter [I] Must be NULL.
838 * RETURNS
839 * Success: DS_OK
840 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
841 * DSERR_OUTOFMEMORY
843 * NOTES
844 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
845 * or NULL for the default device or DSDEVID_DefaultCapture or
846 * DSDEVID_DefaultVoiceCapture.
848 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
850 HRESULT WINAPI DirectSoundCaptureCreate8(
851 LPCGUID lpcGUID,
852 LPDIRECTSOUNDCAPTURE8 *ppDSC8,
853 LPUNKNOWN pUnkOuter)
855 HRESULT hr;
856 LPDIRECTSOUNDCAPTURE8 pDSC8;
857 TRACE("(%s,%p,%p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
859 if (ppDSC8 == NULL) {
860 WARN("invalid parameter: ppDSC8 == NULL\n");
861 return DSERR_INVALIDPARAM;
864 if (pUnkOuter) {
865 WARN("invalid parameter: pUnkOuter != NULL\n");
866 *ppDSC8 = NULL;
867 return DSERR_NOAGGREGATION;
870 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture8, &pDSC8);
871 if (hr == DS_OK) {
872 hr = IDirectSoundCapture_Initialize(pDSC8, lpcGUID);
873 if (hr != DS_OK) {
874 IDirectSoundCapture_Release(pDSC8);
875 pDSC8 = 0;
879 *ppDSC8 = pDSC8;
881 return hr;
884 /*******************************************************************************
885 * DirectSound ClassFactory
888 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
890 typedef struct {
891 IClassFactory IClassFactory_iface;
892 LONG ref;
893 REFCLSID rclsid;
894 FnCreateInstance pfnCreateInstance;
895 } IClassFactoryImpl;
897 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
899 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
902 static HRESULT WINAPI DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
904 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
905 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
906 if (ppobj == NULL)
907 return E_POINTER;
908 if (IsEqualIID(riid, &IID_IUnknown) ||
909 IsEqualIID(riid, &IID_IClassFactory))
911 *ppobj = iface;
912 IUnknown_AddRef(iface);
913 return S_OK;
915 *ppobj = NULL;
916 return E_NOINTERFACE;
919 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
921 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
922 ULONG ref = InterlockedIncrement(&(This->ref));
923 TRACE("(%p) ref was %d\n", This, ref - 1);
924 return ref;
927 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
929 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
930 ULONG ref = InterlockedDecrement(&(This->ref));
931 TRACE("(%p) ref was %d\n", This, ref + 1);
932 /* static class, won't be freed */
933 return ref;
936 static HRESULT WINAPI DSCF_CreateInstance(
937 LPCLASSFACTORY iface,
938 LPUNKNOWN pOuter,
939 REFIID riid,
940 LPVOID *ppobj)
942 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
943 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
945 if (pOuter)
946 return CLASS_E_NOAGGREGATION;
948 if (ppobj == NULL) {
949 WARN("invalid parameter\n");
950 return DSERR_INVALIDPARAM;
952 *ppobj = NULL;
953 return This->pfnCreateInstance(riid, ppobj);
956 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
958 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
959 FIXME("(%p, %d) stub!\n", This, dolock);
960 return S_OK;
963 static const IClassFactoryVtbl DSCF_Vtbl = {
964 DSCF_QueryInterface,
965 DSCF_AddRef,
966 DSCF_Release,
967 DSCF_CreateInstance,
968 DSCF_LockServer
971 static IClassFactoryImpl DSOUND_CF[] = {
972 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound, DSOUND_Create },
973 { {&DSCF_Vtbl}, 1, &CLSID_DirectSound8, DSOUND_Create8 },
974 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture, (FnCreateInstance)DSOUND_CaptureCreate },
975 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture8, (FnCreateInstance)DSOUND_CaptureCreate8 },
976 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundFullDuplex, (FnCreateInstance)DSOUND_FullDuplexCreate },
977 { {&DSCF_Vtbl}, 1, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
978 { {NULL}, 0, NULL, NULL }
981 /*******************************************************************************
982 * DllGetClassObject [DSOUND.@]
983 * Retrieves class object from a DLL object
985 * NOTES
986 * Docs say returns STDAPI
988 * PARAMS
989 * rclsid [I] CLSID for the class object
990 * riid [I] Reference to identifier of interface for class object
991 * ppv [O] Address of variable to receive interface pointer for riid
993 * RETURNS
994 * Success: S_OK
995 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
996 * E_UNEXPECTED
998 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1000 int i = 0;
1001 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1003 if (ppv == NULL) {
1004 WARN("invalid parameter\n");
1005 return E_INVALIDARG;
1008 *ppv = NULL;
1010 if (!IsEqualIID(riid, &IID_IClassFactory) &&
1011 !IsEqualIID(riid, &IID_IUnknown)) {
1012 WARN("no interface for %s\n", debugstr_guid(riid));
1013 return E_NOINTERFACE;
1016 while (NULL != DSOUND_CF[i].rclsid) {
1017 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
1018 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
1019 *ppv = &DSOUND_CF[i];
1020 return S_OK;
1022 i++;
1025 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
1026 debugstr_guid(riid), ppv);
1027 return CLASS_E_CLASSNOTAVAILABLE;
1031 /*******************************************************************************
1032 * DllCanUnloadNow [DSOUND.4]
1033 * Determines whether the DLL is in use.
1035 * RETURNS
1036 * Success: S_OK
1037 * Failure: S_FALSE
1039 HRESULT WINAPI DllCanUnloadNow(void)
1041 FIXME("(void): stub\n");
1042 return S_FALSE;
1045 /***********************************************************************
1046 * DllMain (DSOUND.init)
1048 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
1050 TRACE("(%p, %d, %p)\n", hInstDLL, fdwReason, lpvReserved);
1052 switch (fdwReason) {
1053 case DLL_PROCESS_ATTACH:
1054 TRACE("DLL_PROCESS_ATTACH\n");
1055 load_libopenal();
1056 DisableThreadLibraryCalls(hInstDLL);
1057 /* Increase refcount on dsound by 1 */
1058 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
1059 break;
1060 case DLL_PROCESS_DETACH:
1061 TRACE("DLL_PROCESS_DETACH\n");
1062 #ifdef SONAME_LIBOPENAL
1063 if (openal_handle)
1064 wine_dlclose(openal_handle, NULL, 0);
1065 #endif /*SONAME_LIBOPENAL*/
1066 break;
1067 default:
1068 TRACE("UNKNOWN REASON\n");
1069 break;
1071 return TRUE;