Handle wParam in WM_PAINT properly: if non-null, it is the hdc we are
[wine] / dlls / winmm / wineaudioio / audio.c
1 /*
2  * Wine Driver for Libaudioio
3  * Derived from the Wine OSS Sample Driver
4  * Copyright 1994 Martin Ayotte
5  *           1999 Eric Pouech (async playing in waveOut/waveIn)
6  *           2000 Eric Pouech (loops in waveOut)
7  *           2002 Robert Lunnon (Modifications for libaudioio)
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  *
24  * Note large hacks done to effectively disable DSound altogether
25  * Also Input is not yet working (Lots more work to be done there)
26  * But this does make a reasonable output driver for solaris until the arts
27  * driver comes up to speed
28  */
29
30 /*
31  * FIXME:
32  *      pause in waveOut does not work correctly
33  *      full duplex (in/out) is not working (device is opened twice for Out
34  *      and In) (OSS is known for its poor duplex capabilities, alsa is
35  *      better)
36  */
37
38 #include "config.h"
39
40 #include <stdlib.h>
41 #include <stdarg.h>
42 #include <stdio.h>
43 #include <string.h>
44 #ifdef HAVE_UNISTD_H
45 # include <unistd.h>
46 #endif
47 #include <errno.h>
48 #include <fcntl.h>
49 #ifdef HAVE_SYS_IOCTL_H
50 # include <sys/ioctl.h>
51 #endif
52 #ifdef HAVE_SYS_MMAN_H
53 # include <sys/mman.h>
54 #endif
55 #ifdef HAVE_LIBAUDIOIO_H
56 #include <libaudioio.h>
57 #endif
58 #include "windef.h"
59 #include "winbase.h"
60 #include "wingdi.h"
61 #include "winerror.h"
62 #include "mmddk.h"
63 #include "dsound.h"
64 #include "dsdriver.h"
65 #include "wine/debug.h"
66
67 WINE_DEFAULT_DEBUG_CHANNEL(wave);
68
69 #ifdef HAVE_LIBAUDIOIO
70
71 /* Allow 1% deviation for sample rates (some ES137x cards) */
72 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
73
74 #define SOUND_DEV "/dev/audio"
75 #define DEFAULT_FRAGMENT_SIZE 4096
76
77
78
79 #define MAX_WAVEOUTDRV  (1)
80 #define MAX_WAVEINDRV   (1)
81
82 /* state diagram for waveOut writing:
83  *
84  * +---------+-------------+---------------+---------------------------------+
85  * |  state  |  function   |     event     |            new state            |
86  * +---------+-------------+---------------+---------------------------------+
87  * |         | open()      |               | STOPPED                         |
88  * | PAUSED  | write()     |               | PAUSED                          |
89  * | STOPPED | write()     | <thrd create> | PLAYING                         |
90  * | PLAYING | write()     | HEADER        | PLAYING                         |
91  * | (other) | write()     | <error>       |                                 |
92  * | (any)   | pause()     | PAUSING       | PAUSED                          |
93  * | PAUSED  | restart()   | RESTARTING    | PLAYING (if no thrd => STOPPED) |
94  * | (any)   | reset()     | RESETTING     | STOPPED                         |
95  * | (any)   | close()     | CLOSING       | CLOSED                          |
96  * +---------+-------------+---------------+---------------------------------+
97  */
98
99 /* states of the playing device */
100 #define WINE_WS_PLAYING         0
101 #define WINE_WS_PAUSED          1
102 #define WINE_WS_STOPPED         2
103 #define WINE_WS_CLOSED          3
104
105 /* events to be send to device */
106 #define WINE_WM_PAUSING         (WM_USER + 1)
107 #define WINE_WM_RESTARTING      (WM_USER + 2)
108 #define WINE_WM_RESETTING       (WM_USER + 3)
109 #define WINE_WM_CLOSING         (WM_USER + 4)
110 #define WINE_WM_HEADER          (WM_USER + 5)
111
112 #define WINE_WM_FIRST WINE_WM_PAUSING
113 #define WINE_WM_LAST WINE_WM_HEADER
114
115 typedef struct {
116     int msg;
117     DWORD param;
118 } WWO_MSG;
119
120 typedef struct {
121     int                         unixdev;
122     volatile int                state;                  /* one of the WINE_WS_ manifest constants */
123     DWORD                       dwFragmentSize;         /* size of OSS buffer fragment */
124     WAVEOPENDESC                waveDesc;
125     WORD                        wFlags;
126     PCMWAVEFORMAT               format;
127     LPWAVEHDR                   lpQueuePtr;             /* start of queued WAVEHDRs (waiting to be notified) */
128     LPWAVEHDR                   lpPlayPtr;              /* start of not yet fully played buffers */
129     LPWAVEHDR                   lpLoopPtr;              /* pointer of first buffer in loop, if any */
130     DWORD                       dwLoops;                /* private copy of loop counter */
131
132     DWORD                       dwLastFragDone;         /* time in ms, when last played fragment will be actually played */
133     DWORD                       dwPlayedTotal;          /* number of bytes played since opening */
134
135     /* info on current lpQueueHdr->lpWaveHdr */
136     DWORD                       dwOffCurrHdr;           /* offset in lpPlayPtr->lpData for fragments */
137     DWORD                       dwRemain;               /* number of bytes to write to end the current fragment  */
138
139     /* synchronization stuff */
140     HANDLE                      hThread;
141     DWORD                       dwThreadID;
142     HANDLE                      hEvent;
143 #define WWO_RING_BUFFER_SIZE    30
144     WWO_MSG                     messages[WWO_RING_BUFFER_SIZE];
145     int                         msg_tosave;
146     int                         msg_toget;
147     HANDLE                      msg_event;
148     CRITICAL_SECTION            msg_crst;
149     WAVEOUTCAPSW                caps;
150
151     /* DirectSound stuff */
152     LPBYTE                      mapping;
153     DWORD                       maplen;
154 } WINE_WAVEOUT;
155
156 typedef struct {
157     int                         unixdev;
158     volatile int                state;
159     DWORD                       dwFragmentSize;         /* OpenSound '/dev/dsp' give us that size */
160     WAVEOPENDESC                waveDesc;
161     WORD                        wFlags;
162     PCMWAVEFORMAT               format;
163     LPWAVEHDR                   lpQueuePtr;
164     DWORD                       dwTotalRecorded;
165     WAVEINCAPSW                 caps;
166     BOOL                        bTriggerSupport;
167
168     /* synchronization stuff */
169     HANDLE                      hThread;
170     DWORD                       dwThreadID;
171     HANDLE                      hEvent;
172 } WINE_WAVEIN;
173
174 static WINE_WAVEOUT     WOutDev   [MAX_WAVEOUTDRV];
175 static WINE_WAVEIN      WInDev    [MAX_WAVEINDRV ];
176
177 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
178 static DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv);
179 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc);
180 static DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc);
181
182 static DWORD bytes_to_mmtime(LPMMTIME lpTime, DWORD position,
183                              PCMWAVEFORMAT* format)
184 {
185     TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%lu nChannels=%u nAvgBytesPerSec=%lu\n",
186           lpTime->wType, format->wBitsPerSample, format->wf.nSamplesPerSec,
187           format->wf.nChannels, format->wf.nAvgBytesPerSec);
188     TRACE("Position in bytes=%lu\n", position);
189
190     switch (lpTime->wType) {
191     case TIME_SAMPLES:
192         lpTime->u.sample = position / (format->wBitsPerSample / 8 * format->wf.nChannels);
193         TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
194         break;
195     case TIME_MS:
196         lpTime->u.ms = 1000.0 * position / (format->wBitsPerSample / 8 * format->wf.nChannels * format->wf.nSamplesPerSec);
197         TRACE("TIME_MS=%lu\n", lpTime->u.ms);
198         break;
199     case TIME_SMPTE:
200         lpTime->u.smpte.fps = 30;
201         position = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
202         position += (format->Format.nSamplesPerSec / lpTime->u.smpte.fps) - 1; /* round up */
203         lpTime->u.smpte.sec = position / format->Format.nSamplesPerSec;
204         position -= lpTime->u.smpte.sec * format->Format.nSamplesPerSec;
205         lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
206         lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
207         lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
208         lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
209         lpTime->u.smpte.fps = 30;
210         lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->Format.nSamplesPerSec;
211         TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
212               lpTime->u.smpte.hour, lpTime->u.smpte.min,
213               lpTime->u.smpte.sec, lpTime->u.smpte.frame);
214         break;
215     default:
216         FIXME("Format %d not supported, using TIME_BYTES !\n", lpTime->wType);
217         lpTime->wType = TIME_BYTES;
218         /* fall through */
219     case TIME_BYTES:
220         lpTime->u.cb = position;
221         TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
222         break;
223     }
224     return MMSYSERR_NOERROR;
225 }
226
227 /*======================================================================*
228  *                  Low level WAVE implementation                       *
229  *======================================================================*/
230 SampleSpec_t spec[2];
231
232
233
234 LONG LIBAUDIOIO_WaveInit(void)
235 {
236     int         audio;
237     int         smplrate;
238     int         samplesize = 16;
239     int         dsp_stereo = 1;
240     int         bytespersmpl;
241     int         caps;
242     int         mask;
243     int         i;
244
245     int audio_fd;
246     int mode;
247
248     static const WCHAR ini_out[] = {'A','u','d','i','o','I','O',' ','W','a','v','e','O','u','t',' ','D','r','i','v','e','r',0};
249     static const WCHAR ini_in [] = {'A','u','d','i','o','I','O',' ','W','a','v','e','I','n',' ',' ','D','r','i','v','e','r',0};
250
251     TRACE("Init ENTERED rate = %d\n",spec[PLAYBACK].rate);
252     spec[RECORD].channels=spec[PLAYBACK].channels=2;
253     spec[RECORD].max_blocks=spec[PLAYBACK].max_blocks=16;
254     spec[RECORD].rate=spec[PLAYBACK].rate=44100;
255     spec[RECORD].encoding=spec[PLAYBACK].encoding= ENCODE_PCM;
256     spec[RECORD].precision=spec[PLAYBACK].precision=16 ;
257     spec[RECORD].endian=spec[PLAYBACK].endian=ENDIAN_INTEL;
258     spec[RECORD].disable_threads=spec[PLAYBACK].disable_threads=1;
259     spec[RECORD].type=spec[PLAYBACK].type=TYPE_SIGNED; /* in 16 bit mode this is what typical PC hardware expects */
260
261     mode = O_WRONLY|O_NDELAY;
262
263     /* start with output device */
264
265     /* initialize all device handles to -1 */
266     for (i = 0; i < MAX_WAVEOUTDRV; ++i)
267     {
268         WOutDev[i].unixdev = -1;
269     }
270
271     /* FIXME: only one device is supported */
272     memset(&WOutDev[0].caps, 0, sizeof(WOutDev[0].caps));
273
274     WOutDev[0].caps.wMid = 0x00FF;      /* Manufac ID */
275     WOutDev[0].caps.wPid = 0x0001;      /* Product ID */
276     strcpyW(WOutDev[0].caps.szPname, ini_out);
277     WOutDev[0].caps.vDriverVersion = 0x0100;
278     WOutDev[0].caps.dwFormats = 0x00000000;
279     WOutDev[0].caps.dwSupport = WAVECAPS_VOLUME;
280
281     /*
282      * Libaudioio works differently, you tell it what spec audio you want to write
283      * and it guarantees to match it by converting to the final format on the fly.
284      * So we don't need to read  back and compare.
285      */
286
287     bytespersmpl = spec[PLAYBACK].precision/8;
288
289     WOutDev[0].caps.wChannels = spec[PLAYBACK].channels;
290
291
292 /* Fixme: Libaudioio 0.2 doesn't support balance yet (Libaudioio 0.3 does so this must be fixed later)*/
293
294 /*    if (WOutDev[0].caps.wChannels > 1) WOutDev[0].caps.dwSupport |= WAVECAPS_LRVOLUME;*/
295     TRACE("Init sammplerate= %d\n",spec[PLAYBACK].rate);
296
297     smplrate = spec[PLAYBACK].rate;
298 /*
299  * We have set up the data format to be 16 bit signed in intel format
300  * For Big Endian machines libaudioio will convert the data to bigendian for us
301  */
302
303     WOutDev[0].caps.dwFormats |= WAVE_FORMAT_4M16;
304     if (WOutDev[0].caps.wChannels > 1)
305         WOutDev[0].caps.dwFormats |= WAVE_FORMAT_4S16;
306
307 /* Don't understand this yet, but I don't think this functionality is portable, leave it here for future evaluation
308  *   if (IOCTL(audio, SNDCTL_DSP_GETCAPS, caps) == 0) {
309  *      TRACE("OSS dsp out caps=%08X\n", caps);
310  *      if ((caps & DSP_CAP_REALTIME) && !(caps & DSP_CAP_BATCH)) {
311  *          WOutDev[0].caps.dwSupport |= WAVECAPS_SAMPLEACCURATE;
312  *      }
313  */     /* well, might as well use the DirectSound cap flag for something */
314 /*      if ((caps & DSP_CAP_TRIGGER) && (caps & DSP_CAP_MMAP) &&
315  *          !(caps & DSP_CAP_BATCH))
316  *          WOutDev[0].caps.dwSupport |= WAVECAPS_DIRECTSOUND;
317  *   }
318  */
319
320
321 /*
322  * Note that libaudioio audio capture is not proven yet, in our open call
323  * set the spec for input audio the same as for output
324  */
325     TRACE("out dwFormats = %08lX, dwSupport = %08lX\n",
326           WOutDev[0].caps.dwFormats, WOutDev[0].caps.dwSupport);
327
328     for (i = 0; i < MAX_WAVEINDRV; ++i)
329     {
330         WInDev[i].unixdev = -1;
331     }
332
333     memset(&WInDev[0].caps, 0, sizeof(WInDev[0].caps));
334
335     WInDev[0].caps.wMid = 0x00FF;       /* Manufac ID */
336     WInDev[0].caps.wPid = 0x0001;       /* Product ID */
337     strcpyW(WInDev[0].caps.szPname, ini_in);
338     WInDev[0].caps.dwFormats = 0x00000000;
339     WInDev[0].caps.wChannels = spec[RECORD].channels;
340
341     WInDev[0].bTriggerSupport = TRUE;    /* Maybe :-) */
342
343     bytespersmpl = spec[RECORD].precision/8;
344     smplrate = spec[RECORD].rate;
345
346             WInDev[0].caps.dwFormats |= WAVE_FORMAT_4M16;
347             if (WInDev[0].caps.wChannels > 1)
348                 WInDev[0].caps.dwFormats |= WAVE_FORMAT_4S16;
349
350
351     TRACE("in dwFormats = %08lX\n", WInDev[0].caps.dwFormats);
352
353     return 0;
354 }
355
356 /**************************************************************************
357  *                      LIBAUDIOIO_NotifyClient                 [internal]
358  */
359 static DWORD LIBAUDIOIO_NotifyClient(UINT wDevID, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
360 {
361     TRACE("wDevID = %04X wMsg = 0x%04x dwParm1 = %04lX dwParam2 = %04lX\n",wDevID, wMsg, dwParam1, dwParam2);
362
363     switch (wMsg) {
364     case WOM_OPEN:
365     case WOM_CLOSE:
366     case WOM_DONE:
367         if (wDevID >= MAX_WAVEOUTDRV) return MCIERR_INTERNAL;
368
369         if (WOutDev[wDevID].wFlags != DCB_NULL &&
370             !DriverCallback(WOutDev[wDevID].waveDesc.dwCallback,
371                             WOutDev[wDevID].wFlags,
372                             WOutDev[wDevID].waveDesc.hWave,
373                             wMsg,
374                             WOutDev[wDevID].waveDesc.dwInstance,
375                             dwParam1,
376                             dwParam2)) {
377             WARN("can't notify client !\n");
378             return MMSYSERR_NOERROR;
379         }
380         break;
381
382     case WIM_OPEN:
383     case WIM_CLOSE:
384     case WIM_DATA:
385         if (wDevID >= MAX_WAVEINDRV) return MCIERR_INTERNAL;
386
387         if (WInDev[wDevID].wFlags != DCB_NULL &&
388             !DriverCallback(WInDev[wDevID].waveDesc.dwCallback,
389                             WInDev[wDevID].wFlags,
390                             WInDev[wDevID].waveDesc.hWave,
391                             wMsg,
392                             WInDev[wDevID].waveDesc.dwInstance,
393                             dwParam1,
394                             dwParam2)) {
395             WARN("can't notify client !\n");
396             return MMSYSERR_NOERROR;
397         }
398         break;
399     default:
400         FIXME("Unknown CB message %u\n", wMsg);
401         break;
402     }
403     return 0;
404 }
405
406 /*======================================================================*
407  *                  Low level WAVE OUT implementation                   *
408  *======================================================================*/
409
410 /**************************************************************************
411  *                              wodPlayer_WriteFragments        [internal]
412  *
413  * wodPlayer helper. Writes as many fragments as it can to unixdev.
414  * Returns TRUE in case of buffer underrun.
415  */
416 static  BOOL    wodPlayer_WriteFragments(WINE_WAVEOUT* wwo)
417 {
418     LPWAVEHDR           lpWaveHdr;
419     LPBYTE              lpData;
420     int                 count;
421
422 TRACE("wodPlayer_WriteFragments sammplerate= %d\n",spec[PLAYBACK].rate);
423     for (;;) {
424
425
426 /*
427  * Libaudioio doesn't buffer the same way as linux, you can write data is any block size
428  *And libaudioio just tracks the number of blocks in the streams queue to control latency
429  */
430
431         if (!AudioIOCheckWriteReady()) return FALSE;   /*  Returns false if you have execeeded your specified latency (No space left)*/
432
433         lpWaveHdr = wwo->lpPlayPtr;
434         if (!lpWaveHdr) {
435             if (wwo->dwRemain > 0 &&            /* still data to send to complete current fragment */
436                 wwo->dwLastFragDone &&          /* first fragment has been played */
437                 AudioIOCheckUnderrun()) {   /* done with all waveOutWrite()' fragments */
438                 /* FIXME: should do better handling here */
439                 WARN("Oooch, buffer underrun !\n");
440                 return TRUE; /* force resetting of waveOut device */
441             }
442             return FALSE;       /* wait a bit */
443         }
444
445         if (wwo->dwOffCurrHdr == 0) {
446             TRACE("Starting a new wavehdr %p of %ld bytes\n", lpWaveHdr, lpWaveHdr->dwBufferLength);
447             if (lpWaveHdr->dwFlags & WHDR_BEGINLOOP) {
448                 if (wwo->lpLoopPtr) {
449                     WARN("Already in a loop. Discarding loop on this header (%p)\n", lpWaveHdr);
450                 } else {
451                     TRACE("Starting loop (%ldx) with %p\n", lpWaveHdr->dwLoops, lpWaveHdr);
452                     wwo->lpLoopPtr = lpWaveHdr;
453                     /* Windows does not touch WAVEHDR.dwLoops,
454                      * so we need to make an internal copy */
455                     wwo->dwLoops = lpWaveHdr->dwLoops;
456                 }
457             }
458         }
459
460         lpData = lpWaveHdr->lpData;
461
462         /* finish current wave hdr ? */
463         if (wwo->dwOffCurrHdr + wwo->dwRemain >= lpWaveHdr->dwBufferLength) {
464             DWORD       toWrite = lpWaveHdr->dwBufferLength - wwo->dwOffCurrHdr;
465
466             /* write end of current wave hdr */
467             count = AudioIOWrite(lpData + wwo->dwOffCurrHdr, toWrite);
468             TRACE("write(%p[%5lu], %5lu) => %d\n", lpData, wwo->dwOffCurrHdr, toWrite, count);
469
470             if (count > 0 || toWrite == 0) {
471                 DWORD   tc = GetTickCount();
472
473                 if (wwo->dwLastFragDone /* + guard time ?? */ < tc)
474                     wwo->dwLastFragDone = tc;
475                 wwo->dwLastFragDone += (toWrite * 1000) / wwo->format.wf.nAvgBytesPerSec;
476
477                 lpWaveHdr->reserved = wwo->dwLastFragDone;
478                 TRACE("Tagging hdr %p with %08lx\n", lpWaveHdr, wwo->dwLastFragDone);
479
480                 /* WAVEHDR written, go to next one */
481                 if ((lpWaveHdr->dwFlags & WHDR_ENDLOOP) && wwo->lpLoopPtr) {
482                     if (--wwo->dwLoops > 0) {
483                         wwo->lpPlayPtr = wwo->lpLoopPtr;
484                     } else {
485                         /* last one played */
486                         if (wwo->lpLoopPtr != lpWaveHdr && (lpWaveHdr->dwFlags & WHDR_BEGINLOOP)) {
487                             FIXME("Correctly handled case ? (ending loop buffer also starts a new loop)\n");
488                             /* shall we consider the END flag for the closing loop or for
489                              * the opening one or for both ???
490                              * code assumes for closing loop only
491                              */
492                             wwo->lpLoopPtr = lpWaveHdr;
493                         } else {
494                             wwo->lpLoopPtr = NULL;
495                         }
496                         wwo->lpPlayPtr = lpWaveHdr->lpNext;
497                     }
498                 } else {
499                     wwo->lpPlayPtr = lpWaveHdr->lpNext;
500                 }
501                 wwo->dwOffCurrHdr = 0;
502                 if ((wwo->dwRemain -= count) == 0) {
503                     wwo->dwRemain = wwo->dwFragmentSize;
504                 }
505             }
506             continue; /* try to go to use next wavehdr */
507         }  else {
508             count = AudioIOWrite( lpData + wwo->dwOffCurrHdr, wwo->dwRemain);
509             TRACE("write(%p[%5lu], %5lu) => %d\n", lpData, wwo->dwOffCurrHdr, wwo->dwRemain, count);
510             if (count > 0) {
511                 DWORD   tc = GetTickCount();
512
513                 if (wwo->dwLastFragDone /* + guard time ?? */ < tc)
514                     wwo->dwLastFragDone = tc;
515                 wwo->dwLastFragDone += (wwo->dwRemain * 1000) / wwo->format.wf.nAvgBytesPerSec;
516
517                 TRACE("Tagging frag with %08lx\n", wwo->dwLastFragDone);
518
519                 wwo->dwOffCurrHdr += count;
520                 wwo->dwRemain = wwo->dwFragmentSize;
521             }
522         }
523     }
524
525 }
526
527 int wodPlayer_Message(WINE_WAVEOUT *wwo, int msg, DWORD param)
528 {
529     TRACE("wodPlayerMessage sammplerate= %d  msg=%d\n",spec[PLAYBACK].rate,msg);
530     EnterCriticalSection(&wwo->msg_crst);
531     if ((wwo->msg_tosave == wwo->msg_toget) /* buffer overflow ? */
532     &&  (wwo->messages[wwo->msg_toget].msg))
533     {
534         ERR("buffer overflow !?\n");
535         LeaveCriticalSection(&wwo->msg_crst);
536         return 0;
537     }
538
539     wwo->messages[wwo->msg_tosave].msg = msg;
540     wwo->messages[wwo->msg_tosave].param = param;
541     wwo->msg_tosave++;
542     if (wwo->msg_tosave > WWO_RING_BUFFER_SIZE-1)
543         wwo->msg_tosave = 0;
544     LeaveCriticalSection(&wwo->msg_crst);
545     /* signal a new message */
546     SetEvent(wwo->msg_event);
547     return 1;
548 }
549
550 int wodPlayer_RetrieveMessage(WINE_WAVEOUT *wwo, int *msg, DWORD *param)
551 {
552     EnterCriticalSection(&wwo->msg_crst);
553
554     if (wwo->msg_toget == wwo->msg_tosave) /* buffer empty ? */
555     {
556         LeaveCriticalSection(&wwo->msg_crst);
557         return 0;
558     }
559
560     *msg = wwo->messages[wwo->msg_toget].msg;
561     wwo->messages[wwo->msg_toget].msg = 0;
562     *param = wwo->messages[wwo->msg_toget].param;
563     wwo->msg_toget++;
564     if (wwo->msg_toget > WWO_RING_BUFFER_SIZE-1)
565         wwo->msg_toget = 0;
566     LeaveCriticalSection(&wwo->msg_crst);
567     return 1;
568 }
569
570 /**************************************************************************
571  *                              wodPlayer_Notify                [internal]
572  *
573  * wodPlayer helper. Notifies (and remove from queue) all the wavehdr which content
574  * have been played (actually to speaker, not to unixdev fd).
575  */
576 static  void    wodPlayer_Notify(WINE_WAVEOUT* wwo, WORD uDevID, BOOL force)
577 {
578     LPWAVEHDR           lpWaveHdr;
579     DWORD               tc = GetTickCount();
580
581     while (wwo->lpQueuePtr &&
582            (force ||
583             (wwo->lpQueuePtr != wwo->lpPlayPtr && wwo->lpQueuePtr != wwo->lpLoopPtr))) {
584         lpWaveHdr = wwo->lpQueuePtr;
585
586         if (lpWaveHdr->reserved > tc && !force) break;
587
588         wwo->dwPlayedTotal += lpWaveHdr->dwBufferLength;
589         wwo->lpQueuePtr = lpWaveHdr->lpNext;
590
591         lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
592         lpWaveHdr->dwFlags |= WHDR_DONE;
593
594         TRACE("Notifying client with %p\n", lpWaveHdr);
595         if (LIBAUDIOIO_NotifyClient(uDevID, WOM_DONE, (DWORD)lpWaveHdr, 0) != MMSYSERR_NOERROR) {
596             WARN("can't notify client !\n");
597         }
598     }
599 }
600
601 /**************************************************************************
602  *                              wodPlayer_Reset                 [internal]
603  *
604  * wodPlayer helper. Resets current output stream.
605  */
606 static  void    wodPlayer_Reset(WINE_WAVEOUT* wwo, WORD uDevID, BOOL reset)
607 {
608     /* updates current notify list */
609     wodPlayer_Notify(wwo, uDevID, FALSE);
610
611     /* flush all possible output */
612 /*
613  *FIXME In the original code I think this aborted IO. With Libaudioio you have to wait
614  * The following function just blocks until I/O is complete
615  * There is possibly a way to abort the blocks buffered in streams
616  * but this approach seems to work OK
617  */
618         AudioIOFlush();
619
620
621
622     wwo->dwOffCurrHdr = 0;
623     wwo->dwRemain = wwo->dwFragmentSize;
624     if (reset) {
625         /* empty notify list */
626         wodPlayer_Notify(wwo, uDevID, TRUE);
627
628         wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
629         wwo->state = WINE_WS_STOPPED;
630         wwo->dwPlayedTotal = 0;
631     } else {
632         /* FIXME: this is not accurate when looping, but can be do better ? */
633         wwo->lpPlayPtr = (wwo->lpLoopPtr) ? wwo->lpLoopPtr : wwo->lpQueuePtr;
634         wwo->state = WINE_WS_PAUSED;
635     }
636 }
637
638 /**************************************************************************
639  *                              wodPlayer                       [internal]
640  */
641 static  DWORD   CALLBACK        wodPlayer(LPVOID pmt)
642 {
643     WORD                uDevID = (DWORD)pmt;
644     WINE_WAVEOUT*       wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
645     WAVEHDR*            lpWaveHdr;
646     DWORD               dwSleepTime;
647     int                 msg;
648     DWORD               param;
649     DWORD               tc;
650     BOOL                had_msg;
651
652     wwo->state = WINE_WS_STOPPED;
653
654     wwo->dwLastFragDone = 0;
655     wwo->dwOffCurrHdr = 0;
656     wwo->dwRemain = wwo->dwFragmentSize;
657     wwo->lpQueuePtr = wwo->lpPlayPtr = wwo->lpLoopPtr = NULL;
658     wwo->dwPlayedTotal = 0;
659
660     TRACE("imhere[0]\n");
661     SetEvent(wwo->hEvent);
662
663     for (;;) {
664         /* wait for dwSleepTime or an event in thread's queue
665          * FIXME:
666          * - is wait time calculation optimal ?
667          * - these 100 ms parts should be changed, but Eric reports
668          *   that the wodPlayer thread might lock up if we use INFINITE
669          *   (strange !), so I better don't change that now... */
670         if (wwo->state != WINE_WS_PLAYING)
671             dwSleepTime = 100;
672         else
673         {
674             tc = GetTickCount();
675             if (tc < wwo->dwLastFragDone)
676             {
677                 /* calculate sleep time depending on when the last fragment
678                    will be played */
679                 dwSleepTime = (wwo->dwLastFragDone - tc)*7/10;
680                 if (dwSleepTime > 100)
681                     dwSleepTime = 100;
682             }
683             else
684                 dwSleepTime = 0;
685         }
686
687         TRACE("imhere[1] tc = %08lx\n", GetTickCount());
688         if (dwSleepTime)
689             WaitForSingleObject(wwo->msg_event, dwSleepTime);
690         TRACE("imhere[2] (q=%p p=%p) tc = %08lx\n", wwo->lpQueuePtr,
691               wwo->lpPlayPtr, GetTickCount());
692         had_msg = FALSE;
693         while (wodPlayer_RetrieveMessage(wwo, &msg, &param)) {
694             had_msg = TRUE;
695             switch (msg) {
696             case WINE_WM_PAUSING:
697                 wodPlayer_Reset(wwo, uDevID, FALSE);
698                 wwo->state = WINE_WS_PAUSED;
699                 SetEvent(wwo->hEvent);
700                 break;
701             case WINE_WM_RESTARTING:
702                 wwo->state = WINE_WS_PLAYING;
703                 SetEvent(wwo->hEvent);
704                 break;
705             case WINE_WM_HEADER:
706                 lpWaveHdr = (LPWAVEHDR)param;
707
708                 /* insert buffer at the end of queue */
709                 {
710                     LPWAVEHDR*  wh;
711                     for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
712                     *wh = lpWaveHdr;
713                 }
714                 if (!wwo->lpPlayPtr) wwo->lpPlayPtr = lpWaveHdr;
715                 if (wwo->state == WINE_WS_STOPPED)
716                     wwo->state = WINE_WS_PLAYING;
717                 break;
718             case WINE_WM_RESETTING:
719                 wodPlayer_Reset(wwo, uDevID, TRUE);
720                 SetEvent(wwo->hEvent);
721                 break;
722             case WINE_WM_CLOSING:
723                 /* sanity check: this should not happen since the device must have been reset before */
724                 if (wwo->lpQueuePtr || wwo->lpPlayPtr) ERR("out of sync\n");
725                 wwo->hThread = 0;
726                 wwo->state = WINE_WS_CLOSED;
727                 SetEvent(wwo->hEvent);
728                 ExitThread(0);
729                 /* shouldn't go here */
730             default:
731                 FIXME("unknown message %d\n", msg);
732                 break;
733             }
734             if (wwo->state == WINE_WS_PLAYING) {
735                 wodPlayer_WriteFragments(wwo);
736             }
737             wodPlayer_Notify(wwo, uDevID, FALSE);
738         }
739
740         if (!had_msg) { /* if we've received a msg we've just done this so we
741                            won't repeat it */
742             if (wwo->state == WINE_WS_PLAYING) {
743                 wodPlayer_WriteFragments(wwo);
744             }
745             wodPlayer_Notify(wwo, uDevID, FALSE);
746         }
747     }
748     ExitThread(0);
749     /* just for not generating compilation warnings... should never be executed */
750     return 0;
751 }
752
753 /**************************************************************************
754  *                      wodGetDevCaps                           [internal]
755  */
756 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSW lpCaps, DWORD dwSize)
757 {
758     TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
759
760     if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
761
762     if (wDevID >= MAX_WAVEOUTDRV) {
763         TRACE("MAX_WAVOUTDRV reached !\n");
764         return MMSYSERR_BADDEVICEID;
765     }
766
767     memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
768     return MMSYSERR_NOERROR;
769 }
770
771 /**************************************************************************
772  *                              wodOpen                         [internal]
773  */
774 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
775 {
776     int                 audio;
777     int                 format;
778     int                 sample_rate;
779     int                 dsp_stereo;
780     int                 fragment_size;
781     int                 audio_fragment;
782     WINE_WAVEOUT*       wwo;
783
784     TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
785     if (lpDesc == NULL) {
786         WARN("Invalid Parameter !\n");
787         return MMSYSERR_INVALPARAM;
788     }
789     if (wDevID >= MAX_WAVEOUTDRV) {
790         TRACE("MAX_WAVOUTDRV reached !\n");
791         return MMSYSERR_BADDEVICEID;
792     }
793
794     /* only PCM format is supported so far... */
795     if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
796         lpDesc->lpFormat->nChannels == 0 ||
797         lpDesc->lpFormat->nSamplesPerSec == 0 ||
798         (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
799         WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
800              lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
801              lpDesc->lpFormat->nSamplesPerSec);
802         return WAVERR_BADFORMAT;
803     }
804
805     if (dwFlags & WAVE_FORMAT_QUERY) {
806         TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
807              lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
808              lpDesc->lpFormat->nSamplesPerSec);
809         return MMSYSERR_NOERROR;
810     }
811
812     wwo = &WOutDev[wDevID];
813
814     if ((dwFlags & WAVE_DIRECTSOUND) && !(wwo->caps.dwSupport & WAVECAPS_DIRECTSOUND))
815         /* not supported, ignore it */
816         dwFlags &= ~WAVE_DIRECTSOUND;
817
818     if (access(SOUND_DEV, 0) != 0)
819         return MMSYSERR_NOTENABLED;
820
821         audio = AudioIOOpenX( O_WRONLY|O_NDELAY,&spec[PLAYBACK],&spec[PLAYBACK]);
822
823     if (audio == -1) {
824         WARN("can't open sound device %s (%s)!\n", SOUND_DEV, strerror(errno));
825         return MMSYSERR_ALLOCATED;
826     }
827  /*   fcntl(audio, F_SETFD, 1); *//* set close on exec flag - Dunno about this (RL)*/
828     wwo->unixdev = audio;
829     wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
830
831     memcpy(&wwo->waveDesc, lpDesc,           sizeof(WAVEOPENDESC));
832     memcpy(&wwo->format,   lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
833
834     if (wwo->format.wBitsPerSample == 0) {
835         WARN("Resetting zeroed wBitsPerSample\n");
836         wwo->format.wBitsPerSample = 8 *
837             (wwo->format.wf.nAvgBytesPerSec /
838              wwo->format.wf.nSamplesPerSec) /
839             wwo->format.wf.nChannels;
840     }
841
842     if (dwFlags & WAVE_DIRECTSOUND) {
843         if (wwo->caps.dwSupport & WAVECAPS_SAMPLEACCURATE)
844             /* we have realtime DirectSound, fragments just waste our time,
845              * but a large buffer is good, so choose 64KB (32 * 2^11) */
846             audio_fragment = 0x0020000B;
847         else
848             /* to approximate realtime, we must use small fragments,
849              * let's try to fragment the above 64KB (256 * 2^8) */
850             audio_fragment = 0x01000008;
851     } else {
852         /* shockwave player uses only 4 1k-fragments at a rate of 22050 bytes/sec
853          * thus leading to 46ms per fragment, and a turnaround time of 185ms
854          */
855         /* 16 fragments max, 2^10=1024 bytes per fragment */
856         audio_fragment = 0x000F000A;
857     }
858     sample_rate = wwo->format.wf.nSamplesPerSec;
859     dsp_stereo = (wwo->format.wf.nChannels > 1) ? 1 : 0;
860
861
862
863     /*Set the sample rate*/
864     spec[PLAYBACK].rate=sample_rate;
865
866     /*And the size and signedness*/
867     spec[PLAYBACK].precision=(wwo->format.wBitsPerSample );
868     if (spec[PLAYBACK].precision==16) spec[PLAYBACK].type=TYPE_SIGNED; else spec[PLAYBACK].type=TYPE_UNSIGNED;
869     spec[PLAYBACK].channels=(wwo->format.wf.nChannels);
870     spec[PLAYBACK].encoding=ENCODE_PCM;
871     spec[PLAYBACK].endian=ENDIAN_INTEL;
872     spec[PLAYBACK].max_blocks=16;  /*FIXME This is the libaudioio equivalent to fragments, it controls latency*/
873
874         audio = AudioIOOpenX( O_WRONLY|O_NDELAY,&spec[PLAYBACK],&spec[PLAYBACK]);
875
876     if (audio == -1) {
877         WARN("can't open sound device %s (%s)!\n", SOUND_DEV, strerror(errno));
878         return MMSYSERR_ALLOCATED;
879     }
880  /*   fcntl(audio, F_SETFD, 1); *//* set close on exec flag */
881     wwo->unixdev = audio;
882
883
884     /* even if we set fragment size above, read it again, just in case */
885
886     wwo->dwFragmentSize = DEFAULT_FRAGMENT_SIZE;
887
888     wwo->msg_toget = 0;
889     wwo->msg_tosave = 0;
890     wwo->msg_event = CreateEventW(NULL, FALSE, FALSE, NULL);
891     memset(wwo->messages, 0, sizeof(WWO_MSG)*WWO_RING_BUFFER_SIZE);
892     InitializeCriticalSection(&wwo->msg_crst);
893
894     if (!(dwFlags & WAVE_DIRECTSOUND)) {
895         wwo->hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
896         wwo->hThread = CreateThread(NULL, 0, wodPlayer, (LPVOID)(DWORD)wDevID, 0, &(wwo->dwThreadID));
897         WaitForSingleObject(wwo->hEvent, INFINITE);
898     } else {
899         wwo->hEvent = INVALID_HANDLE_VALUE;
900         wwo->hThread = INVALID_HANDLE_VALUE;
901         wwo->dwThreadID = 0;
902     }
903
904     TRACE("fd=%d fragmentSize=%ld\n",
905           wwo->unixdev, wwo->dwFragmentSize);
906     if (wwo->dwFragmentSize % wwo->format.wf.nBlockAlign)
907         ERR("Fragment doesn't contain an integral number of data blocks\n");
908
909     TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
910           wwo->format.wBitsPerSample, wwo->format.wf.nAvgBytesPerSec,
911           wwo->format.wf.nSamplesPerSec, wwo->format.wf.nChannels,
912           wwo->format.wf.nBlockAlign);
913
914     if (LIBAUDIOIO_NotifyClient(wDevID, WOM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
915         WARN("can't notify client !\n");
916         return MMSYSERR_INVALPARAM;
917     }
918     return MMSYSERR_NOERROR;
919 }
920
921 /**************************************************************************
922  *                              wodClose                        [internal]
923  */
924 static DWORD wodClose(WORD wDevID)
925 {
926     DWORD               ret = MMSYSERR_NOERROR;
927     WINE_WAVEOUT*       wwo;
928
929     TRACE("(%u);\n", wDevID);
930
931     if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
932         WARN("bad device ID !\n");
933         return MMSYSERR_BADDEVICEID;
934     }
935
936     wwo = &WOutDev[wDevID];
937     if (wwo->lpQueuePtr) {
938         WARN("buffers still playing !\n");
939         ret = WAVERR_STILLPLAYING;
940     } else {
941         TRACE("imhere[3-close]\n");
942         if (wwo->hEvent != INVALID_HANDLE_VALUE) {
943             wodPlayer_Message(wwo, WINE_WM_CLOSING, 0);
944             WaitForSingleObject(wwo->hEvent, INFINITE);
945             CloseHandle(wwo->hEvent);
946         }
947         if (wwo->mapping) {
948             munmap(wwo->mapping, wwo->maplen);
949             wwo->mapping = NULL;
950         }
951
952         AudioIOClose();
953         wwo->unixdev = -1;
954         wwo->dwFragmentSize = 0;
955         if (LIBAUDIOIO_NotifyClient(wDevID, WOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
956             WARN("can't notify client !\n");
957             ret = MMSYSERR_INVALPARAM;
958         }
959     }
960     return ret;
961 }
962
963 /**************************************************************************
964  *                              wodWrite                        [internal]
965  *
966  */
967 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
968 {
969     TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
970
971     /* first, do the sanity checks... */
972     if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
973         WARN("bad dev ID !\n");
974         return MMSYSERR_BADDEVICEID;
975     }
976
977     if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
978         return WAVERR_UNPREPARED;
979
980     if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
981         return WAVERR_STILLPLAYING;
982
983     lpWaveHdr->dwFlags &= ~WHDR_DONE;
984     lpWaveHdr->dwFlags |= WHDR_INQUEUE;
985     lpWaveHdr->lpNext = 0;
986
987     TRACE("imhere[3-HEADER]\n");
988     wodPlayer_Message(&WOutDev[wDevID], WINE_WM_HEADER, (DWORD)lpWaveHdr);
989
990     return MMSYSERR_NOERROR;
991 }
992
993 /**************************************************************************
994  *                      wodPause                                [internal]
995  */
996 static DWORD wodPause(WORD wDevID)
997 {
998     TRACE("(%u);!\n", wDevID);
999
1000     if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
1001         WARN("bad device ID !\n");
1002         return MMSYSERR_BADDEVICEID;
1003     }
1004
1005     TRACE("imhere[3-PAUSING]\n");
1006     wodPlayer_Message(&WOutDev[wDevID], WINE_WM_PAUSING, 0);
1007     WaitForSingleObject(WOutDev[wDevID].hEvent, INFINITE);
1008
1009     return MMSYSERR_NOERROR;
1010 }
1011
1012 /**************************************************************************
1013  *                      wodRestart                              [internal]
1014  */
1015 static DWORD wodRestart(WORD wDevID)
1016 {
1017     TRACE("(%u);\n", wDevID);
1018
1019     if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
1020         WARN("bad device ID !\n");
1021         return MMSYSERR_BADDEVICEID;
1022     }
1023
1024     if (WOutDev[wDevID].state == WINE_WS_PAUSED) {
1025         TRACE("imhere[3-RESTARTING]\n");
1026         wodPlayer_Message(&WOutDev[wDevID], WINE_WM_RESTARTING, 0);
1027         WaitForSingleObject(WOutDev[wDevID].hEvent, INFINITE);
1028     }
1029
1030     /* FIXME: is NotifyClient with WOM_DONE right ? (Comet Busters 1.3.3 needs this notification) */
1031     /* FIXME: Myst crashes with this ... hmm -MM
1032        if (LIBAUDIOIO_NotifyClient(wDevID, WOM_DONE, 0L, 0L) != MMSYSERR_NOERROR) {
1033        WARN("can't notify client !\n");
1034        return MMSYSERR_INVALPARAM;
1035        }
1036     */
1037
1038     return MMSYSERR_NOERROR;
1039 }
1040
1041 /**************************************************************************
1042  *                      wodReset                                [internal]
1043  */
1044 static DWORD wodReset(WORD wDevID)
1045 {
1046     TRACE("(%u);\n", wDevID);
1047
1048     if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
1049         WARN("bad device ID !\n");
1050         return MMSYSERR_BADDEVICEID;
1051     }
1052
1053     TRACE("imhere[3-RESET]\n");
1054     wodPlayer_Message(&WOutDev[wDevID], WINE_WM_RESETTING, 0);
1055     WaitForSingleObject(WOutDev[wDevID].hEvent, INFINITE);
1056
1057     return MMSYSERR_NOERROR;
1058 }
1059
1060
1061 /**************************************************************************
1062  *                              wodGetPosition                  [internal]
1063  */
1064 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1065 {
1066     WINE_WAVEOUT*       wwo;
1067
1068     TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1069
1070     if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
1071         WARN("bad device ID !\n");
1072         return MMSYSERR_BADDEVICEID;
1073     }
1074
1075     if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1076
1077     wwo = &WOutDev[wDevID];
1078
1079     return bytes_to_mmtime(lpTime, wwo->dwPlayedTotal, &wwo->format);
1080 }
1081
1082 /**************************************************************************
1083  *                              wodGetVolume                    [internal]
1084  */
1085 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1086 {
1087     int         mixer;
1088     int         vol,bal;
1089     DWORD       left, right;
1090
1091     TRACE("(%u, %p);\n", wDevID, lpdwVol);
1092
1093     if (lpdwVol == NULL)
1094         return MMSYSERR_NOTENABLED;
1095
1096      vol=AudioIOGetPlaybackVolume();
1097      bal=AudioIOGetPlaybackBalance();
1098
1099
1100      if(bal<0) {
1101         left = vol;
1102         right=-(vol*(-100+bal)/100);
1103      }
1104       else
1105      {
1106         right = vol;
1107         left=(vol*(100-bal)/100);
1108      }
1109
1110     *lpdwVol = ((left * 0xFFFFl) / 100) + (((right * 0xFFFFl) / 100) << 16);
1111     return MMSYSERR_NOERROR;
1112 }
1113
1114
1115 /**************************************************************************
1116  *                              wodSetVolume                    [internal]
1117  */
1118 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1119 {
1120     int         mixer;
1121     int         volume,bal;
1122     DWORD       left, right;
1123
1124     TRACE("(%u, %08lX);\n", wDevID, dwParam);
1125
1126     left  = (LOWORD(dwParam) * 100) / 0xFFFFl;
1127     right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1128     volume = max(left , right );
1129     bal=min(left,right);
1130     bal=bal*100/volume;
1131     if(right>left) bal=-100+bal; else bal=100-bal;
1132
1133     AudioIOSetPlaybackVolume(volume);
1134     AudioIOSetPlaybackBalance(bal);
1135
1136     return MMSYSERR_NOERROR;
1137 }
1138
1139 /**************************************************************************
1140  *                              wodGetNumDevs                   [internal]
1141  */
1142 static  DWORD   wodGetNumDevs(void)
1143 {
1144     DWORD       ret = 1;
1145
1146     /* FIXME: For now, only one sound device (SOUND_DEV) is allowed */
1147     int audio = open(SOUND_DEV, O_WRONLY|O_NDELAY, 0);
1148
1149     if (audio == -1) {
1150         if (errno != EBUSY)
1151             ret = 0;
1152     } else {
1153         close(audio);
1154
1155     }
1156     TRACE("NumDrivers = %d\n",ret);
1157     return ret;
1158 }
1159
1160 /**************************************************************************
1161  *                              wodMessage (WINEAUDIOIO.@)
1162  */
1163 DWORD WINAPI LIBAUDIOIO_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1164                             DWORD dwParam1, DWORD dwParam2)
1165 {
1166     TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1167           wDevID, wMsg, dwUser, dwParam1, dwParam2);
1168
1169     switch (wMsg) {
1170     case DRVM_INIT:
1171     case DRVM_EXIT:
1172     case DRVM_ENABLE:
1173     case DRVM_DISABLE:
1174         /* FIXME: Pretend this is supported */
1175         return 0;
1176     case WODM_OPEN:             return wodOpen          (wDevID, (LPWAVEOPENDESC)dwParam1,      dwParam2);
1177     case WODM_CLOSE:            return wodClose         (wDevID);
1178     case WODM_WRITE:            return wodWrite         (wDevID, (LPWAVEHDR)dwParam1,           dwParam2);
1179     case WODM_PAUSE:            return wodPause         (wDevID);
1180     case WODM_GETPOS:           return wodGetPosition   (wDevID, (LPMMTIME)dwParam1,            dwParam2);
1181     case WODM_BREAKLOOP:        return MMSYSERR_NOTSUPPORTED;
1182     case WODM_PREPARE:          return MMSYSERR_NOTSUPPORTED;
1183     case WODM_UNPREPARE:        return MMSYSERR_NOTSUPPORTED;
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_GETVOLUME:        return wodGetVolume     (wDevID, (LPDWORD)dwParam1);
1191     case WODM_SETVOLUME:        return wodSetVolume     (wDevID, dwParam1);
1192     case WODM_RESTART:          return wodRestart       (wDevID);
1193     case WODM_RESET:            return wodReset         (wDevID);
1194
1195     case DRV_QUERYDSOUNDIFACE:  return wodDsCreate      (wDevID, (PIDSDRIVER*)dwParam1);
1196     case DRV_QUERYDSOUNDDESC:   return wodDsDesc        (wDevID, (PDSDRIVERDESC)dwParam1);
1197     default:
1198         FIXME("unknown message %d!\n", wMsg);
1199     }
1200     return MMSYSERR_NOTSUPPORTED;
1201 }
1202
1203 /*======================================================================*
1204  *                  Low level DSOUND implementation                     *
1205  *              While I have tampered somewhat with this code it is wholely unlikely that it works
1206  *              Elsewhere the driver returns Not Implemented for DIrectSound
1207  *              While it may be possible to map the sound device on Solaris
1208  *              Doing so would bypass the libaudioio library and therefore break any conversions
1209  *              that the libaudioio sample specification converter is doing
1210  *              **** All this is untested so far
1211  *======================================================================*/
1212
1213 typedef struct IDsDriverImpl IDsDriverImpl;
1214 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
1215
1216 struct IDsDriverImpl
1217 {
1218     /* IUnknown fields */
1219     IDsDriverVtbl      *lpVtbl;
1220     DWORD               ref;
1221     /* IDsDriverImpl fields */
1222     UINT                wDevID;
1223     IDsDriverBufferImpl*primary;
1224 };
1225
1226 struct IDsDriverBufferImpl
1227 {
1228     /* IUnknown fields */
1229     IDsDriverBufferVtbl *lpVtbl;
1230     DWORD               ref;
1231     /* IDsDriverBufferImpl fields */
1232     IDsDriverImpl*      drv;
1233     DWORD               buflen;
1234 };
1235
1236 static HRESULT DSDB_MapPrimary(IDsDriverBufferImpl *dsdb)
1237 {
1238     WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
1239     if (!wwo->mapping) {
1240         wwo->mapping = mmap(NULL, wwo->maplen, PROT_WRITE, MAP_SHARED,
1241                             wwo->unixdev, 0);
1242         if (wwo->mapping == (LPBYTE)-1) {
1243             ERR("(%p): Could not map sound device for direct access (errno=%d)\n", dsdb, errno);
1244             return DSERR_GENERIC;
1245         }
1246         TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dsdb, wwo->mapping, wwo->maplen);
1247
1248         /* for some reason, es1371 and sblive! sometimes have junk in here. */
1249         memset(wwo->mapping,0,wwo->maplen); /* clear it, or we get junk noise */
1250     }
1251     return DS_OK;
1252 }
1253
1254 static HRESULT DSDB_UnmapPrimary(IDsDriverBufferImpl *dsdb)
1255 {
1256     WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
1257     if (wwo->mapping) {
1258         if (munmap(wwo->mapping, wwo->maplen) < 0) {
1259             ERR("(%p): Could not unmap sound device (errno=%d)\n", dsdb, errno);
1260             return DSERR_GENERIC;
1261         }
1262         wwo->mapping = NULL;
1263         TRACE("(%p): sound device unmapped\n", dsdb);
1264     }
1265     return DS_OK;
1266 }
1267
1268 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
1269 {
1270     /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
1271     FIXME("(): stub!\n");
1272     return DSERR_UNSUPPORTED;
1273 }
1274
1275 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
1276 {
1277     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
1278     return InterlockedIncrement(&This->ref);
1279 }
1280
1281 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
1282 {
1283     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
1284     ULONG refCount = InterlockedDecrement(&This->ref);
1285
1286     if (refCount)
1287         return refCount;
1288     if (This == This->drv->primary)
1289         This->drv->primary = NULL;
1290     DSDB_UnmapPrimary(This);
1291     HeapFree(GetProcessHeap(),0,This);
1292     return 0;
1293 }
1294
1295 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
1296                                                LPVOID*ppvAudio1,LPDWORD pdwLen1,
1297                                                LPVOID*ppvAudio2,LPDWORD pdwLen2,
1298                                                DWORD dwWritePosition,DWORD dwWriteLen,
1299                                                DWORD dwFlags)
1300 {
1301     /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
1302     /* since we (GetDriverDesc flags) have specified DSDDESC_DONTNEEDPRIMARYLOCK,
1303      * and that we don't support secondary buffers, this method will never be called */
1304     TRACE("(%p): stub\n",iface);
1305     return DSERR_UNSUPPORTED;
1306 }
1307
1308 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
1309                                                  LPVOID pvAudio1,DWORD dwLen1,
1310                                                  LPVOID pvAudio2,DWORD dwLen2)
1311 {
1312     /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
1313     TRACE("(%p): stub\n",iface);
1314     return DSERR_UNSUPPORTED;
1315 }
1316
1317 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
1318                                                     LPWAVEFORMATEX pwfx)
1319 {
1320     /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
1321
1322     TRACE("(%p,%p)\n",iface,pwfx);
1323     /* On our request (GetDriverDesc flags), DirectSound has by now used
1324      * waveOutClose/waveOutOpen to set the format...
1325      * unfortunately, this means our mmap() is now gone...
1326      * so we need to somehow signal to our DirectSound implementation
1327      * that it should completely recreate this HW buffer...
1328      * this unexpected error code should do the trick... */
1329     return DSERR_BUFFERLOST;
1330 }
1331
1332 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
1333 {
1334     /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
1335     TRACE("(%p,%ld): stub\n",iface,dwFreq);
1336     return DSERR_UNSUPPORTED;
1337 }
1338
1339 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
1340 {
1341     /* IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface; */
1342     FIXME("(%p,%p): stub!\n",iface,pVolPan);
1343     return DS_OK;
1344 }
1345
1346 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
1347 {
1348     /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
1349     TRACE("(%p,%ld): stub\n",iface,dwNewPos);
1350     return DSERR_UNSUPPORTED;
1351 }
1352
1353 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
1354                                                       LPDWORD lpdwPlay, LPDWORD lpdwWrite)
1355 {
1356     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
1357 #if 0
1358     count_info info;
1359 #endif
1360     DWORD ptr;
1361
1362     TRACE("(%p)\n",iface);
1363     if (WOutDev[This->drv->wDevID].unixdev == -1) {
1364         ERR("device not open, but accessing?\n");
1365         return DSERR_UNINITIALIZED;
1366     }
1367     /*Libaudioio doesn't support this (Yet anyway)*/
1368      return DSERR_UNSUPPORTED;
1369
1370 }
1371
1372 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
1373 {
1374     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
1375 #if 0
1376     int enable = PCM_ENABLE_OUTPUT;
1377     TRACE("(%p,%lx,%lx,%lx)\n",iface,dwRes1,dwRes2,dwFlags);
1378     if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1379         ERR("ioctl failed (%d)\n", errno);
1380         return DSERR_GENERIC;
1381     }
1382 #endif
1383     return DS_OK;
1384 }
1385
1386 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
1387 {
1388     IDsDriverBufferImpl *This = (IDsDriverBufferImpl *)iface;
1389     int enable = 0;
1390 #if 0
1391     TRACE("(%p)\n",iface);
1392     /* no more playing */
1393     if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1394         ERR("ioctl failed (%d)\n", errno);
1395         return DSERR_GENERIC;
1396     }
1397 #endif
1398 #if 0
1399     /* the play position must be reset to the beginning of the buffer */
1400     if (ioctl(WOutDev[This->drv->wDevID].unixdev, SNDCTL_DSP_RESET, 0) < 0) {
1401         ERR("ioctl failed (%d)\n", errno);
1402         return DSERR_GENERIC;
1403     }
1404 #endif
1405     /* Most OSS drivers just can't stop the playback without closing the device...
1406      * so we need to somehow signal to our DirectSound implementation
1407      * that it should completely recreate this HW buffer...
1408      * this unexpected error code should do the trick... */
1409     return DSERR_BUFFERLOST;
1410 }
1411
1412 static IDsDriverBufferVtbl dsdbvt =
1413 {
1414     IDsDriverBufferImpl_QueryInterface,
1415     IDsDriverBufferImpl_AddRef,
1416     IDsDriverBufferImpl_Release,
1417     IDsDriverBufferImpl_Lock,
1418     IDsDriverBufferImpl_Unlock,
1419     IDsDriverBufferImpl_SetFormat,
1420     IDsDriverBufferImpl_SetFrequency,
1421     IDsDriverBufferImpl_SetVolumePan,
1422     IDsDriverBufferImpl_SetPosition,
1423     IDsDriverBufferImpl_GetPosition,
1424     IDsDriverBufferImpl_Play,
1425     IDsDriverBufferImpl_Stop
1426 };
1427
1428 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
1429 {
1430     /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
1431     FIXME("(%p): stub!\n",iface);
1432     return DSERR_UNSUPPORTED;
1433 }
1434
1435 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
1436 {
1437     IDsDriverImpl *This = (IDsDriverImpl *)iface;
1438     return InterlockedIncrement(&This->ref);
1439 }
1440
1441 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
1442 {
1443     IDsDriverImpl *This = (IDsDriverImpl *)iface;
1444     ULONG refCount = InterlockedDecrement(&This->ref);
1445
1446     if (refCount)
1447         return refCount;
1448     HeapFree(GetProcessHeap(),0,This);
1449     return 0;
1450 }
1451
1452 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
1453 {
1454     IDsDriverImpl *This = (IDsDriverImpl *)iface;
1455     TRACE("(%p,%p)\n",iface,pDesc);
1456     pDesc->dwFlags = DSDDESC_DOMMSYSTEMOPEN | DSDDESC_DOMMSYSTEMSETFORMAT |
1457         DSDDESC_USESYSTEMMEMORY | DSDDESC_DONTNEEDPRIMARYLOCK;
1458     strcpy(pDesc->szDesc,"Wine AudioIO DirectSound Driver");
1459     strcpy(pDesc->szDrvname,"wineaudioio.drv");
1460     pDesc->dnDevNode            = WOutDev[This->wDevID].waveDesc.dnDevNode;
1461     pDesc->wVxdId               = 0;
1462     pDesc->wReserved            = 0;
1463     pDesc->ulDeviceNum          = This->wDevID;
1464     pDesc->dwHeapType           = DSDHEAP_NOHEAP;
1465     pDesc->pvDirectDrawHeap     = NULL;
1466     pDesc->dwMemStartAddress    = 0;
1467     pDesc->dwMemEndAddress      = 0;
1468     pDesc->dwMemAllocExtra      = 0;
1469     pDesc->pvReserved1          = NULL;
1470     pDesc->pvReserved2          = NULL;
1471     return DS_OK;
1472 }
1473
1474 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
1475 {
1476     IDsDriverImpl *This = (IDsDriverImpl *)iface;
1477     int enable = 0;
1478
1479     TRACE("(%p)\n",iface);
1480     /* make sure the card doesn't start playing before we want it to */
1481 #if 0
1482     if (ioctl(WOutDev[This->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1483         ERR("ioctl failed (%d)\n", errno);
1484         return DSERR_GENERIC;
1485     }
1486 #endif
1487     return DS_OK;
1488 }
1489
1490 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
1491 {
1492     IDsDriverImpl *This = (IDsDriverImpl *)iface;
1493     TRACE("(%p)\n",iface);
1494     if (This->primary) {
1495         ERR("problem with DirectSound: primary not released\n");
1496         return DSERR_GENERIC;
1497     }
1498     return DS_OK;
1499 }
1500
1501 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
1502 {
1503     /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
1504     TRACE("(%p,%p)\n",iface,pCaps);
1505     memset(pCaps, 0, sizeof(*pCaps));
1506     /* FIXME: need to check actual capabilities */
1507     pCaps->dwFlags = DSCAPS_PRIMARYMONO | DSCAPS_PRIMARYSTEREO |
1508         DSCAPS_PRIMARY8BIT | DSCAPS_PRIMARY16BIT;
1509     pCaps->dwPrimaryBuffers = 1;
1510     pCaps->dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
1511     pCaps->dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
1512     /* the other fields only apply to secondary buffers, which we don't support
1513      * (unless we want to mess with wavetable synthesizers and MIDI) */
1514     return DS_OK;
1515 }
1516
1517 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
1518                                                       LPWAVEFORMATEX pwfx,
1519                                                       DWORD dwFlags, DWORD dwCardAddress,
1520                                                       LPDWORD pdwcbBufferSize,
1521                                                       LPBYTE *ppbBuffer,
1522                                                       LPVOID *ppvObj)
1523 {
1524     IDsDriverImpl *This = (IDsDriverImpl *)iface;
1525     IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
1526     HRESULT err;
1527 #if 0
1528     audio_buf_info info;
1529 #endif
1530     int enable = 0;
1531
1532     TRACE("(%p,%p,%lx,%lx)\n",iface,pwfx,dwFlags,dwCardAddress);
1533     /* we only support primary buffers */
1534     if (!(dwFlags & DSBCAPS_PRIMARYBUFFER))
1535         return DSERR_UNSUPPORTED;
1536     if (This->primary)
1537         return DSERR_ALLOCATED;
1538     if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
1539         return DSERR_CONTROLUNAVAIL;
1540
1541     *ippdsdb = HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverBufferImpl));
1542     if (*ippdsdb == NULL)
1543         return DSERR_OUTOFMEMORY;
1544     (*ippdsdb)->lpVtbl  = &dsdbvt;
1545     (*ippdsdb)->ref     = 1;
1546     (*ippdsdb)->drv     = This;
1547
1548     /* check how big the DMA buffer is now */
1549 #if 0
1550     if (ioctl(WOutDev[This->wDevID].unixdev, SNDCTL_DSP_GETOSPACE, &info) < 0) {
1551         ERR("ioctl failed (%d)\n", errno);
1552         HeapFree(GetProcessHeap(),0,*ippdsdb);
1553         *ippdsdb = NULL;
1554         return DSERR_GENERIC;
1555     }
1556 #endif
1557     WOutDev[This->wDevID].maplen =64*1024; /* Map 64 K at a time */
1558
1559 #if 0
1560     (*ippdsdb)->buflen = info.fragstotal * info.fragsize;
1561 #endif
1562     /* map the DMA buffer */
1563     err = DSDB_MapPrimary(*ippdsdb);
1564     if (err != DS_OK) {
1565         HeapFree(GetProcessHeap(),0,*ippdsdb);
1566         *ippdsdb = NULL;
1567         return err;
1568     }
1569
1570     /* primary buffer is ready to go */
1571     *pdwcbBufferSize    = WOutDev[This->wDevID].maplen;
1572     *ppbBuffer          = WOutDev[This->wDevID].mapping;
1573
1574     /* some drivers need some extra nudging after mapping */
1575 #if 0
1576     if (ioctl(WOutDev[This->wDevID].unixdev, SNDCTL_DSP_SETTRIGGER, &enable) < 0) {
1577         ERR("ioctl failed (%d)\n", errno);
1578         return DSERR_GENERIC;
1579     }
1580 #endif
1581
1582     This->primary = *ippdsdb;
1583
1584     return DS_OK;
1585 }
1586
1587 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
1588                                                          PIDSDRIVERBUFFER pBuffer,
1589                                                          LPVOID *ppvObj)
1590 {
1591     /* IDsDriverImpl *This = (IDsDriverImpl *)iface; */
1592     TRACE("(%p,%p): stub\n",iface,pBuffer);
1593     return DSERR_INVALIDCALL;
1594 }
1595
1596 static IDsDriverVtbl dsdvt =
1597 {
1598     IDsDriverImpl_QueryInterface,
1599     IDsDriverImpl_AddRef,
1600     IDsDriverImpl_Release,
1601     IDsDriverImpl_GetDriverDesc,
1602     IDsDriverImpl_Open,
1603     IDsDriverImpl_Close,
1604     IDsDriverImpl_GetCaps,
1605     IDsDriverImpl_CreateSoundBuffer,
1606     IDsDriverImpl_DuplicateSoundBuffer
1607 };
1608
1609 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
1610 {
1611     IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
1612
1613     /* the HAL isn't much better than the HEL if we can't do mmap() */
1614     if (!(WOutDev[wDevID].caps.dwSupport & WAVECAPS_DIRECTSOUND)) {
1615         ERR("DirectSound flag not set\n");
1616         MESSAGE("This sound card's driver does not support direct access\n");
1617         MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
1618         return MMSYSERR_NOTSUPPORTED;
1619     }
1620
1621     *idrv = HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
1622     if (!*idrv)
1623         return MMSYSERR_NOMEM;
1624     (*idrv)->lpVtbl     = &dsdvt;
1625     (*idrv)->ref        = 1;
1626
1627     (*idrv)->wDevID     = wDevID;
1628     (*idrv)->primary    = NULL;
1629     return MMSYSERR_NOERROR;
1630 }
1631
1632 static DWORD wodDsDesc(UINT wDevID, PDSDRIVERDESC desc)
1633 {
1634     memset(desc, 0, sizeof(*desc));
1635     strcpy(desc->szDesc, "Wine LIBAUDIOIO DirectSound Driver");
1636     strcpy(desc->szDrvname, "wineaudioio.drv");
1637     return MMSYSERR_NOERROR;
1638 }
1639
1640 /*======================================================================*
1641  *                  Low level WAVE IN implementation                    *
1642  *======================================================================*/
1643
1644 /**************************************************************************
1645  *                      widGetDevCaps                           [internal]
1646  */
1647 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSW lpCaps, DWORD dwSize)
1648 {
1649     TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1650
1651     if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1652
1653     if (wDevID >= MAX_WAVEINDRV) {
1654         TRACE("MAX_WAVINDRV reached !\n");
1655         return MMSYSERR_BADDEVICEID;
1656     }
1657
1658     memcpy(lpCaps, &WInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1659     return MMSYSERR_NOERROR;
1660 }
1661
1662 /**************************************************************************
1663  *                              widRecorder                     [internal]
1664  */
1665 static  DWORD   CALLBACK        widRecorder(LPVOID pmt)
1666 {
1667     WORD                uDevID = (DWORD)pmt;
1668     WINE_WAVEIN*        wwi = (WINE_WAVEIN*)&WInDev[uDevID];
1669     WAVEHDR*            lpWaveHdr;
1670     DWORD               dwSleepTime;
1671     MSG                 msg;
1672     DWORD               bytesRead;
1673
1674
1675         int fragments;
1676         int fragsize;
1677         int fragstotal;
1678         int bytes;
1679
1680
1681     int xs;
1682
1683         LPVOID          buffer = HeapAlloc(GetProcessHeap(),
1684                                            HEAP_ZERO_MEMORY,
1685                                        wwi->dwFragmentSize);
1686
1687     LPVOID              pOffset = buffer;
1688
1689     PeekMessageA(&msg, 0, 0, 0, 0);
1690     wwi->state = WINE_WS_STOPPED;
1691     wwi->dwTotalRecorded = 0;
1692
1693     SetEvent(wwi->hEvent);
1694
1695
1696         /* make sleep time to be # of ms to output a fragment */
1697     dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->format.wf.nAvgBytesPerSec;
1698     TRACE("sleeptime=%ld ms\n", dwSleepTime);
1699
1700     for (; ; ) {
1701         /* wait for dwSleepTime or an event in thread's queue */
1702         /* FIXME: could improve wait time depending on queue state,
1703          * ie, number of queued fragments
1704          */
1705
1706         if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
1707         {
1708             lpWaveHdr = wwi->lpQueuePtr;
1709
1710             bytes=fragsize=AudioIORecordingAvailable();
1711        fragments=fragstotal=1;
1712
1713             TRACE("info={frag=%d fsize=%d ftotal=%d bytes=%d}\n", fragments, fragsize, fragstotal, bytes);
1714
1715
1716             /* read all the fragments accumulated so far */
1717             while ((fragments > 0) && (wwi->lpQueuePtr))
1718             {
1719                 fragments --;
1720
1721                 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded >= wwi->dwFragmentSize)
1722                 {
1723                     /* directly read fragment in wavehdr */
1724                     bytesRead = AudioIORead(
1725                                      lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
1726                                      wwi->dwFragmentSize);
1727
1728                     TRACE("bytesRead=%ld (direct)\n", bytesRead);
1729                     if (bytesRead != (DWORD) -1)
1730                     {
1731                         /* update number of bytes recorded in current buffer and by this device */
1732                         lpWaveHdr->dwBytesRecorded += bytesRead;
1733                         wwi->dwTotalRecorded       += bytesRead;
1734
1735                         /* buffer is full. notify client */
1736                         if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
1737                         {
1738                             /* must copy the value of next waveHdr, because we have no idea of what
1739                              * will be done with the content of lpWaveHdr in callback
1740                              */
1741                             LPWAVEHDR   lpNext = lpWaveHdr->lpNext;
1742
1743                             lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1744                             lpWaveHdr->dwFlags |=  WHDR_DONE;
1745
1746                             if (LIBAUDIOIO_NotifyClient(uDevID, WIM_DATA,
1747                                                  (DWORD)lpWaveHdr, 0) != MMSYSERR_NOERROR)
1748                             {
1749                                 WARN("can't notify client !\n");
1750                             }
1751                             lpWaveHdr = wwi->lpQueuePtr = lpNext;
1752                         }
1753                     }
1754                 }
1755                 else
1756                 {
1757                     /* read the fragment in a local buffer */
1758                     bytesRead = AudioIORead( buffer, wwi->dwFragmentSize);
1759                     pOffset = buffer;
1760
1761                     TRACE("bytesRead=%ld (local)\n", bytesRead);
1762
1763                     /* copy data in client buffers */
1764                     while (bytesRead != (DWORD) -1 && bytesRead > 0)
1765                     {
1766                         DWORD dwToCopy = min (bytesRead, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
1767
1768                         memcpy(lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
1769                                pOffset,
1770                                dwToCopy);
1771
1772                         /* update number of bytes recorded in current buffer and by this device */
1773                         lpWaveHdr->dwBytesRecorded += dwToCopy;
1774                         wwi->dwTotalRecorded += dwToCopy;
1775                         bytesRead -= dwToCopy;
1776                         pOffset   += dwToCopy;
1777
1778                         /* client buffer is full. notify client */
1779                         if (lpWaveHdr->dwBytesRecorded == lpWaveHdr->dwBufferLength)
1780                         {
1781                             /* must copy the value of next waveHdr, because we have no idea of what
1782                              * will be done with the content of lpWaveHdr in callback
1783                              */
1784                             LPWAVEHDR   lpNext = lpWaveHdr->lpNext;
1785                             TRACE("lpNext=%p\n", lpNext);
1786
1787                             lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1788                             lpWaveHdr->dwFlags |=  WHDR_DONE;
1789
1790                             if (LIBAUDIOIO_NotifyClient(uDevID, WIM_DATA,
1791                                                  (DWORD)lpWaveHdr, 0) != MMSYSERR_NOERROR)
1792                             {
1793                                 WARN("can't notify client !\n");
1794                             }
1795
1796                             wwi->lpQueuePtr = lpWaveHdr = lpNext;
1797                             if (!lpNext && bytesRead) {
1798                                 /* no more buffer to copy data to, but we did read more.
1799                                  * what hasn't been copied will be dropped
1800                                  */
1801                                 WARN("buffer under run! %lu bytes dropped.\n", bytesRead);
1802                                 wwi->lpQueuePtr = NULL;
1803                                 break;
1804                             }
1805                         }
1806                     }
1807                 }
1808             }
1809         }
1810
1811         MsgWaitForMultipleObjects(0, NULL, FALSE, dwSleepTime, QS_POSTMESSAGE);
1812
1813         while (PeekMessageA(&msg, 0, WINE_WM_FIRST, WINE_WM_LAST, PM_REMOVE)) {
1814
1815             TRACE("msg=0x%x wParam=0x%x lParam=0x%lx\n", msg.message, msg.wParam, msg.lParam);
1816             switch (msg.message) {
1817             case WINE_WM_PAUSING:
1818                 wwi->state = WINE_WS_PAUSED;
1819
1820                 AudioIORecordingPause();
1821                 SetEvent(wwi->hEvent);
1822                 break;
1823             case WINE_WM_RESTARTING:
1824             {
1825
1826                 wwi->state = WINE_WS_PLAYING;
1827
1828                 if (wwi->bTriggerSupport)
1829                 {
1830                     /* start the recording */
1831                     AudioIORecordingResume();
1832                 }
1833                 else
1834                 {
1835                     unsigned char data[4];
1836                     /* read 4 bytes to start the recording */
1837                     AudioIORead( data, 4);
1838                 }
1839
1840                 SetEvent(wwi->hEvent);
1841                 break;
1842             }
1843             case WINE_WM_HEADER:
1844                 lpWaveHdr = (LPWAVEHDR)msg.lParam;
1845                 lpWaveHdr->lpNext = 0;
1846
1847                 /* insert buffer at the end of queue */
1848                 {
1849                     LPWAVEHDR*  wh;
1850                     for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1851                     *wh = lpWaveHdr;
1852                 }
1853                 break;
1854             case WINE_WM_RESETTING:
1855                 wwi->state = WINE_WS_STOPPED;
1856                 /* return all buffers to the app */
1857                 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
1858                     TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
1859                     lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1860                     lpWaveHdr->dwFlags |= WHDR_DONE;
1861
1862                     if (LIBAUDIOIO_NotifyClient(uDevID, WIM_DATA,
1863                                          (DWORD)lpWaveHdr, 0) != MMSYSERR_NOERROR) {
1864                         WARN("can't notify client !\n");
1865                     }
1866                 }
1867                 wwi->lpQueuePtr = NULL;
1868                 SetEvent(wwi->hEvent);
1869                 break;
1870             case WINE_WM_CLOSING:
1871                 wwi->hThread = 0;
1872                 wwi->state = WINE_WS_CLOSED;
1873                 SetEvent(wwi->hEvent);
1874                 HeapFree(GetProcessHeap(), 0, buffer);
1875                 ExitThread(0);
1876                 /* shouldn't go here */
1877             default:
1878                 FIXME("unknown message %d\n", msg.message);
1879                 break;
1880             }
1881         }
1882   }
1883     ExitThread(0);
1884     /* just for not generating compilation warnings... should never be executed */
1885     return 0;
1886 }
1887
1888
1889 /**************************************************************************
1890  *                              widOpen                         [internal]
1891  */
1892 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1893 {
1894     int                 audio;
1895     int                 fragment_size;
1896     int                 sample_rate;
1897     int                 format;
1898     int                 dsp_stereo;
1899     WINE_WAVEIN*        wwi;
1900     int                 audio_fragment;
1901
1902     TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1903     if (lpDesc == NULL) {
1904         WARN("Invalid Parameter !\n");
1905         return MMSYSERR_INVALPARAM;
1906     }
1907     if (wDevID >= MAX_WAVEINDRV) return MMSYSERR_BADDEVICEID;
1908
1909     /* only PCM format is supported so far... */
1910     if (lpDesc->lpFormat->wFormatTag != WAVE_FORMAT_PCM ||
1911         lpDesc->lpFormat->nChannels == 0 ||
1912         lpDesc->lpFormat->nSamplesPerSec == 0 ||
1913         (lpDesc->lpFormat->wBitsPerSample!=8 && lpDesc->lpFormat->wBitsPerSample!=16)) {
1914         WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1915              lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1916              lpDesc->lpFormat->nSamplesPerSec);
1917         return WAVERR_BADFORMAT;
1918     }
1919
1920     if (dwFlags & WAVE_FORMAT_QUERY) {
1921         TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%ld !\n",
1922              lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
1923              lpDesc->lpFormat->nSamplesPerSec);
1924         return MMSYSERR_NOERROR;
1925     }
1926
1927     if (access(SOUND_DEV,0) != 0) return MMSYSERR_NOTENABLED;
1928     audio = AudioIOOpenX( O_RDONLY|O_NDELAY, &spec[RECORD],&spec[RECORD]);
1929     if (audio == -1) {
1930         WARN("can't open sound device %s (%s)!\n", SOUND_DEV, strerror(errno));
1931         return MMSYSERR_ALLOCATED;
1932     }
1933     fcntl(audio, F_SETFD, 1); /* set close on exec flag */
1934
1935     wwi = &WInDev[wDevID];
1936     if (wwi->lpQueuePtr) {
1937         WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
1938         wwi->lpQueuePtr = NULL;
1939     }
1940     wwi->unixdev = audio;
1941     wwi->dwTotalRecorded = 0;
1942     wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1943
1944     memcpy(&wwi->waveDesc, lpDesc,           sizeof(WAVEOPENDESC));
1945     memcpy(&wwi->format,   lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
1946
1947     if (wwi->format.wBitsPerSample == 0) {
1948         WARN("Resetting zeroed wBitsPerSample\n");
1949         wwi->format.wBitsPerSample = 8 *
1950             (wwi->format.wf.nAvgBytesPerSec /
1951              wwi->format.wf.nSamplesPerSec) /
1952             wwi->format.wf.nChannels;
1953     }
1954
1955     spec[RECORD].rate=sample_rate = wwi->format.wf.nSamplesPerSec;
1956     dsp_stereo = ((spec[RECORD].channels=wwi->format.wf.nChannels) > 1) ? TRUE : FALSE;
1957     spec[RECORD].precision= wwi->format.wBitsPerSample;
1958     spec[RECORD].type=(spec[RECORD].precision==16)?TYPE_SIGNED:TYPE_UNSIGNED;
1959
1960     /* This is actually hand tuned to work so that my SB Live:
1961      * - does not skip
1962      * - does not buffer too much
1963      * when sending with the Shoutcast winamp plugin
1964      */
1965     /* 7 fragments max, 2^10 = 1024 bytes per fragment */
1966     audio_fragment = 0x0007000A;
1967     fragment_size=4096;
1968     if (fragment_size == -1) {
1969         AudioIOClose();
1970         wwi->unixdev = -1;
1971         return MMSYSERR_NOTENABLED;
1972     }
1973     wwi->dwFragmentSize = fragment_size;
1974
1975     TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%lu, nSamplesPerSec=%lu, nChannels=%u nBlockAlign=%u!\n",
1976           wwi->format.wBitsPerSample, wwi->format.wf.nAvgBytesPerSec,
1977           wwi->format.wf.nSamplesPerSec, wwi->format.wf.nChannels,
1978           wwi->format.wf.nBlockAlign);
1979
1980     wwi->hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
1981     wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
1982     WaitForSingleObject(wwi->hEvent, INFINITE);
1983
1984    if (LIBAUDIOIO_NotifyClient(wDevID, WIM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
1985         WARN("can't notify client !\n");
1986         return MMSYSERR_INVALPARAM;
1987     }
1988     return MMSYSERR_NOERROR;
1989 }
1990
1991 /**************************************************************************
1992  *                              widClose                        [internal]
1993  */
1994 static DWORD widClose(WORD wDevID)
1995 {
1996     WINE_WAVEIN*        wwi;
1997
1998     TRACE("(%u);\n", wDevID);
1999     if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2000         WARN("can't close !\n");
2001         return MMSYSERR_INVALHANDLE;
2002     }
2003
2004     wwi = &WInDev[wDevID];
2005
2006     if (wwi->lpQueuePtr != NULL) {
2007         WARN("still buffers open !\n");
2008         return WAVERR_STILLPLAYING;
2009     }
2010
2011     PostThreadMessageA(wwi->dwThreadID, WINE_WM_CLOSING, 0, 0);
2012     WaitForSingleObject(wwi->hEvent, INFINITE);
2013     CloseHandle(wwi->hEvent);
2014     AudioIOClose();
2015     wwi->unixdev = -1;
2016     wwi->dwFragmentSize = 0;
2017     if (LIBAUDIOIO_NotifyClient(wDevID, WIM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
2018         WARN("can't notify client !\n");
2019         return MMSYSERR_INVALPARAM;
2020     }
2021     return MMSYSERR_NOERROR;
2022 }
2023
2024 /**************************************************************************
2025  *                              widAddBuffer            [internal]
2026  */
2027 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
2028 {
2029     TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
2030
2031     if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2032         WARN("can't do it !\n");
2033         return MMSYSERR_INVALHANDLE;
2034     }
2035     if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
2036         TRACE("never been prepared !\n");
2037         return WAVERR_UNPREPARED;
2038     }
2039     if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
2040         TRACE("header already in use !\n");
2041         return WAVERR_STILLPLAYING;
2042     }
2043
2044     lpWaveHdr->dwFlags |= WHDR_INQUEUE;
2045     lpWaveHdr->dwFlags &= ~WHDR_DONE;
2046     lpWaveHdr->dwBytesRecorded = 0;
2047         lpWaveHdr->lpNext = NULL;
2048
2049     PostThreadMessageA(WInDev[wDevID].dwThreadID, WINE_WM_HEADER, 0, (DWORD)lpWaveHdr);
2050     return MMSYSERR_NOERROR;
2051 }
2052
2053 /**************************************************************************
2054  *                      widStart                                [internal]
2055  */
2056 static DWORD widStart(WORD wDevID)
2057 {
2058     TRACE("(%u);\n", wDevID);
2059     if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2060         WARN("can't start recording !\n");
2061         return MMSYSERR_INVALHANDLE;
2062     }
2063
2064     PostThreadMessageA(WInDev[wDevID].dwThreadID, WINE_WM_RESTARTING, 0, 0);
2065     WaitForSingleObject(WInDev[wDevID].hEvent, INFINITE);
2066     return MMSYSERR_NOERROR;
2067 }
2068
2069 /**************************************************************************
2070  *                      widStop                                 [internal]
2071  */
2072 static DWORD widStop(WORD wDevID)
2073 {
2074     TRACE("(%u);\n", wDevID);
2075     if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2076         WARN("can't stop !\n");
2077         return MMSYSERR_INVALHANDLE;
2078     }
2079     /* FIXME: reset aint stop */
2080     PostThreadMessageA(WInDev[wDevID].dwThreadID, WINE_WM_RESETTING, 0, 0);
2081     WaitForSingleObject(WInDev[wDevID].hEvent, INFINITE);
2082
2083     return MMSYSERR_NOERROR;
2084 }
2085
2086 /**************************************************************************
2087  *                      widReset                                [internal]
2088  */
2089 static DWORD widReset(WORD wDevID)
2090 {
2091     TRACE("(%u);\n", wDevID);
2092     if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2093         WARN("can't reset !\n");
2094         return MMSYSERR_INVALHANDLE;
2095     }
2096     PostThreadMessageA(WInDev[wDevID].dwThreadID, WINE_WM_RESETTING, 0, 0);
2097     WaitForSingleObject(WInDev[wDevID].hEvent, INFINITE);
2098     return MMSYSERR_NOERROR;
2099 }
2100
2101 /**************************************************************************
2102  *                              widGetPosition                  [internal]
2103  */
2104 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
2105 {
2106     WINE_WAVEIN*        wwi;
2107
2108     TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
2109
2110     if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
2111         WARN("can't get pos !\n");
2112         return MMSYSERR_INVALHANDLE;
2113     }
2114     if (lpTime == NULL) return MMSYSERR_INVALPARAM;
2115
2116     wwi = &WInDev[wDevID];
2117
2118     return bytes_to_mmtime(lpTime, wwi->dwTotalRecorded, &wwi->format);
2119 }
2120
2121 /**************************************************************************
2122  *                              widMessage (WINEAUDIOIO.@)
2123  */
2124 DWORD WINAPI LIBAUDIOIO_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2125                             DWORD dwParam1, DWORD dwParam2)
2126 {
2127     TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
2128           wDevID, wMsg, dwUser, dwParam1, dwParam2);
2129
2130     switch (wMsg) {
2131     case DRVM_INIT:
2132     case DRVM_EXIT:
2133     case DRVM_ENABLE:
2134     case DRVM_DISABLE:
2135         /* FIXME: Pretend this is supported */
2136         return 0;
2137     case WIDM_OPEN:             return widOpen       (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
2138     case WIDM_CLOSE:            return widClose      (wDevID);
2139     case WIDM_ADDBUFFER:        return widAddBuffer  (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
2140     case WIDM_PREPARE:          return MMSYSERR_NOTSUPPORTED;
2141     case WIDM_UNPREPARE:        return MMSYSERR_NOTSUPPORTED;
2142     case WIDM_GETDEVCAPS:       return widGetDevCaps (wDevID, (LPWAVEINCAPSW)dwParam1, dwParam2);
2143     case WIDM_GETNUMDEVS:       return wodGetNumDevs ();        /* same number of devices in output as in input */
2144     case WIDM_GETPOS:           return widGetPosition(wDevID, (LPMMTIME)dwParam1, dwParam2);
2145     case WIDM_RESET:            return widReset      (wDevID);
2146     case WIDM_START:            return widStart      (wDevID);
2147     case WIDM_STOP:             return widStop       (wDevID);
2148     case DRV_QUERYDSOUNDIFACE:  return widDsCreate   (wDevID, (PIDSCDRIVER*)dwParam1);
2149     case DRV_QUERYDSOUNDDESC:   return widDsDesc     (wDevID, (PDSDRIVERDESC)dwParam1);
2150     default:
2151         FIXME("unknown message %u!\n", wMsg);
2152     }
2153     return MMSYSERR_NOTSUPPORTED;
2154 }
2155
2156 /*======================================================================*
2157  *                  Low level DSOUND capture implementation             *
2158  *======================================================================*/
2159 static DWORD widDsCreate(UINT wDevID, PIDSCDRIVER* drv)
2160 {
2161     /* we can't perform memory mapping as we don't have a file stream
2162         interface with arts like we do with oss */
2163     MESSAGE("This sound card's driver does not support direct access\n");
2164     MESSAGE("The (slower) DirectSound HEL mode will be used instead.\n");
2165     return MMSYSERR_NOTSUPPORTED;
2166 }
2167
2168 static DWORD widDsDesc(UINT wDevID, PDSDRIVERDESC desc)
2169 {
2170     memset(desc, 0, sizeof(*desc));
2171     strcpy(desc->szDesc, "Wine LIBAUDIOIO DirectSound Driver");
2172     strcpy(desc->szDrvname, "wineaudioio.drv");
2173     return MMSYSERR_NOERROR;
2174 }
2175
2176 #else /* HAVE_LIBAUDIOIO */
2177
2178 /**************************************************************************
2179  *                              wodMessage (WINEAUDIOIO.@)
2180  */
2181 DWORD WINAPI LIBAUDIOIO_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2182                             DWORD dwParam1, DWORD dwParam2)
2183 {
2184     FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2185     return MMSYSERR_NOTENABLED;
2186 }
2187
2188 /**************************************************************************
2189  *                              widMessage (WINEAUDIOIO.@)
2190  */
2191 DWORD WINAPI LIBAUDIOIO_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2192                             DWORD dwParam1, DWORD dwParam2)
2193 {
2194     FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2195     return MMSYSERR_NOTENABLED;
2196 }
2197
2198 #endif /* HAVE_LIBAUDIOIO */