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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Most thread locking is complete. There may be a few race
23 * conditions still lurking.
25 * Tested with a Soundblaster clone, a Gravis UltraSound Classic,
26 * and a Turtle Beach Tropez+.
29 * Implement SetCooperativeLevel properly (need to address focus issues)
30 * Implement DirectSound3DBuffers (stubs in place)
31 * Use hardware 3D support if available
32 * Add critical section locking inside Release and AddRef methods
33 * Handle static buffers - put those in hardware, non-static not in hardware
34 * Hardware DuplicateSoundBuffer
35 * Proper volume calculation, and setting volume in HEL primary buffer
36 * Optimize WINMM and negotiate fragment size, decrease DS_HEL_MARGIN
40 #include "wine/port.h"
44 #include <sys/types.h>
45 #include <sys/fcntl.h>
49 #include <math.h> /* Insomnia - pow() function */
61 #include "wine/windef16.h"
62 #include "wine/winbase16.h"
63 #include "wine/debug.h"
66 #include "dsound_private.h"
68 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
70 /* these are eligible for tuning... they must be high on slow machines... */
71 /* some stuff may get more responsive with lower values though... */
72 #define DS_EMULDRIVER 0 /* some games (Quake 2, UT) refuse to accept
73 emulated dsound devices. set to 0 ! */
74 #define DS_HEL_MARGIN 5 /* HEL only: number of waveOut fragments ahead to mix in new buffers
75 * (keep this close or equal to DS_HEL_QUEUE for best results) */
76 #define DS_HEL_QUEUE 5 /* HEL only: number of waveOut fragments ahead to queue to driver
77 * (this will affect HEL sound reliability and latency) */
79 #define DS_SND_QUEUE_MAX 28 /* max number of fragments to prebuffer */
80 #define DS_SND_QUEUE_MIN 12 /* min number of fragments to prebuffer */
82 IDirectSoundImpl* dsound = NULL;
84 static HRESULT mmErr(UINT err)
87 case MMSYSERR_NOERROR:
89 case MMSYSERR_ALLOCATED:
90 return DSERR_ALLOCATED;
91 case MMSYSERR_INVALHANDLE:
92 return DSERR_GENERIC; /* FIXME */
93 case MMSYSERR_NODRIVER:
94 return DSERR_NODRIVER;
96 return DSERR_OUTOFMEMORY;
97 case MMSYSERR_INVALPARAM:
98 return DSERR_INVALIDPARAM;
100 FIXME("Unknown MMSYS error %d\n",err);
101 return DSERR_GENERIC;
105 int ds_emuldriver = DS_EMULDRIVER;
106 int ds_hel_margin = DS_HEL_MARGIN;
107 int ds_hel_queue = DS_HEL_QUEUE;
108 int ds_snd_queue_max = DS_SND_QUEUE_MAX;
109 int ds_snd_queue_min = DS_SND_QUEUE_MIN;
112 * Call the callback provided to DirectSoundEnumerateA.
115 inline static void enumerate_devices(LPDSENUMCALLBACKA lpDSEnumCallback,
118 if (lpDSEnumCallback != NULL)
119 if (lpDSEnumCallback(NULL, "Primary DirectSound Driver",
121 lpDSEnumCallback((LPGUID)&DSDEVID_WinePlayback,
122 "WINE DirectSound", "sound",
128 * Get a config key from either the app-specific or the default config
131 inline static DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
132 char *buffer, DWORD size )
134 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, buffer, &size )) return 0;
135 return RegQueryValueExA( defkey, name, 0, NULL, buffer, &size );
140 * Setup the dsound options.
143 inline static void setup_dsound_options(void)
145 char buffer[MAX_PATH+1];
146 HKEY hkey, appkey = 0;
148 buffer[MAX_PATH]='\0';
150 if (RegCreateKeyExA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\dsound", 0, NULL,
151 REG_OPTION_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, NULL ))
153 ERR("Cannot create config registry key\n" );
157 if (GetModuleFileName16( GetCurrentTask(), buffer, MAX_PATH ) ||
158 GetModuleFileNameA( 0, buffer, MAX_PATH ))
162 if (!RegOpenKeyA( HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\AppDefaults", &tmpkey ))
164 char appname[MAX_PATH+16];
165 char *p = strrchr( buffer, '\\' );
167 appname[MAX_PATH]='\0';
168 strncpy(appname,p+1,MAX_PATH);
169 strcat(appname,"\\dsound");
170 TRACE("appname = [%s] \n",appname);
171 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
172 RegCloseKey( tmpkey );
179 if (!get_config_key( hkey, appkey, "EmulDriver", buffer, MAX_PATH ))
180 ds_emuldriver = strcmp(buffer, "N");
182 if (!get_config_key( hkey, appkey, "HELmargin", buffer, MAX_PATH ))
183 ds_hel_margin = atoi(buffer);
185 if (!get_config_key( hkey, appkey, "HELqueue", buffer, MAX_PATH ))
186 ds_hel_queue = atoi(buffer);
188 if (!get_config_key( hkey, appkey, "SndQueueMax", buffer, MAX_PATH ))
189 ds_snd_queue_max = atoi(buffer);
191 if (!get_config_key( hkey, appkey, "SndQueueMin", buffer, MAX_PATH ))
192 ds_snd_queue_min = atoi(buffer);
194 if (appkey) RegCloseKey( appkey );
197 if (ds_emuldriver != DS_EMULDRIVER )
198 WARN("ds_emuldriver = %d (default=%d)\n",ds_emuldriver, DS_EMULDRIVER);
199 if (ds_hel_margin != DS_HEL_MARGIN )
200 WARN("ds_hel_margin = %d (default=%d)\n",ds_hel_margin, DS_HEL_MARGIN );
201 if (ds_hel_queue != DS_HEL_QUEUE )
202 WARN("ds_hel_queue = %d (default=%d)\n",ds_hel_queue, DS_HEL_QUEUE );
203 if (ds_snd_queue_max != DS_SND_QUEUE_MAX)
204 WARN("ds_snd_queue_max = %d (default=%d)\n",ds_snd_queue_max ,DS_SND_QUEUE_MAX);
205 if (ds_snd_queue_min != DS_SND_QUEUE_MIN)
206 WARN("ds_snd_queue_min = %d (default=%d)\n",ds_snd_queue_min ,DS_SND_QUEUE_MIN);
212 /***************************************************************************
213 * DirectSoundEnumerateA [DSOUND.2]
215 * Enumerate all DirectSound drivers installed in the system
219 * Failure: DSERR_INVALIDPARAM
221 HRESULT WINAPI DirectSoundEnumerateA(
222 LPDSENUMCALLBACKA lpDSEnumCallback,
228 TRACE("lpDSEnumCallback = %p, lpContext = %p\n",
229 lpDSEnumCallback, lpContext);
231 devs = waveOutGetNumDevs();
232 for (wod = 0; wod < devs; ++wod) {
233 waveOutGetDevCapsA(wod, &wcaps, sizeof(wcaps));
234 if (wcaps.dwSupport & WAVECAPS_DIRECTSOUND) {
235 TRACE("- Device %u supports DirectSound\n", wod);
236 enumerate_devices(lpDSEnumCallback, lpContext);
243 /***************************************************************************
244 * DirectSoundEnumerateW [DSOUND.3]
246 * Enumerate all DirectSound drivers installed in the system
250 * Failure: DSERR_INVALIDPARAM
252 HRESULT WINAPI DirectSoundEnumerateW(
253 LPDSENUMCALLBACKW lpDSEnumCallback,
256 FIXME("lpDSEnumCallback = %p, lpContext = %p: stub\n",
257 lpDSEnumCallback, lpContext);
263 static void _dump_DSBCAPS(DWORD xmask) {
268 #define FE(x) { x, #x },
269 FE(DSBCAPS_PRIMARYBUFFER)
271 FE(DSBCAPS_LOCHARDWARE)
272 FE(DSBCAPS_LOCSOFTWARE)
274 FE(DSBCAPS_CTRLFREQUENCY)
276 FE(DSBCAPS_CTRLVOLUME)
277 FE(DSBCAPS_CTRLPOSITIONNOTIFY)
278 FE(DSBCAPS_CTRLDEFAULT)
280 FE(DSBCAPS_STICKYFOCUS)
281 FE(DSBCAPS_GLOBALFOCUS)
282 FE(DSBCAPS_GETCURRENTPOSITION2)
283 FE(DSBCAPS_MUTE3DATMAXDISTANCE)
288 for (i=0;i<sizeof(flags)/sizeof(flags[0]);i++)
289 if ((flags[i].mask & xmask) == flags[i].mask)
290 DPRINTF("%s ",flags[i].name);
293 /*******************************************************************************
297 static HRESULT WINAPI IDirectSoundImpl_SetCooperativeLevel(
298 LPDIRECTSOUND8 iface,HWND hwnd,DWORD level
300 ICOM_THIS(IDirectSoundImpl,iface);
302 FIXME("(%p,%08lx,%ld):stub\n",This,(DWORD)hwnd,level);
304 This->priolevel = level;
309 static HRESULT WINAPI IDirectSoundImpl_CreateSoundBuffer(
310 LPDIRECTSOUND8 iface,LPDSBUFFERDESC dsbd,LPLPDIRECTSOUNDBUFFER8 ppdsb,LPUNKNOWN lpunk
312 ICOM_THIS(IDirectSoundImpl,iface);
315 TRACE("(%p,%p,%p,%p)\n",This,dsbd,ppdsb,lpunk);
317 if ((This == NULL) || (dsbd == NULL) || (ppdsb == NULL))
318 return DSERR_INVALIDPARAM;
320 if (TRACE_ON(dsound)) {
321 TRACE("(structsize=%ld)\n",dsbd->dwSize);
322 TRACE("(flags=0x%08lx:\n",dsbd->dwFlags);
323 _dump_DSBCAPS(dsbd->dwFlags);
325 TRACE("(bufferbytes=%ld)\n",dsbd->dwBufferBytes);
326 TRACE("(lpwfxFormat=%p)\n",dsbd->lpwfxFormat);
329 wfex = dsbd->lpwfxFormat;
332 TRACE("(formattag=0x%04x,chans=%d,samplerate=%ld,"
333 "bytespersec=%ld,blockalign=%d,bitspersamp=%d,cbSize=%d)\n",
334 wfex->wFormatTag, wfex->nChannels, wfex->nSamplesPerSec,
335 wfex->nAvgBytesPerSec, wfex->nBlockAlign,
336 wfex->wBitsPerSample, wfex->cbSize);
338 if (dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)
339 return PrimaryBuffer_Create(This, (PrimaryBufferImpl**)ppdsb, dsbd);
341 return SecondaryBuffer_Create(This, (IDirectSoundBufferImpl**)ppdsb, dsbd);
344 static HRESULT WINAPI IDirectSoundImpl_DuplicateSoundBuffer(
345 LPDIRECTSOUND8 iface,LPDIRECTSOUNDBUFFER8 pdsb,LPLPDIRECTSOUNDBUFFER8 ppdsb
347 ICOM_THIS(IDirectSoundImpl,iface);
348 IDirectSoundBufferImpl* ipdsb=(IDirectSoundBufferImpl*)pdsb;
349 IDirectSoundBufferImpl** ippdsb=(IDirectSoundBufferImpl**)ppdsb;
350 TRACE("(%p,%p,%p)\n",This,ipdsb,ippdsb);
352 if (ipdsb->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) {
353 ERR("trying to duplicate primary buffer\n");
354 return DSERR_INVALIDCALL;
358 FIXME("need to duplicate hardware buffer\n");
361 if (ipdsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
362 FIXME("need to duplicate 3D buffer\n");
365 *ippdsb = (IDirectSoundBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundBufferImpl));
367 IDirectSoundBuffer8_AddRef(pdsb);
368 memcpy(*ippdsb, ipdsb, sizeof(IDirectSoundBufferImpl));
370 (*ippdsb)->state = STATE_STOPPED;
371 (*ippdsb)->playpos = 0;
372 (*ippdsb)->buf_mixpos = 0;
373 (*ippdsb)->dsound = This;
374 (*ippdsb)->parent = ipdsb;
375 (*ippdsb)->hwbuf = NULL;
376 (*ippdsb)->ds3db = NULL; /* FIXME? */
377 (*ippdsb)->iks = NULL; /* FIXME? */
378 memcpy(&((*ippdsb)->wfx), &(ipdsb->wfx), sizeof((*ippdsb)->wfx));
379 InitializeCriticalSection(&(*ippdsb)->lock);
380 /* register buffer */
381 RtlAcquireResourceExclusive(&(This->lock), TRUE);
383 IDirectSoundBufferImpl **newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,This->buffers,sizeof(IDirectSoundBufferImpl**)*(This->nrofbuffers+1));
385 This->buffers = newbuffers;
386 This->buffers[This->nrofbuffers] = *ippdsb;
388 TRACE("buffer count is now %d\n", This->nrofbuffers);
390 ERR("out of memory for buffer list! Current buffer count is %d\n", This->nrofbuffers);
391 /* FIXME: release buffer */
394 RtlReleaseResource(&(This->lock));
395 IDirectSound_AddRef(iface);
400 static HRESULT WINAPI IDirectSoundImpl_GetCaps(LPDIRECTSOUND8 iface,LPDSCAPS caps) {
401 ICOM_THIS(IDirectSoundImpl,iface);
402 TRACE("(%p,%p)\n",This,caps);
403 TRACE("(flags=0x%08lx)\n",caps->dwFlags);
406 return DSERR_INVALIDPARAM;
408 /* We should check this value, not set it. See Inside DirectX, p215. */
409 caps->dwSize = sizeof(*caps);
411 caps->dwFlags = This->drvcaps.dwFlags;
413 /* FIXME: copy caps from This->drvcaps */
414 caps->dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
415 caps->dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
417 caps->dwPrimaryBuffers = 1;
419 caps->dwMaxHwMixingAllBuffers = 0;
420 caps->dwMaxHwMixingStaticBuffers = 0;
421 caps->dwMaxHwMixingStreamingBuffers = 0;
423 caps->dwFreeHwMixingAllBuffers = 0;
424 caps->dwFreeHwMixingStaticBuffers = 0;
425 caps->dwFreeHwMixingStreamingBuffers = 0;
427 caps->dwMaxHw3DAllBuffers = 0;
428 caps->dwMaxHw3DStaticBuffers = 0;
429 caps->dwMaxHw3DStreamingBuffers = 0;
431 caps->dwFreeHw3DAllBuffers = 0;
432 caps->dwFreeHw3DStaticBuffers = 0;
433 caps->dwFreeHw3DStreamingBuffers = 0;
435 caps->dwTotalHwMemBytes = 0;
437 caps->dwFreeHwMemBytes = 0;
439 caps->dwMaxContigFreeHwMemBytes = 0;
441 caps->dwUnlockTransferRateHwBuffers = 4096; /* But we have none... */
443 caps->dwPlayCpuOverheadSwBuffers = 1; /* 1% */
448 static ULONG WINAPI IDirectSoundImpl_AddRef(LPDIRECTSOUND8 iface) {
449 ICOM_THIS(IDirectSoundImpl,iface);
450 return ++(This->ref);
453 static ULONG WINAPI IDirectSoundImpl_Release(LPDIRECTSOUND8 iface) {
454 ICOM_THIS(IDirectSoundImpl,iface);
455 TRACE("(%p), ref was %ld\n",This,This->ref);
456 if (!--(This->ref)) {
459 timeKillEvent(This->timerID);
460 timeEndPeriod(DS_TIME_RES);
463 for( i=0;i<This->nrofbuffers;i++)
464 IDirectSoundBuffer8_Release((LPDIRECTSOUNDBUFFER8)This->buffers[i]);
467 DSOUND_PrimaryDestroy(This);
469 RtlDeleteResource(&This->lock);
470 DeleteCriticalSection(&This->mixlock);
472 IDsDriver_Close(This->driver);
475 for (c=0; c<DS_HEL_FRAGS; c++)
476 HeapFree(GetProcessHeap(),0,This->pwave[c]);
478 if (This->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN) {
479 waveOutClose(This->hwo);
482 IDsDriver_Release(This->driver);
484 HeapFree(GetProcessHeap(),0,This);
491 static HRESULT WINAPI IDirectSoundImpl_SetSpeakerConfig(
492 LPDIRECTSOUND8 iface,DWORD config
494 ICOM_THIS(IDirectSoundImpl,iface);
495 FIXME("(%p,0x%08lx):stub\n",This,config);
499 static HRESULT WINAPI IDirectSoundImpl_QueryInterface(
500 LPDIRECTSOUND8 iface,REFIID riid,LPVOID *ppobj
502 ICOM_THIS(IDirectSoundImpl,iface);
504 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
505 ERR("app requested IDirectSound3DListener on dsound object\n");
510 FIXME("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
511 return E_NOINTERFACE;
514 static HRESULT WINAPI IDirectSoundImpl_Compact(
515 LPDIRECTSOUND8 iface)
517 ICOM_THIS(IDirectSoundImpl,iface);
518 TRACE("(%p)\n", This);
522 static HRESULT WINAPI IDirectSoundImpl_GetSpeakerConfig(
523 LPDIRECTSOUND8 iface,
524 LPDWORD lpdwSpeakerConfig)
526 ICOM_THIS(IDirectSoundImpl,iface);
527 TRACE("(%p, %p)\n", This, lpdwSpeakerConfig);
528 *lpdwSpeakerConfig = DSSPEAKER_STEREO | (DSSPEAKER_GEOMETRY_NARROW << 16);
532 static HRESULT WINAPI IDirectSoundImpl_Initialize(
533 LPDIRECTSOUND8 iface,
536 ICOM_THIS(IDirectSoundImpl,iface);
537 TRACE("(%p, %p)\n", This, lpcGuid);
541 static HRESULT WINAPI IDirectSoundImpl_VerifyCertification(
542 LPDIRECTSOUND8 iface,
543 LPDWORD pdwCertified)
545 ICOM_THIS(IDirectSoundImpl,iface);
546 TRACE("(%p, %p)\n", This, pdwCertified);
547 *pdwCertified = DS_CERTIFIED;
551 static ICOM_VTABLE(IDirectSound8) dsvt =
553 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
554 IDirectSoundImpl_QueryInterface,
555 IDirectSoundImpl_AddRef,
556 IDirectSoundImpl_Release,
557 IDirectSoundImpl_CreateSoundBuffer,
558 IDirectSoundImpl_GetCaps,
559 IDirectSoundImpl_DuplicateSoundBuffer,
560 IDirectSoundImpl_SetCooperativeLevel,
561 IDirectSoundImpl_Compact,
562 IDirectSoundImpl_GetSpeakerConfig,
563 IDirectSoundImpl_SetSpeakerConfig,
564 IDirectSoundImpl_Initialize,
565 IDirectSoundImpl_VerifyCertification
569 /*******************************************************************************
570 * DirectSoundCreate (DSOUND.1)
572 HRESULT WINAPI DirectSoundCreate8(REFGUID lpGUID,LPDIRECTSOUND8 *ppDS,IUnknown *pUnkOuter )
574 IDirectSoundImpl** ippDS=(IDirectSoundImpl**)ppDS;
575 PIDSDRIVER drv = NULL;
581 TRACE("(%p,%p,%p)\n",lpGUID,ippDS,pUnkOuter);
583 TRACE("DirectSoundCreate (%p)\n", ippDS);
586 return DSERR_INVALIDPARAM;
589 IDirectSound_AddRef((LPDIRECTSOUND)dsound);
594 /* Get dsound configuration */
595 setup_dsound_options();
597 /* Enumerate WINMM audio devices and find the one we want */
598 wodn = waveOutGetNumDevs();
599 if (!wodn) return DSERR_NODRIVER;
601 /* FIXME: How do we find the GUID of an audio device? */
602 wod = 0; /* start at the first audio device */
604 /* Get output device caps */
605 waveOutGetDevCapsA(wod, &wcaps, sizeof(wcaps));
606 /* DRV_QUERYDSOUNDIFACE is a "Wine extension" to get the DSound interface */
607 waveOutMessage(wod, DRV_QUERYDSOUNDIFACE, (DWORD)&drv, 0);
609 /* Allocate memory */
610 *ippDS = (IDirectSoundImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirectSoundImpl));
612 return DSERR_OUTOFMEMORY;
614 ICOM_VTBL(*ippDS) = &dsvt;
617 (*ippDS)->driver = drv;
618 (*ippDS)->priolevel = DSSCL_NORMAL;
619 (*ippDS)->fraglen = 0;
620 (*ippDS)->hwbuf = NULL;
621 (*ippDS)->buffer = NULL;
622 (*ippDS)->buflen = 0;
623 (*ippDS)->writelead = 0;
624 (*ippDS)->state = STATE_STOPPED;
625 (*ippDS)->nrofbuffers = 0;
626 (*ippDS)->buffers = NULL;
627 /* (*ippDS)->primary = NULL; */
628 (*ippDS)->listener = NULL;
630 (*ippDS)->prebuf = ds_snd_queue_max;
632 /* Get driver description */
634 IDsDriver_GetDriverDesc(drv,&((*ippDS)->drvdesc));
636 /* if no DirectSound interface available, use WINMM API instead */
637 (*ippDS)->drvdesc.dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT;
638 (*ippDS)->drvdesc.dnDevNode = wod; /* FIXME? */
641 /* Set default wave format (may need it for waveOutOpen) */
642 (*ippDS)->wfx.wFormatTag = WAVE_FORMAT_PCM;
643 (*ippDS)->wfx.nChannels = 2;
644 (*ippDS)->wfx.nSamplesPerSec = 22050;
645 (*ippDS)->wfx.nAvgBytesPerSec = 44100;
646 (*ippDS)->wfx.nBlockAlign = 2;
647 (*ippDS)->wfx.wBitsPerSample = 8;
649 /* If the driver requests being opened through MMSYSTEM
650 * (which is recommended by the DDK), it is supposed to happen
651 * before the DirectSound interface is opened */
652 if ((*ippDS)->drvdesc.dwFlags & DSDDESC_DOMMSYSTEMOPEN)
654 /* FIXME: is this right? */
655 (*ippDS)->drvdesc.dnDevNode = 0;
656 err = DSERR_ALLOCATED;
658 /* if this device is busy try the next one */
659 while((err == DSERR_ALLOCATED) &&
660 ((*ippDS)->drvdesc.dnDevNode < wodn))
662 err = mmErr(waveOutOpen(&((*ippDS)->hwo),
663 (*ippDS)->drvdesc.dnDevNode, &((*ippDS)->wfx),
664 (DWORD)DSOUND_callback, (DWORD)(*ippDS),
665 CALLBACK_FUNCTION | WAVE_DIRECTSOUND));
666 (*ippDS)->drvdesc.dnDevNode++; /* next wave device */
669 (*ippDS)->drvdesc.dnDevNode--; /* take away last increment */
672 if (drv && (err == DS_OK))
673 err = IDsDriver_Open(drv);
675 /* FIXME: do we want to handle a temporarily busy device? */
677 HeapFree(GetProcessHeap(),0,*ippDS);
682 /* the driver is now open, so it's now allowed to call GetCaps */
684 IDsDriver_GetCaps(drv,&((*ippDS)->drvcaps));
688 /* FIXME: look at wcaps */
689 (*ippDS)->drvcaps.dwFlags =
690 DSCAPS_PRIMARY16BIT | DSCAPS_PRIMARYSTEREO;
692 (*ippDS)->drvcaps.dwFlags |= DSCAPS_EMULDRIVER;
694 /* Allocate memory for HEL buffer headers */
695 for (c=0; c<DS_HEL_FRAGS; c++) {
696 (*ippDS)->pwave[c] = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(WAVEHDR));
697 if (!(*ippDS)->pwave[c]) {
698 /* Argh, out of memory */
700 HeapFree(GetProcessHeap(),0,(*ippDS)->pwave[c]);
701 waveOutClose((*ippDS)->hwo);
702 HeapFree(GetProcessHeap(),0,*ippDS);
704 return DSERR_OUTOFMEMORY;
710 DSOUND_RecalcVolPan(&((*ippDS)->volpan));
712 InitializeCriticalSection(&((*ippDS)->mixlock));
713 RtlInitializeResource(&((*ippDS)->lock));
717 DSOUND_PrimaryCreate(dsound);
718 timeBeginPeriod(DS_TIME_RES);
719 dsound->timerID = timeSetEvent(DS_TIME_DEL, DS_TIME_RES, DSOUND_timer,
720 (DWORD)dsound, TIME_PERIODIC | TIME_CALLBACK_FUNCTION);
726 /*******************************************************************************
727 * DirectSound ClassFactory
731 /* IUnknown fields */
732 ICOM_VFIELD(IClassFactory);
736 static HRESULT WINAPI
737 DSCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
738 ICOM_THIS(IClassFactoryImpl,iface);
740 FIXME("(%p)->(%s,%p),stub!\n",This,debugstr_guid(riid),ppobj);
741 return E_NOINTERFACE;
745 DSCF_AddRef(LPCLASSFACTORY iface) {
746 ICOM_THIS(IClassFactoryImpl,iface);
747 return ++(This->ref);
750 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface) {
751 ICOM_THIS(IClassFactoryImpl,iface);
752 /* static class, won't be freed */
753 return --(This->ref);
756 static HRESULT WINAPI DSCF_CreateInstance(
757 LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj
759 ICOM_THIS(IClassFactoryImpl,iface);
761 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
762 if ( IsEqualGUID( &IID_IDirectSound, riid ) ||
763 IsEqualGUID( &IID_IDirectSound8, riid ) ) {
764 /* FIXME: reuse already created dsound if present? */
765 return DirectSoundCreate8(riid,(LPDIRECTSOUND8*)ppobj,pOuter);
767 return E_NOINTERFACE;
770 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
771 ICOM_THIS(IClassFactoryImpl,iface);
772 FIXME("(%p)->(%d),stub!\n",This,dolock);
776 static ICOM_VTABLE(IClassFactory) DSCF_Vtbl = {
777 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
784 static IClassFactoryImpl DSOUND_CF = {&DSCF_Vtbl, 1 };
786 /*******************************************************************************
787 * DllGetClassObject [DSOUND.5]
788 * Retrieves class object from a DLL object
791 * Docs say returns STDAPI
794 * rclsid [I] CLSID for the class object
795 * riid [I] Reference to identifier of interface for class object
796 * ppv [O] Address of variable to receive interface pointer for riid
800 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
803 DWORD WINAPI DSOUND_DllGetClassObject(REFCLSID rclsid,REFIID riid,LPVOID *ppv)
805 TRACE("(%p,%p,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
806 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
807 *ppv = (LPVOID)&DSOUND_CF;
808 IClassFactory_AddRef((IClassFactory*)*ppv);
812 FIXME("(%p,%p,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
813 return CLASS_E_CLASSNOTAVAILABLE;
817 /*******************************************************************************
818 * DllCanUnloadNow [DSOUND.4] Determines whether the DLL is in use.
824 DWORD WINAPI DSOUND_DllCanUnloadNow(void)
826 FIXME("(void): stub\n");