Authors: Eric Pouech <pouech-eric@wanadoo.fr>, Filip Navara <xnavara@volny.cz>
[wine/wine64.git] / dlls / winmm / wineoss / dsrender.c
bloba6a13d36ee9184cd7256698597d8f048a8601997
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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_SYS_POLL_H
43 # include <sys/poll.h>
44 #endif
46 #include "windef.h"
47 #include "winbase.h"
48 #include "wingdi.h"
49 #include "winuser.h"
50 #include "winerror.h"
51 #include "mmddk.h"
52 #include "mmreg.h"
53 #include "dsound.h"
54 #include "dsdriver.h"
55 #include "oss.h"
56 #include "wine/debug.h"
58 #include "audio.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(wave);
62 #ifdef HAVE_OSS
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
75 /* IUnknown fields */
76 IDsDriverPropertySetVtbl *lpVtbl;
77 DWORD ref;
79 IDsDriverBufferImpl* buffer;
82 struct IDsDriverNotifyImpl
84 /* IUnknown fields */
85 IDsDriverNotifyVtbl *lpVtbl;
86 DWORD ref;
88 /* IDsDriverNotifyImpl fields */
89 LPDSBPOSITIONNOTIFY notifies;
90 int nrofnotifies;
92 IDsDriverBufferImpl* buffer;
95 struct IDsDriverImpl
97 /* IUnknown fields */
98 IDsDriverVtbl *lpVtbl;
99 DWORD ref;
101 /* IDsDriverImpl fields */
102 UINT wDevID;
103 IDsDriverBufferImpl* primary;
105 int nrofsecondaries;
106 IDsDriverBufferImpl** secondaries;
109 struct IDsDriverBufferImpl
111 /* IUnknown fields */
112 IDsDriverBufferVtbl *lpVtbl;
113 DWORD ref;
115 /* IDsDriverBufferImpl fields */
116 IDsDriverImpl* drv;
117 DWORD buflen;
118 WAVEFORMATPCMEX wfex;
119 LPBYTE mapping;
120 DWORD maplen;
121 int fd;
122 DWORD dwFlags;
124 /* IDsDriverNotifyImpl fields */
125 IDsDriverNotifyImpl* notify;
126 int notify_index;
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,
146 REFIID riid,
147 LPVOID *ppobj)
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;
156 return DS_OK;
159 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
161 *ppobj = 0;
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;
176 DWORD ref;
177 TRACE("(%p) ref was %ld\n", This, This->ref);
179 ref = InterlockedDecrement(&(This->ref));
180 if (ref == 0) {
181 IDsDriverBuffer_Release((PIDSDRIVERBUFFER)This->buffer);
182 HeapFree(GetProcessHeap(),0,This);
183 TRACE("(%p) released\n",This);
185 return ref;
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,
218 ULONG PropertyId,
219 PULONG pSupport )
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,
242 REFIID riid,
243 LPVOID *ppobj)
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);
251 *ppobj = This;
252 return DS_OK;
255 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
257 *ppobj = 0;
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;
272 DWORD ref;
273 TRACE("(%p) ref was %ld\n", This, This->ref);
275 ref = InterlockedDecrement(&(This->ref));
276 if (ref == 0) {
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);
283 return ref;
286 static HRESULT WINAPI IDsDriverNotifyImpl_SetNotificationPositions(
287 PIDSDRIVERNOTIFY iface,
288 DWORD howmuch,
289 LPCDSBPOSITIONNOTIFY notify)
291 IDsDriverNotifyImpl *This = (IDsDriverNotifyImpl *)iface;
292 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
294 if (!notify) {
295 WARN("invalid parameter\n");
296 return DSERR_INVALIDPARAM;
299 if (TRACE_ON(wave)) {
300 int i;
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. */
308 if (This->notifies)
309 This->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
310 This->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
311 else
312 This->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
313 howmuch * sizeof(DSBPOSITIONNOTIFY));
315 memcpy(This->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
316 This->nrofnotifies = howmuch;
318 return S_OK;
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,
339 dsdb->fd, 0);
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
364 int b = len >> 2;
365 unsigned long* p4 = (unsigned long*)p1;
367 while (b--) *p4++ = ulsilence;
368 /* prepare for filling the rest */
369 len &= 3;
370 p1 = (unsigned char*)p4;
372 /* in all cases, fill the remaining bytes */
373 while (len-- != 0) *p1++ = silence;
376 return DS_OK;
379 static HRESULT DSDB_UnmapBuffer(IDsDriverBufferImpl *dsdb)
381 TRACE("(%p)\n",dsdb);
382 if (dsdb->mapping) {
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);
390 return DS_OK;
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;
402 return DS_OK;
405 if ( IsEqualGUID( &IID_IDsDriverNotify, riid ) ) {
406 if (!This->notify)
407 IDsDriverNotifyImpl_Create(This, &(This->notify));
408 if (This->notify) {
409 IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY)This->notify);
410 *ppobj = (LPVOID)This->notify;
411 return DS_OK;
413 *ppobj = 0;
414 return E_FAIL;
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;
423 return DS_OK;
425 *ppobj = 0;
426 return E_FAIL;
429 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
431 *ppobj = 0;
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;
447 DWORD ref;
448 TRACE("(%p) ref was %ld\n", This, This->ref);
450 ref = InterlockedDecrement(&(This->ref));
451 if (ref)
452 return ref;
454 if (This == This->drv->primary)
455 This->drv->primary = NULL;
456 else {
457 int i;
458 for (i = 0; i < This->drv->nrofsecondaries; i++)
459 if (This->drv->secondaries[i] == This)
460 break;
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);
478 return 0;
481 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
482 LPVOID*ppvAudio1,LPDWORD pdwLen1,
483 LPVOID*ppvAudio2,LPDWORD pdwLen2,
484 DWORD dwWritePosition,DWORD dwWriteLen,
485 DWORD dwFlags)
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,
504 LPWAVEFORMATEX pwfx)
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)
527 DWORD vol;
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;
538 return DS_OK;
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;
552 count_info info;
553 DWORD ptr;
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;
567 if (lpdwWrite) {
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;
571 else
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);
577 return DS_OK;
580 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
582 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
583 int enable;
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;
594 else
595 WOutDev[This->drv->wDevID].ossdev->bInputEnabled = FALSE;
596 /* try it again */
597 enable = getEnables(WOutDev[This->drv->wDevID].ossdev);
598 if (ioctl(This->fd, SNDCTL_DSP_SETTRIGGER, &enable) >= 0)
599 return DS_OK;
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;
607 return DS_OK;
610 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
612 IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
613 int enable;
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;
622 #if 0
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;
628 #endif
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;
637 return DS_OK;
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;
665 return DS_OK;
668 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
670 *ppobj = 0;
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;
686 DWORD ref;
687 TRACE("(%p) ref was %ld\n", This, This->ref);
689 ref = InterlockedDecrement(&(This->ref));
690 if (ref == 0) {
691 HeapFree(GetProcessHeap(),0,This);
692 TRACE("(%p) released\n",This);
694 return ref;
697 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface,
698 PDSDRIVERDESC pDesc)
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;
710 pDesc->wVxdId = 0;
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;
720 return DS_OK;
723 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
725 IDsDriverImpl *This = (IDsDriverImpl *)iface;
726 int enable;
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;
736 return DS_OK;
739 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
741 IDsDriverImpl *This = (IDsDriverImpl *)iface;
742 TRACE("(%p)\n",iface);
743 if (This->primary) {
744 ERR("problem with DirectSound: primary not released\n");
745 return DSERR_GENERIC;
747 return DS_OK;
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));
755 return DS_OK;
758 static HRESULT WINAPI DSD_CreatePrimaryBuffer(PIDSDRIVER iface,
759 LPWAVEFORMATEX pwfx,
760 DWORD dwFlags,
761 DWORD dwCardAddress,
762 LPDWORD pdwcbBufferSize,
763 LPBYTE *ppbBuffer,
764 LPVOID *ppvObj)
766 IDsDriverImpl *This = (IDsDriverImpl *)iface;
767 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
768 HRESULT err;
769 audio_buf_info info;
770 int enable = 0;
771 TRACE("(%p,%p,%lx,%lx,%p,%p,%p)\n",iface,pwfx,dwFlags,dwCardAddress,pdwcbBufferSize,ppbBuffer,ppvObj);
773 if (This->primary)
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;
782 (*ippdsdb)->ref = 1;
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);
793 *ippdsdb = NULL;
794 return DSERR_GENERIC;
796 (*ippdsdb)->maplen = (*ippdsdb)->buflen = info.fragstotal * info.fragsize;
798 /* map the DMA buffer */
799 err = DSDB_MapBuffer(*ippdsdb);
800 if (err != DS_OK) {
801 HeapFree(GetProcessHeap(),0,*ippdsdb);
802 *ippdsdb = NULL;
803 return err;
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;
821 return DS_OK;
824 static HRESULT WINAPI DSD_CreateSecondaryBuffer(PIDSDRIVER iface,
825 LPWAVEFORMATEX pwfx,
826 DWORD dwFlags,
827 DWORD dwCardAddress,
828 LPDWORD pdwcbBufferSize,
829 LPBYTE *ppbBuffer,
830 LPVOID *ppvObj)
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);
836 *ippdsdb = 0;
837 return DSERR_UNSUPPORTED;
840 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
841 LPWAVEFORMATEX pwfx,
842 DWORD dwFlags,
843 DWORD dwCardAddress,
844 LPDWORD pdwcbBufferSize,
845 LPBYTE *ppbBuffer,
846 LPVOID *ppvObj)
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,
858 LPVOID *ppvObj)
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,
871 IDsDriverImpl_Open,
872 IDsDriverImpl_Close,
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));
886 if (dsdps == NULL) {
887 WARN("out of memory\n");
888 return DSERR_OUTOFMEMORY;
891 dsdps->ref = 0;
892 dsdps->lpVtbl = &dsdpsvt;
893 dsdps->buffer = dsdb;
894 dsdb->property_set = dsdps;
895 IDsDriverBuffer_AddRef((PIDSDRIVER)dsdb);
897 *pdsdps = dsdps;
898 return DS_OK;
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));
910 if (dsdn == NULL) {
911 WARN("out of memory\n");
912 return DSERR_OUTOFMEMORY;
915 dsdn->ref = 0;
916 dsdn->lpVtbl = &dsdnvt;
917 dsdn->buffer = dsdb;
918 dsdb->notify = dsdn;
919 IDsDriverBuffer_AddRef((PIDSDRIVER)dsdb);
921 *pdsdn = dsdn;
922 return DS_OK;
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));
939 if (!*idrv)
940 return MMSYSERR_NOMEM;
941 (*idrv)->lpVtbl = &dsdvt;
942 (*idrv)->ref = 1;
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 */