Add and use EAX_MAX_ENVIRONMENT
[dsound-openal.git] / dsound_main.c
blob8e244b8f6cc66d060c88275de5ae276d7806c3ff
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 if(alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT"))
379 return alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER);
380 return alcGetString(NULL, ALC_DEVICE_SPECIFIER);
383 const ALCchar *DSOUND_getcapturedevicestrings(void)
385 return alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
388 /***************************************************************************
389 * GetDeviceID [DSOUND.9]
391 * Retrieves unique identifier of default device specified
393 * PARAMS
394 * pGuidSrc [I] Address of device GUID.
395 * pGuidDest [O] Address to receive unique device GUID.
397 * RETURNS
398 * Success: DS_OK
399 * Failure: DSERR_INVALIDPARAM
401 * NOTES
402 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
403 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
404 * DSDEVID_DefaultVoiceCapture.
405 * Returns pGuidSrc if pGuidSrc is a valid device or the device
406 * GUID for the specified constants.
408 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
410 TRACE("(%s, %p)\n", debugstr_guid(pGuidSrc), pGuidDest);
412 if(pGuidSrc == NULL)
414 WARN("invalid parameter: pGuidSrc == NULL\n");
415 return DSERR_INVALIDPARAM;
418 if(pGuidDest == NULL)
420 WARN("invalid parameter: pGuidDest == NULL\n");
421 return DSERR_INVALIDPARAM;
424 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuidSrc) ||
425 IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuidSrc))
426 *pGuidDest = DSOUND_renderer_guid;
427 else if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuidSrc) ||
428 IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuidSrc))
429 *pGuidDest = DSOUND_capture_guid;
430 else
431 *pGuidDest = *pGuidSrc;
433 TRACE("returns %s\n", debugstr_guid(pGuidDest));
434 return DS_OK;
437 struct morecontext
439 LPDSENUMCALLBACKW callW;
440 LPVOID data;
443 static BOOL CALLBACK w_to_a_callback(LPGUID guid, LPCSTR descA, LPCSTR modA, LPVOID data)
445 struct morecontext *context = data;
446 WCHAR descW[MAXPNAMELEN], modW[MAXPNAMELEN];
448 MultiByteToWideChar(CP_ACP, 0, descA, -1, descW, sizeof(descW)/sizeof(descW[0]));
449 MultiByteToWideChar(CP_ACP, 0, modA, -1, modW, sizeof(modW)/sizeof(modW[0]));
451 return context->callW(guid, descW, modW, context->data);
454 /***************************************************************************
455 * DirectSoundEnumerateA [DSOUND.2]
457 * Enumerate all DirectSound drivers installed in the system
459 * PARAMS
460 * lpDSEnumCallback [I] Address of callback function.
461 * lpContext [I] Address of user defined context passed to callback function.
463 * RETURNS
464 * Success: DS_OK
465 * Failure: DSERR_INVALIDPARAM
467 HRESULT WINAPI DirectSoundEnumerateA(
468 LPDSENUMCALLBACKA lpDSEnumCallback,
469 LPVOID lpContext)
471 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
472 lpDSEnumCallback, lpContext);
474 if (lpDSEnumCallback == NULL) {
475 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
476 return DSERR_INVALIDPARAM;
478 else if (openal_loaded)
480 const ALCchar *devstr;
481 GUID guid;
483 EnterCriticalSection(&openal_crst);
484 devstr = DSOUND_getdevicestrings();
485 if(!devstr || !*devstr)
486 goto out;
488 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
489 "Primary Sound Driver","",lpContext);
490 if(lpDSEnumCallback(NULL, "Primary Sound Driver", "", lpContext) == FALSE)
491 goto out;
493 guid = DSOUND_renderer_guid;
494 do {
495 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
496 debugstr_guid(&guid),devstr,"wineal.drv",lpContext);
497 if(lpDSEnumCallback(&guid, devstr, "wineal.drv", lpContext) == FALSE)
498 goto out;
499 guid.Data4[7]++;
500 devstr += strlen(devstr)+1;
501 } while(*devstr);
502 out:
503 LeaveCriticalSection(&openal_crst);
505 else
507 ERR("Attempting to enumerate sound cards without OpenAL support\n");
508 ERR("Please recompile wine with OpenAL for sound to work\n");
510 return DS_OK;
513 /***************************************************************************
514 * DirectSoundEnumerateW [DSOUND.3]
516 * Enumerate all DirectSound drivers installed in the system
518 * PARAMS
519 * lpDSEnumCallback [I] Address of callback function.
520 * lpContext [I] Address of user defined context passed to callback function.
522 * RETURNS
523 * Success: DS_OK
524 * Failure: DSERR_INVALIDPARAM
526 HRESULT WINAPI DirectSoundEnumerateW(
527 LPDSENUMCALLBACKW lpDSEnumCallback,
528 LPVOID lpContext )
530 struct morecontext context;
532 if(lpDSEnumCallback == NULL) {
533 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
534 return DSERR_INVALIDPARAM;
537 context.callW = lpDSEnumCallback;
538 context.data = lpContext;
540 return DirectSoundEnumerateA(w_to_a_callback, &context);
543 /***************************************************************************
544 * DirectSoundCaptureEnumerateA [DSOUND.7]
546 * Enumerate all DirectSound drivers installed in the system.
548 * PARAMS
549 * lpDSEnumCallback [I] Address of callback function.
550 * lpContext [I] Address of user defined context passed to callback function.
552 * RETURNS
553 * Success: DS_OK
554 * Failure: DSERR_INVALIDPARAM
556 HRESULT WINAPI DirectSoundCaptureEnumerateA(
557 LPDSENUMCALLBACKA lpDSEnumCallback,
558 LPVOID lpContext)
560 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
562 if (lpDSEnumCallback == NULL) {
563 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
564 return DSERR_INVALIDPARAM;
566 else if (openal_loaded)
568 const ALCchar *devstr;
569 GUID guid;
571 EnterCriticalSection(&openal_crst);
572 devstr = DSOUND_getcapturedevicestrings();
573 if(!devstr || !*devstr)
574 goto out;
576 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
577 "Primary Sound Capture Driver","",lpContext);
578 if(lpDSEnumCallback(NULL, "Primary Sound Driver", "", lpContext) == FALSE)
579 goto out;
581 guid = DSOUND_capture_guid;
582 do {
583 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
584 debugstr_guid(&guid),devstr,"wineal.drv",lpContext);
585 if(lpDSEnumCallback(&guid, devstr, "wineal.drv", lpContext) == FALSE)
586 goto out;
587 guid.Data4[7]++;
588 devstr += strlen(devstr)+1;
589 } while(*devstr);
590 out:
591 LeaveCriticalSection(&openal_crst);
593 else
595 ERR("Attempting to enumerate sound cards without OpenAL support\n");
596 ERR("Please recompile wine with OpenAL for sound to work\n");
598 return DS_OK;
601 /***************************************************************************
602 * DirectSoundCaptureEnumerateW [DSOUND.8]
604 * Enumerate all DirectSound drivers installed in the system.
606 * PARAMS
607 * lpDSEnumCallback [I] Address of callback function.
608 * lpContext [I] Address of user defined context passed to callback function.
610 * RETURNS
611 * Success: DS_OK
612 * Failure: DSERR_INVALIDPARAM
614 HRESULT WINAPI
615 DirectSoundCaptureEnumerateW(
616 LPDSENUMCALLBACKW lpDSEnumCallback,
617 LPVOID lpContext)
619 struct morecontext context;
621 if (lpDSEnumCallback == NULL) {
622 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
623 return DSERR_INVALIDPARAM;
626 context.callW = lpDSEnumCallback;
627 context.data = lpContext;
629 return DirectSoundCaptureEnumerateA(w_to_a_callback, &context);
632 /*******************************************************************************
633 * DirectSoundCreate (DSOUND.1)
635 * Creates and initializes a DirectSound interface.
637 * PARAMS
638 * lpcGUID [I] Address of the GUID that identifies the sound device.
639 * ppDS [O] Address of a variable to receive the interface pointer.
640 * pUnkOuter [I] Must be NULL.
642 * RETURNS
643 * Success: DS_OK
644 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
645 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
647 HRESULT WINAPI
648 DirectSoundCreate(LPCGUID lpcGUID, IDirectSound **ppDS, IUnknown *pUnkOuter)
650 HRESULT hr;
651 void *pDS;
653 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
655 if (ppDS == NULL) {
656 WARN("invalid parameter: ppDS == NULL\n");
657 return DSERR_INVALIDPARAM;
659 *ppDS = NULL;
661 if (pUnkOuter != NULL) {
662 WARN("invalid parameter: pUnkOuter != NULL\n");
663 return DSERR_INVALIDPARAM;
666 hr = DSOUND_Create(&IID_IDirectSound, &pDS);
667 if(SUCCEEDED(hr))
669 *ppDS = pDS;
670 hr = IDirectSound_Initialize(*ppDS, lpcGUID);
671 if(FAILED(hr))
673 IDirectSound_Release(*ppDS);
674 *ppDS = NULL;
678 return hr;
681 /*******************************************************************************
682 * DirectSoundCreate8 (DSOUND.11)
684 * Creates and initializes a DirectSound8 interface.
686 * PARAMS
687 * lpcGUID [I] Address of the GUID that identifies the sound device.
688 * ppDS [O] Address of a variable to receive the interface pointer.
689 * pUnkOuter [I] Must be NULL.
691 * RETURNS
692 * Success: DS_OK
693 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
694 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
696 HRESULT WINAPI
697 DirectSoundCreate8(LPCGUID lpcGUID, IDirectSound8 **ppDS, IUnknown *pUnkOuter)
699 HRESULT hr;
700 void *pDS;
702 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDS, pUnkOuter);
704 if (ppDS == NULL) {
705 WARN("invalid parameter: ppDS == NULL\n");
706 return DSERR_INVALIDPARAM;
708 *ppDS = NULL;
710 if (pUnkOuter != NULL) {
711 WARN("invalid parameter: pUnkOuter != NULL\n");
712 return DSERR_INVALIDPARAM;
715 hr = DSOUND_Create8(&IID_IDirectSound8, &pDS);
716 if(SUCCEEDED(hr))
718 *ppDS = pDS;
719 hr = IDirectSound8_Initialize(*ppDS, lpcGUID);
720 if(FAILED(hr))
722 IDirectSound8_Release(*ppDS);
723 *ppDS = NULL;
727 return hr;
730 /***************************************************************************
731 * DirectSoundCaptureCreate [DSOUND.6]
733 * Create and initialize a DirectSoundCapture interface.
735 * PARAMS
736 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
737 * lplpDSC [O] Address of a variable to receive the interface pointer.
738 * pUnkOuter [I] Must be NULL.
740 * RETURNS
741 * Success: DS_OK
742 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
743 * DSERR_OUTOFMEMORY
745 * NOTES
746 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
747 * or NULL for the default device or DSDEVID_DefaultCapture or
748 * DSDEVID_DefaultVoiceCapture.
750 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
752 HRESULT WINAPI
753 DirectSoundCaptureCreate(LPCGUID lpcGUID, IDirectSoundCapture **ppDSC, IUnknown *pUnkOuter)
755 HRESULT hr;
756 void *pDSC;
758 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
760 if(ppDSC == NULL)
762 WARN("invalid parameter: ppDSC == NULL\n");
763 return DSERR_INVALIDPARAM;
765 *ppDSC = NULL;
767 if(pUnkOuter)
769 WARN("invalid parameter: pUnkOuter != NULL\n");
770 return DSERR_NOAGGREGATION;
773 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, &pDSC);
774 if(SUCCEEDED(hr))
776 *ppDSC = pDSC;
777 hr = IDirectSoundCapture_Initialize(*ppDSC, lpcGUID);
778 if(FAILED(hr))
780 IDirectSoundCapture_Release(*ppDSC);
781 *ppDSC = NULL;
785 return hr;
788 /***************************************************************************
789 * DirectSoundCaptureCreate8 [DSOUND.12]
791 * Create and initialize a DirectSoundCapture interface.
793 * PARAMS
794 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
795 * lplpDSC [O] Address of a variable to receive the interface pointer.
796 * pUnkOuter [I] Must be NULL.
798 * RETURNS
799 * Success: DS_OK
800 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
801 * DSERR_OUTOFMEMORY
803 * NOTES
804 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
805 * or NULL for the default device or DSDEVID_DefaultCapture or
806 * DSDEVID_DefaultVoiceCapture.
808 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
810 HRESULT WINAPI
811 DirectSoundCaptureCreate8(LPCGUID lpcGUID, IDirectSoundCapture8 **ppDSC8, IUnknown *pUnkOuter)
813 HRESULT hr;
814 void *pDSC8;
816 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
818 if(ppDSC8 == NULL)
820 WARN("invalid parameter: ppDSC8 == NULL\n");
821 return DSERR_INVALIDPARAM;
823 *ppDSC8 = NULL;
825 if(pUnkOuter)
827 WARN("invalid parameter: pUnkOuter != NULL\n");
828 return DSERR_NOAGGREGATION;
831 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture, &pDSC8);
832 if(SUCCEEDED(hr))
834 *ppDSC8 = pDSC8;
835 hr = IDirectSoundCapture_Initialize(*ppDSC8, lpcGUID);
836 if(FAILED(hr))
838 IDirectSoundCapture_Release(*ppDSC8);
839 *ppDSC8 = NULL;
843 return hr;
846 /*******************************************************************************
847 * DirectSound ClassFactory
850 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
852 typedef struct {
853 IClassFactory IClassFactory_iface;
854 LONG ref;
855 REFCLSID rclsid;
856 FnCreateInstance pfnCreateInstance;
857 } IClassFactoryImpl;
859 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
861 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
864 static HRESULT WINAPI DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
866 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
867 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
868 if (ppobj == NULL)
869 return E_POINTER;
870 if (IsEqualIID(riid, &IID_IUnknown) ||
871 IsEqualIID(riid, &IID_IClassFactory))
873 *ppobj = iface;
874 IUnknown_AddRef(iface);
875 return S_OK;
877 *ppobj = NULL;
878 return E_NOINTERFACE;
881 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
883 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
884 ULONG ref = InterlockedIncrement(&(This->ref));
885 TRACE("(%p) ref was %"LONGFMT"u\n", This, ref - 1);
886 return ref;
889 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
891 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
892 ULONG ref = InterlockedDecrement(&(This->ref));
893 TRACE("(%p) ref was %"LONGFMT"u\n", This, ref + 1);
894 /* static class, won't be freed */
895 return ref;
898 static HRESULT WINAPI DSCF_CreateInstance(
899 LPCLASSFACTORY iface,
900 LPUNKNOWN pOuter,
901 REFIID riid,
902 LPVOID *ppobj)
904 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
905 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
907 if (pOuter)
908 return CLASS_E_NOAGGREGATION;
910 if (ppobj == NULL) {
911 WARN("invalid parameter\n");
912 return DSERR_INVALIDPARAM;
914 *ppobj = NULL;
915 return This->pfnCreateInstance(riid, ppobj);
918 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
920 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
921 FIXME("(%p, %d) stub!\n", This, dolock);
922 return S_OK;
925 static const IClassFactoryVtbl DSCF_Vtbl = {
926 DSCF_QueryInterface,
927 DSCF_AddRef,
928 DSCF_Release,
929 DSCF_CreateInstance,
930 DSCF_LockServer
933 static IClassFactoryImpl DSOUND_CF[] = {
934 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSound, DSOUND_Create },
935 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSound8, DSOUND_Create8 },
936 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
937 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
938 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
939 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
940 { {NULL}, 0, NULL, NULL }
943 /*******************************************************************************
944 * DllGetClassObject [DSOUND.@]
945 * Retrieves class object from a DLL object
947 * NOTES
948 * Docs say returns STDAPI
950 * PARAMS
951 * rclsid [I] CLSID for the class object
952 * riid [I] Reference to identifier of interface for class object
953 * ppv [O] Address of variable to receive interface pointer for riid
955 * RETURNS
956 * Success: S_OK
957 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
958 * E_UNEXPECTED
960 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
962 int i = 0;
963 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
965 if (ppv == NULL) {
966 WARN("invalid parameter\n");
967 return E_INVALIDARG;
970 *ppv = NULL;
972 if (!IsEqualIID(riid, &IID_IClassFactory) &&
973 !IsEqualIID(riid, &IID_IUnknown)) {
974 WARN("no interface for %s\n", debugstr_guid(riid));
975 return E_NOINTERFACE;
978 while (NULL != DSOUND_CF[i].rclsid) {
979 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
980 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
981 *ppv = &DSOUND_CF[i];
982 return S_OK;
984 i++;
987 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
988 debugstr_guid(riid), ppv);
989 return CLASS_E_CLASSNOTAVAILABLE;
993 /*******************************************************************************
994 * DllCanUnloadNow [DSOUND.4]
995 * Determines whether the DLL is in use.
997 * RETURNS
998 * Success: S_OK
999 * Failure: S_FALSE
1001 HRESULT WINAPI DllCanUnloadNow(void)
1003 FIXME("(void): stub\n");
1004 return S_FALSE;
1007 /***********************************************************************
1008 * DllMain (DSOUND.init)
1010 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
1012 TRACE("(%p, %"LONGFMT"u, %p)\n", hInstDLL, fdwReason, lpvReserved);
1014 switch(fdwReason)
1016 case DLL_PROCESS_ATTACH:
1017 TRACE("DLL_PROCESS_ATTACH\n");
1018 instance = hInstDLL;
1019 load_libopenal();
1020 DisableThreadLibraryCalls(hInstDLL);
1021 /* Increase refcount on dsound by 1 */
1022 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
1023 break;
1024 case DLL_PROCESS_DETACH:
1025 TRACE("DLL_PROCESS_DETACH\n");
1026 #ifdef SONAME_LIBOPENAL
1027 if(openal_handle)
1028 wine_dlclose(openal_handle, NULL, 0);
1029 #endif /*SONAME_LIBOPENAL*/
1030 break;
1031 default:
1032 TRACE("UNKNOWN REASON\n");
1033 break;
1035 return TRUE;
1038 #ifdef __WINESRC__
1039 /***********************************************************************
1040 * DllRegisterServer (DSOUND.@)
1042 HRESULT WINAPI DllRegisterServer(void)
1044 return __wine_register_resources(instance);
1047 /***********************************************************************
1048 * DllUnregisterServer (DSOUND.@)
1050 HRESULT WINAPI DllUnregisterServer(void)
1052 return __wine_unregister_resources(instance);
1054 #endif