2 * Sample Wine Driver for Open Sound System (featured in Linux and FreeBSD)
4 * Copyright 1994 Martin Ayotte
5 * 1999 Eric Pouech (async playing in waveOut/waveIn)
6 * 2000 Eric Pouech (loops in waveOut)
7 * 2002 Eric Pouech (full duplex)
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/port.h"
36 #ifdef HAVE_SYS_IOCTL_H
37 # include <sys/ioctl.h>
39 #ifdef HAVE_SYS_MMAN_H
40 # include <sys/mman.h>
42 #ifdef HAVE_SYS_POLL_H
43 # include <sys/poll.h>
56 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(wave
);
64 /*======================================================================*
65 * Low level DSOUND definitions *
66 *======================================================================*/
68 typedef struct IDsDriverPropertySetImpl IDsDriverPropertySetImpl
;
69 typedef struct IDsDriverNotifyImpl IDsDriverNotifyImpl
;
70 typedef struct IDsDriverImpl IDsDriverImpl
;
71 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl
;
73 struct IDsDriverPropertySetImpl
76 IDsDriverPropertySetVtbl
*lpVtbl
;
79 IDsDriverBufferImpl
* buffer
;
82 struct IDsDriverNotifyImpl
85 IDsDriverNotifyVtbl
*lpVtbl
;
88 /* IDsDriverNotifyImpl fields */
89 LPDSBPOSITIONNOTIFY notifies
;
92 IDsDriverBufferImpl
* buffer
;
98 IDsDriverVtbl
*lpVtbl
;
101 /* IDsDriverImpl fields */
103 IDsDriverBufferImpl
* primary
;
106 IDsDriverBufferImpl
** secondaries
;
109 struct IDsDriverBufferImpl
111 /* IUnknown fields */
112 IDsDriverBufferVtbl
*lpVtbl
;
115 /* IDsDriverBufferImpl fields */
118 WAVEFORMATPCMEX wfex
;
124 /* IDsDriverNotifyImpl fields */
125 IDsDriverNotifyImpl
* notify
;
128 /* IDsDriverPropertySetImpl fields */
129 IDsDriverPropertySetImpl
* property_set
;
132 static HRESULT WINAPI
IDsDriverPropertySetImpl_Create(
133 IDsDriverBufferImpl
* dsdb
,
134 IDsDriverPropertySetImpl
**pdsdps
);
136 static HRESULT WINAPI
IDsDriverNotifyImpl_Create(
137 IDsDriverBufferImpl
* dsdb
,
138 IDsDriverNotifyImpl
**pdsdn
);
140 /*======================================================================*
141 * Low level DSOUND property set implementation *
142 *======================================================================*/
144 static HRESULT WINAPI
IDsDriverPropertySetImpl_QueryInterface(
145 PIDSDRIVERPROPERTYSET iface
,
149 IDsDriverPropertySetImpl
*This
= (IDsDriverPropertySetImpl
*)iface
;
150 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
152 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
153 IsEqualGUID(riid
, &IID_IDsDriverPropertySet
) ) {
154 IDsDriverPropertySet_AddRef(iface
);
155 *ppobj
= (LPVOID
)This
;
159 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
162 return E_NOINTERFACE
;
165 static ULONG WINAPI
IDsDriverPropertySetImpl_AddRef(PIDSDRIVERPROPERTYSET iface
)
167 IDsDriverPropertySetImpl
*This
= (IDsDriverPropertySetImpl
*)iface
;
168 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
170 return InterlockedIncrement(&(This
->ref
));
173 static ULONG WINAPI
IDsDriverPropertySetImpl_Release(PIDSDRIVERPROPERTYSET iface
)
175 IDsDriverPropertySetImpl
*This
= (IDsDriverPropertySetImpl
*)iface
;
177 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
179 ref
= InterlockedDecrement(&(This
->ref
));
181 IDsDriverBuffer_Release((PIDSDRIVERBUFFER
)This
->buffer
);
182 HeapFree(GetProcessHeap(),0,This
);
183 TRACE("(%p) released\n",This
);
188 static HRESULT WINAPI
IDsDriverPropertySetImpl_Get(
189 PIDSDRIVERPROPERTYSET iface
,
190 PDSPROPERTY pDsProperty
,
191 LPVOID pPropertyParams
,
192 ULONG cbPropertyParams
,
193 LPVOID pPropertyData
,
194 ULONG cbPropertyData
,
195 PULONG pcbReturnedData
)
197 IDsDriverPropertySetImpl
*This
= (IDsDriverPropertySetImpl
*)iface
;
198 FIXME("(%p,%p,%p,%lx,%p,%lx,%p)\n",This
,pDsProperty
,pPropertyParams
,cbPropertyParams
,pPropertyData
,cbPropertyData
,pcbReturnedData
);
199 return DSERR_UNSUPPORTED
;
202 static HRESULT WINAPI
IDsDriverPropertySetImpl_Set(
203 PIDSDRIVERPROPERTYSET iface
,
204 PDSPROPERTY pDsProperty
,
205 LPVOID pPropertyParams
,
206 ULONG cbPropertyParams
,
207 LPVOID pPropertyData
,
208 ULONG cbPropertyData
)
210 IDsDriverPropertySetImpl
*This
= (IDsDriverPropertySetImpl
*)iface
;
211 FIXME("(%p,%p,%p,%lx,%p,%lx)\n",This
,pDsProperty
,pPropertyParams
,cbPropertyParams
,pPropertyData
,cbPropertyData
);
212 return DSERR_UNSUPPORTED
;
215 static HRESULT WINAPI
IDsDriverPropertySetImpl_QuerySupport(
216 PIDSDRIVERPROPERTYSET iface
,
217 REFGUID PropertySetId
,
221 IDsDriverPropertySetImpl
*This
= (IDsDriverPropertySetImpl
*)iface
;
222 FIXME("(%p,%s,%lx,%p)\n",This
,debugstr_guid(PropertySetId
),PropertyId
,pSupport
);
223 return DSERR_UNSUPPORTED
;
226 IDsDriverPropertySetVtbl dsdpsvt
=
228 IDsDriverPropertySetImpl_QueryInterface
,
229 IDsDriverPropertySetImpl_AddRef
,
230 IDsDriverPropertySetImpl_Release
,
231 IDsDriverPropertySetImpl_Get
,
232 IDsDriverPropertySetImpl_Set
,
233 IDsDriverPropertySetImpl_QuerySupport
,
236 /*======================================================================*
237 * Low level DSOUND notify implementation *
238 *======================================================================*/
240 static HRESULT WINAPI
IDsDriverNotifyImpl_QueryInterface(
241 PIDSDRIVERNOTIFY iface
,
245 IDsDriverNotifyImpl
*This
= (IDsDriverNotifyImpl
*)iface
;
246 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
248 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
249 IsEqualGUID(riid
, &IID_IDsDriverNotify
) ) {
250 IDsDriverNotify_AddRef(iface
);
255 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
258 return E_NOINTERFACE
;
261 static ULONG WINAPI
IDsDriverNotifyImpl_AddRef(PIDSDRIVERNOTIFY iface
)
263 IDsDriverNotifyImpl
*This
= (IDsDriverNotifyImpl
*)iface
;
264 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
266 return InterlockedIncrement(&(This
->ref
));
269 static ULONG WINAPI
IDsDriverNotifyImpl_Release(PIDSDRIVERNOTIFY iface
)
271 IDsDriverNotifyImpl
*This
= (IDsDriverNotifyImpl
*)iface
;
273 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
275 ref
= InterlockedDecrement(&(This
->ref
));
277 IDsDriverBuffer_Release((PIDSDRIVERBUFFER
)This
->buffer
);
278 if (This
->notifies
!= NULL
)
279 HeapFree(GetProcessHeap(), 0, This
->notifies
);
280 HeapFree(GetProcessHeap(),0,This
);
281 TRACE("(%p) released\n",This
);
286 static HRESULT WINAPI
IDsDriverNotifyImpl_SetNotificationPositions(
287 PIDSDRIVERNOTIFY iface
,
289 LPCDSBPOSITIONNOTIFY notify
)
291 IDsDriverNotifyImpl
*This
= (IDsDriverNotifyImpl
*)iface
;
292 TRACE("(%p,0x%08lx,%p)\n",This
,howmuch
,notify
);
295 WARN("invalid parameter\n");
296 return DSERR_INVALIDPARAM
;
299 if (TRACE_ON(wave
)) {
301 for (i
=0;i
<howmuch
;i
++)
302 TRACE("notify at %ld to 0x%08lx\n",
303 notify
[i
].dwOffset
,(DWORD
)notify
[i
].hEventNotify
);
306 /* Make an internal copy of the caller-supplied array.
307 * Replace the existing copy if one is already present. */
309 This
->notifies
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
310 This
->notifies
, howmuch
* sizeof(DSBPOSITIONNOTIFY
));
312 This
->notifies
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
313 howmuch
* sizeof(DSBPOSITIONNOTIFY
));
315 memcpy(This
->notifies
, notify
, howmuch
* sizeof(DSBPOSITIONNOTIFY
));
316 This
->nrofnotifies
= howmuch
;
321 IDsDriverNotifyVtbl dsdnvt
=
323 IDsDriverNotifyImpl_QueryInterface
,
324 IDsDriverNotifyImpl_AddRef
,
325 IDsDriverNotifyImpl_Release
,
326 IDsDriverNotifyImpl_SetNotificationPositions
,
329 /*======================================================================*
330 * Low level DSOUND implementation *
331 *======================================================================*/
333 static HRESULT
DSDB_MapBuffer(IDsDriverBufferImpl
*dsdb
)
335 TRACE("(%p), format=%ldx%dx%d\n", dsdb
, dsdb
->wfex
.Format
.nSamplesPerSec
,
336 dsdb
->wfex
.Format
.wBitsPerSample
, dsdb
->wfex
.Format
.nChannels
);
337 if (!dsdb
->mapping
) {
338 dsdb
->mapping
= mmap(NULL
, dsdb
->maplen
, PROT_WRITE
, MAP_SHARED
,
340 if (dsdb
->mapping
== (LPBYTE
)-1) {
341 ERR("Could not map sound device for direct access (%s)\n", strerror(errno
));
342 ERR("Use: \"HardwareAcceleration\" = \"Emulation\" in the [dsound] section of your config file.\n");
343 return DSERR_GENERIC
;
345 TRACE("The sound device has been mapped for direct access at %p, size=%ld\n", dsdb
->mapping
, dsdb
->maplen
);
347 /* for some reason, es1371 and sblive! sometimes have junk in here.
348 * clear it, or we get junk noise */
349 /* some libc implementations are buggy: their memset reads from the buffer...
350 * to work around it, we have to zero the block by hand. We don't do the expected:
351 * memset(dsdb->mapping,0, dsdb->maplen);
354 unsigned char* p1
= dsdb
->mapping
;
355 unsigned len
= dsdb
->maplen
;
356 unsigned char silence
= (dsdb
->wfex
.Format
.wBitsPerSample
== 8) ? 128 : 0;
357 unsigned long ulsilence
= (dsdb
->wfex
.Format
.wBitsPerSample
== 8) ? 0x80808080 : 0;
359 if (len
>= 16) /* so we can have at least a 4 long area to store... */
361 /* the mmap:ed value is (at least) dword aligned
362 * so, start filling the complete unsigned long:s
365 unsigned long* p4
= (unsigned long*)p1
;
367 while (b
--) *p4
++ = ulsilence
;
368 /* prepare for filling the rest */
370 p1
= (unsigned char*)p4
;
372 /* in all cases, fill the remaining bytes */
373 while (len
-- != 0) *p1
++ = silence
;
379 static HRESULT
DSDB_UnmapBuffer(IDsDriverBufferImpl
*dsdb
)
381 TRACE("(%p)\n",dsdb
);
383 if (munmap(dsdb
->mapping
, dsdb
->maplen
) < 0) {
384 ERR("(%p): Could not unmap sound device (%s)\n", dsdb
, strerror(errno
));
385 return DSERR_GENERIC
;
387 dsdb
->mapping
= NULL
;
388 TRACE("(%p): sound device unmapped\n", dsdb
);
393 static HRESULT WINAPI
IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface
, REFIID riid
, LPVOID
*ppobj
)
395 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
396 TRACE("(%p,%s,%p)\n",iface
,debugstr_guid(riid
),*ppobj
);
398 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
399 IsEqualGUID(riid
, &IID_IDsDriverBuffer
) ) {
400 IDsDriverBuffer_AddRef(iface
);
401 *ppobj
= (LPVOID
)This
;
405 if ( IsEqualGUID( &IID_IDsDriverNotify
, riid
) ) {
407 IDsDriverNotifyImpl_Create(This
, &(This
->notify
));
409 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY
)This
->notify
);
410 *ppobj
= (LPVOID
)This
->notify
;
417 if ( IsEqualGUID( &IID_IDsDriverPropertySet
, riid
) ) {
418 if (!This
->property_set
)
419 IDsDriverPropertySetImpl_Create(This
, &(This
->property_set
));
420 if (This
->property_set
) {
421 IDsDriverPropertySet_AddRef((PIDSDRIVERPROPERTYSET
)This
->property_set
);
422 *ppobj
= (LPVOID
)This
->property_set
;
429 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
433 return E_NOINTERFACE
;
436 static ULONG WINAPI
IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface
)
438 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
439 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
441 return InterlockedIncrement(&(This
->ref
));
444 static ULONG WINAPI
IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface
)
446 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
448 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
450 ref
= InterlockedDecrement(&(This
->ref
));
454 if (This
== This
->drv
->primary
)
455 This
->drv
->primary
= NULL
;
458 for (i
= 0; i
< This
->drv
->nrofsecondaries
; i
++)
459 if (This
->drv
->secondaries
[i
] == This
)
461 if (i
< This
->drv
->nrofsecondaries
) {
462 /* Put the last buffer of the list in the (now empty) position */
463 This
->drv
->secondaries
[i
] = This
->drv
->secondaries
[This
->drv
->nrofsecondaries
- 1];
464 This
->drv
->nrofsecondaries
--;
465 This
->drv
->secondaries
= HeapReAlloc(GetProcessHeap(),0,
466 This
->drv
->secondaries
,
467 sizeof(PIDSDRIVERBUFFER
)*This
->drv
->nrofsecondaries
);
468 TRACE("(%p) buffer count is now %d\n", This
, This
->drv
->nrofsecondaries
);
471 WOutDev
[This
->drv
->wDevID
].ossdev
->ds_caps
.dwFreeHwMixingAllBuffers
++;
472 WOutDev
[This
->drv
->wDevID
].ossdev
->ds_caps
.dwFreeHwMixingStreamingBuffers
++;
475 DSDB_UnmapBuffer(This
);
476 HeapFree(GetProcessHeap(),0,This
);
477 TRACE("(%p) released\n",This
);
481 static HRESULT WINAPI
IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface
,
482 LPVOID
*ppvAudio1
,LPDWORD pdwLen1
,
483 LPVOID
*ppvAudio2
,LPDWORD pdwLen2
,
484 DWORD dwWritePosition
,DWORD dwWriteLen
,
487 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
488 /* since we (GetDriverDesc flags) have specified DSDDESC_DONTNEEDPRIMARYLOCK,
489 * and that we don't support secondary buffers, this method will never be called */
490 TRACE("(%p): stub\n",iface
);
491 return DSERR_UNSUPPORTED
;
494 static HRESULT WINAPI
IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface
,
495 LPVOID pvAudio1
,DWORD dwLen1
,
496 LPVOID pvAudio2
,DWORD dwLen2
)
498 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
499 TRACE("(%p): stub\n",iface
);
500 return DSERR_UNSUPPORTED
;
503 static HRESULT WINAPI
IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface
,
506 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
508 TRACE("(%p,%p)\n",iface
,pwfx
);
509 /* On our request (GetDriverDesc flags), DirectSound has by now used
510 * waveOutClose/waveOutOpen to set the format...
511 * unfortunately, this means our mmap() is now gone...
512 * so we need to somehow signal to our DirectSound implementation
513 * that it should completely recreate this HW buffer...
514 * this unexpected error code should do the trick... */
515 return DSERR_BUFFERLOST
;
518 static HRESULT WINAPI
IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface
, DWORD dwFreq
)
520 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
521 TRACE("(%p,%ld): stub\n",iface
,dwFreq
);
522 return DSERR_UNSUPPORTED
;
525 static HRESULT WINAPI
IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface
, PDSVOLUMEPAN pVolPan
)
528 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
529 TRACE("(%p,%p)\n",This
,pVolPan
);
531 vol
= pVolPan
->dwTotalLeftAmpFactor
| (pVolPan
->dwTotalRightAmpFactor
<< 16);
533 if (wodSetVolume(This
->drv
->wDevID
, vol
) != MMSYSERR_NOERROR
) {
534 WARN("wodSetVolume failed\n");
535 return DSERR_INVALIDPARAM
;
541 static HRESULT WINAPI
IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface
, DWORD dwNewPos
)
543 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
544 TRACE("(%p,%ld): stub\n",iface
,dwNewPos
);
545 return DSERR_UNSUPPORTED
;
548 static HRESULT WINAPI
IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface
,
549 LPDWORD lpdwPlay
, LPDWORD lpdwWrite
)
551 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
555 TRACE("(%p)\n",iface
);
556 if (WOutDev
[This
->drv
->wDevID
].state
== WINE_WS_CLOSED
) {
557 ERR("device not open, but accessing?\n");
558 return DSERR_UNINITIALIZED
;
560 if (ioctl(This
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
561 ERR("ioctl(%s, SNDCTL_DSP_GETOPTR) failed (%s)\n",
562 WOutDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
563 return DSERR_GENERIC
;
565 ptr
= info
.ptr
& ~3; /* align the pointer, just in case */
566 if (lpdwPlay
) *lpdwPlay
= ptr
;
568 /* add some safety margin (not strictly necessary, but...) */
569 if (WOutDev
[This
->drv
->wDevID
].ossdev
->duplex_out_caps
.dwSupport
& WAVECAPS_SAMPLEACCURATE
)
570 *lpdwWrite
= ptr
+ 32;
572 *lpdwWrite
= ptr
+ WOutDev
[This
->drv
->wDevID
].dwFragmentSize
;
573 while (*lpdwWrite
> This
->buflen
)
574 *lpdwWrite
-= This
->buflen
;
576 TRACE("playpos=%ld, writepos=%ld\n", lpdwPlay
?*lpdwPlay
:0, lpdwWrite
?*lpdwWrite
:0);
580 static HRESULT WINAPI
IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface
, DWORD dwRes1
, DWORD dwRes2
, DWORD dwFlags
)
582 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
584 TRACE("(%p,%lx,%lx,%lx)\n",iface
,dwRes1
,dwRes2
,dwFlags
);
585 WOutDev
[This
->drv
->wDevID
].ossdev
->bOutputEnabled
= TRUE
;
586 enable
= getEnables(WOutDev
[This
->drv
->wDevID
].ossdev
);
587 if (ioctl(This
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
588 if (errno
== EINVAL
) {
589 /* Don't give up yet. OSS trigger support is inconsistent. */
590 if (WOutDev
[This
->drv
->wDevID
].ossdev
->open_count
== 1) {
591 /* try the opposite input enable */
592 if (WOutDev
[This
->drv
->wDevID
].ossdev
->bInputEnabled
== FALSE
)
593 WOutDev
[This
->drv
->wDevID
].ossdev
->bInputEnabled
= TRUE
;
595 WOutDev
[This
->drv
->wDevID
].ossdev
->bInputEnabled
= FALSE
;
597 enable
= getEnables(WOutDev
[This
->drv
->wDevID
].ossdev
);
598 if (ioctl(This
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) >= 0)
602 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",
603 WOutDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
604 WOutDev
[This
->drv
->wDevID
].ossdev
->bOutputEnabled
= FALSE
;
605 return DSERR_GENERIC
;
610 static HRESULT WINAPI
IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface
)
612 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
614 TRACE("(%p)\n",iface
);
615 /* no more playing */
616 WOutDev
[This
->drv
->wDevID
].ossdev
->bOutputEnabled
= FALSE
;
617 enable
= getEnables(WOutDev
[This
->drv
->wDevID
].ossdev
);
618 if (ioctl(This
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
619 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WOutDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
620 return DSERR_GENERIC
;
623 /* the play position must be reset to the beginning of the buffer */
624 if (ioctl(This
->fd
, SNDCTL_DSP_RESET
, 0) < 0) {
625 ERR("ioctl(%s, SNDCTL_DSP_RESET) failed (%s)\n", WOutDev
[This
->drv
->wDevID
].ossdev
->dev_name
, strerror(errno
));
626 return DSERR_GENERIC
;
629 /* Most OSS drivers just can't stop the playback without closing the device...
630 * so we need to somehow signal to our DirectSound implementation
631 * that it should completely recreate this HW buffer...
632 * this unexpected error code should do the trick... */
633 /* FIXME: ...unless we are doing full duplex, then it's not nice to close the device */
634 if (WOutDev
[This
->drv
->wDevID
].ossdev
->open_count
== 1)
635 return DSERR_BUFFERLOST
;
640 static IDsDriverBufferVtbl dsdbvt
=
642 IDsDriverBufferImpl_QueryInterface
,
643 IDsDriverBufferImpl_AddRef
,
644 IDsDriverBufferImpl_Release
,
645 IDsDriverBufferImpl_Lock
,
646 IDsDriverBufferImpl_Unlock
,
647 IDsDriverBufferImpl_SetFormat
,
648 IDsDriverBufferImpl_SetFrequency
,
649 IDsDriverBufferImpl_SetVolumePan
,
650 IDsDriverBufferImpl_SetPosition
,
651 IDsDriverBufferImpl_GetPosition
,
652 IDsDriverBufferImpl_Play
,
653 IDsDriverBufferImpl_Stop
656 static HRESULT WINAPI
IDsDriverImpl_QueryInterface(PIDSDRIVER iface
, REFIID riid
, LPVOID
*ppobj
)
658 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
659 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
661 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
662 IsEqualGUID(riid
, &IID_IDsDriver
) ) {
663 IDsDriver_AddRef(iface
);
664 *ppobj
= (LPVOID
)This
;
668 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
672 return E_NOINTERFACE
;
675 static ULONG WINAPI
IDsDriverImpl_AddRef(PIDSDRIVER iface
)
677 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
678 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
680 return InterlockedIncrement(&(This
->ref
));
683 static ULONG WINAPI
IDsDriverImpl_Release(PIDSDRIVER iface
)
685 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
687 TRACE("(%p) ref was %ld\n", This
, This
->ref
);
689 ref
= InterlockedDecrement(&(This
->ref
));
691 HeapFree(GetProcessHeap(),0,This
);
692 TRACE("(%p) released\n",This
);
697 static HRESULT WINAPI
IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface
,
700 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
701 TRACE("(%p,%p)\n",iface
,pDesc
);
703 /* copy version from driver */
704 memcpy(pDesc
, &(WOutDev
[This
->wDevID
].ossdev
->ds_desc
), sizeof(DSDRIVERDESC
));
706 pDesc
->dwFlags
|= DSDDESC_DOMMSYSTEMOPEN
| DSDDESC_DOMMSYSTEMSETFORMAT
|
707 DSDDESC_USESYSTEMMEMORY
| DSDDESC_DONTNEEDPRIMARYLOCK
|
708 DSDDESC_DONTNEEDSECONDARYLOCK
;
709 pDesc
->dnDevNode
= WOutDev
[This
->wDevID
].waveDesc
.dnDevNode
;
711 pDesc
->wReserved
= 0;
712 pDesc
->ulDeviceNum
= This
->wDevID
;
713 pDesc
->dwHeapType
= DSDHEAP_NOHEAP
;
714 pDesc
->pvDirectDrawHeap
= NULL
;
715 pDesc
->dwMemStartAddress
= 0;
716 pDesc
->dwMemEndAddress
= 0;
717 pDesc
->dwMemAllocExtra
= 0;
718 pDesc
->pvReserved1
= NULL
;
719 pDesc
->pvReserved2
= NULL
;
723 static HRESULT WINAPI
IDsDriverImpl_Open(PIDSDRIVER iface
)
725 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
727 TRACE("(%p)\n",iface
);
729 /* make sure the card doesn't start playing before we want it to */
730 WOutDev
[This
->wDevID
].ossdev
->bOutputEnabled
= FALSE
;
731 enable
= getEnables(WOutDev
[This
->wDevID
].ossdev
);
732 if (ioctl(WOutDev
[This
->wDevID
].ossdev
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
733 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",WOutDev
[This
->wDevID
].ossdev
->dev_name
, strerror(errno
));
734 return DSERR_GENERIC
;
739 static HRESULT WINAPI
IDsDriverImpl_Close(PIDSDRIVER iface
)
741 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
742 TRACE("(%p)\n",iface
);
744 ERR("problem with DirectSound: primary not released\n");
745 return DSERR_GENERIC
;
750 static HRESULT WINAPI
IDsDriverImpl_GetCaps(PIDSDRIVER iface
, PDSDRIVERCAPS pCaps
)
752 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
753 TRACE("(%p,%p)\n",iface
,pCaps
);
754 memcpy(pCaps
, &(WOutDev
[This
->wDevID
].ossdev
->ds_caps
), sizeof(DSDRIVERCAPS
));
758 static HRESULT WINAPI
DSD_CreatePrimaryBuffer(PIDSDRIVER iface
,
762 LPDWORD pdwcbBufferSize
,
766 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
767 IDsDriverBufferImpl
** ippdsdb
= (IDsDriverBufferImpl
**)ppvObj
;
771 TRACE("(%p,%p,%lx,%lx,%p,%p,%p)\n",iface
,pwfx
,dwFlags
,dwCardAddress
,pdwcbBufferSize
,ppbBuffer
,ppvObj
);
774 return DSERR_ALLOCATED
;
775 if (dwFlags
& (DSBCAPS_CTRLFREQUENCY
| DSBCAPS_CTRLPAN
))
776 return DSERR_CONTROLUNAVAIL
;
778 *ippdsdb
= (IDsDriverBufferImpl
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDsDriverBufferImpl
));
779 if (*ippdsdb
== NULL
)
780 return DSERR_OUTOFMEMORY
;
781 (*ippdsdb
)->lpVtbl
= &dsdbvt
;
783 (*ippdsdb
)->drv
= This
;
784 copy_format(pwfx
, &(*ippdsdb
)->wfex
);
785 (*ippdsdb
)->fd
= WOutDev
[This
->wDevID
].ossdev
->fd
;
786 (*ippdsdb
)->dwFlags
= dwFlags
;
788 /* check how big the DMA buffer is now */
789 if (ioctl((*ippdsdb
)->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
790 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n",
791 WOutDev
[This
->wDevID
].ossdev
->dev_name
, strerror(errno
));
792 HeapFree(GetProcessHeap(),0,*ippdsdb
);
794 return DSERR_GENERIC
;
796 (*ippdsdb
)->maplen
= (*ippdsdb
)->buflen
= info
.fragstotal
* info
.fragsize
;
798 /* map the DMA buffer */
799 err
= DSDB_MapBuffer(*ippdsdb
);
801 HeapFree(GetProcessHeap(),0,*ippdsdb
);
806 /* primary buffer is ready to go */
807 *pdwcbBufferSize
= (*ippdsdb
)->maplen
;
808 *ppbBuffer
= (*ippdsdb
)->mapping
;
810 /* some drivers need some extra nudging after mapping */
811 WOutDev
[This
->wDevID
].ossdev
->bOutputEnabled
= FALSE
;
812 enable
= getEnables(WOutDev
[This
->wDevID
].ossdev
);
813 if (ioctl((*ippdsdb
)->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
814 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",
815 WOutDev
[This
->wDevID
].ossdev
->dev_name
, strerror(errno
));
816 return DSERR_GENERIC
;
819 This
->primary
= *ippdsdb
;
824 static HRESULT WINAPI
DSD_CreateSecondaryBuffer(PIDSDRIVER iface
,
828 LPDWORD pdwcbBufferSize
,
832 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
833 IDsDriverBufferImpl
** ippdsdb
= (IDsDriverBufferImpl
**)ppvObj
;
834 FIXME("(%p,%p,%lx,%lx,%p,%p,%p): stub\n",This
,pwfx
,dwFlags
,dwCardAddress
,pdwcbBufferSize
,ppbBuffer
,ppvObj
);
837 return DSERR_UNSUPPORTED
;
840 static HRESULT WINAPI
IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface
,
844 LPDWORD pdwcbBufferSize
,
848 TRACE("(%p,%p,%lx,%lx,%p,%p,%p)\n",iface
,pwfx
,dwFlags
,dwCardAddress
,pdwcbBufferSize
,ppbBuffer
,ppvObj
);
850 if (dwFlags
& DSBCAPS_PRIMARYBUFFER
)
851 return DSD_CreatePrimaryBuffer(iface
,pwfx
,dwFlags
,dwCardAddress
,pdwcbBufferSize
,ppbBuffer
,ppvObj
);
853 return DSD_CreateSecondaryBuffer(iface
,pwfx
,dwFlags
,dwCardAddress
,pdwcbBufferSize
,ppbBuffer
,ppvObj
);
856 static HRESULT WINAPI
IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface
,
857 PIDSDRIVERBUFFER pBuffer
,
860 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
861 TRACE("(%p,%p): stub\n",iface
,pBuffer
);
862 return DSERR_INVALIDCALL
;
865 static IDsDriverVtbl dsdvt
=
867 IDsDriverImpl_QueryInterface
,
868 IDsDriverImpl_AddRef
,
869 IDsDriverImpl_Release
,
870 IDsDriverImpl_GetDriverDesc
,
873 IDsDriverImpl_GetCaps
,
874 IDsDriverImpl_CreateSoundBuffer
,
875 IDsDriverImpl_DuplicateSoundBuffer
878 static HRESULT WINAPI
IDsDriverPropertySetImpl_Create(
879 IDsDriverBufferImpl
* dsdb
,
880 IDsDriverPropertySetImpl
**pdsdps
)
882 IDsDriverPropertySetImpl
* dsdps
;
883 TRACE("(%p,%p)\n",dsdb
,pdsdps
);
885 dsdps
= (IDsDriverPropertySetImpl
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(dsdps
));
887 WARN("out of memory\n");
888 return DSERR_OUTOFMEMORY
;
892 dsdps
->lpVtbl
= &dsdpsvt
;
893 dsdps
->buffer
= dsdb
;
894 dsdb
->property_set
= dsdps
;
895 IDsDriverBuffer_AddRef((PIDSDRIVER
)dsdb
);
901 static HRESULT WINAPI
IDsDriverNotifyImpl_Create(
902 IDsDriverBufferImpl
* dsdb
,
903 IDsDriverNotifyImpl
**pdsdn
)
905 IDsDriverNotifyImpl
* dsdn
;
906 TRACE("(%p,%p)\n",dsdb
,pdsdn
);
908 dsdn
= (IDsDriverNotifyImpl
*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(dsdn
));
911 WARN("out of memory\n");
912 return DSERR_OUTOFMEMORY
;
916 dsdn
->lpVtbl
= &dsdnvt
;
919 IDsDriverBuffer_AddRef((PIDSDRIVER
)dsdb
);
925 DWORD
wodDsCreate(UINT wDevID
, PIDSDRIVER
* drv
)
927 IDsDriverImpl
** idrv
= (IDsDriverImpl
**)drv
;
928 TRACE("(%d,%p)\n",wDevID
,drv
);
930 /* the HAL isn't much better than the HEL if we can't do mmap() */
931 if (!(WOutDev
[wDevID
].ossdev
->duplex_out_caps
.dwSupport
& WAVECAPS_DIRECTSOUND
)) {
932 ERR("DirectSound flag not set\n");
933 MESSAGE("This sound card's driver does not support direct access\n");
934 MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
935 return MMSYSERR_NOTSUPPORTED
;
938 *idrv
= (IDsDriverImpl
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDsDriverImpl
));
940 return MMSYSERR_NOMEM
;
941 (*idrv
)->lpVtbl
= &dsdvt
;
943 (*idrv
)->wDevID
= wDevID
;
944 (*idrv
)->primary
= NULL
;
945 (*idrv
)->nrofsecondaries
= 0;
946 (*idrv
)->secondaries
= NULL
;
948 return MMSYSERR_NOERROR
;
951 DWORD
wodDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
953 TRACE("(%d,%p)\n",wDevID
,desc
);
954 memcpy(desc
, &(WOutDev
[wDevID
].ossdev
->ds_desc
), sizeof(DSDRIVERDESC
));
955 return MMSYSERR_NOERROR
;
958 #endif /* HAVE_OSS */