mmdevapi/tests: Add tests for IAudioClient::GetCurrentPadding.
[wine] / dlls / winealsa.drv / waveout.c
1 /*
2  * Sample Wine Driver for Advanced Linux Sound System (ALSA)
3  *      Based on version <final> of the ALSA API
4  *
5  * Copyright    2002 Eric Pouech
6  *              2002 Marco Pietrobono
7  *              2003 Christian Costa : WaveIn support
8  *              2006-2007 Maarten Lankhorst
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 /*======================================================================*
26  *                  Low level WAVE OUT implementation                   *
27  *======================================================================*/
28
29 #include "config.h"
30 #include "wine/port.h"
31
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <string.h>
36 #ifdef HAVE_UNISTD_H
37 # include <unistd.h>
38 #endif
39 #include <errno.h>
40 #include <limits.h>
41 #include <fcntl.h>
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
44 #endif
45 #ifdef HAVE_SYS_MMAN_H
46 # include <sys/mman.h>
47 #endif
48 #include "windef.h"
49 #include "winbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "winnls.h"
53 #include "mmddk.h"
54
55 #include "alsa.h"
56
57 #include "wine/library.h"
58 #include "wine/unicode.h"
59 #include "wine/debug.h"
60
61 WINE_DEFAULT_DEBUG_CHANNEL(wave);
62
63 WINE_WAVEDEV    *WOutDev;
64 DWORD            ALSA_WodNumMallocedDevs;
65 DWORD            ALSA_WodNumDevs;
66
67 /**************************************************************************
68  *                      wodNotifyClient                 [internal]
69  */
70 static void wodNotifyClient(WINE_WAVEDEV* wwo, WORD wMsg, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
71 {
72     TRACE("wMsg = 0x%04x dwParm1 = %lx dwParam2 = %lx\n", wMsg, dwParam1, dwParam2);
73
74     switch (wMsg) {
75     case WOM_OPEN:
76     case WOM_CLOSE:
77     case WOM_DONE:
78         DriverCallback(wwo->waveDesc.dwCallback, wwo->wFlags, (HDRVR)wwo->waveDesc.hWave,
79                        wMsg, wwo->waveDesc.dwInstance, dwParam1, dwParam2);
80         break;
81     default:
82         FIXME("Unknown callback message %u\n", wMsg);
83     }
84 }
85
86 /**************************************************************************
87  *                              wodUpdatePlayedTotal    [internal]
88  *
89  */
90 static BOOL wodUpdatePlayedTotal(WINE_WAVEDEV* wwo, snd_pcm_status_t* ps)
91 {
92     snd_pcm_sframes_t delay;
93     snd_pcm_sframes_t avail;
94     snd_pcm_uframes_t buf_size = 0;
95     snd_pcm_state_t state;
96
97     state = snd_pcm_state(wwo->pcm);
98     avail = snd_pcm_avail_update(wwo->pcm);
99     snd_pcm_hw_params_get_buffer_size(wwo->hw_params, &buf_size);
100     delay = buf_size - avail;
101
102     if (state != SND_PCM_STATE_RUNNING && state != SND_PCM_STATE_PREPARED)
103     {
104         WARN("Unexpected state (%d) while updating Total Played, resetting\n", state);
105         wine_snd_pcm_recover(wwo->pcm, -EPIPE, 0);
106         delay=0;
107     }
108
109     /* A delay < 0 indicates an underrun; for our purposes that's 0.  */
110     if (delay < 0)
111     {
112         WARN("Unexpected delay (%ld) while updating Total Played, resetting\n", delay);
113         delay=0;
114     }
115
116     InterlockedExchange((LONG*)&wwo->dwPlayedTotal, wwo->dwWrittenTotal - snd_pcm_frames_to_bytes(wwo->pcm, delay));
117     return TRUE;
118 }
119
120 /**************************************************************************
121  *                              wodPlayer_BeginWaveHdr          [internal]
122  *
123  * Makes the specified lpWaveHdr the currently playing wave header.
124  * If the specified wave header is a begin loop and we're not already in
125  * a loop, setup the loop.
126  */
127 static void wodPlayer_BeginWaveHdr(WINE_WAVEDEV* wwo, LPWAVEHDR lpWaveHdr)
128 {
129     wwo->lpPlayPtr = lpWaveHdr;
130
131     if (!lpWaveHdr) return;
132
133     if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
134         if (wwo->lpLoopPtr) {
135             WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
136         } else {
137             TRACE("Starting loop (%dx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
138             wwo->lpLoopPtr = lpWaveHdr;
139             /* Windows does not touch WAVEHDR.dwLoops,
140              * so we need to make an internal copy */
141             wwo->dwLoops = lpWaveHdr->dwLoops;
142         }
143     }
144     wwo->dwPartialOffset = 0;
145 }
146
147 /**************************************************************************
148  *                              wodPlayer_PlayPtrNext           [internal]
149  *
150  * Advance the play pointer to the next waveheader, looping if required.
151  */
152 static LPWAVEHDR wodPlayer_PlayPtrNext(WINE_WAVEDEV* wwo)
153 {
154     LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
155
156     wwo->dwPartialOffset = 0;
157     if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
158         /* We're at the end of a loop, loop if required */
159         if (--wwo->dwLoops > 0) {
160             wwo->lpPlayPtr = wwo->lpLoopPtr;
161         } else {
162             /* Handle overlapping loops correctly */
163             if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
164                 FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
165                 /* shall we consider the END flag for the closing loop or for
166                  * the opening one or for both ???
167                  * code assumes for closing loop only
168                  */
169             } else {
170                 lpWaveHdr = lpWaveHdr->lpNext;
171             }
172             wwo->lpLoopPtr = NULL;
173             wodPlayer_BeginWaveHdr(wwo, lpWaveHdr);
174         }
175     } else {
176         /* We're not in a loop.  Advance to the next wave header */
177         wodPlayer_BeginWaveHdr(wwo, lpWaveHdr = lpWaveHdr->lpNext);
178     }
179
180     return lpWaveHdr;
181 }
182
183 /**************************************************************************
184  *                           wodPlayer_DSPWait                  [internal]
185  * Returns the number of milliseconds to wait for the DSP buffer to play a
186  * period
187  */
188 static DWORD wodPlayer_DSPWait(const WINE_WAVEDEV *wwo)
189 {
190     /* time for one period to be played */
191     unsigned int val=0;
192     int dir=0;
193     int err=0;
194     err = snd_pcm_hw_params_get_period_time(wwo->hw_params, &val, &dir);
195     return val / 1000;
196 }
197
198 /**************************************************************************
199  *                           wodPlayer_NotifyWait               [internal]
200  * Returns the number of milliseconds to wait before attempting to notify
201  * completion of the specified wavehdr.
202  * This is based on the number of bytes remaining to be written in the
203  * wave.
204  */
205 static DWORD wodPlayer_NotifyWait(const WINE_WAVEDEV* wwo, LPWAVEHDR lpWaveHdr)
206 {
207     DWORD dwMillis;
208
209     if (lpWaveHdr->reserved < wwo->dwPlayedTotal) {
210         dwMillis = 1;
211     } else {
212         dwMillis = (lpWaveHdr->reserved - wwo->dwPlayedTotal) * 1000 / wwo->format.Format.nAvgBytesPerSec;
213         if (!dwMillis) dwMillis = 1;
214     }
215
216     return dwMillis;
217 }
218
219
220 /**************************************************************************
221  *                           wodPlayer_WriteMaxFrags            [internal]
222  * Writes the maximum number of frames possible to the DSP and returns
223  * the number of frames written.
224  */
225 static int wodPlayer_WriteMaxFrags(WINE_WAVEDEV* wwo, DWORD* frames)
226 {
227     /* Only attempt to write to free frames */
228     LPWAVEHDR lpWaveHdr = wwo->lpPlayPtr;
229     DWORD dwLength = snd_pcm_bytes_to_frames(wwo->pcm, lpWaveHdr->dwBufferLength - wwo->dwPartialOffset);
230     int toWrite = min(dwLength, *frames);
231     int written;
232
233     TRACE("Writing wavehdr %p.%u[%u]\n", lpWaveHdr, wwo->dwPartialOffset, lpWaveHdr->dwBufferLength);
234
235     if (toWrite > 0) {
236         written = (wwo->write)(wwo->pcm, lpWaveHdr->lpData + wwo->dwPartialOffset, toWrite);
237         if ( written < 0) {
238             /* XRUN occurred. let's try to recover */
239             wine_snd_pcm_recover(wwo->pcm, written, 0);
240             written = (wwo->write)(wwo->pcm, lpWaveHdr->lpData + wwo->dwPartialOffset, toWrite);
241         }
242         if (written <= 0) {
243             /* still in error */
244             ERR("Error in writing wavehdr. Reason: %s\n", snd_strerror(written));
245             return written;
246         }
247     } else
248         written = 0;
249
250     wwo->dwPartialOffset += snd_pcm_frames_to_bytes(wwo->pcm, written);
251     if (wwo->dwPartialOffset + wwo->format.Format.nBlockAlign - 1 >= lpWaveHdr->dwBufferLength) {
252         /* this will be used to check if the given wave header has been fully played or not... */
253         wwo->dwPartialOffset = lpWaveHdr->dwBufferLength;
254         /* If we wrote all current wavehdr, skip to the next one */
255         wodPlayer_PlayPtrNext(wwo);
256     }
257     *frames -= written;
258     wwo->dwWrittenTotal += snd_pcm_frames_to_bytes(wwo->pcm, written);
259     TRACE("dwWrittenTotal=%u\n", wwo->dwWrittenTotal);
260
261     return written;
262 }
263
264
265 /**************************************************************************
266  *                              wodPlayer_NotifyCompletions     [internal]
267  *
268  * Notifies and remove from queue all wavehdrs which have been played to
269  * the speaker (ie. they have cleared the ALSA buffer).  If force is true,
270  * we notify all wavehdrs and remove them all from the queue even if they
271  * are unplayed or part of a loop.
272  */
273 static DWORD wodPlayer_NotifyCompletions(WINE_WAVEDEV* wwo, BOOL force)
274 {
275     LPWAVEHDR           lpWaveHdr;
276
277     /* Start from lpQueuePtr and keep notifying until:
278      * - we hit an unwritten wavehdr
279      * - we hit the beginning of a running loop
280      * - we hit a wavehdr which hasn't finished playing
281      */
282     for (;;)
283     {
284         lpWaveHdr = wwo->lpQueuePtr;
285         if (!lpWaveHdr) {TRACE("Empty queue\n"); break;}
286         if (!force)
287         {
288             snd_pcm_uframes_t frames;
289             snd_pcm_hw_params_get_period_size(wwo->hw_params, &frames, NULL);
290
291             if (lpWaveHdr == wwo->lpPlayPtr) {TRACE("play %p\n", lpWaveHdr); break;}
292             if (lpWaveHdr == wwo->lpLoopPtr) {TRACE("loop %p\n", lpWaveHdr); break;}
293             if (lpWaveHdr->reserved > wwo->dwPlayedTotal + frames) {TRACE("still playing %p (%lu/%u)\n", lpWaveHdr, lpWaveHdr->reserved, wwo->dwPlayedTotal);break;}
294         }
295         wwo->dwPlayedTotal += lpWaveHdr->reserved - wwo->dwPlayedTotal;
296         wwo->lpQueuePtr = lpWaveHdr->lpNext;
297
298         lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
299         lpWaveHdr->dwFlags |= WHDR_DONE;
300
301         wodNotifyClient(wwo, WOM_DONE, (DWORD_PTR)lpWaveHdr, 0);
302     }
303     return  (lpWaveHdr && lpWaveHdr != wwo->lpPlayPtr && lpWaveHdr != wwo->lpLoopPtr) ?
304         wodPlayer_NotifyWait(wwo, lpWaveHdr) : INFINITE;
305 }
306
307
308 /**************************************************************************
309  *                              wodPlayer_Reset                 [internal]
310  *
311  * wodPlayer helper. Resets current output stream.
312  */
313 static  void    wodPlayer_Reset(WINE_WAVEDEV* wwo, BOOL reset)
314 {
315     int                         err;
316     TRACE("(%p)\n", wwo);
317
318     wodUpdatePlayedTotal(wwo, NULL);
319     /* updates current notify list */
320     wodPlayer_NotifyCompletions(wwo, FALSE);
321
322     if ( (err = snd_pcm_drop(wwo->pcm)) < 0) {
323         FIXME("flush: %s\n", snd_strerror(err));
324         wwo->hThread = 0;
325         wwo->state = WINE_WS_STOPPED;
326         ExitThread(-1);
327     }
328     if ( (err = snd_pcm_prepare(wwo->pcm)) < 0 )
329         ERR("pcm prepare failed: %s\n", snd_strerror(err));
330
331     if (reset) {
332         enum win_wm_message     msg;
333         DWORD_PTR               param;
334         HANDLE                  ev;
335
336         /* remove any buffer */
337         wodPlayer_NotifyCompletions(wwo, TRUE);
338
339         wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
340         wwo->state = WINE_WS_STOPPED;
341         wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
342         /* Clear partial wavehdr */
343         wwo->dwPartialOffset = 0;
344
345         /* remove any existing message in the ring */
346         EnterCriticalSection(&wwo->msgRing.msg_crst);
347         /* return all pending headers in queue */
348         while (ALSA_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev))
349         {
350             if (msg != WINE_WM_HEADER)
351             {
352                 FIXME("shouldn't have headers left\n");
353                 SetEvent(ev);
354                 continue;
355             }
356             ((LPWAVEHDR)param)->dwFlags &= ~WHDR_INQUEUE;
357             ((LPWAVEHDR)param)->dwFlags |= WHDR_DONE;
358
359             wodNotifyClient(wwo, WOM_DONE, param, 0);
360         }
361         ALSA_ResetRingMessage(&wwo->msgRing);
362         LeaveCriticalSection(&wwo->msgRing.msg_crst);
363     } else {
364         if (wwo->lpLoopPtr) {
365             /* complicated case, not handled yet (could imply modifying the loop counter */
366             FIXME("Pausing while in loop isn't correctly handled yet, expect strange results\n");
367             wwo->lpPlayPtr = wwo->lpLoopPtr;
368             wwo->dwPartialOffset = 0;
369             wwo->dwWrittenTotal = wwo->dwPlayedTotal; /* this is wrong !!! */
370         } else {
371             LPWAVEHDR   ptr;
372             DWORD       sz = wwo->dwPartialOffset;
373
374             /* reset all the data as if we had written only up to lpPlayedTotal bytes */
375             /* compute the max size playable from lpQueuePtr */
376             for (ptr = wwo->lpQueuePtr; ptr != wwo->lpPlayPtr; ptr = ptr->lpNext) {
377                 sz += ptr->dwBufferLength;
378             }
379             /* because the reset lpPlayPtr will be lpQueuePtr */
380             if (wwo->dwWrittenTotal > wwo->dwPlayedTotal + sz) ERR("grin\n");
381             wwo->dwPartialOffset = sz - (wwo->dwWrittenTotal - wwo->dwPlayedTotal);
382             wwo->dwWrittenTotal = wwo->dwPlayedTotal;
383             wwo->lpPlayPtr = wwo->lpQueuePtr;
384         }
385         wwo->state = WINE_WS_PAUSED;
386     }
387 }
388
389 /**************************************************************************
390  *                    wodPlayer_ProcessMessages                 [internal]
391  */
392 static void wodPlayer_ProcessMessages(WINE_WAVEDEV* wwo)
393 {
394     LPWAVEHDR           lpWaveHdr;
395     enum win_wm_message msg;
396     DWORD_PTR           param;
397     HANDLE              ev;
398     int                 err;
399
400     while (ALSA_RetrieveRingMessage(&wwo->msgRing, &msg, &param, &ev)) {
401      TRACE("Received %s %lx\n", ALSA_getCmdString(msg), param);
402
403         switch (msg) {
404         case WINE_WM_PAUSING:
405             if ( snd_pcm_state(wwo->pcm) == SND_PCM_STATE_RUNNING )
406              {
407                 if ( snd_pcm_hw_params_can_pause(wwo->hw_params) )
408                 {
409                     err = snd_pcm_pause(wwo->pcm, 1);
410                     if ( err < 0 )
411                         ERR("pcm_pause failed: %s\n", snd_strerror(err));
412                     wwo->state = WINE_WS_PAUSED;
413                 }
414                 else
415                 {
416                     wodPlayer_Reset(wwo,FALSE);
417                 }
418              }
419             SetEvent(ev);
420             break;
421         case WINE_WM_RESTARTING:
422             if (wwo->state == WINE_WS_PAUSED)
423             {
424                 if ( snd_pcm_state(wwo->pcm) == SND_PCM_STATE_PAUSED )
425                  {
426                     err = snd_pcm_pause(wwo->pcm, 0);
427                     if ( err < 0 )
428                         ERR("pcm_pause failed: %s\n", snd_strerror(err));
429                  }
430                 wwo->state = WINE_WS_PLAYING;
431             }
432             SetEvent(ev);
433             break;
434         case WINE_WM_HEADER:
435             lpWaveHdr = (LPWAVEHDR)param;
436
437             /* insert buffer at the end of queue */
438             {
439                 LPWAVEHDR*      wh;
440                 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
441                 *wh = lpWaveHdr;
442             }
443             if (!wwo->lpPlayPtr)
444                 wodPlayer_BeginWaveHdr(wwo,lpWaveHdr);
445             if (wwo->state == WINE_WS_STOPPED)
446                 wwo->state = WINE_WS_PLAYING;
447             break;
448         case WINE_WM_RESETTING:
449             wodPlayer_Reset(wwo,TRUE);
450             SetEvent(ev);
451             break;
452         case WINE_WM_BREAKLOOP:
453             if (wwo->state == WINE_WS_PLAYING && wwo->lpLoopPtr != NULL) {
454                 /* ensure exit at end of current loop */
455                 wwo->dwLoops = 1;
456             }
457             SetEvent(ev);
458             break;
459         case WINE_WM_CLOSING:
460             /* sanity check: this should not happen since the device must have been reset before */
461             if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
462             wwo->hThread = 0;
463             wwo->state = WINE_WS_CLOSED;
464             SetEvent(ev);
465             ExitThread(0);
466             /* shouldn't go here */
467         default:
468             FIXME("unknown message %d\n", msg);
469             break;
470         }
471     }
472 }
473
474 /**************************************************************************
475  *                           wodPlayer_FeedDSP                  [internal]
476  * Feed as much sound data as we can into the DSP and return the number of
477  * milliseconds before it will be necessary to feed the DSP again.
478  */
479 static DWORD wodPlayer_FeedDSP(WINE_WAVEDEV* wwo)
480 {
481     DWORD               availInQ;
482
483     wodUpdatePlayedTotal(wwo, NULL);
484     availInQ = snd_pcm_avail_update(wwo->pcm);
485
486     /* no more room... no need to try to feed */
487     if (availInQ > 0) {
488         /* Feed from partial wavehdr */
489         if (wwo->lpPlayPtr && wwo->dwPartialOffset != 0) {
490             wodPlayer_WriteMaxFrags(wwo, &availInQ);
491         }
492
493         /* Feed wavehdrs until we run out of wavehdrs or DSP space */
494         if (wwo->dwPartialOffset == 0 && wwo->lpPlayPtr) {
495             do {
496                 TRACE("Setting time to elapse for %p to %u\n",
497                       wwo->lpPlayPtr, wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength);
498                 /* note the value that dwPlayedTotal will return when this wave finishes playing */
499                 wwo->lpPlayPtr->reserved = wwo->dwWrittenTotal + wwo->lpPlayPtr->dwBufferLength;
500             } while (wodPlayer_WriteMaxFrags(wwo, &availInQ) && wwo->lpPlayPtr && availInQ > 0);
501         }
502     }
503
504     return wodPlayer_DSPWait(wwo);
505 }
506
507 /**************************************************************************
508  *                              wodPlayer                       [internal]
509  */
510 static  DWORD   CALLBACK        wodPlayer(LPVOID pmt)
511 {
512     WORD          uDevID = (DWORD_PTR)pmt;
513     WINE_WAVEDEV* wwo = &WOutDev[uDevID];
514     DWORD         dwNextFeedTime = INFINITE;   /* Time before DSP needs feeding */
515     DWORD         dwNextNotifyTime = INFINITE; /* Time before next wave completion */
516     DWORD         dwSleepTime;
517
518     wwo->state = WINE_WS_STOPPED;
519     SetEvent(wwo->hStartUpEvent);
520
521     for (;;) {
522         /** Wait for the shortest time before an action is required.  If there
523          *  are no pending actions, wait forever for a command.
524          */
525         dwSleepTime = min(dwNextFeedTime, dwNextNotifyTime);
526         TRACE("waiting %ums (%u,%u)\n", dwSleepTime, dwNextFeedTime, dwNextNotifyTime);
527         ALSA_WaitRingMessage(&wwo->msgRing, dwSleepTime);
528         wodPlayer_ProcessMessages(wwo);
529         if (wwo->state == WINE_WS_PLAYING) {
530             dwNextFeedTime = wodPlayer_FeedDSP(wwo);
531             dwNextNotifyTime = wodPlayer_NotifyCompletions(wwo, FALSE);
532             if (dwNextFeedTime == INFINITE) {
533                 /* FeedDSP ran out of data, but before giving up, */
534                 /* check that a notification didn't give us more */
535                 wodPlayer_ProcessMessages(wwo);
536                 if (wwo->lpPlayPtr) {
537                     TRACE("recovering\n");
538                     dwNextFeedTime = wodPlayer_FeedDSP(wwo);
539                 }
540             }
541         } else {
542             dwNextFeedTime = dwNextNotifyTime = INFINITE;
543         }
544     }
545     return 0;
546 }
547
548 /**************************************************************************
549  *                      wodGetDevCaps                           [internal]
550  */
551 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
552 {
553     TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
554
555     if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
556
557     if (wDevID >= ALSA_WodNumDevs) {
558         TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
559         return MMSYSERR_BADDEVICEID;
560     }
561
562     memcpy(lpCaps, &WOutDev[wDevID].outcaps, min(dwSize, sizeof(*lpCaps)));
563     return MMSYSERR_NOERROR;
564 }
565
566 /**************************************************************************
567  *                              wodOpen                         [internal]
568  */
569 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
570 {
571     WINE_WAVEDEV*               wwo;
572     snd_pcm_t *                 pcm = NULL;
573     snd_hctl_t *                hctl = NULL;
574     snd_pcm_hw_params_t *       hw_params;
575     snd_pcm_sw_params_t *       sw_params;
576     snd_pcm_access_t            access;
577     snd_pcm_format_t            format = -1;
578     unsigned int                rate;
579     unsigned int                buffer_time = 120000;
580     unsigned int                period_time = 22000;
581     snd_pcm_uframes_t           buffer_size;
582     snd_pcm_uframes_t           period_size;
583     int                         flags;
584     int                         err=0;
585     int                         dir=0;
586     DWORD                       retcode = 0;
587
588     TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
589     if (lpDesc == NULL) {
590         WARN("Invalid Parameter !\n");
591         return MMSYSERR_INVALPARAM;
592     }
593     if (wDevID >= ALSA_WodNumDevs) {
594         TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
595         return MMSYSERR_BADDEVICEID;
596     }
597
598     /* only PCM format is supported so far... */
599     if (!ALSA_supportedFormat(lpDesc->lpFormat)) {
600         WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
601              lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
602              lpDesc->lpFormat->nSamplesPerSec);
603         return WAVERR_BADFORMAT;
604     }
605
606     if (dwFlags & WAVE_FORMAT_QUERY) {
607         TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
608              lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
609              lpDesc->lpFormat->nSamplesPerSec);
610         return MMSYSERR_NOERROR;
611     }
612
613     wwo = &WOutDev[wDevID];
614
615     if (wwo->pcm != NULL) {
616         WARN("%d already allocated\n", wDevID);
617         return MMSYSERR_ALLOCATED;
618     }
619
620     if (dwFlags & WAVE_DIRECTSOUND)
621         FIXME("Why are we called with DirectSound flag? It doesn't use MMSYSTEM any more\n");
622         /* not supported, ignore it */
623     dwFlags &= ~WAVE_DIRECTSOUND;
624
625     flags = SND_PCM_NONBLOCK;
626
627     if ( (err = snd_pcm_open(&pcm, wwo->pcmname, SND_PCM_STREAM_PLAYBACK, flags)) < 0)
628     {
629         ERR("Error open: %s\n", snd_strerror(err));
630         return MMSYSERR_NOTENABLED;
631     }
632
633     if (wwo->ctlname)
634     {
635         err = snd_hctl_open(&hctl, wwo->ctlname, 0);
636         if (err >= 0)
637         {
638             snd_hctl_load(hctl);
639         }
640         else
641         {
642             WARN("Could not open hctl for [%s]: %s\n", wwo->ctlname, snd_strerror(err));
643             hctl = NULL;
644         }
645     }
646
647     wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
648
649     wwo->waveDesc = *lpDesc;
650     ALSA_copyFormat(lpDesc->lpFormat, &wwo->format);
651
652     TRACE("Requested this format: %dx%dx%d %s\n",
653           wwo->format.Format.nSamplesPerSec,
654           wwo->format.Format.wBitsPerSample,
655           wwo->format.Format.nChannels,
656           ALSA_getFormat(wwo->format.Format.wFormatTag));
657
658     if (wwo->format.Format.wBitsPerSample == 0) {
659         WARN("Resetting zeroed wBitsPerSample\n");
660         wwo->format.Format.wBitsPerSample = 8 *
661             (wwo->format.Format.nAvgBytesPerSec /
662              wwo->format.Format.nSamplesPerSec) /
663             wwo->format.Format.nChannels;
664     }
665
666 #define EXIT_ON_ERROR(f,e,txt) do \
667 { \
668     int err; \
669     if ( (err = (f) ) < 0) \
670     { \
671         WARN(txt ": %s\n", snd_strerror(err)); \
672         retcode=e; \
673         goto errexit; \
674     } \
675 } while(0)
676
677     sw_params = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof() );
678     hw_params = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof() );
679     if (!hw_params || !sw_params)
680     {
681         retcode = MMSYSERR_NOMEM;
682         goto errexit;
683     }
684     snd_pcm_hw_params_any(pcm, hw_params);
685
686     access = SND_PCM_ACCESS_MMAP_INTERLEAVED;
687     if ( ( err = snd_pcm_hw_params_set_access(pcm, hw_params, access ) ) < 0) {
688         WARN("mmap not available. switching to standard write.\n");
689         access = SND_PCM_ACCESS_RW_INTERLEAVED;
690         EXIT_ON_ERROR( snd_pcm_hw_params_set_access(pcm, hw_params, access ), MMSYSERR_INVALPARAM, "unable to set access for playback");
691         wwo->write = snd_pcm_writei;
692     }
693     else
694         wwo->write = snd_pcm_mmap_writei;
695
696     if ((err = snd_pcm_hw_params_set_channels(pcm, hw_params, wwo->format.Format.nChannels)) < 0) {
697         WARN("unable to set required channels: %d\n", wwo->format.Format.nChannels);
698         EXIT_ON_ERROR( snd_pcm_hw_params_set_channels(pcm, hw_params, wwo->format.Format.nChannels ), WAVERR_BADFORMAT, "unable to set required channels" );
699     }
700
701     if ((wwo->format.Format.wFormatTag == WAVE_FORMAT_PCM) ||
702         ((wwo->format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
703         IsEqualGUID(&wwo->format.SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) {
704         format = (wwo->format.Format.wBitsPerSample == 8) ? SND_PCM_FORMAT_U8 :
705                  (wwo->format.Format.wBitsPerSample == 16) ? SND_PCM_FORMAT_S16_LE :
706                  (wwo->format.Format.wBitsPerSample == 24) ? SND_PCM_FORMAT_S24_3LE :
707                  (wwo->format.Format.wBitsPerSample == 32) ? SND_PCM_FORMAT_S32_LE : -1;
708     } else if ((wwo->format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
709         IsEqualGUID(&wwo->format.SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)){
710         format = (wwo->format.Format.wBitsPerSample == 32) ? SND_PCM_FORMAT_FLOAT_LE : -1;
711     } else {
712         ERR("invalid format: %0x04x\n", wwo->format.Format.wFormatTag);
713         retcode = WAVERR_BADFORMAT;
714         goto errexit;
715     }
716
717     if ((err = snd_pcm_hw_params_set_format(pcm, hw_params, format)) < 0) {
718         WARN("unable to set required format: %s\n", snd_pcm_format_name(format));
719         EXIT_ON_ERROR( snd_pcm_hw_params_set_format(pcm, hw_params, format), WAVERR_BADFORMAT, "unable to set required format" );
720     }
721
722     rate = wwo->format.Format.nSamplesPerSec;
723     dir=0;
724     err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, &dir);
725     if (err < 0) {
726         WARN("Rate %d Hz not available for playback: %s\n", wwo->format.Format.nSamplesPerSec, snd_strerror(rate));
727         retcode = WAVERR_BADFORMAT;
728         goto errexit;
729     }
730     if (!ALSA_NearMatch(rate, wwo->format.Format.nSamplesPerSec)) {
731         WARN("Rate doesn't match (requested %d Hz, got %d Hz)\n", wwo->format.Format.nSamplesPerSec, rate);
732         retcode = WAVERR_BADFORMAT;
733         goto errexit;
734     }
735
736     TRACE("Got this format: %dx%dx%d %s\n",
737           wwo->format.Format.nSamplesPerSec,
738           wwo->format.Format.wBitsPerSample,
739           wwo->format.Format.nChannels,
740           ALSA_getFormat(wwo->format.Format.wFormatTag));
741
742     dir=0;
743     EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_time, &dir), MMSYSERR_INVALPARAM, "unable to set buffer time");
744     dir=0;
745     EXIT_ON_ERROR( snd_pcm_hw_params_set_period_time_near(pcm, hw_params, &period_time, &dir), MMSYSERR_INVALPARAM, "unable to set period time");
746
747     EXIT_ON_ERROR( snd_pcm_hw_params(pcm, hw_params), MMSYSERR_INVALPARAM, "unable to set hw params for playback");
748
749     err = snd_pcm_hw_params_get_period_size(hw_params, &period_size, &dir);
750     err = snd_pcm_hw_params_get_buffer_size(hw_params, &buffer_size);
751
752     snd_pcm_sw_params_current(pcm, sw_params);
753
754     EXIT_ON_ERROR( snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 1), MMSYSERR_ERROR, "unable to set start threshold");
755     EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence size");
756     EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm, sw_params, period_size), MMSYSERR_ERROR, "unable to set avail min");
757     EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence threshold");
758     EXIT_ON_ERROR( snd_pcm_sw_params(pcm, sw_params), MMSYSERR_ERROR, "unable to set sw params for playback");
759 #undef EXIT_ON_ERROR
760
761     snd_pcm_prepare(pcm);
762
763     if (TRACE_ON(wave))
764         ALSA_TraceParameters(hw_params, sw_params, FALSE);
765
766     /* now, we can save all required data for later use... */
767
768     wwo->dwBufferSize = snd_pcm_frames_to_bytes(pcm, buffer_size);
769     wwo->lpQueuePtr = wwo->lpPlayPtr = wwo->lpLoopPtr = NULL;
770     wwo->dwPlayedTotal = wwo->dwWrittenTotal = 0;
771     wwo->dwPartialOffset = 0;
772
773     ALSA_InitRingMessage(&wwo->msgRing);
774
775     wwo->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
776     wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD_PTR)wDevID, 0, &(wwo->dwThreadID));
777     if (!wwo->hThread) {
778         ERR("Thread creation for the wodPlayer failed!\n");
779         CloseHandle(wwo->hStartUpEvent);
780         retcode = MMSYSERR_NOMEM;
781         goto errexit;
782     }
783     SetThreadPriority(wwo->hThread, THREAD_PRIORITY_TIME_CRITICAL);
784     WaitForSingleObject(wwo->hStartUpEvent, INFINITE);
785     CloseHandle(wwo->hStartUpEvent);
786     wwo->hStartUpEvent = NULL;
787
788     TRACE("handle=%p\n", pcm);
789     TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
790           wwo->format.Format.wBitsPerSample, wwo->format.Format.nAvgBytesPerSec,
791           wwo->format.Format.nSamplesPerSec, wwo->format.Format.nChannels,
792           wwo->format.Format.nBlockAlign);
793
794     HeapFree( GetProcessHeap(), 0, sw_params );
795     wwo->hw_params = hw_params;
796     wwo->hctl = hctl;
797     wwo->pcm = pcm;
798
799     wodNotifyClient(wwo, WOM_OPEN, 0L, 0L);
800     return MMSYSERR_NOERROR;
801
802 errexit:
803     if (pcm)
804         snd_pcm_close(pcm);
805
806     if (hctl)
807     {
808         snd_hctl_free(hctl);
809         snd_hctl_close(hctl);
810     }
811
812     HeapFree( GetProcessHeap(), 0, hw_params );
813     HeapFree( GetProcessHeap(), 0, sw_params );
814     if (wwo->msgRing.ring_buffer_size > 0)
815         ALSA_DestroyRingMessage(&wwo->msgRing);
816
817     return retcode;
818 }
819
820
821 /**************************************************************************
822  *                              wodClose                        [internal]
823  */
824 static DWORD wodClose(WORD wDevID)
825 {
826     WINE_WAVEDEV*       wwo;
827
828     TRACE("(%u);\n", wDevID);
829
830     if (wDevID >= ALSA_WodNumDevs) {
831         TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
832         return MMSYSERR_BADDEVICEID;
833     }
834
835     wwo = &WOutDev[wDevID];
836     if (wwo->pcm == NULL) {
837         WARN("Requested to close already closed device %d!\n", wDevID);
838         return MMSYSERR_BADDEVICEID;
839     }
840
841     if (wwo->lpQueuePtr) {
842         WARN("buffers still playing !\n");
843         return WAVERR_STILLPLAYING;
844     } else {
845         if (wwo->hThread) {
846             ALSA_AddRingMessage(&wwo->msgRing, WINE_WM_CLOSING, 0, TRUE);
847         }
848         ALSA_DestroyRingMessage(&wwo->msgRing);
849
850         HeapFree( GetProcessHeap(), 0, wwo->hw_params );
851         wwo->hw_params = NULL;
852
853         if (wwo->hctl)
854         {
855             snd_hctl_free(wwo->hctl);
856             snd_hctl_close(wwo->hctl);
857             wwo->hctl = NULL;
858         }
859
860         snd_pcm_close(wwo->pcm);
861         wwo->pcm = NULL;
862
863         wodNotifyClient(wwo, WOM_CLOSE, 0L, 0L);
864     }
865
866     return MMSYSERR_NOERROR;
867 }
868
869
870 /**************************************************************************
871  *                              wodWrite                        [internal]
872  *
873  */
874 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
875 {
876     TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
877
878     if (wDevID >= ALSA_WodNumDevs) {
879         TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
880         return MMSYSERR_BADDEVICEID;
881     }
882
883     if (WOutDev[wDevID].pcm == NULL) {
884         WARN("Requested to write to closed device %d!\n", wDevID);
885         return MMSYSERR_BADDEVICEID;
886     }
887
888     if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
889         return WAVERR_UNPREPARED;
890
891     if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
892         return WAVERR_STILLPLAYING;
893
894     lpWaveHdr->dwFlags &= ~WHDR_DONE;
895     lpWaveHdr->dwFlags |= WHDR_INQUEUE;
896     lpWaveHdr->lpNext = 0;
897
898     ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD_PTR)lpWaveHdr, FALSE);
899
900     return MMSYSERR_NOERROR;
901 }
902
903 /**************************************************************************
904  *                      wodPause                                [internal]
905  */
906 static DWORD wodPause(WORD wDevID)
907 {
908     TRACE("(%u);!\n", wDevID);
909
910     if (wDevID >= ALSA_WodNumDevs) {
911         TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
912         return MMSYSERR_BADDEVICEID;
913     }
914
915     if (WOutDev[wDevID].pcm == NULL) {
916         WARN("Requested to pause closed device %d!\n", wDevID);
917         return MMSYSERR_BADDEVICEID;
918     }
919
920     ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_PAUSING, 0, TRUE);
921
922     return MMSYSERR_NOERROR;
923 }
924
925 /**************************************************************************
926  *                      wodRestart                              [internal]
927  */
928 static DWORD wodRestart(WORD wDevID)
929 {
930     TRACE("(%u);\n", wDevID);
931
932     if (wDevID >= ALSA_WodNumDevs) {
933         TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
934         return MMSYSERR_BADDEVICEID;
935     }
936
937     if (WOutDev[wDevID].pcm == NULL) {
938         WARN("Requested to restart closed device %d!\n", wDevID);
939         return MMSYSERR_BADDEVICEID;
940     }
941
942     if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
943         ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESTARTING, 0, TRUE);
944     }
945
946     /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
947     /* FIXME: Myst crashes with this ... hmm -MM
948        return wodNotifyClient(wwo, WOM_DONE, 0L, 0L);
949     */
950
951     return MMSYSERR_NOERROR;
952 }
953
954 /**************************************************************************
955  *                      wodReset                                [internal]
956  */
957 static DWORD wodReset(WORD wDevID)
958 {
959     TRACE("(%u);\n", wDevID);
960
961     if (wDevID >= ALSA_WodNumDevs) {
962         TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
963         return MMSYSERR_BADDEVICEID;
964     }
965
966     if (WOutDev[wDevID].pcm == NULL) {
967         WARN("Requested to reset closed device %d!\n", wDevID);
968         return MMSYSERR_BADDEVICEID;
969     }
970
971     ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
972
973     return MMSYSERR_NOERROR;
974 }
975
976 /**************************************************************************
977  *                              wodGetPosition                  [internal]
978  */
979 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
980 {
981     WINE_WAVEDEV*       wwo;
982
983     TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
984
985     if (wDevID >= ALSA_WodNumDevs) {
986         TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
987         return MMSYSERR_BADDEVICEID;
988     }
989
990     if (WOutDev[wDevID].pcm == NULL) {
991         WARN("Requested to get position of closed device %d!\n", wDevID);
992         return MMSYSERR_BADDEVICEID;
993     }
994
995     if (lpTime == NULL) return MMSYSERR_INVALPARAM;
996
997     wwo = &WOutDev[wDevID];
998     return ALSA_bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->format);
999 }
1000
1001 /**************************************************************************
1002  *                              wodBreakLoop                    [internal]
1003  */
1004 static DWORD wodBreakLoop(WORD wDevID)
1005 {
1006     TRACE("(%u);\n", wDevID);
1007
1008     if (wDevID >= ALSA_WodNumDevs) {
1009         TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
1010         return MMSYSERR_BADDEVICEID;
1011     }
1012
1013     if (WOutDev[wDevID].pcm == NULL) {
1014         WARN("Requested to breakloop of closed device %d!\n", wDevID);
1015         return MMSYSERR_BADDEVICEID;
1016     }
1017
1018     ALSA_AddRingMessage(&WOutDev[wDevID].msgRing, WINE_WM_BREAKLOOP, 0, TRUE);
1019     return MMSYSERR_NOERROR;
1020 }
1021
1022 /**************************************************************************
1023  *                              wodGetVolume                    [internal]
1024  */
1025 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1026 {
1027     WORD               wleft, wright;
1028     WINE_WAVEDEV*      wwo;
1029     int                min, max;
1030     int                left, right;
1031     DWORD              rc;
1032
1033     TRACE("(%u, %p);\n", wDevID, lpdwVol);
1034     if (wDevID >= ALSA_WodNumDevs) {
1035         TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
1036         return MMSYSERR_BADDEVICEID;
1037     }
1038
1039     if (lpdwVol == NULL)
1040         return MMSYSERR_NOTENABLED;
1041
1042     wwo = &WOutDev[wDevID];
1043
1044     rc = ALSA_CheckSetVolume(wwo->hctl, &left, &right, &min, &max, NULL, NULL, NULL);
1045     if (rc == MMSYSERR_NOERROR)
1046     {
1047 #define VOLUME_ALSA_TO_WIN(x) (  ( (((x)-min) * 65535) + (max-min)/2 ) /(max-min))
1048         wleft = VOLUME_ALSA_TO_WIN(left);
1049         wright = VOLUME_ALSA_TO_WIN(right);
1050 #undef VOLUME_ALSA_TO_WIN
1051         TRACE("left=%d,right=%d,converted to windows left %d, right %d\n", left, right, wleft, wright);
1052         *lpdwVol = MAKELONG( wleft, wright );
1053     }
1054     else
1055         TRACE("CheckSetVolume failed; rc %d\n", rc);
1056
1057     return rc;
1058 }
1059
1060 /**************************************************************************
1061  *                              wodSetVolume                    [internal]
1062  */
1063 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1064 {
1065     WORD               wleft, wright;
1066     WINE_WAVEDEV*      wwo;
1067     int                min, max;
1068     int                left, right;
1069     DWORD              rc;
1070
1071     TRACE("(%u, %08X);\n", wDevID, dwParam);
1072     if (wDevID >= ALSA_WodNumDevs) {
1073         TRACE("Asked for device %d, but only %d known!\n", wDevID, ALSA_WodNumDevs);
1074         return MMSYSERR_BADDEVICEID;
1075     }
1076
1077     wwo = &WOutDev[wDevID];
1078
1079     rc = ALSA_CheckSetVolume(wwo->hctl, NULL, NULL, &min, &max, NULL, NULL, NULL);
1080     if (rc == MMSYSERR_NOERROR)
1081     {
1082         wleft  = LOWORD(dwParam);
1083         wright = HIWORD(dwParam);
1084 #define VOLUME_WIN_TO_ALSA(x) ( (  ( ((x) * (max-min)) + 32767) / 65535) + min )
1085         left = VOLUME_WIN_TO_ALSA(wleft);
1086         right = VOLUME_WIN_TO_ALSA(wright);
1087 #undef VOLUME_WIN_TO_ALSA
1088         rc = ALSA_CheckSetVolume(wwo->hctl, NULL, NULL, NULL, NULL, NULL, &left, &right);
1089         if (rc == MMSYSERR_NOERROR)
1090             TRACE("set volume:  wleft=%d, wright=%d, converted to alsa left %d, right %d\n", wleft, wright, left, right);
1091         else
1092             TRACE("SetVolume failed; rc %d\n", rc);
1093     }
1094
1095     return rc;
1096 }
1097
1098 /**************************************************************************
1099  *                              wodGetNumDevs                   [internal]
1100  */
1101 static  DWORD   wodGetNumDevs(void)
1102 {
1103     return ALSA_WodNumDevs;
1104 }
1105
1106 /**************************************************************************
1107  *                              wodDevInterfaceSize             [internal]
1108  */
1109 static DWORD wodDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
1110 {
1111     TRACE("(%u, %p)\n", wDevID, dwParam1);
1112
1113     *dwParam1 = MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].interface_name, -1,
1114                                     NULL, 0 ) * sizeof(WCHAR);
1115     return MMSYSERR_NOERROR;
1116 }
1117
1118 /**************************************************************************
1119  *                              wodDevInterface                 [internal]
1120  */
1121 static DWORD wodDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
1122 {
1123     if (dwParam2 >= MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].interface_name, -1,
1124                                         NULL, 0 ) * sizeof(WCHAR))
1125     {
1126         MultiByteToWideChar(CP_UNIXCP, 0, WOutDev[wDevID].interface_name, -1,
1127                             dwParam1, dwParam2 / sizeof(WCHAR));
1128         return MMSYSERR_NOERROR;
1129     }
1130     return MMSYSERR_INVALPARAM;
1131 }
1132
1133 /**************************************************************************
1134  *                              wodMessage (WINEALSA.@)
1135  */
1136 DWORD WINAPI ALSA_wodMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1137                              DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1138 {
1139     TRACE("(%u, %s, %08lX, %08lX, %08lX);\n",
1140           wDevID, ALSA_getMessage(wMsg), dwUser, dwParam1, dwParam2);
1141
1142     switch (wMsg) {
1143     case DRVM_INIT:
1144         ALSA_WaveInit();
1145     case DRVM_EXIT:
1146     case DRVM_ENABLE:
1147     case DRVM_DISABLE:
1148         /* FIXME: Pretend this is supported */
1149         return 0;
1150     case WODM_OPEN:             return wodOpen          (wDevID, (LPWAVEOPENDESC)dwParam1,      dwParam2);
1151     case WODM_CLOSE:            return wodClose         (wDevID);
1152     case WODM_GETDEVCAPS:       return wodGetDevCaps    (wDevID, (LPWAVEOUTCAPSW)dwParam1,      dwParam2);
1153     case WODM_GETNUMDEVS:       return wodGetNumDevs    ();
1154     case WODM_GETPITCH:         return MMSYSERR_NOTSUPPORTED;
1155     case WODM_SETPITCH:         return MMSYSERR_NOTSUPPORTED;
1156     case WODM_GETPLAYBACKRATE:  return MMSYSERR_NOTSUPPORTED;
1157     case WODM_SETPLAYBACKRATE:  return MMSYSERR_NOTSUPPORTED;
1158     case WODM_WRITE:            return wodWrite         (wDevID, (LPWAVEHDR)dwParam1,           dwParam2);
1159     case WODM_PAUSE:            return wodPause         (wDevID);
1160     case WODM_GETPOS:           return wodGetPosition   (wDevID, (LPMMTIME)dwParam1,            dwParam2);
1161     case WODM_BREAKLOOP:        return wodBreakLoop     (wDevID);
1162     case WODM_PREPARE:          return MMSYSERR_NOTSUPPORTED;
1163     case WODM_UNPREPARE:        return MMSYSERR_NOTSUPPORTED;
1164     case WODM_GETVOLUME:        return wodGetVolume     (wDevID, (LPDWORD)dwParam1);
1165     case WODM_SETVOLUME:        return wodSetVolume     (wDevID, dwParam1);
1166     case WODM_RESTART:          return wodRestart       (wDevID);
1167     case WODM_RESET:            return wodReset         (wDevID);
1168     case DRV_QUERYDEVICEINTERFACESIZE: return wodDevInterfaceSize       (wDevID, (LPDWORD)dwParam1);
1169     case DRV_QUERYDEVICEINTERFACE:     return wodDevInterface           (wDevID, (PWCHAR)dwParam1, dwParam2);
1170     case DRV_QUERYDSOUNDIFACE:  return wodDsCreate      (wDevID, (PIDSDRIVER*)dwParam1);
1171     case DRV_QUERYDSOUNDDESC:   return wodDsDesc        (wDevID, (PDSDRIVERDESC)dwParam1);
1172
1173     default:
1174         FIXME("unknown message %d!\n", wMsg);
1175     }
1176     return MMSYSERR_NOTSUPPORTED;
1177 }