Avoid some casts, and cleanup
[dsound-openal.git] / dsound_main.c
blob6c0fa22bcda7ce3d0883c42fc983ce79c73e2ebd
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 #endif
82 static HINSTANCE instance;
84 const GUID DSOUND_renderer_guid = { 0xbd6dd71a, 0x3deb, 0x11d1, { 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 } };
85 const GUID DSOUND_capture_guid = { 0xbd6dd71b, 0x3deb, 0x11d1, { 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 } };
87 const IID DSPROPSETID_EAX20_ListenerProperties = {
88 0x0306a6a8, 0xb224, 0x11d2, { 0x99, 0xe5, 0x00, 0x00, 0xe8, 0xd8, 0xc7, 0x22 }
91 const EAXLISTENERPROPERTIES EnvironmentDefaults[EAX_ENVIRONMENT_COUNT] =
93 REVERB_PRESET_GENERIC,
94 REVERB_PRESET_PADDEDCELL,
95 REVERB_PRESET_ROOM,
96 REVERB_PRESET_BATHROOM,
97 REVERB_PRESET_LIVINGROOM,
98 REVERB_PRESET_STONEROOM,
99 REVERB_PRESET_AUDITORIUM,
100 REVERB_PRESET_CONCERTHALL,
101 REVERB_PRESET_CAVE,
102 REVERB_PRESET_ARENA,
103 REVERB_PRESET_HANGAR,
104 REVERB_PRESET_CARPETEDHALLWAY,
105 REVERB_PRESET_HALLWAY,
106 REVERB_PRESET_STONECORRIDOR,
107 REVERB_PRESET_ALLEY,
108 REVERB_PRESET_FOREST,
109 REVERB_PRESET_CITY,
110 REVERB_PRESET_MOUNTAINS,
111 REVERB_PRESET_QUARRY,
112 REVERB_PRESET_PLAIN,
113 REVERB_PRESET_PARKINGLOT,
114 REVERB_PRESET_SEWERPIPE,
115 REVERB_PRESET_UNDERWATER,
116 REVERB_PRESET_DRUGGED,
117 REVERB_PRESET_DIZZY,
118 REVERB_PRESET_PSYCHOTIC
121 static CRITICAL_SECTION_DEBUG openal_crst_debug =
123 0, 0, &openal_crst,
124 { &openal_crst_debug.ProcessLocksList,
125 &openal_crst_debug.ProcessLocksList },
126 0, 0, { (DWORD_PTR)(__FILE__ ": openal_crst_debug") }
128 CRITICAL_SECTION openal_crst = { &openal_crst_debug, -1, 0, 0, 0, 0 };
130 int openal_loaded = 0;
131 #ifdef SONAME_LIBOPENAL
132 static void *openal_handle = NULL;
133 LPALCCREATECONTEXT palcCreateContext = NULL;
134 LPALCMAKECONTEXTCURRENT palcMakeContextCurrent = NULL;
135 LPALCPROCESSCONTEXT palcProcessContext = NULL;
136 LPALCSUSPENDCONTEXT palcSuspendContext = NULL;
137 LPALCDESTROYCONTEXT palcDestroyContext = NULL;
138 LPALCGETCURRENTCONTEXT palcGetCurrentContext = NULL;
139 LPALCGETCONTEXTSDEVICE palcGetContextsDevice = NULL;
140 LPALCOPENDEVICE palcOpenDevice = NULL;
141 LPALCCLOSEDEVICE palcCloseDevice = NULL;
142 LPALCGETERROR palcGetError = NULL;
143 LPALCISEXTENSIONPRESENT palcIsExtensionPresent = NULL;
144 LPALCGETPROCADDRESS palcGetProcAddress = NULL;
145 LPALCGETENUMVALUE palcGetEnumValue = NULL;
146 LPALCGETSTRING palcGetString = NULL;
147 LPALCGETINTEGERV palcGetIntegerv = NULL;
148 LPALCCAPTUREOPENDEVICE palcCaptureOpenDevice = NULL;
149 LPALCCAPTURECLOSEDEVICE palcCaptureCloseDevice = NULL;
150 LPALCCAPTURESTART palcCaptureStart = NULL;
151 LPALCCAPTURESTOP palcCaptureStop = NULL;
152 LPALCCAPTURESAMPLES palcCaptureSamples = NULL;
153 LPALENABLE palEnable = NULL;
154 LPALDISABLE palDisable = NULL;
155 LPALISENABLED palIsEnabled = NULL;
156 LPALGETSTRING palGetString = NULL;
157 LPALGETBOOLEANV palGetBooleanv = NULL;
158 LPALGETINTEGERV palGetIntegerv = NULL;
159 LPALGETFLOATV palGetFloatv = NULL;
160 LPALGETDOUBLEV palGetDoublev = NULL;
161 LPALGETBOOLEAN palGetBoolean = NULL;
162 LPALGETINTEGER palGetInteger = NULL;
163 LPALGETFLOAT palGetFloat = NULL;
164 LPALGETDOUBLE palGetDouble = NULL;
165 LPALGETERROR palGetError = NULL;
166 LPALISEXTENSIONPRESENT palIsExtensionPresent = NULL;
167 LPALGETPROCADDRESS palGetProcAddress = NULL;
168 LPALGETENUMVALUE palGetEnumValue = NULL;
169 LPALLISTENERF palListenerf = NULL;
170 LPALLISTENER3F palListener3f = NULL;
171 LPALLISTENERFV palListenerfv = NULL;
172 LPALLISTENERI palListeneri = NULL;
173 LPALLISTENER3I palListener3i = NULL;
174 LPALLISTENERIV palListeneriv = NULL;
175 LPALGETLISTENERF palGetListenerf = NULL;
176 LPALGETLISTENER3F palGetListener3f = NULL;
177 LPALGETLISTENERFV palGetListenerfv = NULL;
178 LPALGETLISTENERI palGetListeneri = NULL;
179 LPALGETLISTENER3I palGetListener3i = NULL;
180 LPALGETLISTENERIV palGetListeneriv = NULL;
181 LPALGENSOURCES palGenSources = NULL;
182 LPALDELETESOURCES palDeleteSources = NULL;
183 LPALISSOURCE palIsSource = NULL;
184 LPALSOURCEF palSourcef = NULL;
185 LPALSOURCE3F palSource3f = NULL;
186 LPALSOURCEFV palSourcefv = NULL;
187 LPALSOURCEI palSourcei = NULL;
188 LPALSOURCE3I palSource3i = NULL;
189 LPALSOURCEIV palSourceiv = NULL;
190 LPALGETSOURCEF palGetSourcef = NULL;
191 LPALGETSOURCE3F palGetSource3f = NULL;
192 LPALGETSOURCEFV palGetSourcefv = NULL;
193 LPALGETSOURCEI palGetSourcei = NULL;
194 LPALGETSOURCE3I palGetSource3i = NULL;
195 LPALGETSOURCEIV palGetSourceiv = NULL;
196 LPALSOURCEPLAYV palSourcePlayv = NULL;
197 LPALSOURCESTOPV palSourceStopv = NULL;
198 LPALSOURCEREWINDV palSourceRewindv = NULL;
199 LPALSOURCEPAUSEV palSourcePausev = NULL;
200 LPALSOURCEPLAY palSourcePlay = NULL;
201 LPALSOURCESTOP palSourceStop = NULL;
202 LPALSOURCEREWIND palSourceRewind = NULL;
203 LPALSOURCEPAUSE palSourcePause = NULL;
204 LPALSOURCEQUEUEBUFFERS palSourceQueueBuffers = NULL;
205 LPALSOURCEUNQUEUEBUFFERS palSourceUnqueueBuffers = NULL;
206 LPALGENBUFFERS palGenBuffers = NULL;
207 LPALDELETEBUFFERS palDeleteBuffers = NULL;
208 LPALISBUFFER palIsBuffer = NULL;
209 LPALBUFFERF palBufferf = NULL;
210 LPALBUFFER3F palBuffer3f = NULL;
211 LPALBUFFERFV palBufferfv = NULL;
212 LPALBUFFERI palBufferi = NULL;
213 LPALBUFFER3I palBuffer3i = NULL;
214 LPALBUFFERIV palBufferiv = NULL;
215 LPALGETBUFFERF palGetBufferf = NULL;
216 LPALGETBUFFER3F palGetBuffer3f = NULL;
217 LPALGETBUFFERFV palGetBufferfv = NULL;
218 LPALGETBUFFERI palGetBufferi = NULL;
219 LPALGETBUFFER3I palGetBuffer3i = NULL;
220 LPALGETBUFFERIV palGetBufferiv = NULL;
221 LPALBUFFERDATA palBufferData = NULL;
222 LPALDOPPLERFACTOR palDopplerFactor = NULL;
223 LPALDOPPLERVELOCITY palDopplerVelocity = NULL;
224 LPALDISTANCEMODEL palDistanceModel = NULL;
225 LPALSPEEDOFSOUND palSpeedOfSound = NULL;
226 #endif
228 LPALCMAKECONTEXTCURRENT set_context;
229 LPALCGETCURRENTCONTEXT get_context;
230 BOOL local_contexts;
232 static void load_libopenal(void)
234 #ifndef __WINESRC__
235 const char *str = getenv("DSOAL_LOGLEVEL");
236 if(str && *str)
237 LogLevel = atoi(str);
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 LPDSENUMCALLBACKW callW;
453 LPVOID data;
456 static BOOL CALLBACK w_to_a_callback(LPGUID guid, LPCSTR descA, LPCSTR modA, LPVOID data)
458 struct morecontext *context = data;
459 WCHAR descW[MAXPNAMELEN], modW[MAXPNAMELEN];
461 MultiByteToWideChar(CP_ACP, 0, descA, -1, descW, sizeof(descW)/sizeof(descW[0]));
462 MultiByteToWideChar(CP_ACP, 0, modA, -1, modW, sizeof(modW)/sizeof(modW[0]));
464 return context->callW(guid, descW, modW, 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 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
485 lpDSEnumCallback, lpContext);
487 if (lpDSEnumCallback == NULL) {
488 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
489 return DSERR_INVALIDPARAM;
491 else if (openal_loaded)
493 const ALCchar *devstr;
494 GUID guid;
496 EnterCriticalSection(&openal_crst);
497 devstr = DSOUND_getdevicestrings();
498 if(!devstr || !*devstr)
499 goto out;
501 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
502 "Primary Sound Driver","",lpContext);
503 if(lpDSEnumCallback(NULL, "Primary Sound Driver", "", lpContext) == FALSE)
504 goto out;
506 guid = DSOUND_renderer_guid;
507 do {
508 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
509 debugstr_guid(&guid),devstr,"wineal.drv",lpContext);
510 if(lpDSEnumCallback(&guid, devstr, "wineal.drv", lpContext) == FALSE)
511 goto out;
512 guid.Data4[7]++;
513 devstr += strlen(devstr)+1;
514 } while(*devstr);
515 out:
516 LeaveCriticalSection(&openal_crst);
518 else
520 ERR("Attempting to enumerate sound cards without OpenAL support\n");
521 ERR("Please recompile wine with OpenAL for sound to work\n");
523 return DS_OK;
526 /***************************************************************************
527 * DirectSoundEnumerateW [DSOUND.3]
529 * Enumerate all DirectSound drivers installed in the system
531 * PARAMS
532 * lpDSEnumCallback [I] Address of callback function.
533 * lpContext [I] Address of user defined context passed to callback function.
535 * RETURNS
536 * Success: DS_OK
537 * Failure: DSERR_INVALIDPARAM
539 HRESULT WINAPI DirectSoundEnumerateW(
540 LPDSENUMCALLBACKW lpDSEnumCallback,
541 LPVOID lpContext )
543 struct morecontext context;
545 if(lpDSEnumCallback == NULL) {
546 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
547 return DSERR_INVALIDPARAM;
550 context.callW = lpDSEnumCallback;
551 context.data = lpContext;
553 return DirectSoundEnumerateA(w_to_a_callback, &context);
556 /***************************************************************************
557 * DirectSoundCaptureEnumerateA [DSOUND.7]
559 * Enumerate all DirectSound drivers installed in the system.
561 * PARAMS
562 * lpDSEnumCallback [I] Address of callback function.
563 * lpContext [I] Address of user defined context passed to callback function.
565 * RETURNS
566 * Success: DS_OK
567 * Failure: DSERR_INVALIDPARAM
569 HRESULT WINAPI DirectSoundCaptureEnumerateA(
570 LPDSENUMCALLBACKA lpDSEnumCallback,
571 LPVOID lpContext)
573 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
575 if (lpDSEnumCallback == NULL) {
576 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
577 return DSERR_INVALIDPARAM;
579 else if (openal_loaded)
581 const ALCchar *devstr;
582 GUID guid;
584 EnterCriticalSection(&openal_crst);
585 devstr = DSOUND_getcapturedevicestrings();
586 if(!devstr || !*devstr)
587 goto out;
589 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
590 "Primary Sound Capture Driver","",lpContext);
591 if(lpDSEnumCallback(NULL, "Primary Sound Driver", "", lpContext) == FALSE)
592 goto out;
594 guid = DSOUND_capture_guid;
595 do {
596 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
597 debugstr_guid(&guid),devstr,"wineal.drv",lpContext);
598 if(lpDSEnumCallback(&guid, devstr, "wineal.drv", lpContext) == FALSE)
599 goto out;
600 guid.Data4[7]++;
601 devstr += strlen(devstr)+1;
602 } while(*devstr);
603 out:
604 LeaveCriticalSection(&openal_crst);
606 else
608 ERR("Attempting to enumerate sound cards without OpenAL support\n");
609 ERR("Please recompile wine with OpenAL for sound to work\n");
611 return DS_OK;
614 /***************************************************************************
615 * DirectSoundCaptureEnumerateW [DSOUND.8]
617 * Enumerate all DirectSound drivers installed in the system.
619 * PARAMS
620 * lpDSEnumCallback [I] Address of callback function.
621 * lpContext [I] Address of user defined context passed to callback function.
623 * RETURNS
624 * Success: DS_OK
625 * Failure: DSERR_INVALIDPARAM
627 HRESULT WINAPI
628 DirectSoundCaptureEnumerateW(
629 LPDSENUMCALLBACKW lpDSEnumCallback,
630 LPVOID lpContext)
632 struct morecontext context;
634 if (lpDSEnumCallback == NULL) {
635 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
636 return DSERR_INVALIDPARAM;
639 context.callW = lpDSEnumCallback;
640 context.data = lpContext;
642 return DirectSoundCaptureEnumerateA(w_to_a_callback, &context);
645 /*******************************************************************************
646 * DirectSoundCreate (DSOUND.1)
648 * Creates and initializes a DirectSound interface.
650 * PARAMS
651 * lpcGUID [I] Address of the GUID that identifies the sound device.
652 * ppDS [O] Address of a variable to receive the interface pointer.
653 * pUnkOuter [I] Must be NULL.
655 * RETURNS
656 * Success: DS_OK
657 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
658 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
660 HRESULT WINAPI
661 DirectSoundCreate(LPCGUID lpcGUID, IDirectSound **ppDS, IUnknown *pUnkOuter)
663 HRESULT hr;
664 void *pDS;
666 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
668 if (ppDS == NULL) {
669 WARN("invalid parameter: ppDS == NULL\n");
670 return DSERR_INVALIDPARAM;
672 *ppDS = NULL;
674 if (pUnkOuter != NULL) {
675 WARN("invalid parameter: pUnkOuter != NULL\n");
676 return DSERR_INVALIDPARAM;
679 hr = DSOUND_Create(&IID_IDirectSound, &pDS);
680 if(SUCCEEDED(hr))
682 *ppDS = pDS;
683 hr = IDirectSound_Initialize(*ppDS, lpcGUID);
684 if(FAILED(hr))
686 IDirectSound_Release(*ppDS);
687 *ppDS = NULL;
691 return hr;
694 /*******************************************************************************
695 * DirectSoundCreate8 (DSOUND.11)
697 * Creates and initializes a DirectSound8 interface.
699 * PARAMS
700 * lpcGUID [I] Address of the GUID that identifies the sound device.
701 * ppDS [O] Address of a variable to receive the interface pointer.
702 * pUnkOuter [I] Must be NULL.
704 * RETURNS
705 * Success: DS_OK
706 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
707 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
709 HRESULT WINAPI
710 DirectSoundCreate8(LPCGUID lpcGUID, IDirectSound8 **ppDS, IUnknown *pUnkOuter)
712 HRESULT hr;
713 void *pDS;
715 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
717 if (ppDS == NULL) {
718 WARN("invalid parameter: ppDS == NULL\n");
719 return DSERR_INVALIDPARAM;
721 *ppDS = NULL;
723 if (pUnkOuter != NULL) {
724 WARN("invalid parameter: pUnkOuter != NULL\n");
725 return DSERR_INVALIDPARAM;
728 hr = DSOUND_Create8(&IID_IDirectSound8, &pDS);
729 if(SUCCEEDED(hr))
731 *ppDS = pDS;
732 hr = IDirectSound8_Initialize(*ppDS, lpcGUID);
733 if(FAILED(hr))
735 IDirectSound8_Release(*ppDS);
736 *ppDS = NULL;
740 return hr;
743 /***************************************************************************
744 * DirectSoundCaptureCreate [DSOUND.6]
746 * Create and initialize a DirectSoundCapture interface.
748 * PARAMS
749 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
750 * lplpDSC [O] Address of a variable to receive the interface pointer.
751 * pUnkOuter [I] Must be NULL.
753 * RETURNS
754 * Success: DS_OK
755 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
756 * DSERR_OUTOFMEMORY
758 * NOTES
759 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
760 * or NULL for the default device or DSDEVID_DefaultCapture or
761 * DSDEVID_DefaultVoiceCapture.
763 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
765 HRESULT WINAPI
766 DirectSoundCaptureCreate(LPCGUID lpcGUID, IDirectSoundCapture **ppDSC, IUnknown *pUnkOuter)
768 HRESULT hr;
769 void *pDSC;
771 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
773 if(ppDSC == NULL)
775 WARN("invalid parameter: ppDSC == NULL\n");
776 return DSERR_INVALIDPARAM;
778 *ppDSC = NULL;
780 if(pUnkOuter)
782 WARN("invalid parameter: pUnkOuter != NULL\n");
783 return DSERR_NOAGGREGATION;
786 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, &pDSC);
787 if(SUCCEEDED(hr))
789 *ppDSC = pDSC;
790 hr = IDirectSoundCapture_Initialize(*ppDSC, lpcGUID);
791 if(FAILED(hr))
793 IDirectSoundCapture_Release(*ppDSC);
794 *ppDSC = NULL;
798 return hr;
801 /***************************************************************************
802 * DirectSoundCaptureCreate8 [DSOUND.12]
804 * Create and initialize a DirectSoundCapture interface.
806 * PARAMS
807 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
808 * lplpDSC [O] Address of a variable to receive the interface pointer.
809 * pUnkOuter [I] Must be NULL.
811 * RETURNS
812 * Success: DS_OK
813 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
814 * DSERR_OUTOFMEMORY
816 * NOTES
817 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
818 * or NULL for the default device or DSDEVID_DefaultCapture or
819 * DSDEVID_DefaultVoiceCapture.
821 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
823 HRESULT WINAPI
824 DirectSoundCaptureCreate8(LPCGUID lpcGUID, IDirectSoundCapture8 **ppDSC8, IUnknown *pUnkOuter)
826 HRESULT hr;
827 void *pDSC8;
829 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
831 if(ppDSC8 == NULL)
833 WARN("invalid parameter: ppDSC8 == NULL\n");
834 return DSERR_INVALIDPARAM;
836 *ppDSC8 = NULL;
838 if(pUnkOuter)
840 WARN("invalid parameter: pUnkOuter != NULL\n");
841 return DSERR_NOAGGREGATION;
844 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture8, &pDSC8);
845 if(SUCCEEDED(hr))
847 *ppDSC8 = pDSC8;
848 hr = IDirectSoundCapture_Initialize(*ppDSC8, lpcGUID);
849 if(FAILED(hr))
851 IDirectSoundCapture_Release(*ppDSC8);
852 *ppDSC8 = NULL;
856 return hr;
859 /*******************************************************************************
860 * DirectSound ClassFactory
863 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
865 typedef struct {
866 IClassFactory IClassFactory_iface;
867 LONG ref;
868 REFCLSID rclsid;
869 FnCreateInstance pfnCreateInstance;
870 } IClassFactoryImpl;
872 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
874 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
877 static HRESULT WINAPI DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
879 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
880 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
881 if (ppobj == NULL)
882 return E_POINTER;
883 if (IsEqualIID(riid, &IID_IUnknown) ||
884 IsEqualIID(riid, &IID_IClassFactory))
886 *ppobj = iface;
887 IUnknown_AddRef(iface);
888 return S_OK;
890 *ppobj = NULL;
891 return E_NOINTERFACE;
894 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
896 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
897 ULONG ref = InterlockedIncrement(&(This->ref));
898 TRACE("(%p) ref was %"LONGFMT"u\n", This, ref - 1);
899 return ref;
902 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
904 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
905 ULONG ref = InterlockedDecrement(&(This->ref));
906 TRACE("(%p) ref was %"LONGFMT"u\n", This, ref + 1);
907 /* static class, won't be freed */
908 return ref;
911 static HRESULT WINAPI DSCF_CreateInstance(
912 LPCLASSFACTORY iface,
913 LPUNKNOWN pOuter,
914 REFIID riid,
915 LPVOID *ppobj)
917 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
918 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
920 if (pOuter)
921 return CLASS_E_NOAGGREGATION;
923 if (ppobj == NULL) {
924 WARN("invalid parameter\n");
925 return DSERR_INVALIDPARAM;
927 *ppobj = NULL;
928 return This->pfnCreateInstance(riid, ppobj);
931 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
933 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
934 FIXME("(%p, %d) stub!\n", This, dolock);
935 return S_OK;
938 static const IClassFactoryVtbl DSCF_Vtbl = {
939 DSCF_QueryInterface,
940 DSCF_AddRef,
941 DSCF_Release,
942 DSCF_CreateInstance,
943 DSCF_LockServer
946 static IClassFactoryImpl DSOUND_CF[] = {
947 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSound, DSOUND_Create },
948 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSound8, DSOUND_Create8 },
949 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
950 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
951 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
952 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
953 { {NULL}, 0, NULL, NULL }
956 /*******************************************************************************
957 * DllGetClassObject [DSOUND.@]
958 * Retrieves class object from a DLL object
960 * NOTES
961 * Docs say returns STDAPI
963 * PARAMS
964 * rclsid [I] CLSID for the class object
965 * riid [I] Reference to identifier of interface for class object
966 * ppv [O] Address of variable to receive interface pointer for riid
968 * RETURNS
969 * Success: S_OK
970 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
971 * E_UNEXPECTED
973 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
975 int i = 0;
976 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
978 if (ppv == NULL) {
979 WARN("invalid parameter\n");
980 return E_INVALIDARG;
983 *ppv = NULL;
985 if (!IsEqualIID(riid, &IID_IClassFactory) &&
986 !IsEqualIID(riid, &IID_IUnknown)) {
987 WARN("no interface for %s\n", debugstr_guid(riid));
988 return E_NOINTERFACE;
991 while (NULL != DSOUND_CF[i].rclsid) {
992 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
993 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
994 *ppv = &DSOUND_CF[i];
995 return S_OK;
997 i++;
1000 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
1001 debugstr_guid(riid), ppv);
1002 return CLASS_E_CLASSNOTAVAILABLE;
1006 /*******************************************************************************
1007 * DllCanUnloadNow [DSOUND.4]
1008 * Determines whether the DLL is in use.
1010 * RETURNS
1011 * Success: S_OK
1012 * Failure: S_FALSE
1014 HRESULT WINAPI DllCanUnloadNow(void)
1016 FIXME("(void): stub\n");
1017 return S_FALSE;
1020 /***********************************************************************
1021 * DllMain (DSOUND.init)
1023 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
1025 TRACE("(%p, %"LONGFMT"u, %p)\n", hInstDLL, fdwReason, lpvReserved);
1027 switch(fdwReason)
1029 case DLL_PROCESS_ATTACH:
1030 TRACE("DLL_PROCESS_ATTACH\n");
1031 instance = hInstDLL;
1032 load_libopenal();
1033 DisableThreadLibraryCalls(hInstDLL);
1034 /* Increase refcount on dsound by 1 */
1035 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
1036 break;
1037 case DLL_PROCESS_DETACH:
1038 TRACE("DLL_PROCESS_DETACH\n");
1039 #ifdef SONAME_LIBOPENAL
1040 if(openal_handle)
1041 wine_dlclose(openal_handle, NULL, 0);
1042 #endif /*SONAME_LIBOPENAL*/
1043 break;
1044 default:
1045 TRACE("UNKNOWN REASON\n");
1046 break;
1048 return TRUE;
1051 #ifdef __WINESRC__
1052 /***********************************************************************
1053 * DllRegisterServer (DSOUND.@)
1055 HRESULT WINAPI DllRegisterServer(void)
1057 return __wine_register_resources(instance);
1060 /***********************************************************************
1061 * DllUnregisterServer (DSOUND.@)
1063 HRESULT WINAPI DllUnregisterServer(void)
1065 return __wine_unregister_resources(instance);
1067 #endif