winealsa: Clear WAVECAPS_DIRECTSOUND for capture.
[wine] / dlls / winealsa.drv / dsoutput.c
1 /*
2  * Sample Wine Driver for Advanced Linux Sound System (ALSA)
3  *      Based on version <final> of the ALSA API
4  *
5  * Copyright    2002 Eric Pouech
6  *              2002 Marco Pietrobono
7  *              2003 Christian Costa : WaveIn support
8  *              2006-2007 Maarten Lankhorst
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 /*======================================================================*
26  *              Low level dsound output implementation                  *
27  *======================================================================*/
28
29 #include "config.h"
30 #include "wine/port.h"
31
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <string.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 #include <errno.h>
40 #include <limits.h>
41 #include <fcntl.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
44 #endif
45 #ifdef HAVE_SYS_MMAN_H
46 # include <sys/mman.h>
47 #endif
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winerror.h"
52 #include "winuser.h"
53 #include "winnls.h"
54 #include "mmddk.h"
55
56 #include "alsa.h"
57 #include "wine/library.h"
58 #include "wine/unicode.h"
59 #include "wine/debug.h"
60
61 #ifdef HAVE_ALSA
62
63 WINE_DEFAULT_DEBUG_CHANNEL(wave);
64 WINE_DECLARE_DEBUG_CHANNEL(waveloop);
65
66 typedef struct IDsDriverImpl IDsDriverImpl;
67 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
68
69 struct IDsDriverImpl
70 {
71     /* IUnknown fields */
72     const IDsDriverVtbl *lpVtbl;
73     LONG                ref;
74     /* IDsDriverImpl fields */
75     UINT                wDevID;
76     IDsDriverBufferImpl*primary;
77 };
78
79 struct IDsDriverBufferImpl
80 {
81     /* IUnknown fields */
82     const IDsDriverBufferVtbl *lpVtbl;
83     LONG                      ref;
84     /* IDsDriverBufferImpl fields */
85     IDsDriverImpl*            drv;
86
87     LPVOID                    mmap_buffer;
88     DWORD                     mmap_buflen_bytes;
89     snd_pcm_uframes_t         mmap_buflen_frames;
90     snd_pcm_channel_area_t *  mmap_areas;
91     snd_pcm_uframes_t         mmap_ppos; /* play position */
92     snd_pcm_uframes_t         mmap_wpos; /* write position */
93     HANDLE                    mmap_thread;
94     ALSA_MSG_RING             mmap_ring; /* sync object */
95 };
96
97 static void DSDB_CheckXRUN(IDsDriverBufferImpl* pdbi)
98 {
99     WINE_WAVEDEV *     wwo = &(WOutDev[pdbi->drv->wDevID]);
100     snd_pcm_state_t    state = snd_pcm_state(wwo->pcm);
101     snd_pcm_avail_update(wwo->pcm);
102
103     if ( state == SND_PCM_STATE_XRUN )
104     {
105         int err = snd_pcm_prepare(wwo->pcm);
106         WARN_(waveloop)("xrun occurred\n");
107         if ( err < 0 )
108             ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err));
109     }
110     else if ( state == SND_PCM_STATE_SUSPENDED )
111     {
112         int err = snd_pcm_resume(wwo->pcm);
113         TRACE_(waveloop)("recovery from suspension occurred\n");
114         if (err < 0 && err != -EAGAIN){
115             err = snd_pcm_prepare(wwo->pcm);
116             if (err < 0)
117                 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
118         }
119     } else if ( state != SND_PCM_STATE_RUNNING ) {
120         WARN_(waveloop)("Unhandled state: %d\n", state);
121     }
122 }
123
124 /**
125  * The helper thread for DirectSound
126  *
127  * Basically it does an infinite loop until it is told to die
128  * 
129  * snd_pcm_wait() is a call that polls the sound buffer and waits
130  * until there is at least 1 period free before it returns.
131  *
132  * We then commit the buffer filled by the owner of this
133  * IDSDriverBuffer */
134 static DWORD CALLBACK DBSB_MMAPLoop(LPVOID data)
135 {
136     IDsDriverBufferImpl* pdbi = (IDsDriverBufferImpl*)data;
137     WINE_WAVEDEV *wwo = &(WOutDev[pdbi->drv->wDevID]);
138     snd_pcm_uframes_t frames, wanted, ofs;
139     const snd_pcm_channel_area_t *areas;
140     int state = WINE_WS_STOPPED;
141     snd_pcm_state_t alsastate;
142
143     TRACE_(waveloop)("0x%8p\n", data);
144     TRACE("0x%8p, framelength: %lu, area: %8p\n", data, pdbi->mmap_buflen_frames, pdbi->mmap_areas);
145
146     do {
147         enum win_wm_message msg;
148         DWORD param;
149         HANDLE hEvent;
150         int err;
151         snd_pcm_format_t format;
152
153         if (state != WINE_WS_PLAYING)
154         {
155             ALSA_WaitRingMessage(&pdbi->mmap_ring, 1000000);
156             ALSA_RetrieveRingMessage(&pdbi->mmap_ring, &msg, &param, &hEvent);
157         }
158         else
159         while (!ALSA_RetrieveRingMessage(&pdbi->mmap_ring, &msg, &param, &hEvent))
160         {
161             snd_pcm_wait(wwo->pcm, -1);
162             DSDB_CheckXRUN(pdbi);
163
164             wanted = frames = pdbi->mmap_buflen_frames;
165
166             err = snd_pcm_mmap_begin(wwo->pcm, &areas, &ofs, &frames);
167             snd_pcm_mmap_commit(wwo->pcm, ofs, frames);
168
169             /* mark our current play position */
170             pdbi->mmap_ppos = ofs;
171             TRACE_(waveloop)("Updated position to %lx [%d, 0x%8p, %lu]\n", ofs, err, areas, frames);
172             /* Check to make sure we committed all we want to commit. ALSA
173              * only gives a contiguous linear region, so we need to check this
174              * in case we've reached the end of the buffer, in which case we
175              * can wrap around back to the beginning. */
176             if (frames < wanted) {
177                 frames = wanted - frames;
178                 snd_pcm_mmap_begin(wwo->pcm, &areas, &ofs, &frames);
179                 snd_pcm_mmap_commit(wwo->pcm, ofs, frames);
180             }
181             wanted = 0;
182             snd_pcm_mmap_begin(wwo->pcm, &areas, &ofs, &wanted);
183             snd_pcm_mmap_commit(wwo->pcm, ofs, wanted);
184             pdbi->mmap_wpos = ofs;
185         }
186
187         switch (msg) {
188         case WINE_WM_STARTING:
189             if (state == WINE_WS_PLAYING)
190                 break;
191
192             alsastate = snd_pcm_state(wwo->pcm);
193             if ( alsastate == SND_PCM_STATE_SETUP )
194             {
195                 err = snd_pcm_prepare(wwo->pcm);
196                 alsastate = snd_pcm_state(wwo->pcm);
197             }
198
199             snd_pcm_avail_update(wwo->pcm);
200
201             /* Rewind, and initialise */
202             frames = 0;
203             snd_pcm_mmap_begin(wwo->pcm, &areas, &ofs, &frames);
204             snd_pcm_mmap_commit(wwo->pcm, ofs, frames);
205             snd_pcm_rewind(wwo->pcm, ofs);
206             snd_pcm_hw_params_get_format(wwo->hw_params, &format);
207             snd_pcm_format_set_silence(format, pdbi->mmap_buffer, pdbi->mmap_buflen_frames);
208             pdbi->mmap_ppos = 0;
209             snd_pcm_hw_params_get_period_size(wwo->hw_params, &wanted, NULL);
210             pdbi->mmap_wpos = 2*wanted;
211             if ( alsastate == SND_PCM_STATE_PREPARED )
212             {
213                 err = snd_pcm_start(wwo->pcm);
214                 TRACE("Starting 0x%8p err: %d\n", wwo->pcm, err);
215             }
216             state = WINE_WS_PLAYING;
217             break;
218
219         case WINE_WM_STOPPING:
220             state = WINE_WS_STOPPED;
221             snd_pcm_drop(wwo->pcm);
222             break;
223
224         case WINE_WM_CLOSING:
225             if (wwo->pcm != NULL)
226                 snd_pcm_drop(wwo->pcm);
227             pdbi->mmap_thread = NULL;
228             SetEvent(hEvent);
229             goto out;
230         default:
231             ERR("Unhandled event %s\n", ALSA_getCmdString(msg));
232             break;
233         }
234         if (hEvent != INVALID_HANDLE_VALUE)
235             SetEvent(hEvent);
236     } while (1);
237 out:
238     TRACE_(waveloop)("Destroyed MMAP thread\n");
239     TRACE("Destroyed MMAP thread\n");
240     return 0;
241 }
242
243 /**
244  * Allocate the memory-mapped buffer for direct sound, and set up the
245  * callback.
246  */
247 static int DSDB_CreateMMAP(IDsDriverBufferImpl* pdbi)
248 {
249     WINE_WAVEDEV *     wwo = &(WOutDev[pdbi->drv->wDevID]);
250     snd_pcm_format_t   format;
251     snd_pcm_uframes_t  frames, ofs, avail, psize;
252     unsigned int       channels, bits_per_sample, bits_per_frame;
253     int                err, mmap_mode;
254
255     mmap_mode = snd_pcm_type(wwo->pcm);
256
257     ALSA_InitRingMessage(&pdbi->mmap_ring);
258
259     if (mmap_mode == SND_PCM_TYPE_HW) {
260         TRACE("mmap'd buffer is a hardware buffer.\n");
261     }
262     else {
263 #if 0 /* Horribly hack, shouldn't be used */
264         err = snd_pcm_hw_params_get_period_size(wwo->hw_params, &psize, NULL);
265         /* Set only a buffer of 2 period sizes, to decrease latency */
266         if (err >= 0)
267             err = snd_pcm_hw_params_set_buffer_size_near(wwo->pcm, wwo->hw_params, &psize);
268
269         psize *= 2;
270         if (err < 0) {
271             ERR("Errno %d (%s) occurred when setting buffer size\n", err, strerror(errno));
272         }
273 #endif
274         TRACE("mmap'd buffer is an ALSA emulation of hardware buffer.\n");
275     }
276
277     err = snd_pcm_hw_params_get_period_size(wwo->hw_params, &psize, NULL);
278     err = snd_pcm_hw_params_get_format(wwo->hw_params, &format);
279     err = snd_pcm_hw_params_get_buffer_size(wwo->hw_params, &frames);
280     err = snd_pcm_hw_params_get_channels(wwo->hw_params, &channels);
281     bits_per_sample = snd_pcm_format_physical_width(format);
282     bits_per_frame = bits_per_sample * channels;
283
284     if (TRACE_ON(wave))
285         ALSA_TraceParameters(wwo->hw_params, NULL, FALSE);
286
287     TRACE("format=%s  frames=%ld  channels=%d  bits_per_sample=%d  bits_per_frame=%d\n",
288           snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame);
289
290     pdbi->mmap_buflen_frames = frames;
291     pdbi->mmap_buflen_bytes = snd_pcm_frames_to_bytes( wwo->pcm, frames );
292
293     avail = snd_pcm_avail_update(wwo->pcm);
294     if (avail < 0)
295     {
296         ERR("No buffer is available: %s.\n", snd_strerror(avail));
297         return DSERR_GENERIC;
298     }
299     err = snd_pcm_mmap_begin(wwo->pcm, (const snd_pcm_channel_area_t **)&pdbi->mmap_areas, &ofs, &avail);
300     if ( err < 0 )
301     {
302         ERR("Can't map sound device for direct access: %s\n", snd_strerror(err));
303         return DSERR_GENERIC;
304     }
305     avail = 0;/* We don't have any data to commit yet */
306     err = snd_pcm_mmap_commit(wwo->pcm, ofs, avail);
307     if (ofs > 0)
308         err = snd_pcm_rewind(wwo->pcm, ofs);
309     pdbi->mmap_buffer = pdbi->mmap_areas->addr;
310     pdbi->mmap_thread = NULL;
311
312     TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
313         frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer);
314
315     return DS_OK;
316 }
317
318 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
319 {
320     /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
321     FIXME("(): stub!\n");
322     return DSERR_UNSUPPORTED;
323 }
324
325 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
326 {
327     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
328     ULONG refCount = InterlockedIncrement(&This->ref);
329
330     TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
331
332     return refCount;
333 }
334
335 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
336 {
337     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
338     ULONG refCount = InterlockedDecrement(&This->ref);
339
340     TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
341
342     if (refCount)
343         return refCount;
344
345     if (This->mmap_thread != NULL)
346         ALSA_AddRingMessage(&This->mmap_ring, WINE_WM_CLOSING, 0, 1);
347
348     TRACE("mmap buffer %p destroyed\n", This->mmap_buffer);
349     ALSA_DestroyRingMessage(&This->mmap_ring);
350
351     if (This == This->drv->primary)
352         This->drv->primary = NULL;
353     HeapFree(GetProcessHeap(), 0, This);
354     return 0;
355 }
356
357 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
358                                                LPVOID*ppvAudio1,LPDWORD pdwLen1,
359                                                LPVOID*ppvAudio2,LPDWORD pdwLen2,
360                                                DWORD dwWritePosition,DWORD dwWriteLen,
361                                                DWORD dwFlags)
362 {
363     /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
364     TRACE("(%p)\n",iface);
365     return DSERR_UNSUPPORTED;
366 }
367
368 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
369                                                  LPVOID pvAudio1,DWORD dwLen1,
370                                                  LPVOID pvAudio2,DWORD dwLen2)
371 {
372     /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
373     TRACE("(%p)\n",iface);
374     return DSERR_UNSUPPORTED;
375 }
376
377 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
378                                                     LPWAVEFORMATEX pwfx)
379 {
380     /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
381     TRACE("(%p,%p)\n",iface,pwfx);
382     return DSERR_BUFFERLOST;
383 }
384
385 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
386 {
387     /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
388     TRACE("(%p,%d): stub\n",iface,dwFreq);
389     return DSERR_UNSUPPORTED;
390 }
391
392 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
393 {
394     DWORD vol;
395     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
396     TRACE("(%p,%p)\n",iface,pVolPan);
397     vol = pVolPan->dwTotalLeftAmpFactor | (pVolPan->dwTotalRightAmpFactor << 16);
398
399     if (wodSetVolume(This->drv->wDevID, vol) != MMSYSERR_NOERROR) {
400         WARN("wodSetVolume failed\n");
401         return DSERR_INVALIDPARAM;
402     }
403
404     return DS_OK;
405 }
406
407 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
408 {
409     /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
410     FIXME("(%p,%d): stub\n",iface,dwNewPos);
411     return DSERR_UNSUPPORTED;
412 }
413
414 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
415                                                       LPDWORD lpdwPlay, LPDWORD lpdwWrite)
416 {
417     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
418     WINE_WAVEDEV *      wwo = &(WOutDev[This->drv->wDevID]);
419     snd_pcm_uframes_t   hw_pptr, hw_wptr;
420     snd_pcm_state_t     state;
421
422     if (wwo->hw_params == NULL || wwo->pcm == NULL) return DSERR_GENERIC;
423
424 #if 0 /* Shouldn't be needed */
425     /* we need to track down buffer underruns */
426     DSDB_CheckXRUN(This);
427 #endif
428
429     state = snd_pcm_state(wwo->pcm);
430     if (state == SND_PCM_STATE_RUNNING)
431     {
432         hw_pptr = This->mmap_ppos;
433         hw_wptr = This->mmap_wpos;
434     }
435     else
436         hw_pptr = hw_wptr = 0;
437
438     if (lpdwPlay)
439         *lpdwPlay = snd_pcm_frames_to_bytes(wwo->pcm, hw_pptr) % This->mmap_buflen_bytes;
440     if (lpdwWrite)
441         *lpdwWrite = snd_pcm_frames_to_bytes(wwo->pcm, hw_wptr) % This->mmap_buflen_bytes;
442
443     TRACE_(waveloop)("hw_pptr=0x%08x, hw_wptr=0x%08x playpos=%d, writepos=%d\n", (unsigned int)hw_pptr, (unsigned int)hw_wptr, lpdwPlay?*lpdwPlay:-1, lpdwWrite?*lpdwWrite:-1);
444     return DS_OK;
445 }
446
447 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
448 {
449     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
450
451     TRACE("(%p,%x,%x,%x)\n",iface,dwRes1,dwRes2,dwFlags);
452
453     if (This->mmap_thread == NULL)
454         This->mmap_thread = CreateThread(NULL, 0, DBSB_MMAPLoop, (LPVOID)(DWORD)This, 0, NULL);
455
456     if (This->mmap_thread == NULL)
457         ERR("Cannot create sound thread, don't expect any sound at all\n");
458     else
459         ALSA_AddRingMessage(&This->mmap_ring, WINE_WM_STARTING, 0, 1);
460
461     return DS_OK;
462 }
463
464 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
465 {
466     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
467
468     TRACE("(%p)\n",iface);
469
470     if (This->mmap_thread != NULL)
471         ALSA_AddRingMessage(&This->mmap_ring, WINE_WM_STOPPING, 0, 1);
472
473     return DS_OK;
474 }
475
476 static const IDsDriverBufferVtbl dsdbvt =
477 {
478     IDsDriverBufferImpl_QueryInterface,
479     IDsDriverBufferImpl_AddRef,
480     IDsDriverBufferImpl_Release,
481     IDsDriverBufferImpl_Lock,
482     IDsDriverBufferImpl_Unlock,
483     IDsDriverBufferImpl_SetFormat,
484     IDsDriverBufferImpl_SetFrequency,
485     IDsDriverBufferImpl_SetVolumePan,
486     IDsDriverBufferImpl_SetPosition,
487     IDsDriverBufferImpl_GetPosition,
488     IDsDriverBufferImpl_Play,
489     IDsDriverBufferImpl_Stop
490 };
491
492 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
493 {
494     /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
495     FIXME("(%p): stub!\n",iface);
496     return DSERR_UNSUPPORTED;
497 }
498
499 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
500 {
501     IDsDriverImpl *This = (IDsDriverImpl *)iface;
502     ULONG refCount = InterlockedIncrement(&This->ref);
503
504     TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
505
506     return refCount;
507 }
508
509 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
510 {
511     IDsDriverImpl *This = (IDsDriverImpl *)iface;
512     ULONG refCount = InterlockedDecrement(&This->ref);
513
514     TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
515
516     if (refCount)
517         return refCount;
518     HeapFree(GetProcessHeap(),0,This);
519     return 0;
520 }
521
522 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
523 {
524     IDsDriverImpl *This = (IDsDriverImpl *)iface;
525     TRACE("(%p,%p)\n",iface,pDesc);
526     memcpy(pDesc, &(WOutDev[This->wDevID].ds_desc), sizeof(DSDRIVERDESC));
527     pDesc->dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
528         DSDDESC_USESYSTEMMEMORY | DSDDESC_DONTNEEDPRIMARYLOCK;
529     pDesc->dnDevNode            = WOutDev[This->wDevID].waveDesc.dnDevNode;
530     pDesc->wVxdId               = 0;
531     pDesc->wReserved            = 0;
532     pDesc->ulDeviceNum          = This->wDevID;
533     pDesc->dwHeapType           = DSDHEAP_NOHEAP;
534     pDesc->pvDirectDrawHeap     = NULL;
535     pDesc->dwMemStartAddress    = 0;
536     pDesc->dwMemEndAddress      = 0;
537     pDesc->dwMemAllocExtra      = 0;
538     pDesc->pvReserved1          = NULL;
539     pDesc->pvReserved2          = NULL;
540     return DS_OK;
541 }
542
543 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
544 {
545     /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
546     TRACE("(%p)\n",iface);
547     return DS_OK;
548 }
549
550 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
551 {
552     /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
553     TRACE("(%p)\n",iface);
554     return DS_OK;
555 }
556
557 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
558 {
559     IDsDriverImpl *This = (IDsDriverImpl *)iface;
560     TRACE("(%p,%p)\n",iface,pCaps);
561     memcpy(pCaps, &(WOutDev[This->wDevID].ds_caps), sizeof(DSDRIVERCAPS));
562     return DS_OK;
563 }
564
565 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
566                                                       LPWAVEFORMATEX pwfx,
567                                                       DWORD dwFlags, DWORD dwCardAddress,
568                                                       LPDWORD pdwcbBufferSize,
569                                                       LPBYTE *ppbBuffer,
570                                                       LPVOID *ppvObj)
571 {
572     IDsDriverImpl *This = (IDsDriverImpl *)iface;
573     IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
574     int err;
575
576     TRACE("(%p,%p,%x,%x)\n",iface,pwfx,dwFlags,dwCardAddress);
577     /* we only support primary buffers */
578     if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
579         return DSERR_UNSUPPORTED;
580     if (This->primary)
581         return DSERR_ALLOCATED;
582     if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
583         return DSERR_CONTROLUNAVAIL;
584
585     *ippdsdb = HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverBufferImpl));
586     if (*ippdsdb == NULL)
587         return DSERR_OUTOFMEMORY;
588     (*ippdsdb)->lpVtbl  = &dsdbvt;
589     (*ippdsdb)->ref     = 1;
590     (*ippdsdb)->drv     = This;
591
592     err = DSDB_CreateMMAP((*ippdsdb));
593     if ( err != DS_OK )
594      {
595         HeapFree(GetProcessHeap(), 0, *ippdsdb);
596         *ippdsdb = NULL;
597         return err;
598      }
599     *ppbBuffer = (*ippdsdb)->mmap_buffer;
600     *pdwcbBufferSize = (*ippdsdb)->mmap_buflen_bytes;
601
602     This->primary = *ippdsdb;
603
604     /* buffer is ready to go */
605     TRACE("buffer created at %p\n", *ippdsdb);
606     return DS_OK;
607 }
608
609 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
610                                                          PIDSDRIVERBUFFER pBuffer,
611                                                          LPVOID *ppvObj)
612 {
613     /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
614     TRACE("(%p,%p): stub\n",iface,pBuffer);
615     return DSERR_INVALIDCALL;
616 }
617
618 static const IDsDriverVtbl dsdvt =
619 {
620     IDsDriverImpl_QueryInterface,
621     IDsDriverImpl_AddRef,
622     IDsDriverImpl_Release,
623     IDsDriverImpl_GetDriverDesc,
624     IDsDriverImpl_Open,
625     IDsDriverImpl_Close,
626     IDsDriverImpl_GetCaps,
627     IDsDriverImpl_CreateSoundBuffer,
628     IDsDriverImpl_DuplicateSoundBuffer
629 };
630
631 DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
632 {
633     IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
634
635     TRACE("driver created\n");
636
637     /* the HAL isn't much better than the HEL if we can't do mmap() */
638     if (!(WOutDev[wDevID].outcaps.dwSupport & WAVECAPS_DIRECTSOUND)) {
639         ERR("DirectSound flag not set\n");
640         MESSAGE("This sound card's driver does not support direct access\n");
641         MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
642         return MMSYSERR_NOTSUPPORTED;
643     }
644
645     *idrv = HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
646     if (!*idrv)
647         return MMSYSERR_NOMEM;
648     (*idrv)->lpVtbl     = &dsdvt;
649     (*idrv)->ref        = 1;
650
651     (*idrv)->wDevID     = wDevID;
652     (*idrv)->primary    = NULL;
653     return MMSYSERR_NOERROR;
654 }
655
656 DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
657 {
658     memcpy(desc, &(WOutDev[wDevID].ds_desc), sizeof(DSDRIVERDESC));
659     return MMSYSERR_NOERROR;
660 }
661
662 #endif /* HAVE_ALSA */