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