Fix for native headers specifying non-const vtables
[wine/multimedia.git] / dsound_main.c
blobb32a0bb267fdedf51ce685647a55f33a6691e57a
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 0x0600
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 #ifndef __WINESRC__
232 const char *str = getenv("DSOAL_LOGLEVEL");
233 if(str && *str)
234 LogLevel = atoi(str);
235 #elif defined(SONAME_LIBOPENAL)
236 BOOL failed = FALSE;
237 char error[128];
239 openal_handle = wine_dlopen(SONAME_LIBOPENAL, RTLD_NOW, error, sizeof(error));
240 if(!openal_handle)
241 ERR("Couldn't load " SONAME_LIBOPENAL ": %s\n", error);
242 #define LOAD_FUNCPTR(f) \
243 if((p##f = wine_dlsym(openal_handle, #f, NULL, 0)) == NULL) { \
244 ERR("Couldn't lookup %s in " SONAME_LIBOPENAL "\n", #f); \
245 failed = TRUE; \
248 LOAD_FUNCPTR(alcCreateContext);
249 LOAD_FUNCPTR(alcMakeContextCurrent);
250 LOAD_FUNCPTR(alcProcessContext);
251 LOAD_FUNCPTR(alcSuspendContext);
252 LOAD_FUNCPTR(alcDestroyContext);
253 LOAD_FUNCPTR(alcGetCurrentContext);
254 LOAD_FUNCPTR(alcGetContextsDevice);
255 LOAD_FUNCPTR(alcOpenDevice);
256 LOAD_FUNCPTR(alcCloseDevice);
257 LOAD_FUNCPTR(alcGetError);
258 LOAD_FUNCPTR(alcIsExtensionPresent);
259 LOAD_FUNCPTR(alcGetProcAddress);
260 LOAD_FUNCPTR(alcGetEnumValue);
261 LOAD_FUNCPTR(alcGetString);
262 LOAD_FUNCPTR(alcGetIntegerv);
263 LOAD_FUNCPTR(alcCaptureOpenDevice);
264 LOAD_FUNCPTR(alcCaptureCloseDevice);
265 LOAD_FUNCPTR(alcCaptureStart);
266 LOAD_FUNCPTR(alcCaptureStop);
267 LOAD_FUNCPTR(alcCaptureSamples);
268 LOAD_FUNCPTR(alEnable);
269 LOAD_FUNCPTR(alDisable);
270 LOAD_FUNCPTR(alIsEnabled);
271 LOAD_FUNCPTR(alGetString);
272 LOAD_FUNCPTR(alGetBooleanv);
273 LOAD_FUNCPTR(alGetIntegerv);
274 LOAD_FUNCPTR(alGetFloatv);
275 LOAD_FUNCPTR(alGetDoublev);
276 LOAD_FUNCPTR(alGetBoolean);
277 LOAD_FUNCPTR(alGetInteger);
278 LOAD_FUNCPTR(alGetFloat);
279 LOAD_FUNCPTR(alGetDouble);
280 LOAD_FUNCPTR(alGetError);
281 LOAD_FUNCPTR(alIsExtensionPresent);
282 LOAD_FUNCPTR(alGetProcAddress);
283 LOAD_FUNCPTR(alGetEnumValue);
284 LOAD_FUNCPTR(alListenerf);
285 LOAD_FUNCPTR(alListener3f);
286 LOAD_FUNCPTR(alListenerfv);
287 LOAD_FUNCPTR(alListeneri);
288 LOAD_FUNCPTR(alListener3i);
289 LOAD_FUNCPTR(alListeneriv);
290 LOAD_FUNCPTR(alGetListenerf);
291 LOAD_FUNCPTR(alGetListener3f);
292 LOAD_FUNCPTR(alGetListenerfv);
293 LOAD_FUNCPTR(alGetListeneri);
294 LOAD_FUNCPTR(alGetListener3i);
295 LOAD_FUNCPTR(alGetListeneriv);
296 LOAD_FUNCPTR(alGenSources);
297 LOAD_FUNCPTR(alDeleteSources);
298 LOAD_FUNCPTR(alIsSource);
299 LOAD_FUNCPTR(alSourcef);
300 LOAD_FUNCPTR(alSource3f);
301 LOAD_FUNCPTR(alSourcefv);
302 LOAD_FUNCPTR(alSourcei);
303 LOAD_FUNCPTR(alSource3i);
304 LOAD_FUNCPTR(alSourceiv);
305 LOAD_FUNCPTR(alGetSourcef);
306 LOAD_FUNCPTR(alGetSource3f);
307 LOAD_FUNCPTR(alGetSourcefv);
308 LOAD_FUNCPTR(alGetSourcei);
309 LOAD_FUNCPTR(alGetSource3i);
310 LOAD_FUNCPTR(alGetSourceiv);
311 LOAD_FUNCPTR(alSourcePlayv);
312 LOAD_FUNCPTR(alSourceStopv);
313 LOAD_FUNCPTR(alSourceRewindv);
314 LOAD_FUNCPTR(alSourcePausev);
315 LOAD_FUNCPTR(alSourcePlay);
316 LOAD_FUNCPTR(alSourceStop);
317 LOAD_FUNCPTR(alSourceRewind);
318 LOAD_FUNCPTR(alSourcePause);
319 LOAD_FUNCPTR(alSourceQueueBuffers);
320 LOAD_FUNCPTR(alSourceUnqueueBuffers);
321 LOAD_FUNCPTR(alGenBuffers);
322 LOAD_FUNCPTR(alDeleteBuffers);
323 LOAD_FUNCPTR(alIsBuffer);
324 LOAD_FUNCPTR(alBufferf);
325 LOAD_FUNCPTR(alBuffer3f);
326 LOAD_FUNCPTR(alBufferfv);
327 LOAD_FUNCPTR(alBufferi);
328 LOAD_FUNCPTR(alBuffer3i);
329 LOAD_FUNCPTR(alBufferiv);
330 LOAD_FUNCPTR(alGetBufferf);
331 LOAD_FUNCPTR(alGetBuffer3f);
332 LOAD_FUNCPTR(alGetBufferfv);
333 LOAD_FUNCPTR(alGetBufferi);
334 LOAD_FUNCPTR(alGetBuffer3i);
335 LOAD_FUNCPTR(alGetBufferiv);
336 LOAD_FUNCPTR(alBufferData);
337 LOAD_FUNCPTR(alDopplerFactor);
338 LOAD_FUNCPTR(alDopplerVelocity);
339 LOAD_FUNCPTR(alDistanceModel);
340 LOAD_FUNCPTR(alSpeedOfSound);
341 #undef LOAD_FUNCPTR
342 if (failed)
344 WARN("Unloading openal\n");
345 if (openal_handle != RTLD_DEFAULT)
346 wine_dlclose(openal_handle, NULL, 0);
347 openal_handle = NULL;
349 else
350 #endif
352 openal_loaded = 1;
354 local_contexts = alcIsExtensionPresent(NULL, "ALC_EXT_thread_local_context");
355 if(local_contexts)
357 set_context = alcGetProcAddress(NULL, "alcSetThreadContext");
358 get_context = alcGetProcAddress(NULL, "alcGetThreadContext");
359 if(!set_context || !get_context)
361 ERR("TLS advertised but functions not found, disabling thread local context\n");
362 local_contexts = 0;
365 if(!local_contexts)
367 set_context = alcMakeContextCurrent;
368 get_context = alcGetCurrentContext;
373 const ALCchar *DSOUND_getdevicestrings(void)
375 const ALCchar *str = NULL;
376 int multiple;
378 multiple = alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT");
379 if (multiple)
380 str = alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER);
381 else
382 str = alcGetString(NULL, ALC_DEVICE_SPECIFIER);
383 return str;
386 const ALCchar *DSOUND_getcapturedevicestrings(void)
388 const ALCchar *str = NULL;
389 str = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
390 return str;
393 /***************************************************************************
394 * GetDeviceID [DSOUND.9]
396 * Retrieves unique identifier of default device specified
398 * PARAMS
399 * pGuidSrc [I] Address of device GUID.
400 * pGuidDest [O] Address to receive unique device GUID.
402 * RETURNS
403 * Success: DS_OK
404 * Failure: DSERR_INVALIDPARAM
406 * NOTES
407 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
408 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
409 * DSDEVID_DefaultVoiceCapture.
410 * Returns pGuidSrc if pGuidSrc is a valid device or the device
411 * GUID for the specified constants.
413 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
415 TRACE("(%s,%p)\n", debugstr_guid(pGuidSrc),pGuidDest);
417 if ( pGuidSrc == NULL) {
418 WARN("invalid parameter: pGuidSrc == NULL\n");
419 return DSERR_INVALIDPARAM;
422 if ( pGuidDest == NULL ) {
423 WARN("invalid parameter: pGuidDest == NULL\n");
424 return DSERR_INVALIDPARAM;
427 if ( IsEqualGUID( &DSDEVID_DefaultPlayback, pGuidSrc ) ||
428 IsEqualGUID( &DSDEVID_DefaultVoicePlayback, pGuidSrc ) ) {
429 *pGuidDest = DSOUND_renderer_guid;
430 TRACE("returns %s\n", debugstr_guid(pGuidDest));
431 return DS_OK;
434 if ( IsEqualGUID( &DSDEVID_DefaultCapture, pGuidSrc ) ||
435 IsEqualGUID( &DSDEVID_DefaultVoiceCapture, pGuidSrc ) ) {
436 *pGuidDest = DSOUND_capture_guid;
437 TRACE("returns %s\n", debugstr_guid(pGuidDest));
438 return DS_OK;
441 *pGuidDest = *pGuidSrc;
442 TRACE("returns %s\n", debugstr_guid(pGuidDest));
444 return DS_OK;
447 struct morecontext
449 LPDSENUMCALLBACKA callA;
450 LPVOID data;
453 static BOOL CALLBACK a_to_w_callback(LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
455 struct morecontext *context = data;
456 char descA[MAXPNAMELEN], modA[MAXPNAMELEN];
458 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, sizeof(descA), NULL, NULL);
459 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, sizeof(modA), NULL, NULL);
461 return context->callA(guid, descA, modA, context->data);
464 /***************************************************************************
465 * DirectSoundEnumerateA [DSOUND.2]
467 * Enumerate all DirectSound drivers installed in the system
469 * PARAMS
470 * lpDSEnumCallback [I] Address of callback function.
471 * lpContext [I] Address of user defined context passed to callback function.
473 * RETURNS
474 * Success: DS_OK
475 * Failure: DSERR_INVALIDPARAM
477 HRESULT WINAPI DirectSoundEnumerateA(
478 LPDSENUMCALLBACKA lpDSEnumCallback,
479 LPVOID lpContext)
481 struct morecontext context;
483 if (lpDSEnumCallback == NULL) {
484 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
485 return DSERR_INVALIDPARAM;
488 context.callA = lpDSEnumCallback;
489 context.data = lpContext;
491 return DirectSoundEnumerateW(a_to_w_callback, &context);
494 /***************************************************************************
495 * DirectSoundEnumerateW [DSOUND.3]
497 * Enumerate all DirectSound drivers installed in the system
499 * PARAMS
500 * lpDSEnumCallback [I] Address of callback function.
501 * lpContext [I] Address of user defined context passed to callback function.
503 * RETURNS
504 * Success: DS_OK
505 * Failure: DSERR_INVALIDPARAM
507 HRESULT WINAPI DirectSoundEnumerateW(
508 LPDSENUMCALLBACKW lpDSEnumCallback,
509 LPVOID lpContext )
511 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
512 lpDSEnumCallback, lpContext);
514 if (lpDSEnumCallback == NULL) {
515 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
516 return DSERR_INVALIDPARAM;
518 else if (openal_loaded)
520 GUID guid;
521 WCHAR wDesc[MAXPNAMELEN];
522 WCHAR wName[MAXPNAMELEN];
523 const ALCchar *devstr;
524 static const WCHAR empty[] = { 0 };
525 EnterCriticalSection(&openal_crst);
526 devstr = DSOUND_getdevicestrings();
527 if (!devstr || !*devstr)
528 goto out;
530 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
531 "Primary Sound Driver","",lpContext);
532 MultiByteToWideChar( CP_ACP, 0, "Primary Sound Driver", -1,
533 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
534 if (lpDSEnumCallback(NULL, wDesc, empty, lpContext) == FALSE)
535 goto out;
537 guid = DSOUND_renderer_guid;
538 do {
539 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
540 debugstr_guid(&guid),devstr,"wineal.drv",lpContext);
541 MultiByteToWideChar( CP_UNIXCP, 0, devstr, -1,
542 wDesc, sizeof(wDesc)/sizeof(WCHAR)-1 );
543 wDesc[sizeof(wDesc)/sizeof(WCHAR)-1] = 0;
544 MultiByteToWideChar( CP_ACP, 0, "wineal.drv", -1,
545 wName, sizeof(wName)/sizeof(WCHAR)-1 );
546 if (lpDSEnumCallback(&guid, wDesc, wName, lpContext) == FALSE)
547 goto out;
548 guid.Data4[7]++;
549 devstr += strlen(devstr)+1;
550 } while (*devstr);
552 out:
553 LeaveCriticalSection(&openal_crst);
555 else
557 ERR("Attempting to enumerate sound cards without OpenAL support\n");
558 ERR("Please recompile wine with OpenAL for sound to work\n");
560 return DS_OK;
563 /***************************************************************************
564 * DirectSoundCaptureEnumerateA [DSOUND.7]
566 * Enumerate all DirectSound drivers installed in the system.
568 * PARAMS
569 * lpDSEnumCallback [I] Address of callback function.
570 * lpContext [I] Address of user defined context passed to callback function.
572 * RETURNS
573 * Success: DS_OK
574 * Failure: DSERR_INVALIDPARAM
576 HRESULT WINAPI DirectSoundCaptureEnumerateA(
577 LPDSENUMCALLBACKA lpDSEnumCallback,
578 LPVOID lpContext)
580 struct morecontext context;
582 if (lpDSEnumCallback == NULL) {
583 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
584 return DSERR_INVALIDPARAM;
587 context.callA = lpDSEnumCallback;
588 context.data = lpContext;
590 return DirectSoundCaptureEnumerateW(a_to_w_callback, &context);
593 /***************************************************************************
594 * DirectSoundCaptureEnumerateW [DSOUND.8]
596 * Enumerate all DirectSound drivers installed in the system.
598 * PARAMS
599 * lpDSEnumCallback [I] Address of callback function.
600 * lpContext [I] Address of user defined context passed to callback function.
602 * RETURNS
603 * Success: DS_OK
604 * Failure: DSERR_INVALIDPARAM
606 HRESULT WINAPI
607 DirectSoundCaptureEnumerateW(
608 LPDSENUMCALLBACKW lpDSEnumCallback,
609 LPVOID lpContext)
611 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
613 if (lpDSEnumCallback == NULL) {
614 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
615 return DSERR_INVALIDPARAM;
617 else if (openal_loaded)
619 GUID guid;
620 WCHAR wDesc[MAXPNAMELEN];
621 WCHAR wName[MAXPNAMELEN];
622 const ALCchar *devstr;
623 static const WCHAR empty[] = { 0 };
625 EnterCriticalSection(&openal_crst);
626 devstr = DSOUND_getcapturedevicestrings();
627 if (!devstr || !*devstr)
628 goto out;
630 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
631 "Primary Sound Capture Driver","",lpContext);
632 MultiByteToWideChar( CP_ACP, 0, "Primary Sound Driver", -1,
633 wDesc, sizeof(wDesc)/sizeof(WCHAR) );
634 if (lpDSEnumCallback(NULL, wDesc, empty, lpContext) == FALSE)
635 goto out;
637 guid = DSOUND_capture_guid;
638 do {
639 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
640 debugstr_guid(&guid),devstr,"wineal.drv",lpContext);
641 MultiByteToWideChar( CP_UNIXCP, 0, devstr, -1,
642 wDesc, sizeof(wDesc)/sizeof(WCHAR)-1 );
643 wDesc[sizeof(wDesc)/sizeof(WCHAR)-1] = 0;
644 MultiByteToWideChar( CP_ACP, 0, "wineal.drv", -1,
645 wName, sizeof(wName)/sizeof(WCHAR)-1 );
646 if (lpDSEnumCallback(&guid, wDesc, wName, lpContext) == FALSE)
647 goto out;
648 guid.Data4[7]++;
649 devstr += strlen(devstr)+1;
650 } while (*devstr);
652 out:
653 LeaveCriticalSection(&openal_crst);
655 else
657 ERR("Attempting to enumerate sound cards without OpenAL support\n");
658 ERR("Please recompile wine with OpenAL for sound to work\n");
660 return DS_OK;
663 /*******************************************************************************
664 * DirectSoundCreate (DSOUND.1)
666 * Creates and initializes a DirectSound interface.
668 * PARAMS
669 * lpcGUID [I] Address of the GUID that identifies the sound device.
670 * ppDS [O] Address of a variable to receive the interface pointer.
671 * pUnkOuter [I] Must be NULL.
673 * RETURNS
674 * Success: DS_OK
675 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
676 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
678 HRESULT WINAPI DirectSoundCreate(
679 LPCGUID lpcGUID,
680 LPDIRECTSOUND *ppDS,
681 IUnknown *pUnkOuter)
683 HRESULT hr;
684 LPDIRECTSOUND pDS;
686 TRACE("(%s,%p,%p)\n",debugstr_guid(lpcGUID),ppDS,pUnkOuter);
688 if (ppDS == NULL) {
689 WARN("invalid parameter: ppDS == NULL\n");
690 return DSERR_INVALIDPARAM;
693 if (pUnkOuter != NULL) {
694 WARN("invalid parameter: pUnkOuter != NULL\n");
695 *ppDS = 0;
696 return DSERR_INVALIDPARAM;
699 hr = DSOUND_Create(&IID_IDirectSound, (void **)&pDS);
700 if (hr == DS_OK) {
701 hr = IDirectSound_Initialize(pDS, lpcGUID);
702 if (hr != DS_OK) {
703 if (hr != DSERR_ALREADYINITIALIZED) {
704 IDirectSound_Release(pDS);
705 pDS = 0;
706 } else
707 hr = DS_OK;
711 *ppDS = pDS;
713 return hr;
716 /*******************************************************************************
717 * DirectSoundCreate8 (DSOUND.11)
719 * Creates and initializes a DirectSound8 interface.
721 * PARAMS
722 * lpcGUID [I] Address of the GUID that identifies the sound device.
723 * ppDS [O] Address of a variable to receive the interface pointer.
724 * pUnkOuter [I] Must be NULL.
726 * RETURNS
727 * Success: DS_OK
728 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
729 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
731 HRESULT WINAPI DirectSoundCreate8(
732 LPCGUID lpcGUID,
733 LPDIRECTSOUND8 *ppDS,
734 IUnknown *pUnkOuter)
736 HRESULT hr;
737 LPDIRECTSOUND8 pDS;
739 TRACE("(%s,%p,%p)\n",debugstr_guid(lpcGUID),ppDS,pUnkOuter);
741 if (ppDS == NULL) {
742 WARN("invalid parameter: ppDS == NULL\n");
743 return DSERR_INVALIDPARAM;
746 if (pUnkOuter != NULL) {
747 WARN("invalid parameter: pUnkOuter != NULL\n");
748 *ppDS = 0;
749 return DSERR_INVALIDPARAM;
752 hr = DSOUND_Create8(&IID_IDirectSound8, (void**)&pDS);
753 if (hr == DS_OK) {
754 hr = IDirectSound8_Initialize(pDS, lpcGUID);
755 if (hr != DS_OK) {
756 if (hr != DSERR_ALREADYINITIALIZED) {
757 IDirectSound8_Release(pDS);
758 pDS = 0;
759 } else
760 hr = DS_OK;
764 *ppDS = pDS;
766 return hr;
769 /***************************************************************************
770 * DirectSoundCaptureCreate [DSOUND.6]
772 * Create and initialize a DirectSoundCapture interface.
774 * PARAMS
775 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
776 * lplpDSC [O] Address of a variable to receive the interface pointer.
777 * pUnkOuter [I] Must be NULL.
779 * RETURNS
780 * Success: DS_OK
781 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
782 * DSERR_OUTOFMEMORY
784 * NOTES
785 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
786 * or NULL for the default device or DSDEVID_DefaultCapture or
787 * DSDEVID_DefaultVoiceCapture.
789 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
791 HRESULT WINAPI DirectSoundCaptureCreate(
792 LPCGUID lpcGUID,
793 IDirectSoundCapture **ppDSC,
794 IUnknown *pUnkOuter)
796 void *pDSC;
797 HRESULT hr;
799 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
801 if(ppDSC == NULL)
803 WARN("invalid parameter: ppDSC == NULL\n");
804 return DSERR_INVALIDPARAM;
806 *ppDSC = NULL;
808 if(pUnkOuter)
810 WARN("invalid parameter: pUnkOuter != NULL\n");
811 return DSERR_NOAGGREGATION;
814 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, &pDSC);
815 if(hr == DS_OK)
817 *ppDSC = pDSC;
818 hr = IDirectSoundCapture_Initialize(*ppDSC, lpcGUID);
819 if(hr != DS_OK)
821 IDirectSoundCapture_Release(*ppDSC);
822 *ppDSC = 0;
826 return hr;
829 /***************************************************************************
830 * DirectSoundCaptureCreate8 [DSOUND.12]
832 * Create and initialize a DirectSoundCapture interface.
834 * PARAMS
835 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
836 * lplpDSC [O] Address of a variable to receive the interface pointer.
837 * pUnkOuter [I] Must be NULL.
839 * RETURNS
840 * Success: DS_OK
841 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
842 * DSERR_OUTOFMEMORY
844 * NOTES
845 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
846 * or NULL for the default device or DSDEVID_DefaultCapture or
847 * DSDEVID_DefaultVoiceCapture.
849 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
851 HRESULT WINAPI DirectSoundCaptureCreate8(
852 LPCGUID lpcGUID,
853 IDirectSoundCapture8 **ppDSC8,
854 IUnknown *pUnkOuter)
856 void *pDSC8;
857 HRESULT hr;
859 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
861 if(ppDSC8 == NULL)
863 WARN("invalid parameter: ppDSC8 == NULL\n");
864 return DSERR_INVALIDPARAM;
866 *ppDSC8 = NULL;
868 if(pUnkOuter)
870 WARN("invalid parameter: pUnkOuter != NULL\n");
871 return DSERR_NOAGGREGATION;
874 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture8, &pDSC8);
875 if(hr == DS_OK)
877 *ppDSC8 = pDSC8;
878 hr = IDirectSoundCapture_Initialize(*ppDSC8, lpcGUID);
879 if(hr != DS_OK)
881 IDirectSoundCapture_Release(*ppDSC8);
882 *ppDSC8 = NULL;
886 return hr;
889 /*******************************************************************************
890 * DirectSound ClassFactory
893 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
895 typedef struct {
896 IClassFactory IClassFactory_iface;
897 LONG ref;
898 REFCLSID rclsid;
899 FnCreateInstance pfnCreateInstance;
900 } IClassFactoryImpl;
902 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
904 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
907 static HRESULT WINAPI DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
909 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
910 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
911 if (ppobj == NULL)
912 return E_POINTER;
913 if (IsEqualIID(riid, &IID_IUnknown) ||
914 IsEqualIID(riid, &IID_IClassFactory))
916 *ppobj = iface;
917 IUnknown_AddRef(iface);
918 return S_OK;
920 *ppobj = NULL;
921 return E_NOINTERFACE;
924 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
926 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
927 ULONG ref = InterlockedIncrement(&(This->ref));
928 TRACE("(%p) ref was %d\n", This, ref - 1);
929 return ref;
932 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
934 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
935 ULONG ref = InterlockedDecrement(&(This->ref));
936 TRACE("(%p) ref was %d\n", This, ref + 1);
937 /* static class, won't be freed */
938 return ref;
941 static HRESULT WINAPI DSCF_CreateInstance(
942 LPCLASSFACTORY iface,
943 LPUNKNOWN pOuter,
944 REFIID riid,
945 LPVOID *ppobj)
947 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
948 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
950 if (pOuter)
951 return CLASS_E_NOAGGREGATION;
953 if (ppobj == NULL) {
954 WARN("invalid parameter\n");
955 return DSERR_INVALIDPARAM;
957 *ppobj = NULL;
958 return This->pfnCreateInstance(riid, ppobj);
961 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
963 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
964 FIXME("(%p, %d) stub!\n", This, dolock);
965 return S_OK;
968 static const IClassFactoryVtbl DSCF_Vtbl = {
969 DSCF_QueryInterface,
970 DSCF_AddRef,
971 DSCF_Release,
972 DSCF_CreateInstance,
973 DSCF_LockServer
976 static IClassFactoryImpl DSOUND_CF[] = {
977 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSound, DSOUND_Create },
978 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSound8, DSOUND_Create8 },
979 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
980 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
981 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
982 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
983 { {NULL}, 0, NULL, NULL }
986 /*******************************************************************************
987 * DllGetClassObject [DSOUND.@]
988 * Retrieves class object from a DLL object
990 * NOTES
991 * Docs say returns STDAPI
993 * PARAMS
994 * rclsid [I] CLSID for the class object
995 * riid [I] Reference to identifier of interface for class object
996 * ppv [O] Address of variable to receive interface pointer for riid
998 * RETURNS
999 * Success: S_OK
1000 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
1001 * E_UNEXPECTED
1003 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1005 int i = 0;
1006 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1008 if (ppv == NULL) {
1009 WARN("invalid parameter\n");
1010 return E_INVALIDARG;
1013 *ppv = NULL;
1015 if (!IsEqualIID(riid, &IID_IClassFactory) &&
1016 !IsEqualIID(riid, &IID_IUnknown)) {
1017 WARN("no interface for %s\n", debugstr_guid(riid));
1018 return E_NOINTERFACE;
1021 while (NULL != DSOUND_CF[i].rclsid) {
1022 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
1023 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
1024 *ppv = &DSOUND_CF[i];
1025 return S_OK;
1027 i++;
1030 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
1031 debugstr_guid(riid), ppv);
1032 return CLASS_E_CLASSNOTAVAILABLE;
1036 /*******************************************************************************
1037 * DllCanUnloadNow [DSOUND.4]
1038 * Determines whether the DLL is in use.
1040 * RETURNS
1041 * Success: S_OK
1042 * Failure: S_FALSE
1044 HRESULT WINAPI DllCanUnloadNow(void)
1046 FIXME("(void): stub\n");
1047 return S_FALSE;
1050 /***********************************************************************
1051 * DllMain (DSOUND.init)
1053 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
1055 TRACE("(%p, %d, %p)\n", hInstDLL, fdwReason, lpvReserved);
1057 switch (fdwReason) {
1058 case DLL_PROCESS_ATTACH:
1059 TRACE("DLL_PROCESS_ATTACH\n");
1060 load_libopenal();
1061 DisableThreadLibraryCalls(hInstDLL);
1062 /* Increase refcount on dsound by 1 */
1063 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
1064 break;
1065 case DLL_PROCESS_DETACH:
1066 TRACE("DLL_PROCESS_DETACH\n");
1067 #ifdef SONAME_LIBOPENAL
1068 if (openal_handle)
1069 wine_dlclose(openal_handle, NULL, 0);
1070 #endif /*SONAME_LIBOPENAL*/
1071 break;
1072 default:
1073 TRACE("UNKNOWN REASON\n");
1074 break;
1076 return TRUE;