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