comctl32/tests: Remove variables cx and cy which are not really used from test_approx...
[wine.git] / dlls / wineoss.drv / dsrender.c
blob02849c78f8fd1c4d07bb7aa867d1019f1f38ad58
1 /*
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
24 #include "config.h"
25 #include "wine/port.h"
27 #include <stdlib.h>
28 #include <stdarg.h>
29 #include <stdio.h>
30 #include <string.h>
31 #ifdef HAVE_UNISTD_H
32 # include <unistd.h>
33 #endif
34 #include <errno.h>
35 #include <fcntl.h>
36 #ifdef HAVE_SYS_IOCTL_H
37 # include <sys/ioctl.h>
38 #endif
39 #ifdef HAVE_SYS_MMAN_H
40 # include <sys/mman.h>
41 #endif
42 #ifdef HAVE_POLL_H
43 #include <poll.h>
44 #endif
45 #ifdef HAVE_SYS_POLL_H
46 # include <sys/poll.h>
47 #endif
49 #include "windef.h"
50 #include "winbase.h"
51 #include "wingdi.h"
52 #include "winuser.h"
53 #include "winerror.h"
54 #include "mmddk.h"
55 #include "mmreg.h"
56 #include "dsound.h"
57 #include "dsdriver.h"
58 #include "oss.h"
59 #include "wine/debug.h"
61 #include "audio.h"
63 WINE_DEFAULT_DEBUG_CHANNEL(wave);
65 #ifdef HAVE_OSS
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
78 /* IUnknown fields */
79 const IDsDriverPropertySetVtbl *lpVtbl;
80 LONG ref;
82 IDsDriverBufferImpl* buffer;
85 struct IDsDriverNotifyImpl
87 /* IUnknown fields */
88 const IDsDriverNotifyVtbl *lpVtbl;
89 LONG ref;
91 /* IDsDriverNotifyImpl fields */
92 LPDSBPOSITIONNOTIFY notifies;
93 int nrofnotifies;
95 IDsDriverBufferImpl* buffer;
98 struct IDsDriverImpl
100 /* IUnknown fields */
101 const IDsDriverVtbl *lpVtbl;
102 LONG ref;
104 /* IDsDriverImpl fields */
105 UINT wDevID;
106 IDsDriverBufferImpl* primary;
108 int nrofsecondaries;
109 IDsDriverBufferImpl** secondaries;
112 struct IDsDriverBufferImpl
114 /* IUnknown fields */
115 const IDsDriverBufferVtbl *lpVtbl;
116 LONG ref;
118 /* IDsDriverBufferImpl fields */
119 IDsDriverImpl* drv;
120 DWORD buflen;
121 WAVEFORMATPCMEX wfex;
122 LPBYTE mapping;
123 DWORD maplen;
124 int fd;
125 DWORD dwFlags;
127 /* IDsDriverNotifyImpl fields */
128 IDsDriverNotifyImpl* notify;
129 int notify_index;
131 /* IDsDriverPropertySetImpl fields */
132 IDsDriverPropertySetImpl* property_set;
135 static HRESULT IDsDriverPropertySetImpl_Create(
136 IDsDriverBufferImpl * dsdb,
137 IDsDriverPropertySetImpl **pdsdps);
139 static HRESULT 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,
149 REFIID riid,
150 LPVOID *ppobj)
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 = This;
159 return DS_OK;
162 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
164 *ppobj = 0;
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);
175 return refCount;
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);
185 if (!refCount) {
186 IDsDriverBuffer_Release((PIDSDRIVERBUFFER)This->buffer);
187 HeapFree(GetProcessHeap(),0,This);
188 TRACE("(%p) released\n",This);
190 return refCount;
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,
223 ULONG PropertyId,
224 PULONG pSupport )
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,
247 REFIID riid,
248 LPVOID *ppobj)
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);
256 *ppobj = This;
257 return DS_OK;
260 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
262 *ppobj = 0;
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);
273 return refCount;
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);
283 if (!refCount) {
284 IDsDriverBuffer_Release((PIDSDRIVERBUFFER)This->buffer);
285 HeapFree(GetProcessHeap(), 0, This->notifies);
286 HeapFree(GetProcessHeap(),0,This);
287 TRACE("(%p) released\n",This);
289 return refCount;
292 static HRESULT WINAPI IDsDriverNotifyImpl_SetNotificationPositions(
293 PIDSDRIVERNOTIFY iface,
294 DWORD howmuch,
295 LPCDSBPOSITIONNOTIFY notify)
297 IDsDriverNotifyImpl *This = (IDsDriverNotifyImpl *)iface;
298 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
300 if (!notify) {
301 WARN("invalid parameter\n");
302 return DSERR_INVALIDPARAM;
305 if (TRACE_ON(wave)) {
306 DWORD i;
307 for (i=0;i<howmuch;i++)
308 TRACE("notify at %d to 0x%08lx\n",
309 notify[i].dwOffset,(DWORD_PTR)notify[i].hEventNotify);
312 /* Make an internal copy of the caller-supplied array.
313 * Replace the existing copy if one is already present. */
314 if (This->notifies)
315 This->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
316 This->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
317 else
318 This->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
319 howmuch * sizeof(DSBPOSITIONNOTIFY));
321 memcpy(This->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
322 This->nrofnotifies = howmuch;
324 return S_OK;
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,
345 dsdb->fd, 0);
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
369 int b = len >> 2;
370 unsigned long* p4 = (unsigned long*)p1;
372 while (b--) *p4++ = ulsilence;
373 /* prepare for filling the rest */
374 len &= 3;
375 p1 = (unsigned char*)p4;
377 /* in all cases, fill the remaining bytes */
378 while (len-- != 0) *p1++ = silence;
381 return DS_OK;
384 static HRESULT DSDB_UnmapBuffer(IDsDriverBufferImpl *dsdb)
386 TRACE("(%p)\n",dsdb);
387 if (dsdb->mapping) {
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);
395 return DS_OK;
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 = This;
407 return DS_OK;
410 if ( IsEqualGUID( &IID_IDsDriverNotify, riid ) ) {
411 if (!This->notify)
412 IDsDriverNotifyImpl_Create(This, &(This->notify));
413 if (This->notify) {
414 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY)This->notify);
415 *ppobj = This->notify;
416 return DS_OK;
418 *ppobj = 0;
419 return E_FAIL;
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 = This->property_set;
428 return DS_OK;
430 *ppobj = 0;
431 return E_FAIL;
434 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
436 *ppobj = 0;
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);
448 return refCount;
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);
458 if (refCount)
459 return refCount;
461 if (This == This->drv->primary)
462 This->drv->primary = NULL;
463 else {
464 int i;
465 for (i = 0; i < This->drv->nrofsecondaries; i++)
466 if (This->drv->secondaries[i] == This)
467 break;
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);
485 return 0;
488 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
489 LPVOID*ppvAudio1,LPDWORD pdwLen1,
490 LPVOID*ppvAudio2,LPDWORD pdwLen2,
491 DWORD dwWritePosition,DWORD dwWriteLen,
492 DWORD dwFlags)
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,
511 LPWAVEFORMATEX pwfx)
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)
534 DWORD vol;
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;
545 return DS_OK;
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;
559 count_info info;
560 DWORD ptr;
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;
574 if (lpdwWrite) {
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;
578 else
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);
584 return DS_OK;
587 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
589 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
590 int enable;
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;
601 else
602 WOutDev[This->drv->wDevID].ossdev.bInputEnabled = FALSE;
603 /* try it again */
604 enable = getEnables(&WOutDev[This->drv->wDevID].ossdev);
605 if (ioctl(This->fd, SNDCTL_DSP_SETTRIGGER, &enable) >= 0)
606 return DS_OK;
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;
614 return DS_OK;
617 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
619 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
620 int enable;
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;
629 #if 0
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;
635 #endif
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;
644 return DS_OK;
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 = This;
672 return DS_OK;
675 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
677 *ppobj = 0;
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);
689 return refCount;
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);
699 if (!refCount) {
700 HeapFree(GetProcessHeap(),0,This);
701 TRACE("(%p) released\n",This);
703 return refCount;
706 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface,
707 PDSDRIVERDESC pDesc)
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;
719 pDesc->wVxdId = 0;
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;
729 return DS_OK;
732 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
734 IDsDriverImpl *This = (IDsDriverImpl *)iface;
735 int enable;
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;
746 return DS_OK;
749 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
751 IDsDriverImpl *This = (IDsDriverImpl *)iface;
752 TRACE("(%p)\n",iface);
753 if (This->primary) {
754 ERR("problem with DirectSound: primary not released\n");
755 return DSERR_GENERIC;
757 return DS_OK;
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;
765 return DS_OK;
768 static HRESULT DSD_CreatePrimaryBuffer(PIDSDRIVER iface,
769 LPWAVEFORMATEX pwfx,
770 DWORD dwFlags,
771 DWORD dwCardAddress,
772 LPDWORD pdwcbBufferSize,
773 LPBYTE *ppbBuffer,
774 LPVOID *ppvObj)
776 IDsDriverImpl *This = (IDsDriverImpl *)iface;
777 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
778 HRESULT err;
779 audio_buf_info info;
780 int enable = 0;
781 TRACE("(%p,%p,%x,%x,%p,%p,%p)\n",iface,pwfx,dwFlags,dwCardAddress,pdwcbBufferSize,ppbBuffer,ppvObj);
783 if (This->primary)
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;
792 (*ippdsdb)->ref = 1;
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);
803 *ippdsdb = NULL;
804 return DSERR_GENERIC;
806 (*ippdsdb)->maplen = (*ippdsdb)->buflen = info.fragstotal * info.fragsize;
808 /* map the DMA buffer */
809 err = DSDB_MapBuffer(*ippdsdb);
810 if (err != DS_OK) {
811 HeapFree(GetProcessHeap(),0,*ippdsdb);
812 *ippdsdb = NULL;
813 return err;
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;
832 return DS_OK;
835 static HRESULT DSD_CreateSecondaryBuffer(PIDSDRIVER iface,
836 LPWAVEFORMATEX pwfx,
837 DWORD dwFlags,
838 DWORD dwCardAddress,
839 LPDWORD pdwcbBufferSize,
840 LPBYTE *ppbBuffer,
841 LPVOID *ppvObj)
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);
847 *ippdsdb = 0;
848 return DSERR_UNSUPPORTED;
851 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
852 LPWAVEFORMATEX pwfx,
853 DWORD dwFlags,
854 DWORD dwCardAddress,
855 LPDWORD pdwcbBufferSize,
856 LPBYTE *ppbBuffer,
857 LPVOID *ppvObj)
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,
869 LPVOID *ppvObj)
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,
882 IDsDriverImpl_Open,
883 IDsDriverImpl_Close,
884 IDsDriverImpl_GetCaps,
885 IDsDriverImpl_CreateSoundBuffer,
886 IDsDriverImpl_DuplicateSoundBuffer
889 static HRESULT 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));
897 if (dsdps == NULL) {
898 WARN("out of memory\n");
899 return DSERR_OUTOFMEMORY;
902 dsdps->ref = 0;
903 dsdps->lpVtbl = &dsdpsvt;
904 dsdps->buffer = dsdb;
905 dsdb->property_set = dsdps;
906 IDsDriverBuffer_AddRef((PIDSDRIVER)dsdb);
908 *pdsdps = dsdps;
909 return DS_OK;
912 static HRESULT 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));
921 if (dsdn == NULL) {
922 WARN("out of memory\n");
923 return DSERR_OUTOFMEMORY;
926 dsdn->ref = 0;
927 dsdn->lpVtbl = &dsdnvt;
928 dsdn->buffer = dsdb;
929 dsdb->notify = dsdn;
930 IDsDriverBuffer_AddRef((PIDSDRIVER)dsdb);
932 *pdsdn = dsdn;
933 return DS_OK;
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));
948 if (!*idrv)
949 return MMSYSERR_NOMEM;
950 (*idrv)->lpVtbl = &dsdvt;
951 (*idrv)->ref = 1;
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 */