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