wintrust: Use path in WIN_TRUST_SUBJECT_FILE structure rather than assuming a path...
[wine] / dlls / wineoss.drv / dsrender.c
1 /*
2  * Sample Wine Driver for Open Sound System (featured in Linux and FreeBSD)
3  *
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)
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
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
48
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"
60
61 #include "audio.h"
62
63 WINE_DEFAULT_DEBUG_CHANNEL(wave);
64
65 #ifdef HAVE_OSS
66
67 /*======================================================================*
68  *                  Low level DSOUND definitions                        *
69  *======================================================================*/
70
71 typedef struct IDsDriverPropertySetImpl IDsDriverPropertySetImpl;
72 typedef struct IDsDriverNotifyImpl IDsDriverNotifyImpl;
73 typedef struct IDsDriverImpl IDsDriverImpl;
74 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
75
76 struct IDsDriverPropertySetImpl
77 {
78     /* IUnknown fields */
79     const IDsDriverPropertySetVtbl *lpVtbl;
80     LONG                        ref;
81
82     IDsDriverBufferImpl*        buffer;
83 };
84
85 struct IDsDriverNotifyImpl
86 {
87     /* IUnknown fields */
88     const IDsDriverNotifyVtbl  *lpVtbl;
89     LONG                        ref;
90
91     /* IDsDriverNotifyImpl fields */
92     LPDSBPOSITIONNOTIFY         notifies;
93     int                         nrofnotifies;
94
95     IDsDriverBufferImpl*        buffer;
96 };
97
98 struct IDsDriverImpl
99 {
100     /* IUnknown fields */
101     const IDsDriverVtbl        *lpVtbl;
102     LONG                        ref;
103
104     /* IDsDriverImpl fields */
105     UINT                        wDevID;
106     IDsDriverBufferImpl*        primary;
107
108     int                         nrofsecondaries;
109     IDsDriverBufferImpl**       secondaries;
110 };
111
112 struct IDsDriverBufferImpl
113 {
114     /* IUnknown fields */
115     const IDsDriverBufferVtbl  *lpVtbl;
116     LONG                        ref;
117
118     /* IDsDriverBufferImpl fields */
119     IDsDriverImpl*              drv;
120     DWORD                       buflen;
121     WAVEFORMATPCMEX             wfex;
122     LPBYTE                      mapping;
123     DWORD                       maplen;
124     int                         fd;
125     DWORD                       dwFlags;
126
127     /* IDsDriverNotifyImpl fields */
128     IDsDriverNotifyImpl*        notify;
129     int                         notify_index;
130
131     /* IDsDriverPropertySetImpl fields */
132     IDsDriverPropertySetImpl*   property_set;
133 };
134
135 static HRESULT WINAPI IDsDriverPropertySetImpl_Create(
136     IDsDriverBufferImpl * dsdb,
137     IDsDriverPropertySetImpl **pdsdps);
138
139 static HRESULT WINAPI IDsDriverNotifyImpl_Create(
140     IDsDriverBufferImpl * dsdb,
141     IDsDriverNotifyImpl **pdsdn);
142
143 /*======================================================================*
144  *                  Low level DSOUND property set implementation        *
145  *======================================================================*/
146
147 static HRESULT WINAPI IDsDriverPropertySetImpl_QueryInterface(
148     PIDSDRIVERPROPERTYSET iface,
149     REFIID riid,
150     LPVOID *ppobj)
151 {
152     IDsDriverPropertySetImpl *This = (IDsDriverPropertySetImpl *)iface;
153     TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
154
155     if ( IsEqualGUID(riid, &IID_IUnknown) ||
156          IsEqualGUID(riid, &IID_IDsDriverPropertySet) ) {
157         IDsDriverPropertySet_AddRef(iface);
158         *ppobj = (LPVOID)This;
159         return DS_OK;
160     }
161
162     FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
163
164     *ppobj = 0;
165     return E_NOINTERFACE;
166 }
167
168 static ULONG WINAPI IDsDriverPropertySetImpl_AddRef(PIDSDRIVERPROPERTYSET iface)
169 {
170     IDsDriverPropertySetImpl *This = (IDsDriverPropertySetImpl *)iface;
171     ULONG refCount = InterlockedIncrement(&This->ref);
172
173     TRACE("(%p) ref was %d\n", This, refCount - 1);
174
175     return refCount;
176 }
177
178 static ULONG WINAPI IDsDriverPropertySetImpl_Release(PIDSDRIVERPROPERTYSET iface)
179 {
180     IDsDriverPropertySetImpl *This = (IDsDriverPropertySetImpl *)iface;
181     ULONG refCount = InterlockedDecrement(&This->ref);
182
183     TRACE("(%p) ref was %d\n", This, refCount + 1);
184
185     if (!refCount) {
186         IDsDriverBuffer_Release((PIDSDRIVERBUFFER)This->buffer);
187         HeapFree(GetProcessHeap(),0,This);
188         TRACE("(%p) released\n",This);
189     }
190     return refCount;
191 }
192
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 )
201 {
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;
205 }
206
207 static HRESULT WINAPI IDsDriverPropertySetImpl_Set(
208     PIDSDRIVERPROPERTYSET iface,
209     PDSPROPERTY pDsProperty,
210     LPVOID pPropertyParams,
211     ULONG cbPropertyParams,
212     LPVOID pPropertyData,
213     ULONG cbPropertyData )
214 {
215     IDsDriverPropertySetImpl *This = (IDsDriverPropertySetImpl *)iface;
216     FIXME("(%p,%p,%p,%x,%p,%x)\n",This,pDsProperty,pPropertyParams,cbPropertyParams,pPropertyData,cbPropertyData);
217     return DSERR_UNSUPPORTED;
218 }
219
220 static HRESULT WINAPI IDsDriverPropertySetImpl_QuerySupport(
221     PIDSDRIVERPROPERTYSET iface,
222     REFGUID PropertySetId,
223     ULONG PropertyId,
224     PULONG pSupport )
225 {
226     IDsDriverPropertySetImpl *This = (IDsDriverPropertySetImpl *)iface;
227     FIXME("(%p,%s,%x,%p)\n",This,debugstr_guid(PropertySetId),PropertyId,pSupport);
228     return DSERR_UNSUPPORTED;
229 }
230
231 static const IDsDriverPropertySetVtbl dsdpsvt =
232 {
233     IDsDriverPropertySetImpl_QueryInterface,
234     IDsDriverPropertySetImpl_AddRef,
235     IDsDriverPropertySetImpl_Release,
236     IDsDriverPropertySetImpl_Get,
237     IDsDriverPropertySetImpl_Set,
238     IDsDriverPropertySetImpl_QuerySupport,
239 };
240
241 /*======================================================================*
242  *                  Low level DSOUND notify implementation              *
243  *======================================================================*/
244
245 static HRESULT WINAPI IDsDriverNotifyImpl_QueryInterface(
246     PIDSDRIVERNOTIFY iface,
247     REFIID riid,
248     LPVOID *ppobj)
249 {
250     IDsDriverNotifyImpl *This = (IDsDriverNotifyImpl *)iface;
251     TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
252
253     if ( IsEqualGUID(riid, &IID_IUnknown) ||
254          IsEqualGUID(riid, &IID_IDsDriverNotify) ) {
255         IDsDriverNotify_AddRef(iface);
256         *ppobj = This;
257         return DS_OK;
258     }
259
260     FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
261
262     *ppobj = 0;
263     return E_NOINTERFACE;
264 }
265
266 static ULONG WINAPI IDsDriverNotifyImpl_AddRef(PIDSDRIVERNOTIFY iface)
267 {
268     IDsDriverNotifyImpl *This = (IDsDriverNotifyImpl *)iface;
269     ULONG refCount = InterlockedIncrement(&This->ref);
270
271     TRACE("(%p) ref was %d\n", This, refCount - 1);
272
273     return refCount;
274 }
275
276 static ULONG WINAPI IDsDriverNotifyImpl_Release(PIDSDRIVERNOTIFY iface)
277 {
278     IDsDriverNotifyImpl *This = (IDsDriverNotifyImpl *)iface;
279     ULONG refCount = InterlockedDecrement(&This->ref);
280
281     TRACE("(%p) ref was %d\n", This, refCount + 1);
282
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);
288     }
289     return refCount;
290 }
291
292 static HRESULT WINAPI IDsDriverNotifyImpl_SetNotificationPositions(
293     PIDSDRIVERNOTIFY iface,
294     DWORD howmuch,
295     LPCDSBPOSITIONNOTIFY notify)
296 {
297     IDsDriverNotifyImpl *This = (IDsDriverNotifyImpl *)iface;
298     TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
299
300     if (!notify) {
301         WARN("invalid parameter\n");
302         return DSERR_INVALIDPARAM;
303     }
304
305     if (TRACE_ON(wave)) {
306         int i;
307         for (i=0;i<howmuch;i++)
308             TRACE("notify at %d to 0x%08x\n",
309                 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
310     }
311
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));
320
321     memcpy(This->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
322     This->nrofnotifies = howmuch;
323
324     return S_OK;
325 }
326
327 static const IDsDriverNotifyVtbl dsdnvt =
328 {
329     IDsDriverNotifyImpl_QueryInterface,
330     IDsDriverNotifyImpl_AddRef,
331     IDsDriverNotifyImpl_Release,
332     IDsDriverNotifyImpl_SetNotificationPositions,
333 };
334
335 /*======================================================================*
336  *                  Low level DSOUND implementation                     *
337  *======================================================================*/
338
339 static HRESULT DSDB_MapBuffer(IDsDriverBufferImpl *dsdb)
340 {
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;
349         }
350         TRACE("The sound device has been mapped for direct access at %p, size=%d\n", dsdb->mapping, dsdb->maplen);
351
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);
357          */
358         {
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;
363
364             if (len >= 16) /* so we can have at least a 4 long area to store... */
365             {
366                 /* the mmap:ed value is (at least) dword aligned
367                  * so, start filling the complete unsigned long:s
368                  */
369                 int             b = len >> 2;
370                 unsigned long*  p4 = (unsigned long*)p1;
371
372                 while (b--) *p4++ = ulsilence;
373                 /* prepare for filling the rest */
374                 len &= 3;
375                 p1 = (unsigned char*)p4;
376             }
377             /* in all cases, fill the remaining bytes */
378             while (len-- != 0) *p1++ = silence;
379         }
380     }
381     return DS_OK;
382 }
383
384 static HRESULT DSDB_UnmapBuffer(IDsDriverBufferImpl *dsdb)
385 {
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;
391         }
392         dsdb->mapping = NULL;
393         TRACE("(%p): sound device unmapped\n", dsdb);
394     }
395     return DS_OK;
396 }
397
398 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
399 {
400     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
401     TRACE("(%p,%s,%p)\n",iface,debugstr_guid(riid),*ppobj);
402
403     if ( IsEqualGUID(riid, &IID_IUnknown) ||
404          IsEqualGUID(riid, &IID_IDsDriverBuffer) ) {
405         IDsDriverBuffer_AddRef(iface);
406         *ppobj = (LPVOID)This;
407         return DS_OK;
408     }
409
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 = (LPVOID)This->notify;
416             return DS_OK;
417         }
418         *ppobj = 0;
419         return E_FAIL;
420     }
421
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;
428             return DS_OK;
429         }
430         *ppobj = 0;
431         return E_FAIL;
432     }
433
434     FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
435
436     *ppobj = 0;
437
438     return E_NOINTERFACE;
439 }
440
441 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
442 {
443     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
444     ULONG refCount = InterlockedIncrement(&This->ref);
445
446     TRACE("(%p) ref was %d\n", This, refCount - 1);
447
448     return refCount;
449 }
450
451 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
452 {
453     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
454     ULONG refCount = InterlockedDecrement(&This->ref);
455
456     TRACE("(%p) ref was %d\n", This, refCount + 1);
457
458     if (refCount)
459         return refCount;
460
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);
476         }
477
478         WOutDev[This->drv->wDevID].ossdev.ds_caps.dwFreeHwMixingAllBuffers++;
479         WOutDev[This->drv->wDevID].ossdev.ds_caps.dwFreeHwMixingStreamingBuffers++;
480     }
481
482     DSDB_UnmapBuffer(This);
483     HeapFree(GetProcessHeap(),0,This);
484     TRACE("(%p) released\n",This);
485     return 0;
486 }
487
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)
493 {
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;
499 }
500
501 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
502                                                  LPVOID pvAudio1,DWORD dwLen1,
503                                                  LPVOID pvAudio2,DWORD dwLen2)
504 {
505     /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
506     TRACE("(%p): stub\n",iface);
507     return DSERR_UNSUPPORTED;
508 }
509
510 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
511                                                     LPWAVEFORMATEX pwfx)
512 {
513     /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
514
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;
523 }
524
525 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
526 {
527     /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
528     TRACE("(%p,%d): stub\n",iface,dwFreq);
529     return DSERR_UNSUPPORTED;
530 }
531
532 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
533 {
534     DWORD vol;
535     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
536     TRACE("(%p,%p)\n",This,pVolPan);
537
538     vol = pVolPan->dwTotalLeftAmpFactor | (pVolPan->dwTotalRightAmpFactor << 16);
539
540     if (wodSetVolume(This->drv->wDevID, vol) != MMSYSERR_NOERROR) {
541         WARN("wodSetVolume failed\n");
542         return DSERR_INVALIDPARAM;
543     }
544
545     return DS_OK;
546 }
547
548 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
549 {
550     /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
551     TRACE("(%p,%d): stub\n",iface,dwNewPos);
552     return DSERR_UNSUPPORTED;
553 }
554
555 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
556                                                       LPDWORD lpdwPlay, LPDWORD lpdwWrite)
557 {
558     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
559     count_info info;
560     DWORD ptr;
561
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;
566     }
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;
571     }
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;
582     }
583     TRACE("playpos=%d, writepos=%d\n", lpdwPlay?*lpdwPlay:0, lpdwWrite?*lpdwWrite:0);
584     return DS_OK;
585 }
586
587 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
588 {
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;
607             }
608         }
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;
613     }
614     return DS_OK;
615 }
616
617 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
618 {
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;
628     }
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;
634     }
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;
643
644     return DS_OK;
645 }
646
647 static const IDsDriverBufferVtbl dsdbvt =
648 {
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
661 };
662
663 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
664 {
665     IDsDriverImpl *This = (IDsDriverImpl *)iface;
666     TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
667
668     if ( IsEqualGUID(riid, &IID_IUnknown) ||
669          IsEqualGUID(riid, &IID_IDsDriver) ) {
670         IDsDriver_AddRef(iface);
671         *ppobj = (LPVOID)This;
672         return DS_OK;
673     }
674
675     FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
676
677     *ppobj = 0;
678
679     return E_NOINTERFACE;
680 }
681
682 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
683 {
684     IDsDriverImpl *This = (IDsDriverImpl *)iface;
685     ULONG refCount = InterlockedIncrement(&This->ref);
686
687     TRACE("(%p) ref was %d\n", This, refCount - 1);
688
689     return refCount;
690 }
691
692 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
693 {
694     IDsDriverImpl *This = (IDsDriverImpl *)iface;
695     ULONG refCount = InterlockedDecrement(&This->ref);
696
697     TRACE("(%p) ref was %d\n", This, refCount + 1);
698
699     if (!refCount) {
700         HeapFree(GetProcessHeap(),0,This);
701         TRACE("(%p) released\n",This);
702     }
703     return refCount;
704 }
705
706 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface,
707                                                   PDSDRIVERDESC pDesc)
708 {
709     IDsDriverImpl *This = (IDsDriverImpl *)iface;
710     TRACE("(%p,%p)\n",iface,pDesc);
711
712     /* copy version from driver */
713     *pDesc = WOutDev[This->wDevID].ossdev.ds_desc;
714
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;
730 }
731
732 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
733 {
734     IDsDriverImpl *This = (IDsDriverImpl *)iface;
735     int enable;
736     TRACE("(%p)\n",iface);
737
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;
745     }
746     return DS_OK;
747 }
748
749 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
750 {
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;
756     }
757     return DS_OK;
758 }
759
760 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
761 {
762     IDsDriverImpl *This = (IDsDriverImpl *)iface;
763     TRACE("(%p,%p)\n",iface,pCaps);
764     *pCaps = WOutDev[This->wDevID].ossdev.ds_caps;
765     return DS_OK;
766 }
767
768 static HRESULT WINAPI DSD_CreatePrimaryBuffer(PIDSDRIVER iface,
769                                               LPWAVEFORMATEX pwfx,
770                                               DWORD dwFlags,
771                                               DWORD dwCardAddress,
772                                               LPDWORD pdwcbBufferSize,
773                                               LPBYTE *ppbBuffer,
774                                               LPVOID *ppvObj)
775 {
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);
782
783     if (This->primary)
784         return DSERR_ALLOCATED;
785     if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
786         return DSERR_CONTROLUNAVAIL;
787
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;
797
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;
805     }
806     (*ippdsdb)->maplen = (*ippdsdb)->buflen = info.fragstotal * info.fragsize;
807
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;
814     }
815
816     /* primary buffer is ready to go */
817     *pdwcbBufferSize    = (*ippdsdb)->maplen;
818     *ppbBuffer          = (*ippdsdb)->mapping;
819
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;
828     }
829
830     This->primary = *ippdsdb;
831
832     return DS_OK;
833 }
834
835 static HRESULT WINAPI DSD_CreateSecondaryBuffer(PIDSDRIVER iface,
836                                                 LPWAVEFORMATEX pwfx,
837                                                 DWORD dwFlags,
838                                                 DWORD dwCardAddress,
839                                                 LPDWORD pdwcbBufferSize,
840                                                 LPBYTE *ppbBuffer,
841                                                 LPVOID *ppvObj)
842 {
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);
846
847     *ippdsdb = 0;
848     return DSERR_UNSUPPORTED;
849 }
850
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)
858 {
859     TRACE("(%p,%p,%x,%x,%p,%p,%p)\n",iface,pwfx,dwFlags,dwCardAddress,pdwcbBufferSize,ppbBuffer,ppvObj);
860
861     if (dwFlags & DSBCAPS_PRIMARYBUFFER)
862         return DSD_CreatePrimaryBuffer(iface,pwfx,dwFlags,dwCardAddress,pdwcbBufferSize,ppbBuffer,ppvObj);
863
864     return DSD_CreateSecondaryBuffer(iface,pwfx,dwFlags,dwCardAddress,pdwcbBufferSize,ppbBuffer,ppvObj);
865 }
866
867 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
868                                                          PIDSDRIVERBUFFER pBuffer,
869                                                          LPVOID *ppvObj)
870 {
871     /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
872     TRACE("(%p,%p): stub\n",iface,pBuffer);
873     return DSERR_INVALIDCALL;
874 }
875
876 static const IDsDriverVtbl dsdvt =
877 {
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
887 };
888
889 static HRESULT WINAPI IDsDriverPropertySetImpl_Create(
890     IDsDriverBufferImpl * dsdb,
891     IDsDriverPropertySetImpl **pdsdps)
892 {
893     IDsDriverPropertySetImpl * dsdps;
894     TRACE("(%p,%p)\n",dsdb,pdsdps);
895
896     dsdps = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dsdps));
897     if (dsdps == NULL) {
898         WARN("out of memory\n");
899         return DSERR_OUTOFMEMORY;
900     }
901
902     dsdps->ref = 0;
903     dsdps->lpVtbl = &dsdpsvt;
904     dsdps->buffer = dsdb;
905     dsdb->property_set = dsdps;
906     IDsDriverBuffer_AddRef((PIDSDRIVER)dsdb);
907
908     *pdsdps = dsdps;
909     return DS_OK;
910 }
911
912 static HRESULT WINAPI IDsDriverNotifyImpl_Create(
913     IDsDriverBufferImpl * dsdb,
914     IDsDriverNotifyImpl **pdsdn)
915 {
916     IDsDriverNotifyImpl * dsdn;
917     TRACE("(%p,%p)\n",dsdb,pdsdn);
918
919     dsdn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dsdn));
920
921     if (dsdn == NULL) {
922         WARN("out of memory\n");
923         return DSERR_OUTOFMEMORY;
924     }
925
926     dsdn->ref = 0;
927     dsdn->lpVtbl = &dsdnvt;
928     dsdn->buffer = dsdb;
929     dsdb->notify = dsdn;
930     IDsDriverBuffer_AddRef((PIDSDRIVER)dsdb);
931
932     *pdsdn = dsdn;
933     return DS_OK;
934 }
935
936 DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
937 {
938     IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
939     TRACE("(%d,%p)\n",wDevID,drv);
940
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;
945     }
946
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;
956
957     return MMSYSERR_NOERROR;
958 }
959
960 DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
961 {
962     TRACE("(%d,%p)\n",wDevID,desc);
963     *desc = WOutDev[wDevID].ossdev.ds_desc;
964     return MMSYSERR_NOERROR;
965 }
966
967 #endif /* HAVE_OSS */