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