wined3d: Move device context array functions to device.c.
[wine] / dlls / winealsa.drv / dscapture.c
1 /*
2  * Sample Wine Driver for Advanced Linux Sound System (ALSA)
3  *      Based on version <final> of the ALSA API
4  *
5  * Copyright 2007 - Maarten Lankhorst
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 /*======================================================================*
23  *              Low level dsound input implementation                   *
24  *======================================================================*/
25
26 #include "config.h"
27 #include "wine/port.h"
28
29 #include <stdlib.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <string.h>
33 #ifdef HAVE_UNISTD_H
34 # include <unistd.h>
35 #endif
36 #include <errno.h>
37 #include <limits.h>
38 #include <fcntl.h>
39 #ifdef HAVE_SYS_IOCTL_H
40 # include <sys/ioctl.h>
41 #endif
42 #ifdef HAVE_SYS_MMAN_H
43 # include <sys/mman.h>
44 #endif
45 #include "windef.h"
46 #include "winbase.h"
47 #include "wingdi.h"
48 #include "winerror.h"
49 #include "winuser.h"
50 #include "mmddk.h"
51
52 #include "alsa.h"
53 #include "wine/library.h"
54 #include "wine/unicode.h"
55 #include "wine/debug.h"
56
57 #ifdef HAVE_ALSA
58
59 /* Notify timer checks every 10 ms with a resolution of 2 ms */
60 #define DS_TIME_DEL 10
61 #define DS_TIME_RES 2
62
63 WINE_DEFAULT_DEBUG_CHANNEL(dsalsa);
64
65 typedef struct IDsCaptureDriverBufferImpl IDsCaptureDriverBufferImpl;
66
67 typedef struct IDsCaptureDriverImpl
68 {
69     const IDsCaptureDriverVtbl *lpVtbl;
70     LONG ref;
71     IDsCaptureDriverBufferImpl* capture_buffer;
72     UINT wDevID;
73 } IDsCaptureDriverImpl;
74
75 typedef struct IDsCaptureDriverNotifyImpl
76 {
77     const IDsDriverNotifyVtbl *lpVtbl;
78     LONG ref;
79     IDsCaptureDriverBufferImpl *buffer;
80     DSBPOSITIONNOTIFY *notifies;
81     DWORD nrofnotifies, playpos;
82     UINT timerID;
83 } IDsCaptureDriverNotifyImpl;
84
85 struct IDsCaptureDriverBufferImpl
86 {
87     const IDsCaptureDriverBufferVtbl *lpVtbl;
88     LONG ref;
89     IDsCaptureDriverImpl *drv;
90     IDsCaptureDriverNotifyImpl *notify;
91
92     CRITICAL_SECTION pcm_crst;
93     LPBYTE mmap_buffer, presented_buffer;
94     DWORD mmap_buflen_bytes, play_looping, mmap_ofs_bytes;
95     BOOL mmap;
96
97     /* Note: snd_pcm_frames_to_bytes(This->pcm, mmap_buflen_frames) != mmap_buflen_bytes */
98     /* The actual buffer may differ in size from the wanted buffer size */
99
100     snd_pcm_t *pcm;
101     snd_pcm_hw_params_t *hw_params;
102     snd_pcm_sw_params_t *sw_params;
103     snd_pcm_uframes_t mmap_buflen_frames, mmap_pos;
104 };
105
106 static void Capture_CheckNotify(IDsCaptureDriverNotifyImpl *This, DWORD from, DWORD len)
107 {
108     unsigned i;
109     for (i = 0; i < This->nrofnotifies; ++i) {
110         LPDSBPOSITIONNOTIFY event = This->notifies + i;
111         DWORD offset = event->dwOffset;
112         TRACE("checking %d, position %d, event = %p\n", i, offset, event->hEventNotify);
113
114         if (offset == DSBPN_OFFSETSTOP) {
115             if (!from && !len) {
116                 SetEvent(event->hEventNotify);
117                 TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
118                 return;
119             }
120             else return;
121         }
122
123         if (offset >= from && offset < (from + len))
124         {
125             TRACE("signalled event %p (%d)\n", event->hEventNotify, i);
126             SetEvent(event->hEventNotify);
127         }
128     }
129 }
130
131 static void CALLBACK Capture_Notify(UINT timerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2)
132 {
133     IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)dwUser;
134     DWORD last_playpos, playpos;
135     PIDSCDRIVERBUFFER iface = (PIDSCDRIVERBUFFER)This;
136
137     /* **** */
138     /* Don't deadlock since there is a critical section can be held by the timer api itself while running this code */
139     if (!TryEnterCriticalSection(&This->pcm_crst)) return;
140
141     IDsDriverBuffer_GetPosition(iface, &playpos, NULL);
142     last_playpos = This->notify->playpos;
143     This->notify->playpos = playpos;
144
145     if (snd_pcm_state(This->pcm) != SND_PCM_STATE_RUNNING || last_playpos == playpos || !This->notify->nrofnotifies || !This->notify->notifies)
146         goto done;
147
148     if (playpos < last_playpos)
149     {
150         Capture_CheckNotify(This->notify, last_playpos, This->mmap_buflen_bytes);
151         if (playpos)
152             Capture_CheckNotify(This->notify, 0, playpos);
153     }
154     else Capture_CheckNotify(This->notify, last_playpos, playpos - last_playpos);
155
156 done:
157     LeaveCriticalSection(&This->pcm_crst);
158     /* **** */
159 }
160
161 static HRESULT WINAPI IDsCaptureDriverNotifyImpl_QueryInterface(PIDSDRIVERNOTIFY iface, REFIID riid, LPVOID *ppobj)
162 {
163     IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
164     TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
165
166     if ( IsEqualGUID(riid, &IID_IUnknown) ||
167          IsEqualGUID(riid, &IID_IDsDriverNotify) ) {
168         IDsDriverNotify_AddRef(iface);
169         *ppobj = This;
170         return DS_OK;
171     }
172
173     FIXME( "Unknown IID %s\n", debugstr_guid(riid));
174
175     *ppobj = 0;
176     return E_NOINTERFACE;
177 }
178
179 static ULONG WINAPI IDsCaptureDriverNotifyImpl_AddRef(PIDSDRIVERNOTIFY iface)
180 {
181     IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
182     ULONG refCount = InterlockedIncrement(&This->ref);
183
184     TRACE("(%p) ref was %d\n", This, refCount - 1);
185
186     return refCount;
187 }
188
189 static ULONG WINAPI IDsCaptureDriverNotifyImpl_Release(PIDSDRIVERNOTIFY iface)
190 {
191     IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
192     ULONG refCount = InterlockedDecrement(&This->ref);
193
194     TRACE("(%p) ref was %d\n", This, refCount + 1);
195
196     if (!refCount) {
197         This->buffer->notify = NULL;
198         if (This->timerID)
199         {
200             timeKillEvent(This->timerID);
201             timeEndPeriod(DS_TIME_RES);
202         }
203         HeapFree(GetProcessHeap(), 0, This->notifies);
204         HeapFree(GetProcessHeap(), 0, This);
205         TRACE("(%p) released\n", This);
206     }
207     return refCount;
208 }
209
210 static HRESULT WINAPI IDsCaptureDriverNotifyImpl_SetNotificationPositions(PIDSDRIVERNOTIFY iface, DWORD howmuch, LPCDSBPOSITIONNOTIFY notify)
211 {
212     DWORD len = howmuch * sizeof(DSBPOSITIONNOTIFY);
213     unsigned i;
214     LPVOID notifies;
215     IDsCaptureDriverNotifyImpl *This = (IDsCaptureDriverNotifyImpl *)iface;
216     TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
217
218     if (!notify) {
219         WARN("invalid parameter\n");
220         return DSERR_INVALIDPARAM;
221     }
222
223     if (TRACE_ON(dsalsa))
224         for (i=0;i<howmuch; ++i)
225             TRACE("notify at %d to %p\n", notify[i].dwOffset, notify[i].hEventNotify);
226
227     /* **** */
228     EnterCriticalSection(&This->buffer->pcm_crst);
229
230     /* Make an internal copy of the caller-supplied array.
231      * Replace the existing copy if one is already present. */
232     if (This->notifies)
233         notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, This->notifies, len);
234     else
235         notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, len);
236
237     if (!notifies)
238     {
239         LeaveCriticalSection(&This->buffer->pcm_crst);
240         /* **** */
241         return DSERR_OUTOFMEMORY;
242     }
243     This->notifies = notifies;
244     memcpy(This->notifies, notify, len);
245     This->nrofnotifies = howmuch;
246     IDsDriverBuffer_GetPosition((PIDSCDRIVERBUFFER)This->buffer, &This->playpos, NULL);
247
248     if (!This->timerID)
249     {
250         timeBeginPeriod(DS_TIME_RES);
251         This->timerID = timeSetEvent(DS_TIME_DEL, DS_TIME_RES, Capture_Notify, (DWORD_PTR)This->buffer, TIME_PERIODIC | TIME_KILL_SYNCHRONOUS);
252     }
253
254     LeaveCriticalSection(&This->buffer->pcm_crst);
255     /* **** */
256
257     return S_OK;
258 }
259
260 static const IDsDriverNotifyVtbl dscdnvt =
261 {
262     IDsCaptureDriverNotifyImpl_QueryInterface,
263     IDsCaptureDriverNotifyImpl_AddRef,
264     IDsCaptureDriverNotifyImpl_Release,
265     IDsCaptureDriverNotifyImpl_SetNotificationPositions,
266 };
267
268 #if 0
269 /** Convert the position an application sees into a position ALSA sees */
270 static snd_pcm_uframes_t fakepos_to_realpos(const IDsCaptureDriverBufferImpl* This, DWORD fakepos)
271 {
272     snd_pcm_uframes_t realpos;
273     if (fakepos < This->mmap_ofs_bytes)
274         realpos = This->mmap_buflen_bytes + fakepos - This->mmap_ofs_bytes;
275     else realpos = fakepos - This->mmap_ofs_bytes;
276     return snd_pcm_bytes_to_frames(This->pcm, realpos) % This->mmap_buflen_frames;
277 }
278 #endif
279
280 /** Convert the position ALSA sees into a position an application sees */
281 static DWORD realpos_to_fakepos(const IDsCaptureDriverBufferImpl* This, snd_pcm_uframes_t realpos)
282 {
283     DWORD realposb = snd_pcm_frames_to_bytes(This->pcm, realpos);
284     return (realposb + This->mmap_ofs_bytes) % This->mmap_buflen_bytes;
285 }
286
287 /** Raw copy data, with buffer wrap around */
288 static void CopyDataWrap(const IDsCaptureDriverBufferImpl* This, LPBYTE dest, DWORD fromwhere, DWORD copylen, DWORD buflen)
289 {
290     DWORD remainder = buflen - fromwhere;
291     if (remainder >= copylen)
292     {
293         CopyMemory(dest, This->mmap_buffer + fromwhere, copylen);
294     }
295     else
296     {
297         CopyMemory(dest, This->mmap_buffer + fromwhere, remainder);
298         copylen -= remainder;
299         CopyMemory(dest, This->mmap_buffer, copylen);
300     }
301 }
302
303 /** Copy data from the mmap buffer to backbuffer, taking into account all wraparounds that may occur */
304 static void CopyData(const IDsCaptureDriverBufferImpl* This, snd_pcm_uframes_t fromwhere, snd_pcm_uframes_t len)
305 {
306     DWORD dlen = snd_pcm_frames_to_bytes(This->pcm, len) % This->mmap_buflen_bytes;
307
308     /* Backbuffer */
309     DWORD ofs = realpos_to_fakepos(This, fromwhere);
310     DWORD remainder = This->mmap_buflen_bytes - ofs;
311
312     /* MMAP buffer */
313     DWORD realbuflen = snd_pcm_frames_to_bytes(This->pcm, This->mmap_buflen_frames);
314     DWORD realofs = snd_pcm_frames_to_bytes(This->pcm, fromwhere);
315
316     if (remainder >= dlen)
317     {
318        CopyDataWrap(This, This->presented_buffer + ofs, realofs, dlen, realbuflen);
319     }
320     else
321     {
322        CopyDataWrap(This, This->presented_buffer + ofs, realofs, remainder, realbuflen);
323        dlen -= remainder;
324        CopyDataWrap(This, This->presented_buffer, (realofs+remainder)%realbuflen, dlen, realbuflen);
325     }
326 }
327
328 /** Fill buffers, for starting and stopping
329  * Alsa won't start playing until everything is filled up
330  * This also updates mmap_pos
331  *
332  * Returns: Amount of periods in use so snd_pcm_avail_update
333  * doesn't have to be called up to 4x in GetPosition()
334  */
335 static snd_pcm_uframes_t CommitAll(IDsCaptureDriverBufferImpl *This, DWORD forced)
336 {
337     const snd_pcm_channel_area_t *areas;
338     snd_pcm_uframes_t used;
339     const snd_pcm_uframes_t commitahead = This->mmap_buflen_frames;
340
341     used = This->mmap_buflen_frames - snd_pcm_avail_update(This->pcm);
342     TRACE("%p needs to commit to %lu, used: %lu\n", This, commitahead, used);
343     if (used < commitahead && (forced || This->play_looping))
344     {
345         snd_pcm_uframes_t done, putin = commitahead - used;
346         if (This->mmap)
347         {
348             snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
349             CopyData(This, This->mmap_pos, putin);
350             done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
351
352             This->mmap_pos += done;
353             used += done;
354             putin = commitahead - used;
355
356             if (This->mmap_pos == This->mmap_buflen_frames && (snd_pcm_sframes_t)putin > 0 && This->play_looping)
357             {
358                 This->mmap_ofs_bytes += snd_pcm_frames_to_bytes(This->pcm, This->mmap_buflen_frames);
359                 This->mmap_ofs_bytes %= This->mmap_buflen_bytes;
360
361                 snd_pcm_mmap_begin(This->pcm, &areas, &This->mmap_pos, &putin);
362                 CopyData(This, This->mmap_pos, putin);
363                 done = snd_pcm_mmap_commit(This->pcm, This->mmap_pos, putin);
364
365                 This->mmap_pos += done;
366                 used += done;
367             }
368         }
369         else
370         {
371             DWORD pos;
372             snd_pcm_sframes_t ret;
373
374             snd_pcm_uframes_t cap = snd_pcm_bytes_to_frames(This->pcm, This->mmap_buflen_bytes);
375             pos = realpos_to_fakepos(This, This->mmap_pos);
376             if (This->mmap_pos + putin > cap)
377                 putin = cap - This->mmap_pos;
378             ret = snd_pcm_readi(This->pcm, This->presented_buffer + pos, putin);
379             if (ret == -EPIPE)
380             {
381                 WARN("Underrun occurred\n");
382                 snd_pcm_prepare(This->pcm);
383                 ret = snd_pcm_readi(This->pcm, This->presented_buffer + pos, putin);
384                 snd_pcm_start(This->pcm);
385             }
386             if (ret < 0)
387             {
388                 WARN("Committing data: %ld / %s (%ld)\n", ret, snd_strerror(ret), putin);
389                 ret = 0;
390             }
391             This->mmap_pos += ret;
392             used += ret;
393             /* At this point mmap_pos may be >= This->mmap_pos this is harmless
394              * realpos_to_fakepos handles it well, and below it is truncated
395              */
396
397             putin = commitahead - used;
398             if (putin > 0)
399             {
400                 pos = realpos_to_fakepos(This, This->mmap_pos);
401                 ret = snd_pcm_readi(This->pcm, This->presented_buffer + pos, putin);
402                 if (ret > 0)
403                 {
404                     This->mmap_pos += ret;
405                     used += ret;
406                 }
407             }
408         }
409
410     }
411
412     if (This->mmap_pos >= This->mmap_buflen_frames)
413     {
414         This->mmap_ofs_bytes += snd_pcm_frames_to_bytes(This->pcm, This->mmap_buflen_frames);
415         This->mmap_ofs_bytes %= This->mmap_buflen_bytes;
416         This->mmap_pos -= This->mmap_buflen_frames;
417     }
418
419     return used;
420 }
421
422 static void CheckXRUN(IDsCaptureDriverBufferImpl* This)
423 {
424     snd_pcm_state_t state = snd_pcm_state(This->pcm);
425     snd_pcm_sframes_t delay;
426     int err;
427
428     snd_pcm_hwsync(This->pcm);
429     snd_pcm_delay(This->pcm, &delay);
430     if ( state == SND_PCM_STATE_XRUN )
431     {
432         err = snd_pcm_prepare(This->pcm);
433         CommitAll(This, FALSE);
434         snd_pcm_start(This->pcm);
435         WARN("xrun occurred\n");
436         if ( err < 0 )
437             ERR("recovery from xrun failed, prepare failed: %s\n", snd_strerror(err));
438     }
439     else if ( state == SND_PCM_STATE_SUSPENDED )
440     {
441         int err = snd_pcm_resume(This->pcm);
442         TRACE("recovery from suspension occurred\n");
443         if (err < 0 && err != -EAGAIN){
444             err = snd_pcm_prepare(This->pcm);
445             if (err < 0)
446                 ERR("recovery from suspend failed, prepare failed: %s\n", snd_strerror(err));
447         }
448     }
449     else if ( state != SND_PCM_STATE_RUNNING)
450     {
451         WARN("Unhandled state: %d\n", state);
452     }
453 }
454
455 /**
456  * Allocate the memory-mapped buffer for direct sound, and set up the
457  * callback.
458  */
459 static int CreateMMAP(IDsCaptureDriverBufferImpl* pdbi)
460 {
461     snd_pcm_t *pcm = pdbi->pcm;
462     snd_pcm_format_t format;
463     snd_pcm_uframes_t frames, ofs, avail, psize, boundary;
464     unsigned int channels, bits_per_sample, bits_per_frame;
465     int err, mmap_mode;
466     const snd_pcm_channel_area_t *areas;
467     snd_pcm_hw_params_t *hw_params = pdbi->hw_params;
468     snd_pcm_sw_params_t *sw_params = pdbi->sw_params;
469
470     mmap_mode = snd_pcm_type(pcm);
471
472     if (mmap_mode == SND_PCM_TYPE_HW)
473         TRACE("mmap'd buffer is a direct hardware buffer.\n");
474     else if (mmap_mode == SND_PCM_TYPE_DMIX)
475         TRACE("mmap'd buffer is an ALSA dmix buffer\n");
476     else
477         TRACE("mmap'd buffer is an ALSA type %d buffer\n", mmap_mode);
478
479     err = snd_pcm_hw_params_get_period_size(hw_params, &psize, NULL);
480     err = snd_pcm_hw_params_get_format(hw_params, &format);
481     err = snd_pcm_hw_params_get_buffer_size(hw_params, &frames);
482     err = snd_pcm_hw_params_get_channels(hw_params, &channels);
483     bits_per_sample = snd_pcm_format_physical_width(format);
484     bits_per_frame = bits_per_sample * channels;
485
486     if (TRACE_ON(dsalsa))
487         ALSA_TraceParameters(hw_params, NULL, FALSE);
488
489     TRACE("format=%s  frames=%ld  channels=%d  bits_per_sample=%d  bits_per_frame=%d\n",
490           snd_pcm_format_name(format), frames, channels, bits_per_sample, bits_per_frame);
491
492     pdbi->mmap_buflen_frames = frames;
493     snd_pcm_sw_params_current(pcm, sw_params);
494     snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 0);
495     snd_pcm_sw_params_get_boundary(sw_params, &boundary);
496     snd_pcm_sw_params_set_stop_threshold(pcm, sw_params, boundary);
497     snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, INT_MAX);
498     snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0);
499     snd_pcm_sw_params_set_avail_min(pcm, sw_params, 0);
500     err = snd_pcm_sw_params(pcm, sw_params);
501
502     pdbi->mmap_ofs_bytes = 0;
503     if (!pdbi->mmap)
504     {
505         pdbi->mmap_buffer = NULL;
506
507         frames = snd_pcm_bytes_to_frames(pdbi->pcm, pdbi->mmap_buflen_bytes);
508         snd_pcm_format_set_silence(format, pdbi->presented_buffer, frames);
509         pdbi->mmap_pos = 0;
510     }
511     else
512     {
513         err = snd_pcm_mmap_begin(pcm, &areas, &ofs, &avail);
514         if ( err < 0 )
515         {
516             ERR("Can't map sound device for direct access: %s/%d\n", snd_strerror(err), err);
517             return DSERR_GENERIC;
518         }
519         snd_pcm_format_set_silence(format, areas->addr, pdbi->mmap_buflen_frames);
520         pdbi->mmap_pos = ofs + snd_pcm_mmap_commit(pcm, ofs, 0);
521         pdbi->mmap_buffer = areas->addr;
522     }
523
524     TRACE("created mmap buffer of %ld frames (%d bytes) at %p\n",
525         pdbi->mmap_buflen_frames, pdbi->mmap_buflen_bytes, pdbi->mmap_buffer);
526
527     return DS_OK;
528 }
529
530 static HRESULT WINAPI IDsCaptureDriverBufferImpl_QueryInterface(PIDSCDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
531 {
532     IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
533     if ( IsEqualGUID(riid, &IID_IUnknown) ||
534          IsEqualGUID(riid, &IID_IDsCaptureDriverBuffer) ) {
535         IDsCaptureDriverBuffer_AddRef(iface);
536         *ppobj = iface;
537         return DS_OK;
538     }
539
540     if ( IsEqualGUID( &IID_IDsDriverNotify, riid ) ) {
541         if (!This->notify)
542         {
543             This->notify = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsCaptureDriverNotifyImpl));
544             if (!This->notify)
545                 return DSERR_OUTOFMEMORY;
546             This->notify->lpVtbl = &dscdnvt;
547             This->notify->buffer = This;
548
549             /* Keep a lock on IDsDriverNotify for ourself, so it is destroyed when the buffer is */
550             IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY)This->notify);
551         }
552         IDsDriverNotify_AddRef((PIDSDRIVERNOTIFY)This->notify);
553         *ppobj = This->notify;
554         return DS_OK;
555     }
556
557     if ( IsEqualGUID( &IID_IDsDriverPropertySet, riid ) ) {
558         FIXME("Unsupported interface IID_IDsDriverPropertySet\n");
559         return E_FAIL;
560     }
561
562     FIXME("(): Unknown interface %s\n", debugstr_guid(riid));
563     return DSERR_UNSUPPORTED;
564 }
565
566 static ULONG WINAPI IDsCaptureDriverBufferImpl_AddRef(PIDSCDRIVERBUFFER iface)
567 {
568     IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
569     ULONG refCount = InterlockedIncrement(&This->ref);
570
571     TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
572
573     return refCount;
574 }
575
576 static ULONG WINAPI IDsCaptureDriverBufferImpl_Release(PIDSCDRIVERBUFFER iface)
577 {
578     IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
579     ULONG refCount = InterlockedDecrement(&This->ref);
580
581     TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
582
583     if (refCount)
584         return refCount;
585
586     EnterCriticalSection(&This->pcm_crst);
587     if (This->notify)
588         IDsDriverNotify_Release((PIDSDRIVERNOTIFY)This->notify);
589     TRACE("mmap buffer %p destroyed\n", This->mmap_buffer);
590
591     This->drv->capture_buffer = NULL;
592     LeaveCriticalSection(&This->pcm_crst);
593     This->pcm_crst.DebugInfo->Spare[0] = 0;
594     DeleteCriticalSection(&This->pcm_crst);
595
596     snd_pcm_drop(This->pcm);
597     snd_pcm_close(This->pcm);
598     This->pcm = NULL;
599     HeapFree(GetProcessHeap(), 0, This->presented_buffer);
600     HeapFree(GetProcessHeap(), 0, This->sw_params);
601     HeapFree(GetProcessHeap(), 0, This->hw_params);
602     HeapFree(GetProcessHeap(), 0, This);
603     return 0;
604 }
605
606 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Lock(PIDSCDRIVERBUFFER iface, LPVOID*ppvAudio1,LPDWORD pdwLen1,LPVOID*ppvAudio2,LPDWORD pdwLen2, DWORD dwWritePosition,DWORD dwWriteLen, DWORD dwFlags)
607 {
608     IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
609     TRACE("(%p,%p,%p,%p,%p,%d,%d,0x%08x)\n", This, ppvAudio1, pdwLen1, ppvAudio2, pdwLen2, dwWritePosition, dwWriteLen, dwFlags);
610
611     if (ppvAudio1)
612         *ppvAudio1 = (LPBYTE)This->presented_buffer + dwWritePosition;
613
614     if (dwWritePosition + dwWriteLen <= This->mmap_buflen_bytes) {
615         if (pdwLen1)
616             *pdwLen1 = dwWriteLen;
617         if (ppvAudio2)
618             *ppvAudio2 = 0;
619         if (pdwLen2)
620             *pdwLen2 = 0;
621     } else {
622         if (pdwLen1)
623             *pdwLen1 = This->mmap_buflen_bytes - dwWritePosition;
624         if (ppvAudio2)
625             *ppvAudio2 = This->presented_buffer;
626         if (pdwLen2)
627             *pdwLen2 = dwWriteLen - (This->mmap_buflen_bytes - dwWritePosition);
628     }
629     return DS_OK;
630 }
631
632 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Unlock(PIDSCDRIVERBUFFER iface, LPVOID pvAudio1,DWORD dwLen1, LPVOID pvAudio2,DWORD dwLen2)
633 {
634     IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
635     TRACE("(%p,%p,%d,%p,%d)\n",This,pvAudio1,dwLen1,pvAudio2,dwLen2);
636     return DS_OK;
637 }
638
639 static HRESULT WINAPI IDsCaptureDriverBufferImpl_SetFormat(PIDSCDRIVERBUFFER iface, LPWAVEFORMATEX pwfx)
640 {
641     IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
642     WINE_WAVEDEV *wwi = &WInDev[This->drv->wDevID];
643     snd_pcm_t *pcm = NULL;
644     snd_pcm_hw_params_t *hw_params = This->hw_params;
645     snd_pcm_format_t format = -1;
646     snd_pcm_uframes_t buffer_size;
647     DWORD rate = pwfx->nSamplesPerSec;
648     int err=0;
649     BOOL mmap;
650
651     TRACE("(%p, %p)\n", iface, pwfx);
652
653     switch (pwfx->wBitsPerSample)
654     {
655         case  8: format = SND_PCM_FORMAT_U8; break;
656         case 16: format = SND_PCM_FORMAT_S16_LE; break;
657         case 24: format = SND_PCM_FORMAT_S24_3LE; break;
658         case 32: format = SND_PCM_FORMAT_S32_LE; break;
659         default: FIXME("Unsupported bpp: %d\n", pwfx->wBitsPerSample); return DSERR_GENERIC;
660     }
661
662     /* **** */
663     EnterCriticalSection(&This->pcm_crst);
664
665     err = snd_pcm_open(&pcm, wwi->pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
666
667     if (err < 0)
668     {
669         if (errno != EBUSY || !This->pcm)
670         {
671             /* **** */
672             LeaveCriticalSection(&This->pcm_crst);
673             WARN("Cannot open sound device: %s\n", snd_strerror(err));
674             return DSERR_GENERIC;
675         }
676         snd_pcm_drop(This->pcm);
677         snd_pcm_close(This->pcm);
678         This->pcm = NULL;
679         err = snd_pcm_open(&pcm, wwi->pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
680         if (err < 0)
681         {
682             /* **** */
683             LeaveCriticalSection(&This->pcm_crst);
684             WARN("Cannot open sound device: %s\n", snd_strerror(err));
685             return DSERR_BUFFERLOST;
686         }
687     }
688
689     /* Set some defaults */
690     snd_pcm_hw_params_any(pcm, hw_params);
691
692     err = snd_pcm_hw_params_set_channels(pcm, hw_params, pwfx->nChannels);
693     if (err < 0) { WARN("Could not set channels to %d\n", pwfx->nChannels); goto err; }
694
695     err = snd_pcm_hw_params_set_format(pcm, hw_params, format);
696     if (err < 0) { WARN("Could not set format to %d bpp\n", pwfx->wBitsPerSample); goto err; }
697
698     err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, NULL);
699     if (err < 0) { rate = pwfx->nSamplesPerSec; WARN("Could not set rate\n"); goto err; }
700
701     if (!ALSA_NearMatch(rate, pwfx->nSamplesPerSec))
702     {
703         WARN("Could not set sound rate to %d, but instead to %d\n", pwfx->nSamplesPerSec, rate);
704         pwfx->nSamplesPerSec = rate;
705         pwfx->nAvgBytesPerSec = rate * pwfx->nBlockAlign;
706         /* Let DirectSound detect this */
707     }
708
709     snd_pcm_hw_params_set_periods_integer(pcm, hw_params);
710     buffer_size = This->mmap_buflen_bytes / pwfx->nBlockAlign;
711     snd_pcm_hw_params_set_buffer_size_near(pcm, hw_params, &buffer_size);
712     buffer_size = 5000;
713     snd_pcm_hw_params_set_period_time_near(pcm, hw_params, (unsigned int*)&buffer_size, NULL);
714
715     err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
716     if (err < 0)
717     {
718         err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
719         if (err < 0) { WARN("Could not set access\n"); goto err; }
720         mmap = 0;
721     }
722     else
723         mmap = 1;
724
725     err = snd_pcm_hw_params(pcm, hw_params);
726     if (err < 0) { WARN("Could not set hw parameters\n"); goto err; }
727
728     if (This->pcm)
729     {
730         snd_pcm_drop(This->pcm);
731         snd_pcm_close(This->pcm);
732     }
733     This->pcm = pcm;
734     This->mmap = mmap;
735
736     snd_pcm_prepare(This->pcm);
737     CreateMMAP(This);
738
739     /* **** */
740     LeaveCriticalSection(&This->pcm_crst);
741     return S_OK;
742
743     err:
744     if (err < 0)
745         WARN("Failed to apply changes: %s\n", snd_strerror(err));
746
747     if (!This->pcm)
748         This->pcm = pcm;
749     else
750         snd_pcm_close(pcm);
751
752     if (This->pcm)
753         snd_pcm_hw_params_current(This->pcm, This->hw_params);
754
755     /* **** */
756     LeaveCriticalSection(&This->pcm_crst);
757     return DSERR_BADFORMAT;
758 }
759
760 static HRESULT WINAPI IDsCaptureDriverBufferImpl_GetPosition(PIDSCDRIVERBUFFER iface, LPDWORD lpdwCappos, LPDWORD lpdwReadpos)
761 {
762     IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
763     snd_pcm_uframes_t hw_pptr, hw_wptr;
764
765     EnterCriticalSection(&This->pcm_crst);
766
767     if (!This->pcm)
768     {
769         FIXME("Bad pointer for pcm: %p\n", This->pcm);
770         LeaveCriticalSection(&This->pcm_crst);
771         return DSERR_GENERIC;
772     }
773
774     if (snd_pcm_state(This->pcm) != SND_PCM_STATE_RUNNING)
775     {
776         CheckXRUN(This);
777         hw_pptr = This->mmap_pos;
778     }
779     else
780     {
781         /* FIXME: Unused at the moment */
782         snd_pcm_uframes_t used = CommitAll(This, FALSE);
783
784         if (This->mmap_pos > used)
785             hw_pptr = This->mmap_pos - used;
786         else
787             hw_pptr = This->mmap_buflen_frames - used + This->mmap_pos;
788     }
789     hw_wptr = This->mmap_pos;
790
791     if (lpdwCappos)
792         *lpdwCappos = realpos_to_fakepos(This, hw_pptr);
793     if (lpdwReadpos)
794         *lpdwReadpos = realpos_to_fakepos(This, hw_wptr);
795
796     LeaveCriticalSection(&This->pcm_crst);
797
798     TRACE("hw_pptr=%u, hw_wptr=%u playpos=%u(%p), writepos=%u(%p)\n", (unsigned int)hw_pptr, (unsigned int)hw_wptr, realpos_to_fakepos(This, hw_pptr), lpdwCappos, realpos_to_fakepos(This, hw_wptr), lpdwReadpos);
799     return DS_OK;
800 }
801
802 static HRESULT WINAPI IDsCaptureDriverBufferImpl_GetStatus(PIDSCDRIVERBUFFER iface, LPDWORD lpdwStatus)
803 {
804     IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
805     snd_pcm_state_t state;
806     TRACE("(%p,%p)\n",iface,lpdwStatus);
807
808     state = snd_pcm_state(This->pcm);
809     switch (state)
810     {
811     case SND_PCM_STATE_XRUN:
812     case SND_PCM_STATE_SUSPENDED:
813     case SND_PCM_STATE_RUNNING:
814         *lpdwStatus = DSCBSTATUS_CAPTURING | (This->play_looping ? DSCBSTATUS_LOOPING : 0);
815         break;
816     default:
817         *lpdwStatus = 0;
818         break;
819     }
820
821     TRACE("State: %d, flags: 0x%08x\n", state, *lpdwStatus);
822     return DS_OK;
823 }
824
825 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Start(PIDSCDRIVERBUFFER iface, DWORD dwFlags)
826 {
827     IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
828     TRACE("(%p,%x)\n",iface,dwFlags);
829
830     /* **** */
831     EnterCriticalSection(&This->pcm_crst);
832     snd_pcm_start(This->pcm);
833     This->play_looping = !!(dwFlags & DSCBSTART_LOOPING);
834     if (!This->play_looping)
835         /* Not well supported because of the difference in ALSA size and DSOUND's notion of size
836          * what it does right now is fill the buffer once.. ALSA size */
837         FIXME("Non-looping buffers are not properly supported!\n");
838     CommitAll(This, TRUE);
839
840     if (This->notify && This->notify->nrofnotifies && This->notify->notifies)
841     {
842         DWORD playpos = realpos_to_fakepos(This, This->mmap_pos);
843         if (playpos)
844             Capture_CheckNotify(This->notify, 0, playpos);
845         This->notify->playpos = playpos;
846     }
847
848     /* **** */
849     LeaveCriticalSection(&This->pcm_crst);
850     return DS_OK;
851 }
852
853 static HRESULT WINAPI IDsCaptureDriverBufferImpl_Stop(PIDSCDRIVERBUFFER iface)
854 {
855     IDsCaptureDriverBufferImpl *This = (IDsCaptureDriverBufferImpl *)iface;
856     TRACE("(%p)\n",iface);
857
858     /* **** */
859     EnterCriticalSection(&This->pcm_crst);
860     This->play_looping = FALSE;
861     snd_pcm_drop(This->pcm);
862     snd_pcm_prepare(This->pcm);
863
864     if (This->notify && This->notify->notifies && This->notify->nrofnotifies)
865         Capture_CheckNotify(This->notify, 0, 0);
866
867     /* **** */
868     LeaveCriticalSection(&This->pcm_crst);
869     return DS_OK;
870 }
871
872 static const IDsCaptureDriverBufferVtbl dsdbvt =
873 {
874     IDsCaptureDriverBufferImpl_QueryInterface,
875     IDsCaptureDriverBufferImpl_AddRef,
876     IDsCaptureDriverBufferImpl_Release,
877     IDsCaptureDriverBufferImpl_Lock,
878     IDsCaptureDriverBufferImpl_Unlock,
879     IDsCaptureDriverBufferImpl_SetFormat,
880     IDsCaptureDriverBufferImpl_GetPosition,
881     IDsCaptureDriverBufferImpl_GetStatus,
882     IDsCaptureDriverBufferImpl_Start,
883     IDsCaptureDriverBufferImpl_Stop
884 };
885
886 static HRESULT WINAPI IDsCaptureDriverImpl_QueryInterface(PIDSCDRIVER iface, REFIID riid, LPVOID *ppobj)
887 {
888     /* IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface; */
889     FIXME("(%p): stub!\n",iface);
890     return DSERR_UNSUPPORTED;
891 }
892
893 static ULONG WINAPI IDsCaptureDriverImpl_AddRef(PIDSCDRIVER iface)
894 {
895     IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
896     ULONG refCount = InterlockedIncrement(&This->ref);
897
898     TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
899
900     return refCount;
901 }
902
903 static ULONG WINAPI IDsCaptureDriverImpl_Release(PIDSCDRIVER iface)
904 {
905     IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
906     ULONG refCount = InterlockedDecrement(&This->ref);
907
908     TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
909
910     if (refCount)
911         return refCount;
912
913     HeapFree(GetProcessHeap(), 0, This);
914     return 0;
915 }
916
917 static HRESULT WINAPI IDsCaptureDriverImpl_GetDriverDesc(PIDSCDRIVER iface, PDSDRIVERDESC pDesc)
918 {
919     IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
920     TRACE("(%p,%p)\n",iface,pDesc);
921     *pDesc                      = WInDev[This->wDevID].ds_desc;
922     pDesc->dwFlags              = 0;
923     pDesc->dnDevNode            = WInDev[This->wDevID].waveDesc.dnDevNode;
924     pDesc->wVxdId               = 0;
925     pDesc->wReserved            = 0;
926     pDesc->ulDeviceNum          = This->wDevID;
927     pDesc->dwHeapType           = DSDHEAP_NOHEAP;
928     pDesc->pvDirectDrawHeap     = NULL;
929     pDesc->dwMemStartAddress    = 0xDEAD0000;
930     pDesc->dwMemEndAddress      = 0xDEAF0000;
931     pDesc->dwMemAllocExtra      = 0;
932     pDesc->pvReserved1          = NULL;
933     pDesc->pvReserved2          = NULL;
934     return DS_OK;
935 }
936
937 static HRESULT WINAPI IDsCaptureDriverImpl_Open(PIDSCDRIVER iface)
938 {
939     HRESULT hr = S_OK;
940     IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
941     int err=0;
942     snd_pcm_t *pcm = NULL;
943     snd_pcm_hw_params_t *hw_params;
944
945     /* While this is not really needed, it is a good idea to do this,
946      * to see if sound can be initialized */
947
948     hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
949     if (!hw_params)
950     {
951         hr = DSERR_OUTOFMEMORY;
952         WARN("--> %08x\n", hr);
953         return hr;
954     }
955
956     err = snd_pcm_open(&pcm, WInDev[This->wDevID].pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
957     if (err < 0) goto err;
958     err = snd_pcm_hw_params_any(pcm, hw_params);
959     if (err < 0) goto err;
960     err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
961     if (err < 0)
962     {
963         err = snd_pcm_hw_params_set_access (pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED);
964         if (err < 0) goto err;
965     }
966
967     TRACE("Success\n");
968     snd_pcm_close(pcm);
969     HeapFree(GetProcessHeap(), 0, hw_params);
970     return hr;
971
972     err:
973     hr = DSERR_GENERIC;
974     WARN("Failed to open device: %s\n", snd_strerror(err));
975     if (pcm)
976         snd_pcm_close(pcm);
977     HeapFree(GetProcessHeap(), 0, hw_params);
978     WARN("--> %08x\n", hr);
979     return hr;
980 }
981
982 static HRESULT WINAPI IDsCaptureDriverImpl_Close(PIDSCDRIVER iface)
983 {
984     IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
985     TRACE("(%p) stub, harmless\n",This);
986     return DS_OK;
987 }
988
989 static HRESULT WINAPI IDsCaptureDriverImpl_GetCaps(PIDSCDRIVER iface, PDSCDRIVERCAPS pCaps)
990 {
991     IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
992     WINE_WAVEDEV *wwi = &WInDev[This->wDevID];
993     TRACE("(%p,%p)\n",iface,pCaps);
994     pCaps->dwSize = sizeof(DSCDRIVERCAPS);
995     pCaps->dwFlags = wwi->ds_caps.dwFlags;
996     pCaps->dwFormats = wwi->incaps.dwFormats;
997     pCaps->dwChannels = wwi->incaps.wChannels;
998     return DS_OK;
999 }
1000
1001 static HRESULT WINAPI IDsCaptureDriverImpl_CreateCaptureBuffer(PIDSCDRIVER iface,
1002                                                       LPWAVEFORMATEX pwfx,
1003                                                       DWORD dwFlags, DWORD dwCardAddress,
1004                                                       LPDWORD pdwcbBufferSize,
1005                                                       LPBYTE *ppbBuffer,
1006                                                       LPVOID *ppvObj)
1007 {
1008     IDsCaptureDriverImpl *This = (IDsCaptureDriverImpl *)iface;
1009     IDsCaptureDriverBufferImpl** ippdsdb = (IDsCaptureDriverBufferImpl**)ppvObj;
1010     HRESULT err;
1011
1012     TRACE("(%p,%p,%x,%x)\n",iface,pwfx,dwFlags,dwCardAddress);
1013
1014     if (This->capture_buffer)
1015         return DSERR_ALLOCATED;
1016
1017     This->capture_buffer = *ippdsdb = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDsCaptureDriverBufferImpl));
1018     if (*ippdsdb == NULL)
1019         return DSERR_OUTOFMEMORY;
1020
1021     (*ippdsdb)->hw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof());
1022     (*ippdsdb)->sw_params = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof());
1023     (*ippdsdb)->presented_buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, *pdwcbBufferSize);
1024     if (!(*ippdsdb)->hw_params || !(*ippdsdb)->sw_params || !(*ippdsdb)->presented_buffer)
1025     {
1026         HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
1027         HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
1028         HeapFree(GetProcessHeap(), 0, (*ippdsdb)->presented_buffer);
1029         return DSERR_OUTOFMEMORY;
1030     }
1031     (*ippdsdb)->lpVtbl = &dsdbvt;
1032     (*ippdsdb)->ref = 1;
1033     (*ippdsdb)->drv = This;
1034     (*ippdsdb)->mmap_buflen_bytes = *pdwcbBufferSize;
1035     InitializeCriticalSection(&(*ippdsdb)->pcm_crst);
1036     (*ippdsdb)->pcm_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_DSCAPTURE.pcm_crst");
1037
1038     /* SetFormat initialises pcm */
1039     err = IDsDriverBuffer_SetFormat((IDsDriverBuffer*)*ppvObj, pwfx);
1040     if (FAILED(err))
1041     {
1042         WARN("Error occurred: %08x\n", err);
1043         goto err;
1044     }
1045     *ppbBuffer = (*ippdsdb)->presented_buffer;
1046
1047     /* buffer is ready to go */
1048     TRACE("buffer created at %p\n", *ippdsdb);
1049     return err;
1050
1051     err:
1052     HeapFree(GetProcessHeap(), 0, (*ippdsdb)->presented_buffer);
1053     HeapFree(GetProcessHeap(), 0, (*ippdsdb)->sw_params);
1054     HeapFree(GetProcessHeap(), 0, (*ippdsdb)->hw_params);
1055     HeapFree(GetProcessHeap(), 0, *ippdsdb);
1056     *ippdsdb = NULL;
1057     return err;
1058 }
1059
1060 static const IDsCaptureDriverVtbl dscdvt =
1061 {
1062     IDsCaptureDriverImpl_QueryInterface,
1063     IDsCaptureDriverImpl_AddRef,
1064     IDsCaptureDriverImpl_Release,
1065     IDsCaptureDriverImpl_GetDriverDesc,
1066     IDsCaptureDriverImpl_Open,
1067     IDsCaptureDriverImpl_Close,
1068     IDsCaptureDriverImpl_GetCaps,
1069     IDsCaptureDriverImpl_CreateCaptureBuffer
1070 };
1071
1072 /**************************************************************************
1073  *                              widDsCreate                     [internal]
1074  */
1075 DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv)
1076 {
1077     IDsCaptureDriverImpl** idrv = (IDsCaptureDriverImpl**)drv;
1078     TRACE("(%d,%p)\n",wDevID,drv);
1079
1080     *idrv = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDsCaptureDriverImpl));
1081     if (!*idrv)
1082         return MMSYSERR_NOMEM;
1083     (*idrv)->lpVtbl     = &dscdvt;
1084     (*idrv)->ref        = 1;
1085
1086     (*idrv)->wDevID     = wDevID;
1087     return MMSYSERR_NOERROR;
1088 }
1089
1090 /**************************************************************************
1091  *                              widDsDesc                       [internal]
1092  */
1093 DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc)
1094 {
1095     *desc = WInDev[wDevID].ds_desc;
1096     return MMSYSERR_NOERROR;
1097 }
1098
1099 #endif /* HAVE_ALSA */