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