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