Avoid the mutex for the Lock and Unlock methods
[dsound-openal.git] / dsound_main.c
blob0007baf1657dd341d2a230245fdf7f81a4187509
1 /* DirectSound
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 * Most thread locking is complete. There may be a few race
22 * conditions still lurking.
24 * TODO:
25 * Implement SetCooperativeLevel properly (need to address focus issues)
26 * Implement DirectSound3DBuffers (stubs in place)
27 * Use hardware 3D support if available
28 * Add critical section locking inside Release and AddRef methods
29 * Handle static buffers - put those in hardware, non-static not in hardware
30 * Hardware DuplicateSoundBuffer
31 * Proper volume calculation for 3d buffers
32 * Remove DS_HEL_FRAGS and use mixer fragment length for it
35 #include <stdarg.h>
37 #ifdef __WINESRC__
39 #include "wine/library.h"
41 #define COBJMACROS
42 #define NONAMELESSSTRUCT
43 #define NONAMELESSUNION
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winuser.h"
47 #include "winnls.h"
48 #include "winreg.h"
49 #include "mmsystem.h"
50 #include "winternl.h"
51 #include "mmddk.h"
52 #include "wine/debug.h"
53 #include "dsound.h"
54 #include "dsconf.h"
55 #include "ks.h"
56 #include "initguid.h"
57 #include "ksmedia.h"
58 #include "rpcproxy.h"
60 #include "dsound_private.h"
62 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
64 #else
66 #define WINVER 0x0600
67 #include <windows.h>
68 #include <dsound.h>
70 #include "dsound_private.h"
72 #ifndef CP_UNIXCP
73 #define CP_UNIXCP 65010
74 #endif
76 DEFINE_GUID(CLSID_DirectSoundPrivate,0x11ab3ec0,0x25ec,0x11d1,0xa4,0xd8,0x00,0xc0,0x4f,0xc2,0x8a,0xca);
78 int LogLevel = 1;
80 /* From dlls/winecrt0/register.c */
81 static HRESULT __wine_register_resources(HMODULE module, const CLSID *clsid)
83 (void)module;
84 (void)clsid;
85 return E_FAIL;
87 static HRESULT __wine_unregister_resources(HMODULE module, const CLSID *clsid)
89 (void)module;
90 (void)clsid;
91 return E_FAIL;
93 #endif
95 static HINSTANCE instance;
97 const GUID DSOUND_renderer_guid = { 0xbd6dd71a, 0x3deb, 0x11d1, { 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 } };
98 const GUID DSOUND_capture_guid = { 0xbd6dd71b, 0x3deb, 0x11d1, { 0xb1, 0x71, 0x00, 0xc0, 0x4f, 0xc2, 0x00, 0x00 } };
100 const IID DSPROPSETID_EAX20_ListenerProperties = {
101 0x0306a6a8, 0xb224, 0x11d2, { 0x99, 0xe5, 0x00, 0x00, 0xe8, 0xd8, 0xc7, 0x22 }
104 const EAXLISTENERPROPERTIES EnvironmentDefaults[EAX_ENVIRONMENT_COUNT] =
106 REVERB_PRESET_GENERIC,
107 REVERB_PRESET_PADDEDCELL,
108 REVERB_PRESET_ROOM,
109 REVERB_PRESET_BATHROOM,
110 REVERB_PRESET_LIVINGROOM,
111 REVERB_PRESET_STONEROOM,
112 REVERB_PRESET_AUDITORIUM,
113 REVERB_PRESET_CONCERTHALL,
114 REVERB_PRESET_CAVE,
115 REVERB_PRESET_ARENA,
116 REVERB_PRESET_HANGAR,
117 REVERB_PRESET_CARPETEDHALLWAY,
118 REVERB_PRESET_HALLWAY,
119 REVERB_PRESET_STONECORRIDOR,
120 REVERB_PRESET_ALLEY,
121 REVERB_PRESET_FOREST,
122 REVERB_PRESET_CITY,
123 REVERB_PRESET_MOUNTAINS,
124 REVERB_PRESET_QUARRY,
125 REVERB_PRESET_PLAIN,
126 REVERB_PRESET_PARKINGLOT,
127 REVERB_PRESET_SEWERPIPE,
128 REVERB_PRESET_UNDERWATER,
129 REVERB_PRESET_DRUGGED,
130 REVERB_PRESET_DIZZY,
131 REVERB_PRESET_PSYCHOTIC
134 static CRITICAL_SECTION_DEBUG openal_crst_debug =
136 0, 0, &openal_crst,
137 { &openal_crst_debug.ProcessLocksList,
138 &openal_crst_debug.ProcessLocksList },
139 0, 0, { (DWORD_PTR)(__FILE__ ": openal_crst_debug") }
141 CRITICAL_SECTION openal_crst = { &openal_crst_debug, -1, 0, 0, 0, 0 };
143 int openal_loaded = 0;
144 #ifdef SONAME_LIBOPENAL
145 static void *openal_handle = NULL;
146 LPALCCREATECONTEXT palcCreateContext = NULL;
147 LPALCMAKECONTEXTCURRENT palcMakeContextCurrent = NULL;
148 LPALCPROCESSCONTEXT palcProcessContext = NULL;
149 LPALCSUSPENDCONTEXT palcSuspendContext = NULL;
150 LPALCDESTROYCONTEXT palcDestroyContext = NULL;
151 LPALCGETCURRENTCONTEXT palcGetCurrentContext = NULL;
152 LPALCGETCONTEXTSDEVICE palcGetContextsDevice = NULL;
153 LPALCOPENDEVICE palcOpenDevice = NULL;
154 LPALCCLOSEDEVICE palcCloseDevice = NULL;
155 LPALCGETERROR palcGetError = NULL;
156 LPALCISEXTENSIONPRESENT palcIsExtensionPresent = NULL;
157 LPALCGETPROCADDRESS palcGetProcAddress = NULL;
158 LPALCGETENUMVALUE palcGetEnumValue = NULL;
159 LPALCGETSTRING palcGetString = NULL;
160 LPALCGETINTEGERV palcGetIntegerv = NULL;
161 LPALCCAPTUREOPENDEVICE palcCaptureOpenDevice = NULL;
162 LPALCCAPTURECLOSEDEVICE palcCaptureCloseDevice = NULL;
163 LPALCCAPTURESTART palcCaptureStart = NULL;
164 LPALCCAPTURESTOP palcCaptureStop = NULL;
165 LPALCCAPTURESAMPLES palcCaptureSamples = NULL;
166 LPALENABLE palEnable = NULL;
167 LPALDISABLE palDisable = NULL;
168 LPALISENABLED palIsEnabled = NULL;
169 LPALGETSTRING palGetString = NULL;
170 LPALGETBOOLEANV palGetBooleanv = NULL;
171 LPALGETINTEGERV palGetIntegerv = NULL;
172 LPALGETFLOATV palGetFloatv = NULL;
173 LPALGETDOUBLEV palGetDoublev = NULL;
174 LPALGETBOOLEAN palGetBoolean = NULL;
175 LPALGETINTEGER palGetInteger = NULL;
176 LPALGETFLOAT palGetFloat = NULL;
177 LPALGETDOUBLE palGetDouble = NULL;
178 LPALGETERROR palGetError = NULL;
179 LPALISEXTENSIONPRESENT palIsExtensionPresent = NULL;
180 LPALGETPROCADDRESS palGetProcAddress = NULL;
181 LPALGETENUMVALUE palGetEnumValue = NULL;
182 LPALLISTENERF palListenerf = NULL;
183 LPALLISTENER3F palListener3f = NULL;
184 LPALLISTENERFV palListenerfv = NULL;
185 LPALLISTENERI palListeneri = NULL;
186 LPALLISTENER3I palListener3i = NULL;
187 LPALLISTENERIV palListeneriv = NULL;
188 LPALGETLISTENERF palGetListenerf = NULL;
189 LPALGETLISTENER3F palGetListener3f = NULL;
190 LPALGETLISTENERFV palGetListenerfv = NULL;
191 LPALGETLISTENERI palGetListeneri = NULL;
192 LPALGETLISTENER3I palGetListener3i = NULL;
193 LPALGETLISTENERIV palGetListeneriv = NULL;
194 LPALGENSOURCES palGenSources = NULL;
195 LPALDELETESOURCES palDeleteSources = NULL;
196 LPALISSOURCE palIsSource = NULL;
197 LPALSOURCEF palSourcef = NULL;
198 LPALSOURCE3F palSource3f = NULL;
199 LPALSOURCEFV palSourcefv = NULL;
200 LPALSOURCEI palSourcei = NULL;
201 LPALSOURCE3I palSource3i = NULL;
202 LPALSOURCEIV palSourceiv = NULL;
203 LPALGETSOURCEF palGetSourcef = NULL;
204 LPALGETSOURCE3F palGetSource3f = NULL;
205 LPALGETSOURCEFV palGetSourcefv = NULL;
206 LPALGETSOURCEI palGetSourcei = NULL;
207 LPALGETSOURCE3I palGetSource3i = NULL;
208 LPALGETSOURCEIV palGetSourceiv = NULL;
209 LPALSOURCEPLAYV palSourcePlayv = NULL;
210 LPALSOURCESTOPV palSourceStopv = NULL;
211 LPALSOURCEREWINDV palSourceRewindv = NULL;
212 LPALSOURCEPAUSEV palSourcePausev = NULL;
213 LPALSOURCEPLAY palSourcePlay = NULL;
214 LPALSOURCESTOP palSourceStop = NULL;
215 LPALSOURCEREWIND palSourceRewind = NULL;
216 LPALSOURCEPAUSE palSourcePause = NULL;
217 LPALSOURCEQUEUEBUFFERS palSourceQueueBuffers = NULL;
218 LPALSOURCEUNQUEUEBUFFERS palSourceUnqueueBuffers = NULL;
219 LPALGENBUFFERS palGenBuffers = NULL;
220 LPALDELETEBUFFERS palDeleteBuffers = NULL;
221 LPALISBUFFER palIsBuffer = NULL;
222 LPALBUFFERF palBufferf = NULL;
223 LPALBUFFER3F palBuffer3f = NULL;
224 LPALBUFFERFV palBufferfv = NULL;
225 LPALBUFFERI palBufferi = NULL;
226 LPALBUFFER3I palBuffer3i = NULL;
227 LPALBUFFERIV palBufferiv = NULL;
228 LPALGETBUFFERF palGetBufferf = NULL;
229 LPALGETBUFFER3F palGetBuffer3f = NULL;
230 LPALGETBUFFERFV palGetBufferfv = NULL;
231 LPALGETBUFFERI palGetBufferi = NULL;
232 LPALGETBUFFER3I palGetBuffer3i = NULL;
233 LPALGETBUFFERIV palGetBufferiv = NULL;
234 LPALBUFFERDATA palBufferData = NULL;
235 LPALDOPPLERFACTOR palDopplerFactor = NULL;
236 LPALDOPPLERVELOCITY palDopplerVelocity = NULL;
237 LPALDISTANCEMODEL palDistanceModel = NULL;
238 LPALSPEEDOFSOUND palSpeedOfSound = NULL;
239 #endif
241 LPALCMAKECONTEXTCURRENT set_context;
242 LPALCGETCURRENTCONTEXT get_context;
243 BOOL local_contexts;
245 static void load_libopenal(void)
247 #ifndef __WINESRC__
248 const char *str = getenv("DSOAL_LOGLEVEL");
249 if(str && *str)
250 LogLevel = atoi(str);
251 #elif defined(SONAME_LIBOPENAL)
252 BOOL failed = FALSE;
253 char error[128];
255 openal_handle = wine_dlopen(SONAME_LIBOPENAL, RTLD_NOW, error, sizeof(error));
256 if(!openal_handle)
257 ERR("Couldn't load " SONAME_LIBOPENAL ": %s\n", error);
258 #define LOAD_FUNCPTR(f) \
259 if((p##f = wine_dlsym(openal_handle, #f, NULL, 0)) == NULL) { \
260 ERR("Couldn't lookup %s in " SONAME_LIBOPENAL "\n", #f); \
261 failed = TRUE; \
264 LOAD_FUNCPTR(alcCreateContext);
265 LOAD_FUNCPTR(alcMakeContextCurrent);
266 LOAD_FUNCPTR(alcProcessContext);
267 LOAD_FUNCPTR(alcSuspendContext);
268 LOAD_FUNCPTR(alcDestroyContext);
269 LOAD_FUNCPTR(alcGetCurrentContext);
270 LOAD_FUNCPTR(alcGetContextsDevice);
271 LOAD_FUNCPTR(alcOpenDevice);
272 LOAD_FUNCPTR(alcCloseDevice);
273 LOAD_FUNCPTR(alcGetError);
274 LOAD_FUNCPTR(alcIsExtensionPresent);
275 LOAD_FUNCPTR(alcGetProcAddress);
276 LOAD_FUNCPTR(alcGetEnumValue);
277 LOAD_FUNCPTR(alcGetString);
278 LOAD_FUNCPTR(alcGetIntegerv);
279 LOAD_FUNCPTR(alcCaptureOpenDevice);
280 LOAD_FUNCPTR(alcCaptureCloseDevice);
281 LOAD_FUNCPTR(alcCaptureStart);
282 LOAD_FUNCPTR(alcCaptureStop);
283 LOAD_FUNCPTR(alcCaptureSamples);
284 LOAD_FUNCPTR(alEnable);
285 LOAD_FUNCPTR(alDisable);
286 LOAD_FUNCPTR(alIsEnabled);
287 LOAD_FUNCPTR(alGetString);
288 LOAD_FUNCPTR(alGetBooleanv);
289 LOAD_FUNCPTR(alGetIntegerv);
290 LOAD_FUNCPTR(alGetFloatv);
291 LOAD_FUNCPTR(alGetDoublev);
292 LOAD_FUNCPTR(alGetBoolean);
293 LOAD_FUNCPTR(alGetInteger);
294 LOAD_FUNCPTR(alGetFloat);
295 LOAD_FUNCPTR(alGetDouble);
296 LOAD_FUNCPTR(alGetError);
297 LOAD_FUNCPTR(alIsExtensionPresent);
298 LOAD_FUNCPTR(alGetProcAddress);
299 LOAD_FUNCPTR(alGetEnumValue);
300 LOAD_FUNCPTR(alListenerf);
301 LOAD_FUNCPTR(alListener3f);
302 LOAD_FUNCPTR(alListenerfv);
303 LOAD_FUNCPTR(alListeneri);
304 LOAD_FUNCPTR(alListener3i);
305 LOAD_FUNCPTR(alListeneriv);
306 LOAD_FUNCPTR(alGetListenerf);
307 LOAD_FUNCPTR(alGetListener3f);
308 LOAD_FUNCPTR(alGetListenerfv);
309 LOAD_FUNCPTR(alGetListeneri);
310 LOAD_FUNCPTR(alGetListener3i);
311 LOAD_FUNCPTR(alGetListeneriv);
312 LOAD_FUNCPTR(alGenSources);
313 LOAD_FUNCPTR(alDeleteSources);
314 LOAD_FUNCPTR(alIsSource);
315 LOAD_FUNCPTR(alSourcef);
316 LOAD_FUNCPTR(alSource3f);
317 LOAD_FUNCPTR(alSourcefv);
318 LOAD_FUNCPTR(alSourcei);
319 LOAD_FUNCPTR(alSource3i);
320 LOAD_FUNCPTR(alSourceiv);
321 LOAD_FUNCPTR(alGetSourcef);
322 LOAD_FUNCPTR(alGetSource3f);
323 LOAD_FUNCPTR(alGetSourcefv);
324 LOAD_FUNCPTR(alGetSourcei);
325 LOAD_FUNCPTR(alGetSource3i);
326 LOAD_FUNCPTR(alGetSourceiv);
327 LOAD_FUNCPTR(alSourcePlayv);
328 LOAD_FUNCPTR(alSourceStopv);
329 LOAD_FUNCPTR(alSourceRewindv);
330 LOAD_FUNCPTR(alSourcePausev);
331 LOAD_FUNCPTR(alSourcePlay);
332 LOAD_FUNCPTR(alSourceStop);
333 LOAD_FUNCPTR(alSourceRewind);
334 LOAD_FUNCPTR(alSourcePause);
335 LOAD_FUNCPTR(alSourceQueueBuffers);
336 LOAD_FUNCPTR(alSourceUnqueueBuffers);
337 LOAD_FUNCPTR(alGenBuffers);
338 LOAD_FUNCPTR(alDeleteBuffers);
339 LOAD_FUNCPTR(alIsBuffer);
340 LOAD_FUNCPTR(alBufferf);
341 LOAD_FUNCPTR(alBuffer3f);
342 LOAD_FUNCPTR(alBufferfv);
343 LOAD_FUNCPTR(alBufferi);
344 LOAD_FUNCPTR(alBuffer3i);
345 LOAD_FUNCPTR(alBufferiv);
346 LOAD_FUNCPTR(alGetBufferf);
347 LOAD_FUNCPTR(alGetBuffer3f);
348 LOAD_FUNCPTR(alGetBufferfv);
349 LOAD_FUNCPTR(alGetBufferi);
350 LOAD_FUNCPTR(alGetBuffer3i);
351 LOAD_FUNCPTR(alGetBufferiv);
352 LOAD_FUNCPTR(alBufferData);
353 LOAD_FUNCPTR(alDopplerFactor);
354 LOAD_FUNCPTR(alDopplerVelocity);
355 LOAD_FUNCPTR(alDistanceModel);
356 LOAD_FUNCPTR(alSpeedOfSound);
357 #undef LOAD_FUNCPTR
358 if (failed)
360 WARN("Unloading openal\n");
361 if (openal_handle != RTLD_DEFAULT)
362 wine_dlclose(openal_handle, NULL, 0);
363 openal_handle = NULL;
365 else
366 #endif
368 openal_loaded = 1;
370 local_contexts = alcIsExtensionPresent(NULL, "ALC_EXT_thread_local_context");
371 if(local_contexts)
373 set_context = alcGetProcAddress(NULL, "alcSetThreadContext");
374 get_context = alcGetProcAddress(NULL, "alcGetThreadContext");
375 if(!set_context || !get_context)
377 ERR("TLS advertised but functions not found, disabling thread local context\n");
378 local_contexts = 0;
381 if(!local_contexts)
383 set_context = alcMakeContextCurrent;
384 get_context = alcGetCurrentContext;
389 const ALCchar *DSOUND_getdevicestrings(void)
391 const ALCchar *str = NULL;
392 int multiple;
394 multiple = alcIsExtensionPresent(NULL, "ALC_ENUMERATE_ALL_EXT");
395 if (multiple)
396 str = alcGetString(NULL, ALC_ALL_DEVICES_SPECIFIER);
397 else
398 str = alcGetString(NULL, ALC_DEVICE_SPECIFIER);
399 return str;
402 const ALCchar *DSOUND_getcapturedevicestrings(void)
404 const ALCchar *str = NULL;
405 str = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER);
406 return str;
409 /***************************************************************************
410 * GetDeviceID [DSOUND.9]
412 * Retrieves unique identifier of default device specified
414 * PARAMS
415 * pGuidSrc [I] Address of device GUID.
416 * pGuidDest [O] Address to receive unique device GUID.
418 * RETURNS
419 * Success: DS_OK
420 * Failure: DSERR_INVALIDPARAM
422 * NOTES
423 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
424 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
425 * DSDEVID_DefaultVoiceCapture.
426 * Returns pGuidSrc if pGuidSrc is a valid device or the device
427 * GUID for the specified constants.
429 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
431 TRACE("(%s,%p)\n", debugstr_guid(pGuidSrc),pGuidDest);
433 if ( pGuidSrc == NULL) {
434 WARN("invalid parameter: pGuidSrc == NULL\n");
435 return DSERR_INVALIDPARAM;
438 if ( pGuidDest == NULL ) {
439 WARN("invalid parameter: pGuidDest == NULL\n");
440 return DSERR_INVALIDPARAM;
443 if ( IsEqualGUID( &DSDEVID_DefaultPlayback, pGuidSrc ) ||
444 IsEqualGUID( &DSDEVID_DefaultVoicePlayback, pGuidSrc ) ) {
445 *pGuidDest = DSOUND_renderer_guid;
446 TRACE("returns %s\n", debugstr_guid(pGuidDest));
447 return DS_OK;
450 if ( IsEqualGUID( &DSDEVID_DefaultCapture, pGuidSrc ) ||
451 IsEqualGUID( &DSDEVID_DefaultVoiceCapture, pGuidSrc ) ) {
452 *pGuidDest = DSOUND_capture_guid;
453 TRACE("returns %s\n", debugstr_guid(pGuidDest));
454 return DS_OK;
457 *pGuidDest = *pGuidSrc;
458 TRACE("returns %s\n", debugstr_guid(pGuidDest));
460 return DS_OK;
463 struct morecontext
465 LPDSENUMCALLBACKW callW;
466 LPVOID data;
469 static BOOL CALLBACK w_to_a_callback(LPGUID guid, LPCSTR descA, LPCSTR modA, LPVOID data)
471 struct morecontext *context = data;
472 WCHAR descW[MAXPNAMELEN], modW[MAXPNAMELEN];
474 MultiByteToWideChar(CP_ACP, 0, descA, -1, descW, sizeof(descW)/sizeof(descW[0]));
475 MultiByteToWideChar(CP_ACP, 0, modA, -1, modW, sizeof(modW)/sizeof(modW[0]));
477 return context->callW(guid, descW, modW, context->data);
480 /***************************************************************************
481 * DirectSoundEnumerateA [DSOUND.2]
483 * Enumerate all DirectSound drivers installed in the system
485 * PARAMS
486 * lpDSEnumCallback [I] Address of callback function.
487 * lpContext [I] Address of user defined context passed to callback function.
489 * RETURNS
490 * Success: DS_OK
491 * Failure: DSERR_INVALIDPARAM
493 HRESULT WINAPI DirectSoundEnumerateA(
494 LPDSENUMCALLBACKA lpDSEnumCallback,
495 LPVOID lpContext)
497 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
498 lpDSEnumCallback, lpContext);
500 if (lpDSEnumCallback == NULL) {
501 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
502 return DSERR_INVALIDPARAM;
504 else if (openal_loaded)
506 const ALCchar *devstr;
507 GUID guid;
509 EnterCriticalSection(&openal_crst);
510 devstr = DSOUND_getdevicestrings();
511 if(!devstr || !*devstr)
512 goto out;
514 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
515 "Primary Sound Driver","",lpContext);
516 if(lpDSEnumCallback(NULL, "Primary Sound Driver", "", lpContext) == FALSE)
517 goto out;
519 guid = DSOUND_renderer_guid;
520 do {
521 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
522 debugstr_guid(&guid),devstr,"wineal.drv",lpContext);
523 if(lpDSEnumCallback(&guid, devstr, "wineal.drv", lpContext) == FALSE)
524 goto out;
525 guid.Data4[7]++;
526 devstr += strlen(devstr)+1;
527 } while(*devstr);
528 out:
529 LeaveCriticalSection(&openal_crst);
531 else
533 ERR("Attempting to enumerate sound cards without OpenAL support\n");
534 ERR("Please recompile wine with OpenAL for sound to work\n");
536 return DS_OK;
539 /***************************************************************************
540 * DirectSoundEnumerateW [DSOUND.3]
542 * Enumerate all DirectSound drivers installed in the system
544 * PARAMS
545 * lpDSEnumCallback [I] Address of callback function.
546 * lpContext [I] Address of user defined context passed to callback function.
548 * RETURNS
549 * Success: DS_OK
550 * Failure: DSERR_INVALIDPARAM
552 HRESULT WINAPI DirectSoundEnumerateW(
553 LPDSENUMCALLBACKW lpDSEnumCallback,
554 LPVOID lpContext )
556 struct morecontext context;
558 if(lpDSEnumCallback == NULL) {
559 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
560 return DSERR_INVALIDPARAM;
563 context.callW = lpDSEnumCallback;
564 context.data = lpContext;
566 return DirectSoundEnumerateA(w_to_a_callback, &context);
569 /***************************************************************************
570 * DirectSoundCaptureEnumerateA [DSOUND.7]
572 * Enumerate all DirectSound drivers installed in the system.
574 * PARAMS
575 * lpDSEnumCallback [I] Address of callback function.
576 * lpContext [I] Address of user defined context passed to callback function.
578 * RETURNS
579 * Success: DS_OK
580 * Failure: DSERR_INVALIDPARAM
582 HRESULT WINAPI DirectSoundCaptureEnumerateA(
583 LPDSENUMCALLBACKA lpDSEnumCallback,
584 LPVOID lpContext)
586 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
588 if (lpDSEnumCallback == NULL) {
589 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
590 return DSERR_INVALIDPARAM;
592 else if (openal_loaded)
594 const ALCchar *devstr;
595 GUID guid;
597 EnterCriticalSection(&openal_crst);
598 devstr = DSOUND_getcapturedevicestrings();
599 if(!devstr || !*devstr)
600 goto out;
602 TRACE("calling lpDSEnumCallback(NULL,\"%s\",\"%s\",%p)\n",
603 "Primary Sound Capture Driver","",lpContext);
604 if(lpDSEnumCallback(NULL, "Primary Sound Driver", "", lpContext) == FALSE)
605 goto out;
607 guid = DSOUND_capture_guid;
608 do {
609 TRACE("calling lpDSEnumCallback(%s,\"%s\",\"%s\",%p)\n",
610 debugstr_guid(&guid),devstr,"wineal.drv",lpContext);
611 if(lpDSEnumCallback(&guid, devstr, "wineal.drv", lpContext) == FALSE)
612 goto out;
613 guid.Data4[7]++;
614 devstr += strlen(devstr)+1;
615 } while(*devstr);
616 out:
617 LeaveCriticalSection(&openal_crst);
619 else
621 ERR("Attempting to enumerate sound cards without OpenAL support\n");
622 ERR("Please recompile wine with OpenAL for sound to work\n");
624 return DS_OK;
627 /***************************************************************************
628 * DirectSoundCaptureEnumerateW [DSOUND.8]
630 * Enumerate all DirectSound drivers installed in the system.
632 * PARAMS
633 * lpDSEnumCallback [I] Address of callback function.
634 * lpContext [I] Address of user defined context passed to callback function.
636 * RETURNS
637 * Success: DS_OK
638 * Failure: DSERR_INVALIDPARAM
640 HRESULT WINAPI
641 DirectSoundCaptureEnumerateW(
642 LPDSENUMCALLBACKW lpDSEnumCallback,
643 LPVOID lpContext)
645 struct morecontext context;
647 if (lpDSEnumCallback == NULL) {
648 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
649 return DSERR_INVALIDPARAM;
652 context.callW = lpDSEnumCallback;
653 context.data = lpContext;
655 return DirectSoundCaptureEnumerateA(w_to_a_callback, &context);
658 /*******************************************************************************
659 * DirectSoundCreate (DSOUND.1)
661 * Creates and initializes a DirectSound interface.
663 * PARAMS
664 * lpcGUID [I] Address of the GUID that identifies the sound device.
665 * ppDS [O] Address of a variable to receive the interface pointer.
666 * pUnkOuter [I] Must be NULL.
668 * RETURNS
669 * Success: DS_OK
670 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
671 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
673 HRESULT WINAPI DirectSoundCreate(
674 LPCGUID lpcGUID,
675 LPDIRECTSOUND *ppDS,
676 IUnknown *pUnkOuter)
678 HRESULT hr;
679 LPDIRECTSOUND pDS;
681 TRACE("(%s,%p,%p)\n",debugstr_guid(lpcGUID),ppDS,pUnkOuter);
683 if (ppDS == NULL) {
684 WARN("invalid parameter: ppDS == NULL\n");
685 return DSERR_INVALIDPARAM;
688 if (pUnkOuter != NULL) {
689 WARN("invalid parameter: pUnkOuter != NULL\n");
690 *ppDS = 0;
691 return DSERR_INVALIDPARAM;
694 hr = DSOUND_Create(&IID_IDirectSound, (void **)&pDS);
695 if (hr == DS_OK) {
696 hr = IDirectSound_Initialize(pDS, lpcGUID);
697 if (hr != DS_OK) {
698 if (hr != DSERR_ALREADYINITIALIZED) {
699 IDirectSound_Release(pDS);
700 pDS = 0;
701 } else
702 hr = DS_OK;
706 *ppDS = pDS;
708 return hr;
711 /*******************************************************************************
712 * DirectSoundCreate8 (DSOUND.11)
714 * Creates and initializes a DirectSound8 interface.
716 * PARAMS
717 * lpcGUID [I] Address of the GUID that identifies the sound device.
718 * ppDS [O] Address of a variable to receive the interface pointer.
719 * pUnkOuter [I] Must be NULL.
721 * RETURNS
722 * Success: DS_OK
723 * Failure: DSERR_ALLOCATED, DSERR_INVALIDPARAM, DSERR_NOAGGREGATION,
724 * DSERR_NODRIVER, DSERR_OUTOFMEMORY
726 HRESULT WINAPI DirectSoundCreate8(
727 LPCGUID lpcGUID,
728 LPDIRECTSOUND8 *ppDS,
729 IUnknown *pUnkOuter)
731 HRESULT hr;
732 LPDIRECTSOUND8 pDS;
734 TRACE("(%s,%p,%p)\n",debugstr_guid(lpcGUID),ppDS,pUnkOuter);
736 if (ppDS == NULL) {
737 WARN("invalid parameter: ppDS == NULL\n");
738 return DSERR_INVALIDPARAM;
741 if (pUnkOuter != NULL) {
742 WARN("invalid parameter: pUnkOuter != NULL\n");
743 *ppDS = 0;
744 return DSERR_INVALIDPARAM;
747 hr = DSOUND_Create8(&IID_IDirectSound8, (void**)&pDS);
748 if (hr == DS_OK) {
749 hr = IDirectSound8_Initialize(pDS, lpcGUID);
750 if (hr != DS_OK) {
751 if (hr != DSERR_ALREADYINITIALIZED) {
752 IDirectSound8_Release(pDS);
753 pDS = 0;
754 } else
755 hr = DS_OK;
759 *ppDS = pDS;
761 return hr;
764 /***************************************************************************
765 * DirectSoundCaptureCreate [DSOUND.6]
767 * Create and initialize a DirectSoundCapture interface.
769 * PARAMS
770 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
771 * lplpDSC [O] Address of a variable to receive the interface pointer.
772 * pUnkOuter [I] Must be NULL.
774 * RETURNS
775 * Success: DS_OK
776 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
777 * DSERR_OUTOFMEMORY
779 * NOTES
780 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
781 * or NULL for the default device or DSDEVID_DefaultCapture or
782 * DSDEVID_DefaultVoiceCapture.
784 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
786 HRESULT WINAPI DirectSoundCaptureCreate(
787 LPCGUID lpcGUID,
788 IDirectSoundCapture **ppDSC,
789 IUnknown *pUnkOuter)
791 void *pDSC;
792 HRESULT hr;
794 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC, pUnkOuter);
796 if(ppDSC == NULL)
798 WARN("invalid parameter: ppDSC == NULL\n");
799 return DSERR_INVALIDPARAM;
801 *ppDSC = NULL;
803 if(pUnkOuter)
805 WARN("invalid parameter: pUnkOuter != NULL\n");
806 return DSERR_NOAGGREGATION;
809 hr = DSOUND_CaptureCreate(&IID_IDirectSoundCapture, &pDSC);
810 if(hr == DS_OK)
812 *ppDSC = pDSC;
813 hr = IDirectSoundCapture_Initialize(*ppDSC, lpcGUID);
814 if(hr != DS_OK)
816 IDirectSoundCapture_Release(*ppDSC);
817 *ppDSC = 0;
821 return hr;
824 /***************************************************************************
825 * DirectSoundCaptureCreate8 [DSOUND.12]
827 * Create and initialize a DirectSoundCapture interface.
829 * PARAMS
830 * lpcGUID [I] Address of the GUID that identifies the sound capture device.
831 * lplpDSC [O] Address of a variable to receive the interface pointer.
832 * pUnkOuter [I] Must be NULL.
834 * RETURNS
835 * Success: DS_OK
836 * Failure: DSERR_NOAGGREGATION, DSERR_ALLOCATED, DSERR_INVALIDPARAM,
837 * DSERR_OUTOFMEMORY
839 * NOTES
840 * lpcGUID must be one of the values returned from DirectSoundCaptureEnumerate
841 * or NULL for the default device or DSDEVID_DefaultCapture or
842 * DSDEVID_DefaultVoiceCapture.
844 * DSERR_ALLOCATED is returned for sound devices that do not support full duplex.
846 HRESULT WINAPI DirectSoundCaptureCreate8(
847 LPCGUID lpcGUID,
848 IDirectSoundCapture8 **ppDSC8,
849 IUnknown *pUnkOuter)
851 void *pDSC8;
852 HRESULT hr;
854 TRACE("(%s, %p, %p)\n", debugstr_guid(lpcGUID), ppDSC8, pUnkOuter);
856 if(ppDSC8 == NULL)
858 WARN("invalid parameter: ppDSC8 == NULL\n");
859 return DSERR_INVALIDPARAM;
861 *ppDSC8 = NULL;
863 if(pUnkOuter)
865 WARN("invalid parameter: pUnkOuter != NULL\n");
866 return DSERR_NOAGGREGATION;
869 hr = DSOUND_CaptureCreate8(&IID_IDirectSoundCapture8, &pDSC8);
870 if(hr == DS_OK)
872 *ppDSC8 = pDSC8;
873 hr = IDirectSoundCapture_Initialize(*ppDSC8, lpcGUID);
874 if(hr != DS_OK)
876 IDirectSoundCapture_Release(*ppDSC8);
877 *ppDSC8 = NULL;
881 return hr;
884 /*******************************************************************************
885 * DirectSound ClassFactory
888 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
890 typedef struct {
891 IClassFactory IClassFactory_iface;
892 LONG ref;
893 REFCLSID rclsid;
894 FnCreateInstance pfnCreateInstance;
895 } IClassFactoryImpl;
897 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
899 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
902 static HRESULT WINAPI DSCF_QueryInterface(LPCLASSFACTORY iface, REFIID riid, LPVOID *ppobj)
904 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
905 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
906 if (ppobj == NULL)
907 return E_POINTER;
908 if (IsEqualIID(riid, &IID_IUnknown) ||
909 IsEqualIID(riid, &IID_IClassFactory))
911 *ppobj = iface;
912 IUnknown_AddRef(iface);
913 return S_OK;
915 *ppobj = NULL;
916 return E_NOINTERFACE;
919 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
921 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
922 ULONG ref = InterlockedIncrement(&(This->ref));
923 TRACE("(%p) ref was %d\n", This, ref - 1);
924 return ref;
927 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
929 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
930 ULONG ref = InterlockedDecrement(&(This->ref));
931 TRACE("(%p) ref was %d\n", This, ref + 1);
932 /* static class, won't be freed */
933 return ref;
936 static HRESULT WINAPI DSCF_CreateInstance(
937 LPCLASSFACTORY iface,
938 LPUNKNOWN pOuter,
939 REFIID riid,
940 LPVOID *ppobj)
942 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
943 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
945 if (pOuter)
946 return CLASS_E_NOAGGREGATION;
948 if (ppobj == NULL) {
949 WARN("invalid parameter\n");
950 return DSERR_INVALIDPARAM;
952 *ppobj = NULL;
953 return This->pfnCreateInstance(riid, ppobj);
956 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
958 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
959 FIXME("(%p, %d) stub!\n", This, dolock);
960 return S_OK;
963 static const IClassFactoryVtbl DSCF_Vtbl = {
964 DSCF_QueryInterface,
965 DSCF_AddRef,
966 DSCF_Release,
967 DSCF_CreateInstance,
968 DSCF_LockServer
971 static IClassFactoryImpl DSOUND_CF[] = {
972 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSound, DSOUND_Create },
973 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSound8, DSOUND_Create8 },
974 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
975 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
976 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
977 { {(IClassFactoryVtbl*)&DSCF_Vtbl}, 1, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
978 { {NULL}, 0, NULL, NULL }
981 /*******************************************************************************
982 * DllGetClassObject [DSOUND.@]
983 * Retrieves class object from a DLL object
985 * NOTES
986 * Docs say returns STDAPI
988 * PARAMS
989 * rclsid [I] CLSID for the class object
990 * riid [I] Reference to identifier of interface for class object
991 * ppv [O] Address of variable to receive interface pointer for riid
993 * RETURNS
994 * Success: S_OK
995 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
996 * E_UNEXPECTED
998 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
1000 int i = 0;
1001 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
1003 if (ppv == NULL) {
1004 WARN("invalid parameter\n");
1005 return E_INVALIDARG;
1008 *ppv = NULL;
1010 if (!IsEqualIID(riid, &IID_IClassFactory) &&
1011 !IsEqualIID(riid, &IID_IUnknown)) {
1012 WARN("no interface for %s\n", debugstr_guid(riid));
1013 return E_NOINTERFACE;
1016 while (NULL != DSOUND_CF[i].rclsid) {
1017 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
1018 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
1019 *ppv = &DSOUND_CF[i];
1020 return S_OK;
1022 i++;
1025 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
1026 debugstr_guid(riid), ppv);
1027 return CLASS_E_CLASSNOTAVAILABLE;
1031 /*******************************************************************************
1032 * DllCanUnloadNow [DSOUND.4]
1033 * Determines whether the DLL is in use.
1035 * RETURNS
1036 * Success: S_OK
1037 * Failure: S_FALSE
1039 HRESULT WINAPI DllCanUnloadNow(void)
1041 FIXME("(void): stub\n");
1042 return S_FALSE;
1045 /***********************************************************************
1046 * DllMain (DSOUND.init)
1048 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
1050 TRACE("(%p, %d, %p)\n", hInstDLL, fdwReason, lpvReserved);
1052 switch(fdwReason)
1054 case DLL_PROCESS_ATTACH:
1055 TRACE("DLL_PROCESS_ATTACH\n");
1056 instance = hInstDLL;
1057 load_libopenal();
1058 DisableThreadLibraryCalls(hInstDLL);
1059 /* Increase refcount on dsound by 1 */
1060 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
1061 break;
1062 case DLL_PROCESS_DETACH:
1063 TRACE("DLL_PROCESS_DETACH\n");
1064 #ifdef SONAME_LIBOPENAL
1065 if(openal_handle)
1066 wine_dlclose(openal_handle, NULL, 0);
1067 #endif /*SONAME_LIBOPENAL*/
1068 break;
1069 default:
1070 TRACE("UNKNOWN REASON\n");
1071 break;
1073 return TRUE;
1076 /***********************************************************************
1077 * DllRegisterServer (DSOUND.@)
1079 HRESULT WINAPI DllRegisterServer(void)
1081 return __wine_register_resources(instance, NULL);
1084 /***********************************************************************
1085 * DllUnregisterServer (DSOUND.@)
1087 HRESULT WINAPI DllUnregisterServer(void)
1089 return __wine_unregister_resources(instance, NULL);