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