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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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>
45 #ifdef HAVE_SYS_POLL_H
46 # include <sys/poll.h>
59 #include "wine/debug.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(wave
);
67 /*======================================================================*
68 * Low level DSOUND definitions *
69 *======================================================================*/
71 typedef struct IDsDriverPropertySetImpl IDsDriverPropertySetImpl
;
72 typedef struct IDsDriverNotifyImpl IDsDriverNotifyImpl
;
73 typedef struct IDsDriverImpl IDsDriverImpl
;
74 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl
;
76 struct IDsDriverPropertySetImpl
79 const IDsDriverPropertySetVtbl
*lpVtbl
;
82 IDsDriverBufferImpl
* buffer
;
85 struct IDsDriverNotifyImpl
88 const IDsDriverNotifyVtbl
*lpVtbl
;
91 /* IDsDriverNotifyImpl fields */
92 LPDSBPOSITIONNOTIFY notifies
;
95 IDsDriverBufferImpl
* buffer
;
100 /* IUnknown fields */
101 const IDsDriverVtbl
*lpVtbl
;
104 /* IDsDriverImpl fields */
106 IDsDriverBufferImpl
* primary
;
109 IDsDriverBufferImpl
** secondaries
;
112 struct IDsDriverBufferImpl
114 /* IUnknown fields */
115 const IDsDriverBufferVtbl
*lpVtbl
;
118 /* IDsDriverBufferImpl fields */
121 WAVEFORMATPCMEX wfex
;
127 /* IDsDriverNotifyImpl fields */
128 IDsDriverNotifyImpl
* notify
;
131 /* IDsDriverPropertySetImpl fields */
132 IDsDriverPropertySetImpl
* property_set
;
135 static HRESULT WINAPI
IDsDriverPropertySetImpl_Create(
136 IDsDriverBufferImpl
* dsdb
,
137 IDsDriverPropertySetImpl
**pdsdps
);
139 static HRESULT WINAPI
IDsDriverNotifyImpl_Create(
140 IDsDriverBufferImpl
* dsdb
,
141 IDsDriverNotifyImpl
**pdsdn
);
143 /*======================================================================*
144 * Low level DSOUND property set implementation *
145 *======================================================================*/
147 static HRESULT WINAPI
IDsDriverPropertySetImpl_QueryInterface(
148 PIDSDRIVERPROPERTYSET iface
,
152 IDsDriverPropertySetImpl
*This
= (IDsDriverPropertySetImpl
*)iface
;
153 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
155 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
156 IsEqualGUID(riid
, &IID_IDsDriverPropertySet
) ) {
157 IDsDriverPropertySet_AddRef(iface
);
158 *ppobj
= (LPVOID
)This
;
162 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
165 return E_NOINTERFACE
;
168 static ULONG WINAPI
IDsDriverPropertySetImpl_AddRef(PIDSDRIVERPROPERTYSET iface
)
170 IDsDriverPropertySetImpl
*This
= (IDsDriverPropertySetImpl
*)iface
;
171 ULONG refCount
= InterlockedIncrement(&This
->ref
);
173 TRACE("(%p) ref was %d\n", This
, refCount
- 1);
178 static ULONG WINAPI
IDsDriverPropertySetImpl_Release(PIDSDRIVERPROPERTYSET iface
)
180 IDsDriverPropertySetImpl
*This
= (IDsDriverPropertySetImpl
*)iface
;
181 ULONG refCount
= InterlockedDecrement(&This
->ref
);
183 TRACE("(%p) ref was %d\n", This
, refCount
+ 1);
186 IDsDriverBuffer_Release((PIDSDRIVERBUFFER
)This
->buffer
);
187 HeapFree(GetProcessHeap(),0,This
);
188 TRACE("(%p) released\n",This
);
193 static HRESULT WINAPI
IDsDriverPropertySetImpl_Get(
194 PIDSDRIVERPROPERTYSET iface
,
195 PDSPROPERTY pDsProperty
,
196 LPVOID pPropertyParams
,
197 ULONG cbPropertyParams
,
198 LPVOID pPropertyData
,
199 ULONG cbPropertyData
,
200 PULONG pcbReturnedData
)
202 IDsDriverPropertySetImpl
*This
= (IDsDriverPropertySetImpl
*)iface
;
203 FIXME("(%p,%p,%p,%x,%p,%x,%p)\n",This
,pDsProperty
,pPropertyParams
,cbPropertyParams
,pPropertyData
,cbPropertyData
,pcbReturnedData
);
204 return DSERR_UNSUPPORTED
;
207 static HRESULT WINAPI
IDsDriverPropertySetImpl_Set(
208 PIDSDRIVERPROPERTYSET iface
,
209 PDSPROPERTY pDsProperty
,
210 LPVOID pPropertyParams
,
211 ULONG cbPropertyParams
,
212 LPVOID pPropertyData
,
213 ULONG cbPropertyData
)
215 IDsDriverPropertySetImpl
*This
= (IDsDriverPropertySetImpl
*)iface
;
216 FIXME("(%p,%p,%p,%x,%p,%x)\n",This
,pDsProperty
,pPropertyParams
,cbPropertyParams
,pPropertyData
,cbPropertyData
);
217 return DSERR_UNSUPPORTED
;
220 static HRESULT WINAPI
IDsDriverPropertySetImpl_QuerySupport(
221 PIDSDRIVERPROPERTYSET iface
,
222 REFGUID PropertySetId
,
226 IDsDriverPropertySetImpl
*This
= (IDsDriverPropertySetImpl
*)iface
;
227 FIXME("(%p,%s,%x,%p)\n",This
,debugstr_guid(PropertySetId
),PropertyId
,pSupport
);
228 return DSERR_UNSUPPORTED
;
231 static const IDsDriverPropertySetVtbl dsdpsvt
=
233 IDsDriverPropertySetImpl_QueryInterface
,
234 IDsDriverPropertySetImpl_AddRef
,
235 IDsDriverPropertySetImpl_Release
,
236 IDsDriverPropertySetImpl_Get
,
237 IDsDriverPropertySetImpl_Set
,
238 IDsDriverPropertySetImpl_QuerySupport
,
241 /*======================================================================*
242 * Low level DSOUND notify implementation *
243 *======================================================================*/
245 static HRESULT WINAPI
IDsDriverNotifyImpl_QueryInterface(
246 PIDSDRIVERNOTIFY iface
,
250 IDsDriverNotifyImpl
*This
= (IDsDriverNotifyImpl
*)iface
;
251 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
253 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
254 IsEqualGUID(riid
, &IID_IDsDriverNotify
) ) {
255 IDsDriverNotify_AddRef(iface
);
260 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
263 return E_NOINTERFACE
;
266 static ULONG WINAPI
IDsDriverNotifyImpl_AddRef(PIDSDRIVERNOTIFY iface
)
268 IDsDriverNotifyImpl
*This
= (IDsDriverNotifyImpl
*)iface
;
269 ULONG refCount
= InterlockedIncrement(&This
->ref
);
271 TRACE("(%p) ref was %d\n", This
, refCount
- 1);
276 static ULONG WINAPI
IDsDriverNotifyImpl_Release(PIDSDRIVERNOTIFY iface
)
278 IDsDriverNotifyImpl
*This
= (IDsDriverNotifyImpl
*)iface
;
279 ULONG refCount
= InterlockedDecrement(&This
->ref
);
281 TRACE("(%p) ref was %d\n", This
, refCount
+ 1);
284 IDsDriverBuffer_Release((PIDSDRIVERBUFFER
)This
->buffer
);
285 HeapFree(GetProcessHeap(), 0, This
->notifies
);
286 HeapFree(GetProcessHeap(),0,This
);
287 TRACE("(%p) released\n",This
);
292 static HRESULT WINAPI
IDsDriverNotifyImpl_SetNotificationPositions(
293 PIDSDRIVERNOTIFY iface
,
295 LPCDSBPOSITIONNOTIFY notify
)
297 IDsDriverNotifyImpl
*This
= (IDsDriverNotifyImpl
*)iface
;
298 TRACE("(%p,0x%08x,%p)\n",This
,howmuch
,notify
);
301 WARN("invalid parameter\n");
302 return DSERR_INVALIDPARAM
;
305 if (TRACE_ON(wave
)) {
307 for (i
=0;i
<howmuch
;i
++)
308 TRACE("notify at %d to 0x%08x\n",
309 notify
[i
].dwOffset
,(DWORD
)notify
[i
].hEventNotify
);
312 /* Make an internal copy of the caller-supplied array.
313 * Replace the existing copy if one is already present. */
315 This
->notifies
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
316 This
->notifies
, howmuch
* sizeof(DSBPOSITIONNOTIFY
));
318 This
->notifies
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
,
319 howmuch
* sizeof(DSBPOSITIONNOTIFY
));
321 memcpy(This
->notifies
, notify
, howmuch
* sizeof(DSBPOSITIONNOTIFY
));
322 This
->nrofnotifies
= howmuch
;
327 static const IDsDriverNotifyVtbl dsdnvt
=
329 IDsDriverNotifyImpl_QueryInterface
,
330 IDsDriverNotifyImpl_AddRef
,
331 IDsDriverNotifyImpl_Release
,
332 IDsDriverNotifyImpl_SetNotificationPositions
,
335 /*======================================================================*
336 * Low level DSOUND implementation *
337 *======================================================================*/
339 static HRESULT
DSDB_MapBuffer(IDsDriverBufferImpl
*dsdb
)
341 TRACE("(%p), format=%dx%dx%d\n", dsdb
, dsdb
->wfex
.Format
.nSamplesPerSec
,
342 dsdb
->wfex
.Format
.wBitsPerSample
, dsdb
->wfex
.Format
.nChannels
);
343 if (!dsdb
->mapping
) {
344 dsdb
->mapping
= mmap(NULL
, dsdb
->maplen
, PROT_WRITE
, MAP_SHARED
,
346 if (dsdb
->mapping
== (LPBYTE
)-1) {
347 WARN("Could not map sound device for direct access (%s)\n", strerror(errno
));
348 return DSERR_GENERIC
;
350 TRACE("The sound device has been mapped for direct access at %p, size=%d\n", dsdb
->mapping
, dsdb
->maplen
);
352 /* for some reason, es1371 and sblive! sometimes have junk in here.
353 * clear it, or we get junk noise */
354 /* some libc implementations are buggy: their memset reads from the buffer...
355 * to work around it, we have to zero the block by hand. We don't do the expected:
356 * memset(dsdb->mapping,0, dsdb->maplen);
359 unsigned char* p1
= dsdb
->mapping
;
360 unsigned len
= dsdb
->maplen
;
361 unsigned char silence
= (dsdb
->wfex
.Format
.wBitsPerSample
== 8) ? 128 : 0;
362 unsigned long ulsilence
= (dsdb
->wfex
.Format
.wBitsPerSample
== 8) ? 0x80808080 : 0;
364 if (len
>= 16) /* so we can have at least a 4 long area to store... */
366 /* the mmap:ed value is (at least) dword aligned
367 * so, start filling the complete unsigned long:s
370 unsigned long* p4
= (unsigned long*)p1
;
372 while (b
--) *p4
++ = ulsilence
;
373 /* prepare for filling the rest */
375 p1
= (unsigned char*)p4
;
377 /* in all cases, fill the remaining bytes */
378 while (len
-- != 0) *p1
++ = silence
;
384 static HRESULT
DSDB_UnmapBuffer(IDsDriverBufferImpl
*dsdb
)
386 TRACE("(%p)\n",dsdb
);
388 if (munmap(dsdb
->mapping
, dsdb
->maplen
) < 0) {
389 ERR("(%p): Could not unmap sound device (%s)\n", dsdb
, strerror(errno
));
390 return DSERR_GENERIC
;
392 dsdb
->mapping
= NULL
;
393 TRACE("(%p): sound device unmapped\n", dsdb
);
398 static HRESULT WINAPI
IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface
, REFIID riid
, LPVOID
*ppobj
)
400 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
401 TRACE("(%p,%s,%p)\n",iface
,debugstr_guid(riid
),*ppobj
);
403 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
404 IsEqualGUID(riid
, &IID_IDsDriverBuffer
) ) {
405 IDsDriverBuffer_AddRef(iface
);
406 *ppobj
= (LPVOID
)This
;
410 if ( IsEqualGUID( &IID_IDsDriverNotify
, riid
) ) {
412 IDsDriverNotifyImpl_Create(This
, &(This
->notify
));
414 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY
)This
->notify
);
415 *ppobj
= (LPVOID
)This
->notify
;
422 if ( IsEqualGUID( &IID_IDsDriverPropertySet
, riid
) ) {
423 if (!This
->property_set
)
424 IDsDriverPropertySetImpl_Create(This
, &(This
->property_set
));
425 if (This
->property_set
) {
426 IDsDriverPropertySet_AddRef((PIDSDRIVERPROPERTYSET
)This
->property_set
);
427 *ppobj
= (LPVOID
)This
->property_set
;
434 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
438 return E_NOINTERFACE
;
441 static ULONG WINAPI
IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface
)
443 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
444 ULONG refCount
= InterlockedIncrement(&This
->ref
);
446 TRACE("(%p) ref was %d\n", This
, refCount
- 1);
451 static ULONG WINAPI
IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface
)
453 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
454 ULONG refCount
= InterlockedDecrement(&This
->ref
);
456 TRACE("(%p) ref was %d\n", This
, refCount
+ 1);
461 if (This
== This
->drv
->primary
)
462 This
->drv
->primary
= NULL
;
465 for (i
= 0; i
< This
->drv
->nrofsecondaries
; i
++)
466 if (This
->drv
->secondaries
[i
] == This
)
468 if (i
< This
->drv
->nrofsecondaries
) {
469 /* Put the last buffer of the list in the (now empty) position */
470 This
->drv
->secondaries
[i
] = This
->drv
->secondaries
[This
->drv
->nrofsecondaries
- 1];
471 This
->drv
->nrofsecondaries
--;
472 This
->drv
->secondaries
= HeapReAlloc(GetProcessHeap(),0,
473 This
->drv
->secondaries
,
474 sizeof(PIDSDRIVERBUFFER
)*This
->drv
->nrofsecondaries
);
475 TRACE("(%p) buffer count is now %d\n", This
, This
->drv
->nrofsecondaries
);
478 WOutDev
[This
->drv
->wDevID
].ossdev
.ds_caps
.dwFreeHwMixingAllBuffers
++;
479 WOutDev
[This
->drv
->wDevID
].ossdev
.ds_caps
.dwFreeHwMixingStreamingBuffers
++;
482 DSDB_UnmapBuffer(This
);
483 HeapFree(GetProcessHeap(),0,This
);
484 TRACE("(%p) released\n",This
);
488 static HRESULT WINAPI
IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface
,
489 LPVOID
*ppvAudio1
,LPDWORD pdwLen1
,
490 LPVOID
*ppvAudio2
,LPDWORD pdwLen2
,
491 DWORD dwWritePosition
,DWORD dwWriteLen
,
494 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
495 /* since we (GetDriverDesc flags) have specified DSDDESC_DONTNEEDPRIMARYLOCK,
496 * and that we don't support secondary buffers, this method will never be called */
497 TRACE("(%p): stub\n",iface
);
498 return DSERR_UNSUPPORTED
;
501 static HRESULT WINAPI
IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface
,
502 LPVOID pvAudio1
,DWORD dwLen1
,
503 LPVOID pvAudio2
,DWORD dwLen2
)
505 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
506 TRACE("(%p): stub\n",iface
);
507 return DSERR_UNSUPPORTED
;
510 static HRESULT WINAPI
IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface
,
513 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
515 TRACE("(%p,%p)\n",iface
,pwfx
);
516 /* On our request (GetDriverDesc flags), DirectSound has by now used
517 * waveOutClose/waveOutOpen to set the format...
518 * unfortunately, this means our mmap() is now gone...
519 * so we need to somehow signal to our DirectSound implementation
520 * that it should completely recreate this HW buffer...
521 * this unexpected error code should do the trick... */
522 return DSERR_BUFFERLOST
;
525 static HRESULT WINAPI
IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface
, DWORD dwFreq
)
527 /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
528 TRACE("(%p,%d): stub\n",iface
,dwFreq
);
529 return DSERR_UNSUPPORTED
;
532 static HRESULT WINAPI
IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface
, PDSVOLUMEPAN pVolPan
)
535 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
536 TRACE("(%p,%p)\n",This
,pVolPan
);
538 vol
= pVolPan
->dwTotalLeftAmpFactor
| (pVolPan
->dwTotalRightAmpFactor
<< 16);
540 if (wodSetVolume(This
->drv
->wDevID
, vol
) != MMSYSERR_NOERROR
) {
541 WARN("wodSetVolume failed\n");
542 return DSERR_INVALIDPARAM
;
548 static HRESULT WINAPI
IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface
, DWORD dwNewPos
)
550 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
551 TRACE("(%p,%d): stub\n",iface
,dwNewPos
);
552 return DSERR_UNSUPPORTED
;
555 static HRESULT WINAPI
IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface
,
556 LPDWORD lpdwPlay
, LPDWORD lpdwWrite
)
558 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
562 TRACE("(%p)\n",iface
);
563 if (WOutDev
[This
->drv
->wDevID
].state
== WINE_WS_CLOSED
) {
564 ERR("device not open, but accessing?\n");
565 return DSERR_UNINITIALIZED
;
567 if (ioctl(This
->fd
, SNDCTL_DSP_GETOPTR
, &info
) < 0) {
568 ERR("ioctl(%s, SNDCTL_DSP_GETOPTR) failed (%s)\n",
569 WOutDev
[This
->drv
->wDevID
].ossdev
.dev_name
, strerror(errno
));
570 return DSERR_GENERIC
;
572 ptr
= info
.ptr
& ~3; /* align the pointer, just in case */
573 if (lpdwPlay
) *lpdwPlay
= ptr
;
575 /* add some safety margin (not strictly necessary, but...) */
576 if (WOutDev
[This
->drv
->wDevID
].ossdev
.duplex_out_caps
.dwSupport
& WAVECAPS_SAMPLEACCURATE
)
577 *lpdwWrite
= ptr
+ 32;
579 *lpdwWrite
= ptr
+ WOutDev
[This
->drv
->wDevID
].dwFragmentSize
;
580 while (*lpdwWrite
>= This
->buflen
)
581 *lpdwWrite
-= This
->buflen
;
583 TRACE("playpos=%d, writepos=%d\n", lpdwPlay
?*lpdwPlay
:0, lpdwWrite
?*lpdwWrite
:0);
587 static HRESULT WINAPI
IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface
, DWORD dwRes1
, DWORD dwRes2
, DWORD dwFlags
)
589 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
591 TRACE("(%p,%x,%x,%x)\n",iface
,dwRes1
,dwRes2
,dwFlags
);
592 WOutDev
[This
->drv
->wDevID
].ossdev
.bOutputEnabled
= TRUE
;
593 enable
= getEnables(&WOutDev
[This
->drv
->wDevID
].ossdev
);
594 if (ioctl(This
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
595 if (errno
== EINVAL
) {
596 /* Don't give up yet. OSS trigger support is inconsistent. */
597 if (WOutDev
[This
->drv
->wDevID
].ossdev
.open_count
== 1) {
598 /* try the opposite input enable */
599 if (WOutDev
[This
->drv
->wDevID
].ossdev
.bInputEnabled
== FALSE
)
600 WOutDev
[This
->drv
->wDevID
].ossdev
.bInputEnabled
= TRUE
;
602 WOutDev
[This
->drv
->wDevID
].ossdev
.bInputEnabled
= FALSE
;
604 enable
= getEnables(&WOutDev
[This
->drv
->wDevID
].ossdev
);
605 if (ioctl(This
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) >= 0)
609 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",
610 WOutDev
[This
->drv
->wDevID
].ossdev
.dev_name
, strerror(errno
));
611 WOutDev
[This
->drv
->wDevID
].ossdev
.bOutputEnabled
= FALSE
;
612 return DSERR_GENERIC
;
617 static HRESULT WINAPI
IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface
)
619 IDsDriverBufferImpl
*This
= (IDsDriverBufferImpl
*)iface
;
621 TRACE("(%p)\n",iface
);
622 /* no more playing */
623 WOutDev
[This
->drv
->wDevID
].ossdev
.bOutputEnabled
= FALSE
;
624 enable
= getEnables(&WOutDev
[This
->drv
->wDevID
].ossdev
);
625 if (ioctl(This
->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
626 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n", WOutDev
[This
->drv
->wDevID
].ossdev
.dev_name
, strerror(errno
));
627 return DSERR_GENERIC
;
630 /* the play position must be reset to the beginning of the buffer */
631 if (ioctl(This
->fd
, SNDCTL_DSP_RESET
, 0) < 0) {
632 ERR("ioctl(%s, SNDCTL_DSP_RESET) failed (%s)\n", WOutDev
[This
->drv
->wDevID
].ossdev
.dev_name
, strerror(errno
));
633 return DSERR_GENERIC
;
636 /* Most OSS drivers just can't stop the playback without closing the device...
637 * so we need to somehow signal to our DirectSound implementation
638 * that it should completely recreate this HW buffer...
639 * this unexpected error code should do the trick... */
640 /* FIXME: ...unless we are doing full duplex, then it's not nice to close the device */
641 if (WOutDev
[This
->drv
->wDevID
].ossdev
.open_count
== 1)
642 return DSERR_BUFFERLOST
;
647 static const IDsDriverBufferVtbl dsdbvt
=
649 IDsDriverBufferImpl_QueryInterface
,
650 IDsDriverBufferImpl_AddRef
,
651 IDsDriverBufferImpl_Release
,
652 IDsDriverBufferImpl_Lock
,
653 IDsDriverBufferImpl_Unlock
,
654 IDsDriverBufferImpl_SetFormat
,
655 IDsDriverBufferImpl_SetFrequency
,
656 IDsDriverBufferImpl_SetVolumePan
,
657 IDsDriverBufferImpl_SetPosition
,
658 IDsDriverBufferImpl_GetPosition
,
659 IDsDriverBufferImpl_Play
,
660 IDsDriverBufferImpl_Stop
663 static HRESULT WINAPI
IDsDriverImpl_QueryInterface(PIDSDRIVER iface
, REFIID riid
, LPVOID
*ppobj
)
665 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
666 TRACE("(%p,%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
668 if ( IsEqualGUID(riid
, &IID_IUnknown
) ||
669 IsEqualGUID(riid
, &IID_IDsDriver
) ) {
670 IDsDriver_AddRef(iface
);
671 *ppobj
= (LPVOID
)This
;
675 FIXME( "Unknown IID %s\n", debugstr_guid( riid
) );
679 return E_NOINTERFACE
;
682 static ULONG WINAPI
IDsDriverImpl_AddRef(PIDSDRIVER iface
)
684 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
685 ULONG refCount
= InterlockedIncrement(&This
->ref
);
687 TRACE("(%p) ref was %d\n", This
, refCount
- 1);
692 static ULONG WINAPI
IDsDriverImpl_Release(PIDSDRIVER iface
)
694 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
695 ULONG refCount
= InterlockedDecrement(&This
->ref
);
697 TRACE("(%p) ref was %d\n", This
, refCount
+ 1);
700 HeapFree(GetProcessHeap(),0,This
);
701 TRACE("(%p) released\n",This
);
706 static HRESULT WINAPI
IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface
,
709 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
710 TRACE("(%p,%p)\n",iface
,pDesc
);
712 /* copy version from driver */
713 *pDesc
= WOutDev
[This
->wDevID
].ossdev
.ds_desc
;
715 pDesc
->dwFlags
|= DSDDESC_DOMMSYSTEMOPEN
| DSDDESC_DOMMSYSTEMSETFORMAT
|
716 DSDDESC_USESYSTEMMEMORY
| DSDDESC_DONTNEEDPRIMARYLOCK
|
717 DSDDESC_DONTNEEDSECONDARYLOCK
;
718 pDesc
->dnDevNode
= WOutDev
[This
->wDevID
].waveDesc
.dnDevNode
;
720 pDesc
->wReserved
= 0;
721 pDesc
->ulDeviceNum
= This
->wDevID
;
722 pDesc
->dwHeapType
= DSDHEAP_NOHEAP
;
723 pDesc
->pvDirectDrawHeap
= NULL
;
724 pDesc
->dwMemStartAddress
= 0;
725 pDesc
->dwMemEndAddress
= 0;
726 pDesc
->dwMemAllocExtra
= 0;
727 pDesc
->pvReserved1
= NULL
;
728 pDesc
->pvReserved2
= NULL
;
732 static HRESULT WINAPI
IDsDriverImpl_Open(PIDSDRIVER iface
)
734 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
736 TRACE("(%p)\n",iface
);
738 /* make sure the card doesn't start playing before we want it to */
739 WOutDev
[This
->wDevID
].ossdev
.bOutputEnabled
= FALSE
;
740 WOutDev
[This
->wDevID
].ossdev
.bInputEnabled
= FALSE
;
741 enable
= getEnables(&WOutDev
[This
->wDevID
].ossdev
);
742 if (ioctl(WOutDev
[This
->wDevID
].ossdev
.fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
743 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",WOutDev
[This
->wDevID
].ossdev
.dev_name
, strerror(errno
));
744 return DSERR_GENERIC
;
749 static HRESULT WINAPI
IDsDriverImpl_Close(PIDSDRIVER iface
)
751 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
752 TRACE("(%p)\n",iface
);
754 ERR("problem with DirectSound: primary not released\n");
755 return DSERR_GENERIC
;
760 static HRESULT WINAPI
IDsDriverImpl_GetCaps(PIDSDRIVER iface
, PDSDRIVERCAPS pCaps
)
762 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
763 TRACE("(%p,%p)\n",iface
,pCaps
);
764 *pCaps
= WOutDev
[This
->wDevID
].ossdev
.ds_caps
;
768 static HRESULT WINAPI
DSD_CreatePrimaryBuffer(PIDSDRIVER iface
,
772 LPDWORD pdwcbBufferSize
,
776 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
777 IDsDriverBufferImpl
** ippdsdb
= (IDsDriverBufferImpl
**)ppvObj
;
781 TRACE("(%p,%p,%x,%x,%p,%p,%p)\n",iface
,pwfx
,dwFlags
,dwCardAddress
,pdwcbBufferSize
,ppbBuffer
,ppvObj
);
784 return DSERR_ALLOCATED
;
785 if (dwFlags
& (DSBCAPS_CTRLFREQUENCY
| DSBCAPS_CTRLPAN
))
786 return DSERR_CONTROLUNAVAIL
;
788 *ippdsdb
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDsDriverBufferImpl
));
789 if (*ippdsdb
== NULL
)
790 return DSERR_OUTOFMEMORY
;
791 (*ippdsdb
)->lpVtbl
= &dsdbvt
;
793 (*ippdsdb
)->drv
= This
;
794 copy_format(pwfx
, &(*ippdsdb
)->wfex
);
795 (*ippdsdb
)->fd
= WOutDev
[This
->wDevID
].ossdev
.fd
;
796 (*ippdsdb
)->dwFlags
= dwFlags
;
798 /* check how big the DMA buffer is now */
799 if (ioctl((*ippdsdb
)->fd
, SNDCTL_DSP_GETOSPACE
, &info
) < 0) {
800 ERR("ioctl(%s, SNDCTL_DSP_GETOSPACE) failed (%s)\n",
801 WOutDev
[This
->wDevID
].ossdev
.dev_name
, strerror(errno
));
802 HeapFree(GetProcessHeap(),0,*ippdsdb
);
804 return DSERR_GENERIC
;
806 (*ippdsdb
)->maplen
= (*ippdsdb
)->buflen
= info
.fragstotal
* info
.fragsize
;
808 /* map the DMA buffer */
809 err
= DSDB_MapBuffer(*ippdsdb
);
811 HeapFree(GetProcessHeap(),0,*ippdsdb
);
816 /* primary buffer is ready to go */
817 *pdwcbBufferSize
= (*ippdsdb
)->maplen
;
818 *ppbBuffer
= (*ippdsdb
)->mapping
;
820 /* some drivers need some extra nudging after mapping */
821 WOutDev
[This
->wDevID
].ossdev
.bInputEnabled
= FALSE
;
822 WOutDev
[This
->wDevID
].ossdev
.bOutputEnabled
= FALSE
;
823 enable
= getEnables(&WOutDev
[This
->wDevID
].ossdev
);
824 if (ioctl((*ippdsdb
)->fd
, SNDCTL_DSP_SETTRIGGER
, &enable
) < 0) {
825 ERR("ioctl(%s, SNDCTL_DSP_SETTRIGGER) failed (%s)\n",
826 WOutDev
[This
->wDevID
].ossdev
.dev_name
, strerror(errno
));
827 return DSERR_GENERIC
;
830 This
->primary
= *ippdsdb
;
835 static HRESULT WINAPI
DSD_CreateSecondaryBuffer(PIDSDRIVER iface
,
839 LPDWORD pdwcbBufferSize
,
843 IDsDriverImpl
*This
= (IDsDriverImpl
*)iface
;
844 IDsDriverBufferImpl
** ippdsdb
= (IDsDriverBufferImpl
**)ppvObj
;
845 FIXME("(%p,%p,%x,%x,%p,%p,%p): stub\n",This
,pwfx
,dwFlags
,dwCardAddress
,pdwcbBufferSize
,ppbBuffer
,ppvObj
);
848 return DSERR_UNSUPPORTED
;
851 static HRESULT WINAPI
IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface
,
855 LPDWORD pdwcbBufferSize
,
859 TRACE("(%p,%p,%x,%x,%p,%p,%p)\n",iface
,pwfx
,dwFlags
,dwCardAddress
,pdwcbBufferSize
,ppbBuffer
,ppvObj
);
861 if (dwFlags
& DSBCAPS_PRIMARYBUFFER
)
862 return DSD_CreatePrimaryBuffer(iface
,pwfx
,dwFlags
,dwCardAddress
,pdwcbBufferSize
,ppbBuffer
,ppvObj
);
864 return DSD_CreateSecondaryBuffer(iface
,pwfx
,dwFlags
,dwCardAddress
,pdwcbBufferSize
,ppbBuffer
,ppvObj
);
867 static HRESULT WINAPI
IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface
,
868 PIDSDRIVERBUFFER pBuffer
,
871 /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
872 TRACE("(%p,%p): stub\n",iface
,pBuffer
);
873 return DSERR_INVALIDCALL
;
876 static const IDsDriverVtbl dsdvt
=
878 IDsDriverImpl_QueryInterface
,
879 IDsDriverImpl_AddRef
,
880 IDsDriverImpl_Release
,
881 IDsDriverImpl_GetDriverDesc
,
884 IDsDriverImpl_GetCaps
,
885 IDsDriverImpl_CreateSoundBuffer
,
886 IDsDriverImpl_DuplicateSoundBuffer
889 static HRESULT WINAPI
IDsDriverPropertySetImpl_Create(
890 IDsDriverBufferImpl
* dsdb
,
891 IDsDriverPropertySetImpl
**pdsdps
)
893 IDsDriverPropertySetImpl
* dsdps
;
894 TRACE("(%p,%p)\n",dsdb
,pdsdps
);
896 dsdps
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*dsdps
));
898 WARN("out of memory\n");
899 return DSERR_OUTOFMEMORY
;
903 dsdps
->lpVtbl
= &dsdpsvt
;
904 dsdps
->buffer
= dsdb
;
905 dsdb
->property_set
= dsdps
;
906 IDsDriverBuffer_AddRef((PIDSDRIVER
)dsdb
);
912 static HRESULT WINAPI
IDsDriverNotifyImpl_Create(
913 IDsDriverBufferImpl
* dsdb
,
914 IDsDriverNotifyImpl
**pdsdn
)
916 IDsDriverNotifyImpl
* dsdn
;
917 TRACE("(%p,%p)\n",dsdb
,pdsdn
);
919 dsdn
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*dsdn
));
922 WARN("out of memory\n");
923 return DSERR_OUTOFMEMORY
;
927 dsdn
->lpVtbl
= &dsdnvt
;
930 IDsDriverBuffer_AddRef((PIDSDRIVER
)dsdb
);
936 DWORD
wodDsCreate(UINT wDevID
, PIDSDRIVER
* drv
)
938 IDsDriverImpl
** idrv
= (IDsDriverImpl
**)drv
;
939 TRACE("(%d,%p)\n",wDevID
,drv
);
941 /* the HAL isn't much better than the HEL if we can't do mmap() */
942 if (!(WOutDev
[wDevID
].ossdev
.duplex_out_caps
.dwSupport
& WAVECAPS_DIRECTSOUND
)) {
943 WARN("Warn DirectSound flag not set, falling back to HEL layer\n");
944 return MMSYSERR_NOTSUPPORTED
;
947 *idrv
= HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(IDsDriverImpl
));
949 return MMSYSERR_NOMEM
;
950 (*idrv
)->lpVtbl
= &dsdvt
;
952 (*idrv
)->wDevID
= wDevID
;
953 (*idrv
)->primary
= NULL
;
954 (*idrv
)->nrofsecondaries
= 0;
955 (*idrv
)->secondaries
= NULL
;
957 return MMSYSERR_NOERROR
;
960 DWORD
wodDsDesc(UINT wDevID
, PDSDRIVERDESC desc
)
962 TRACE("(%d,%p)\n",wDevID
,desc
);
963 *desc
= WOutDev
[wDevID
].ossdev
.ds_desc
;
964 return MMSYSERR_NOERROR
;
967 #endif /* HAVE_OSS */