1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 * Sample Wine Driver for Open Sound System (featured in Linux and FreeBSD)
5 * Copyright 1994 Martin Ayotte
6 * 1999 Eric Pouech (async playing in waveOut/waveIn)
7 * 2000 Eric Pouech (loops in waveOut)
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
17 /*#define EMULATE_SB16*/
27 #include <sys/ioctl.h>
28 #ifdef HAVE_SYS_MMAN_H
29 # include <sys/mman.h>
34 #include "wine/winuser16.h"
41 #include "debugtools.h"
43 DEFAULT_DEBUG_CHANNEL(wave);
45 /* Allow 1% deviation for sample rates (some ES137x cards) */
46 #define NEAR_MATCH(rate1,rate2) (((100*((int)(rate1)-(int)(rate2)))/(rate1))==0)
50 #define SOUND_DEV "/dev/dsp"
51 #define MIXER_DEV "/dev/mixer"
53 #define MAX_WAVEOUTDRV (1)
54 #define MAX_WAVEINDRV (1)
56 /* state diagram for waveOut writing:
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 * +---------+-------------+---------------+---------------------------------+
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
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)
88 volatile int state; /* one of the WINE_WS_ manifest constants */
89 DWORD dwFragmentSize; /* size of OSS buffer fragment */
90 WAVEOPENDESC waveDesc;
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 */
97 DWORD dwLastFragDone; /* time in ms, when last played fragment will be actually played */
98 DWORD dwPlayedTotal; /* number of bytes played since opening */
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 */
104 /* synchronization stuff */
110 /* DirectSound stuff */
118 DWORD dwFragmentSize; /* OpenSound '/dev/dsp' give us that size */
119 WAVEOPENDESC waveDesc;
121 PCMWAVEFORMAT format;
122 LPWAVEHDR lpQueuePtr;
123 DWORD dwTotalRecorded;
126 /* synchronization stuff */
132 static WINE_WAVEOUT WOutDev [MAX_WAVEOUTDRV];
133 static WINE_WAVEIN WInDev [MAX_WAVEINDRV ];
135 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv);
137 /*======================================================================*
138 * Low level WAVE implemantation *
139 *======================================================================*/
141 LONG OSS_WaveInit(void)
151 /* start with output device */
153 /* FIXME: only one device is supported */
154 memset(&WOutDev[0].caps, 0, sizeof(WOutDev[0].caps));
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));
162 ioctl(audio, SNDCTL_DSP_RESET, 0);
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)
169 WOutDev[0].caps.wMid = 0x0002;
170 WOutDev[0].caps.wPid = 0x0104;
171 strcpy(WOutDev[0].caps.szPname, "SB16 Wave Out");
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");
178 WOutDev[0].caps.vDriverVersion = 0x0100;
179 WOutDev[0].caps.dwFormats = 0x00000000;
180 WOutDev[0].caps.dwSupport = WAVECAPS_VOLUME;
182 IOCTL(audio, SNDCTL_DSP_GETFMTS, mask);
183 TRACE("OSS dsp out mask=%08x\n", mask);
185 /* First bytespersampl, then stereo */
186 bytespersmpl = (IOCTL(audio, SNDCTL_DSP_SAMPLESIZE, samplesize) != 0) ? 1 : 2;
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;
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;
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;
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;
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;
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;
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;
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;
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;
241 TRACE("out dwFormats = %08lX, dwSupport = %08lX\n",
242 WOutDev[0].caps.dwFormats, WOutDev[0].caps.dwSupport);
244 /* then do input device */
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));
254 ioctl(audio, SNDCTL_DSP_RESET, 0);
257 WInDev[0].caps.wMid = 0x0002;
258 WInDev[0].caps.wPid = 0x0004;
259 strcpy(WInDev[0].caps.szPname, "SB16 Wave In");
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");
265 WInDev[0].caps.dwFormats = 0x00000000;
266 WInDev[0].caps.wChannels = (IOCTL(audio, SNDCTL_DSP_STEREO, dsp_stereo) != 0) ? 1 : 2;
268 IOCTL(audio, SNDCTL_DSP_GETFMTS, mask);
269 TRACE("OSS in dsp mask=%08x\n", mask);
271 bytespersmpl = (IOCTL(audio, SNDCTL_DSP_SAMPLESIZE, samplesize) != 0) ? 1 : 2;
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;
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;
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;
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;
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;
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;
312 TRACE("in dwFormats = %08lX\n", WInDev[0].caps.dwFormats);
317 /**************************************************************************
318 * OSS_NotifyClient [internal]
320 static DWORD OSS_NotifyClient(UINT wDevID, WORD wMsg, DWORD dwParam1,
323 TRACE("wDevID = %04X wMsg = %d dwParm1 = %04lX dwParam2 = %04lX\n",wDevID, wMsg, dwParam1, dwParam2);
329 if (wDevID >= MAX_WAVEOUTDRV) return MCIERR_INTERNAL;
331 if (WOutDev[wDevID].wFlags != DCB_NULL &&
332 !DriverCallback(WOutDev[wDevID].waveDesc.dwCallback,
333 WOutDev[wDevID].wFlags,
334 WOutDev[wDevID].waveDesc.hWave,
336 WOutDev[wDevID].waveDesc.dwInstance,
339 WARN("can't notify client !\n");
340 return MMSYSERR_NOERROR;
347 if (wDevID >= MAX_WAVEINDRV) return MCIERR_INTERNAL;
349 if (WInDev[wDevID].wFlags != DCB_NULL &&
350 !DriverCallback(WInDev[wDevID].waveDesc.dwCallback,
351 WInDev[wDevID].wFlags,
352 WInDev[wDevID].waveDesc.hWave,
354 WInDev[wDevID].waveDesc.dwInstance,
357 WARN("can't notify client !\n");
358 return MMSYSERR_NOERROR;
362 FIXME("Unknown CB message %u\n", wMsg);
368 /*======================================================================*
369 * Low level WAVE OUT implemantation *
370 *======================================================================*/
372 /**************************************************************************
373 * wodPlayer_WriteFragments [internal]
375 * wodPlayer helper. Writes as many fragments it can to unixdev.
376 * Returns TRUE in case of buffer underrun.
378 static BOOL wodPlayer_WriteFragments(WINE_WAVEOUT* wwo)
386 if (ioctl(wwo->unixdev, SNDCTL_DSP_GETOSPACE, &info) < 0) {
387 ERR("ioctl failed (%s)\n", strerror(errno));
391 TRACE("Fragments %d/%d\n", info.fragments, info.fragstotal);
393 if (!info.fragments) /* output queue is full, wait a bit */
396 lpWaveHdr = wwo->lpPlayPtr;
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 */
405 return FALSE; /* wait a bit */
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);
414 wwo->lpLoopPtr = lpWaveHdr;
419 lpData = lpWaveHdr->lpData;
421 /* finish current wave hdr ? */
422 if (wwo->dwOffCurrHdr + wwo->dwRemain >= lpWaveHdr->dwBufferLength) {
423 DWORD toWrite = lpWaveHdr->dwBufferLength - wwo->dwOffCurrHdr;
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);
429 if (count > 0 || toWrite == 0) {
430 DWORD tc = GetTickCount();
432 if (wwo->dwLastFragDone /* + guard time ?? */ < tc)
433 wwo->dwLastFragDone = tc;
434 wwo->dwLastFragDone += (toWrite * 1000) / wwo->format.wf.nAvgBytesPerSec;
436 lpWaveHdr->reserved = wwo->dwLastFragDone;
437 TRACE("Tagging hdr %p with %08lx\n", lpWaveHdr, wwo->dwLastFragDone);
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;
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
451 wwo->lpLoopPtr = lpWaveHdr;
453 wwo->lpLoopPtr = NULL;
455 wwo->lpPlayPtr = lpWaveHdr->lpNext;
458 wwo->lpPlayPtr = lpWaveHdr->lpNext;
460 wwo->dwOffCurrHdr = 0;
461 if ((wwo->dwRemain -= count) == 0) {
462 wwo->dwRemain = wwo->dwFragmentSize;
465 continue; /* try to go to use next wavehdr */
467 count = write(wwo->unixdev, lpData + wwo->dwOffCurrHdr, wwo->dwRemain);
468 TRACE("write(%p[%5lu], %5lu) => %d\n", lpData, wwo->dwOffCurrHdr, wwo->dwRemain, count);
470 DWORD tc = GetTickCount();
472 if (wwo->dwLastFragDone /* + guard time ?? */ < tc)
473 wwo->dwLastFragDone = tc;
474 wwo->dwLastFragDone += (wwo->dwRemain * 1000) / wwo->format.wf.nAvgBytesPerSec;
476 TRACE("Tagging frag with %08lx\n", wwo->dwLastFragDone);
478 wwo->dwOffCurrHdr += count;
479 wwo->dwRemain = wwo->dwFragmentSize;
485 /**************************************************************************
486 * wodPlayer_Notify [internal]
488 * wodPlayer helper. Notifies (and remove from queue) all the wavehdr which content
489 * have been played (actually to speaker, not to unixdev fd).
491 static void wodPlayer_Notify(WINE_WAVEOUT* wwo, WORD uDevID, BOOL force)
494 DWORD tc = GetTickCount();
496 while (wwo->lpQueuePtr &&
498 (wwo->lpQueuePtr != wwo->lpPlayPtr && wwo->lpQueuePtr != wwo->lpLoopPtr))) {
499 lpWaveHdr = wwo->lpQueuePtr;
501 if (lpWaveHdr->reserved > tc && !force) break;
503 wwo->dwPlayedTotal += lpWaveHdr->dwBufferLength;
504 wwo->lpQueuePtr = lpWaveHdr->lpNext;
506 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
507 lpWaveHdr->dwFlags |= WHDR_DONE;
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");
516 /**************************************************************************
517 * wodPlayer_Reset [internal]
519 * wodPlayer helper. Resets current output stream.
521 static void wodPlayer_Reset(WINE_WAVEOUT* wwo, WORD uDevID, BOOL reset)
523 /* updates current notify list */
524 wodPlayer_Notify(wwo, uDevID, FALSE);
526 /* flush all possible output */
527 if (ioctl(wwo->unixdev, SNDCTL_DSP_RESET, 0) == -1) {
528 perror("ioctl SNDCTL_DSP_RESET");
530 wwo->state = WINE_WS_STOPPED;
534 wwo->dwOffCurrHdr = 0;
535 wwo->dwRemain = wwo->dwFragmentSize;
537 /* empty notify list */
538 wodPlayer_Notify(wwo, uDevID, TRUE);
540 wwo->lpPlayPtr = wwo->lpQueuePtr = wwo->lpLoopPtr = NULL;
541 wwo->state = WINE_WS_STOPPED;
542 wwo->dwPlayedTotal = 0;
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;
550 /**************************************************************************
551 * wodPlayer [internal]
553 static DWORD CALLBACK wodPlayer(LPVOID pmt)
555 WORD uDevID = (DWORD)pmt;
556 WINE_WAVEOUT* wwo = (WINE_WAVEOUT*)&WOutDev[uDevID];
561 PeekMessageA(&msg, 0, 0, 0, 0);
562 wwo->state = WINE_WS_STOPPED;
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;
570 TRACE("imhere[0]\n");
571 SetEvent(wwo->hEvent);
573 /* make sleep time to be # of ms to output a fragment */
574 dwSleepTime = (wwo->dwFragmentSize * 1000) / wwo->format.wf.nAvgBytesPerSec;
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
581 TRACE("imhere[1]\n");
582 MsgWaitForMultipleObjects(0, NULL, FALSE,
583 (wwo->state == WINE_WS_PLAYING) ?
584 2 * dwSleepTime : /*INFINITE*/100,
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);
595 case WINE_WM_RESTARTING:
596 wwo->state = WINE_WS_PLAYING;
597 SetEvent(wwo->hEvent);
600 lpWaveHdr = (LPWAVEHDR)msg.lParam;
602 /* insert buffer at the end of queue */
605 for (wh = &(wwo->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
608 if (!wwo->lpPlayPtr) wwo->lpPlayPtr = lpWaveHdr;
609 if (wwo->state == WINE_WS_STOPPED)
610 wwo->state = WINE_WS_PLAYING;
612 case WINE_WM_RESETTING:
613 wodPlayer_Reset(wwo, uDevID, TRUE);
614 SetEvent(wwo->hEvent);
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");
620 wwo->state = WINE_WS_CLOSED;
621 SetEvent(wwo->hEvent);
623 /* shouldn't go here */
625 FIXME("unknown message %d\n", msg.message);
629 if (wwo->state == WINE_WS_PLAYING) {
630 wodPlayer_WriteFragments(wwo);
632 wodPlayer_Notify(wwo, uDevID, FALSE);
635 /* just for not generating compilation warnings... should never be executed */
639 /**************************************************************************
640 * wodGetDevCaps [internal]
642 static DWORD wodGetDevCaps(WORD wDevID, LPWAVEOUTCAPSA lpCaps, DWORD dwSize)
644 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
646 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
648 if (wDevID >= MAX_WAVEOUTDRV) {
649 TRACE("MAX_WAVOUTDRV reached !\n");
650 return MMSYSERR_BADDEVICEID;
653 memcpy(lpCaps, &WOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
654 return MMSYSERR_NOERROR;
657 /**************************************************************************
660 static DWORD wodOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
670 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
671 if (lpDesc == NULL) {
672 WARN("Invalid Parameter !\n");
673 return MMSYSERR_INVALPARAM;
675 if (wDevID >= MAX_WAVEOUTDRV) {
676 TRACE("MAX_WAVOUTDRV reached !\n");
677 return MMSYSERR_BADDEVICEID;
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;
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;
697 wwo = &WOutDev[wDevID];
699 if ((dwFlags & WAVE_DIRECTSOUND) && !(wwo->caps.dwSupport & WAVECAPS_DIRECTSOUND))
700 /* not supported, ignore it */
701 dwFlags &= ~WAVE_DIRECTSOUND;
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);
710 audio = open(SOUND_DEV, O_WRONLY|O_NDELAY, 0);
712 WARN("can't open (%s)!\n", strerror(errno));
713 return MMSYSERR_ALLOCATED;
715 fcntl(audio, F_SETFD, 1); /* set close on exec flag */
716 wwo->unixdev = audio;
717 wwo->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
719 memcpy(&wwo->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
720 memcpy(&wwo->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
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;
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;
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
738 /* 2^10=1024 bytes per fragment, 16 fragments max */
739 audio_fragment = 0x000F000A;
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;
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);
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);
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");
768 return MMSYSERR_NOTENABLED;
770 wwo->dwFragmentSize = fragment_size;
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);
777 wwo->hEvent = INVALID_HANDLE_VALUE;
778 wwo->hThread = INVALID_HANDLE_VALUE;
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");
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);
792 if (OSS_NotifyClient(wDevID, WOM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
793 WARN("can't notify client !\n");
794 return MMSYSERR_INVALPARAM;
796 return MMSYSERR_NOERROR;
799 /**************************************************************************
800 * wodClose [internal]
802 static DWORD wodClose(WORD wDevID)
804 DWORD ret = MMSYSERR_NOERROR;
807 TRACE("(%u);\n", wDevID);
809 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
810 WARN("bad device ID !\n");
811 return MMSYSERR_BADDEVICEID;
814 wwo = &WOutDev[wDevID];
815 if (wwo->lpQueuePtr) {
816 WARN("buffers still playing !\n");
817 ret = WAVERR_STILLPLAYING;
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);
826 munmap(wwo->mapping, wwo->maplen);
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;
841 /**************************************************************************
842 * wodWrite [internal]
845 static DWORD wodWrite(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
847 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
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;
855 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
856 return WAVERR_UNPREPARED;
858 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
859 return WAVERR_STILLPLAYING;
861 lpWaveHdr->dwFlags &= ~WHDR_DONE;
862 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
863 lpWaveHdr->lpNext = 0;
865 TRACE("imhere[3-HEADER]\n");
866 PostThreadMessageA(WOutDev[wDevID].dwThreadID, WINE_WM_HEADER, 0, (DWORD)lpWaveHdr);
868 return MMSYSERR_NOERROR;
871 /**************************************************************************
872 * wodPrepare [internal]
874 static DWORD wodPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
876 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
878 if (wDevID >= MAX_WAVEOUTDRV) {
879 WARN("bad device ID !\n");
880 return MMSYSERR_BADDEVICEID;
883 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
884 return WAVERR_STILLPLAYING;
886 lpWaveHdr->dwFlags |= WHDR_PREPARED;
887 lpWaveHdr->dwFlags &= ~WHDR_DONE;
888 return MMSYSERR_NOERROR;
891 /**************************************************************************
892 * wodUnprepare [internal]
894 static DWORD wodUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
896 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
898 if (wDevID >= MAX_WAVEOUTDRV) {
899 WARN("bad device ID !\n");
900 return MMSYSERR_BADDEVICEID;
903 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
904 return WAVERR_STILLPLAYING;
906 lpWaveHdr->dwFlags &= ~WHDR_PREPARED;
907 lpWaveHdr->dwFlags |= WHDR_DONE;
909 return MMSYSERR_NOERROR;
912 /**************************************************************************
913 * wodPause [internal]
915 static DWORD wodPause(WORD wDevID)
917 TRACE("(%u);!\n", wDevID);
919 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
920 WARN("bad device ID !\n");
921 return MMSYSERR_BADDEVICEID;
924 TRACE("imhere[3-PAUSING]\n");
925 PostThreadMessageA(WOutDev[wDevID].dwThreadID, WINE_WM_PAUSING, 0, 0);
926 WaitForSingleObject(WOutDev[wDevID].hEvent, INFINITE);
928 return MMSYSERR_NOERROR;
931 /**************************************************************************
932 * wodRestart [internal]
934 static DWORD wodRestart(WORD wDevID)
936 TRACE("(%u);\n", wDevID);
938 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
939 WARN("bad device ID !\n");
940 return MMSYSERR_BADDEVICEID;
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);
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;
957 return MMSYSERR_NOERROR;
960 /**************************************************************************
961 * wodReset [internal]
963 static DWORD wodReset(WORD wDevID)
965 TRACE("(%u);\n", wDevID);
967 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
968 WARN("bad device ID !\n");
969 return MMSYSERR_BADDEVICEID;
972 TRACE("imhere[3-RESET]\n");
973 PostThreadMessageA(WOutDev[wDevID].dwThreadID, WINE_WM_RESETTING, 0, 0);
974 WaitForSingleObject(WOutDev[wDevID].hEvent, INFINITE);
976 return MMSYSERR_NOERROR;
980 /**************************************************************************
981 * wodGetPosition [internal]
983 static DWORD wodGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
989 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
991 if (wDevID >= MAX_WAVEOUTDRV || WOutDev[wDevID].unixdev == -1) {
992 WARN("bad device ID !\n");
993 return MMSYSERR_BADDEVICEID;
996 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
998 wwo = &WOutDev[wDevID];
999 val = wwo->dwPlayedTotal;
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);
1007 switch (lpTime->wType) {
1010 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1013 lpTime->u.sample = val * 8 / wwo->format.wBitsPerSample;
1014 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
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);
1031 FIXME("Format %d not supported ! use TIME_MS !\n", lpTime->wType);
1032 lpTime->wType = TIME_MS;
1034 lpTime->u.ms = val / (wwo->format.wf.nAvgBytesPerSec / 1000);
1035 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
1038 return MMSYSERR_NOERROR;
1041 /**************************************************************************
1042 * wodGetVolume [internal]
1044 static DWORD wodGetVolume(WORD wDevID, LPDWORD lpdwVol)
1050 TRACE("(%u, %p);\n", wDevID, lpdwVol);
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;
1058 if (ioctl(mixer, SOUND_MIXER_READ_PCM, &volume) == -1) {
1059 WARN("unable read mixer !\n");
1060 return MMSYSERR_NOTENABLED;
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;
1071 /**************************************************************************
1072 * wodSetVolume [internal]
1074 static DWORD wodSetVolume(WORD wDevID, DWORD dwParam)
1080 TRACE("(%u, %08lX);\n", wDevID, dwParam);
1082 left = (LOWORD(dwParam) * 100) / 0xFFFFl;
1083 right = (HIWORD(dwParam) * 100) / 0xFFFFl;
1084 volume = left + (right << 8);
1086 if ((mixer = open(MIXER_DEV, O_WRONLY|O_NDELAY)) < 0) {
1087 WARN("mixer device not available !\n");
1088 return MMSYSERR_NOTENABLED;
1090 if (ioctl(mixer, SOUND_MIXER_WRITE_PCM, &volume) == -1) {
1091 WARN("unable set mixer !\n");
1092 return MMSYSERR_NOTENABLED;
1094 TRACE("volume=%04x\n", (unsigned)volume);
1097 return MMSYSERR_NOERROR;
1100 /**************************************************************************
1101 * wodGetNumDevs [internal]
1103 static DWORD wodGetNumDevs(void)
1107 /* FIXME: For now, only one sound device (SOUND_DEV) is allowed */
1108 int audio = open(SOUND_DEV, O_WRONLY|O_NDELAY, 0);
1119 /**************************************************************************
1120 * OSS_wodMessage [sample driver]
1122 DWORD WINAPI OSS_wodMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
1123 DWORD dwParam1, DWORD dwParam2)
1125 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
1126 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1133 /* FIXME: Pretend this is supported */
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);
1155 FIXME("unknown message %d!\n", wMsg);
1157 return MMSYSERR_NOTSUPPORTED;
1160 /*======================================================================*
1161 * Low level DSOUND implementation *
1162 *======================================================================*/
1164 typedef struct IDsDriverImpl IDsDriverImpl;
1165 typedef struct IDsDriverBufferImpl IDsDriverBufferImpl;
1167 struct IDsDriverImpl
1169 /* IUnknown fields */
1170 ICOM_VFIELD(IDsDriver);
1172 /* IDsDriverImpl fields */
1174 IDsDriverBufferImpl*primary;
1177 struct IDsDriverBufferImpl
1179 /* IUnknown fields */
1180 ICOM_VFIELD(IDsDriverBuffer);
1182 /* IDsDriverBufferImpl fields */
1187 static HRESULT DSDB_MapPrimary(IDsDriverBufferImpl *dsdb)
1189 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
1190 if (!wwo->mapping) {
1191 wwo->mapping = mmap(NULL, wwo->maplen, PROT_WRITE, MAP_SHARED,
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;
1197 TRACE("(%p): sound device has been mapped for direct access at %p, size=%ld\n", dsdb, wwo->mapping, wwo->maplen);
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 */
1205 static HRESULT DSDB_UnmapPrimary(IDsDriverBufferImpl *dsdb)
1207 WINE_WAVEOUT *wwo = &(WOutDev[dsdb->drv->wDevID]);
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;
1213 wwo->mapping = NULL;
1214 TRACE("(%p): sound device unmapped\n", dsdb);
1219 static HRESULT WINAPI IDsDriverBufferImpl_QueryInterface(PIDSDRIVERBUFFER iface, REFIID riid, LPVOID *ppobj)
1221 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1222 FIXME("(): stub!\n");
1223 return DSERR_UNSUPPORTED;
1226 static ULONG WINAPI IDsDriverBufferImpl_AddRef(PIDSDRIVERBUFFER iface)
1228 ICOM_THIS(IDsDriverBufferImpl,iface);
1233 static ULONG WINAPI IDsDriverBufferImpl_Release(PIDSDRIVERBUFFER iface)
1235 ICOM_THIS(IDsDriverBufferImpl,iface);
1238 if (This == This->drv->primary)
1239 This->drv->primary = NULL;
1240 DSDB_UnmapPrimary(This);
1241 HeapFree(GetProcessHeap(),0,This);
1245 static HRESULT WINAPI IDsDriverBufferImpl_Lock(PIDSDRIVERBUFFER iface,
1246 LPVOID*ppvAudio1,LPDWORD pdwLen1,
1247 LPVOID*ppvAudio2,LPDWORD pdwLen2,
1248 DWORD dwWritePosition,DWORD dwWriteLen,
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;
1258 static HRESULT WINAPI IDsDriverBufferImpl_Unlock(PIDSDRIVERBUFFER iface,
1259 LPVOID pvAudio1,DWORD dwLen1,
1260 LPVOID pvAudio2,DWORD dwLen2)
1262 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1263 TRACE("(%p): stub\n",iface);
1264 return DSERR_UNSUPPORTED;
1267 static HRESULT WINAPI IDsDriverBufferImpl_SetFormat(PIDSDRIVERBUFFER iface,
1268 LPWAVEFORMATEX pwfx)
1270 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
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;
1282 static HRESULT WINAPI IDsDriverBufferImpl_SetFrequency(PIDSDRIVERBUFFER iface, DWORD dwFreq)
1284 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1285 TRACE("(%p,%ld): stub\n",iface,dwFreq);
1286 return DSERR_UNSUPPORTED;
1289 static HRESULT WINAPI IDsDriverBufferImpl_SetVolumePan(PIDSDRIVERBUFFER iface, PDSVOLUMEPAN pVolPan)
1291 /* ICOM_THIS(IDsDriverBufferImpl,iface); */
1292 FIXME("(%p,%p): stub!\n",iface,pVolPan);
1293 return DSERR_UNSUPPORTED;
1296 static HRESULT WINAPI IDsDriverBufferImpl_SetPosition(PIDSDRIVERBUFFER iface, DWORD dwNewPos)
1298 /* ICOM_THIS(IDsDriverImpl,iface); */
1299 TRACE("(%p,%ld): stub\n",iface,dwNewPos);
1300 return DSERR_UNSUPPORTED;
1303 static HRESULT WINAPI IDsDriverBufferImpl_GetPosition(PIDSDRIVERBUFFER iface,
1304 LPDWORD lpdwPlay, LPDWORD lpdwWrite)
1306 ICOM_THIS(IDsDriverBufferImpl,iface);
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;
1315 ptr = info.ptr & ~3; /* align the pointer, just in case */
1316 if (lpdwPlay) *lpdwPlay = ptr;
1318 /* add some safety margin (not strictly necessary, but...) */
1319 *lpdwWrite = ptr + 32;
1320 while (*lpdwWrite > This->buflen)
1321 *lpdwWrite -= This->buflen;
1323 TRACE("playpos=%ld, writepos=%ld\n", lpdwPlay?*lpdwPlay:0, lpdwWrite?*lpdwWrite:0);
1324 return DSERR_UNSUPPORTED;
1327 static HRESULT WINAPI IDsDriverBufferImpl_Play(PIDSDRIVERBUFFER iface, DWORD dwRes1, DWORD dwRes2, DWORD dwFlags)
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;
1339 static HRESULT WINAPI IDsDriverBufferImpl_Stop(PIDSDRIVERBUFFER iface)
1341 ICOM_THIS(IDsDriverBufferImpl,iface);
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;
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;
1359 static ICOM_VTABLE(IDsDriverBuffer) dsdbvt =
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
1376 static HRESULT WINAPI IDsDriverImpl_QueryInterface(PIDSDRIVER iface, REFIID riid, LPVOID *ppobj)
1378 /* ICOM_THIS(IDsDriverImpl,iface); */
1379 FIXME("(%p): stub!\n",iface);
1380 return DSERR_UNSUPPORTED;
1383 static ULONG WINAPI IDsDriverImpl_AddRef(PIDSDRIVER iface)
1385 ICOM_THIS(IDsDriverImpl,iface);
1390 static ULONG WINAPI IDsDriverImpl_Release(PIDSDRIVER iface)
1392 ICOM_THIS(IDsDriverImpl,iface);
1395 HeapFree(GetProcessHeap(),0,This);
1399 static HRESULT WINAPI IDsDriverImpl_GetDriverDesc(PIDSDRIVER iface, PDSDRIVERDESC pDesc)
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;
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;
1421 static HRESULT WINAPI IDsDriverImpl_Open(PIDSDRIVER iface)
1423 ICOM_THIS(IDsDriverImpl,iface);
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;
1435 static HRESULT WINAPI IDsDriverImpl_Close(PIDSDRIVER iface)
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;
1446 static HRESULT WINAPI IDsDriverImpl_GetCaps(PIDSDRIVER iface, PDSDRIVERCAPS pCaps)
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) */
1460 static HRESULT WINAPI IDsDriverImpl_CreateSoundBuffer(PIDSDRIVER iface,
1461 LPWAVEFORMATEX pwfx,
1462 DWORD dwFlags, DWORD dwCardAddress,
1463 LPDWORD pdwcbBufferSize,
1467 ICOM_THIS(IDsDriverImpl,iface);
1468 IDsDriverBufferImpl** ippdsdb = (IDsDriverBufferImpl**)ppvObj;
1470 audio_buf_info info;
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;
1477 return DSERR_ALLOCATED;
1478 if (dwFlags & (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN))
1479 return DSERR_CONTROLUNAVAIL;
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;
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);
1493 return DSERR_GENERIC;
1495 WOutDev[This->wDevID].maplen = (*ippdsdb)->buflen = info.fragstotal * info.fragsize;
1497 /* map the DMA buffer */
1498 err = DSDB_MapPrimary(*ippdsdb);
1500 HeapFree(GetProcessHeap(),0,*ippdsdb);
1505 /* primary buffer is ready to go */
1506 *pdwcbBufferSize = WOutDev[This->wDevID].maplen;
1507 *ppbBuffer = WOutDev[This->wDevID].mapping;
1509 This->primary = *ippdsdb;
1514 static HRESULT WINAPI IDsDriverImpl_DuplicateSoundBuffer(PIDSDRIVER iface,
1515 PIDSDRIVERBUFFER pBuffer,
1518 /* ICOM_THIS(IDsDriverImpl,iface); */
1519 TRACE("(%p,%p): stub\n",iface,pBuffer);
1520 return DSERR_INVALIDCALL;
1523 static ICOM_VTABLE(IDsDriver) dsdvt =
1525 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1526 IDsDriverImpl_QueryInterface,
1527 IDsDriverImpl_AddRef,
1528 IDsDriverImpl_Release,
1529 IDsDriverImpl_GetDriverDesc,
1531 IDsDriverImpl_Close,
1532 IDsDriverImpl_GetCaps,
1533 IDsDriverImpl_CreateSoundBuffer,
1534 IDsDriverImpl_DuplicateSoundBuffer
1537 static DWORD wodDsCreate(UINT wDevID, PIDSDRIVER* drv)
1539 IDsDriverImpl** idrv = (IDsDriverImpl**)drv;
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;
1549 *idrv = (IDsDriverImpl*)HeapAlloc(GetProcessHeap(),0,sizeof(IDsDriverImpl));
1551 return MMSYSERR_NOMEM;
1552 ICOM_VTBL(*idrv) = &dsdvt;
1555 (*idrv)->wDevID = wDevID;
1556 (*idrv)->primary = NULL;
1557 return MMSYSERR_NOERROR;
1560 /*======================================================================*
1561 * Low level WAVE IN implemantation *
1562 *======================================================================*/
1564 /**************************************************************************
1565 * widGetDevCaps [internal]
1567 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSA lpCaps, DWORD dwSize)
1569 TRACE("(%u, %p, %lu);\n", wDevID, lpCaps, dwSize);
1571 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
1573 if (wDevID >= MAX_WAVEINDRV) {
1574 TRACE("MAX_WAVINDRV reached !\n");
1575 return MMSYSERR_BADDEVICEID;
1578 memcpy(lpCaps, &WInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1579 return MMSYSERR_NOERROR;
1582 /**************************************************************************
1583 * widRecorder [internal]
1585 static DWORD CALLBACK widRecorder(LPVOID pmt)
1587 WORD uDevID = (DWORD)pmt;
1588 WINE_WAVEIN* wwi = (WINE_WAVEIN*)&WInDev[uDevID];
1594 PeekMessageA(&msg, 0, 0, 0, 0);
1595 wwi->state = WINE_WS_STOPPED;
1596 wwi->dwTotalRecorded = 0;
1598 TRACE("imhere[0]\n");
1599 SetEvent(wwi->hEvent);
1601 /* make sleep time to be # of ms to output a fragment */
1602 dwSleepTime = (wwi->dwFragmentSize * 1000) / wwi->format.wf.nAvgBytesPerSec;
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
1609 TRACE("imhere[1]\n");
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);
1616 bytesRead = read(wwi->unixdev, lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
1617 lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
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;
1627 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
1628 lpWaveHdr->dwFlags |= WHDR_DONE;
1630 if (OSS_NotifyClient(uDevID, WIM_DATA, (DWORD)lpWaveHdr, lpWaveHdr->dwBytesRecorded) != MMSYSERR_NOERROR) {
1631 WARN("can't notify client !\n");
1636 TRACE("No data (%s)\n", strerror(errno));
1640 MsgWaitForMultipleObjects(0, NULL, FALSE, dwSleepTime, QS_POSTMESSAGE);
1641 TRACE("imhere[2] (q=%p)\n", wwi->lpQueuePtr);
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);
1649 case WINE_WM_RESTARTING:
1650 wwi->state = WINE_WS_PLAYING;
1651 SetEvent(wwi->hEvent);
1653 case WINE_WM_HEADER:
1654 lpWaveHdr = (LPWAVEHDR)msg.lParam;
1655 lpWaveHdr->lpNext = 0;
1657 /* insert buffer at the end of queue */
1660 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
1663 if (wwi->state == WINE_WS_STOPPED)
1664 wwi->state = WINE_WS_PLAYING;
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;
1674 if (OSS_NotifyClient(uDevID, WIM_DATA, (DWORD)lpWaveHdr,
1675 lpWaveHdr->dwBytesRecorded) != MMSYSERR_NOERROR) {
1676 WARN("can't notify client !\n");
1679 wwi->lpQueuePtr = NULL;
1680 SetEvent(wwi->hEvent);
1682 case WINE_WM_CLOSING:
1684 wwi->state = WINE_WS_CLOSED;
1685 SetEvent(wwi->hEvent);
1687 /* shouldn't go here */
1689 FIXME("unknown message %d\n", msg.message);
1695 /* just for not generating compilation warnings... should never be executed */
1700 /**************************************************************************
1701 * widOpen [internal]
1703 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
1712 TRACE("(%u, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1713 if (lpDesc == NULL) {
1714 WARN("Invalid Parameter !\n");
1715 return MMSYSERR_INVALPARAM;
1717 if (wDevID >= MAX_WAVEINDRV) return MMSYSERR_BADDEVICEID;
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;
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;
1736 if (access(SOUND_DEV,0) != 0) return MMSYSERR_NOTENABLED;
1737 audio = open(SOUND_DEV, O_RDONLY|O_NDELAY, 0);
1739 WARN("can't open (%s)!\n", strerror(errno));
1740 return MMSYSERR_ALLOCATED;
1742 fcntl(audio, F_SETFD, 1); /* set close on exec flag */
1744 wwi = &WInDev[wDevID];
1745 if (wwi->lpQueuePtr) {
1746 WARN("Should have an empty queue (%p)\n", wwi->lpQueuePtr);
1747 wwi->lpQueuePtr = NULL;
1749 wwi->unixdev = audio;
1750 wwi->dwTotalRecorded = 0;
1751 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1753 memcpy(&wwi->waveDesc, lpDesc, sizeof(WAVEOPENDESC));
1754 memcpy(&wwi->format, lpDesc->lpFormat, sizeof(PCMWAVEFORMAT));
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;
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;
1768 IOCTL(audio, SNDCTL_DSP_SETFMT, format);
1769 IOCTL(audio, SNDCTL_DSP_STEREO, dsp_stereo);
1770 IOCTL(audio, SNDCTL_DSP_SPEED, sample_rate);
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);
1783 IOCTL(audio, SNDCTL_DSP_GETBLKSIZE, fragment_size);
1784 if (fragment_size == -1) {
1785 WARN("IOCTL can't 'SNDCTL_DSP_GETBLKSIZE' !\n");
1788 return MMSYSERR_NOTENABLED;
1790 wwi->dwFragmentSize = fragment_size;
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);
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);
1801 if (OSS_NotifyClient(wDevID, WIM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
1802 WARN("can't notify client !\n");
1803 return MMSYSERR_INVALPARAM;
1805 return MMSYSERR_NOERROR;
1808 /**************************************************************************
1809 * widClose [internal]
1811 static DWORD widClose(WORD wDevID)
1815 TRACE("(%u);\n", wDevID);
1816 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
1817 WARN("can't close !\n");
1818 return MMSYSERR_INVALHANDLE;
1821 wwi = &WInDev[wDevID];
1823 if (wwi->lpQueuePtr != NULL) {
1824 WARN("still buffers open !\n");
1825 return WAVERR_STILLPLAYING;
1828 PostThreadMessageA(wwi->dwThreadID, WINE_WM_CLOSING, 0, 0);
1829 WaitForSingleObject(wwi->hEvent, INFINITE);
1830 CloseHandle(wwi->hEvent);
1831 close(wwi->unixdev);
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;
1838 return MMSYSERR_NOERROR;
1841 /**************************************************************************
1842 * widAddBuffer [internal]
1844 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1846 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1848 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
1849 WARN("can't do it !\n");
1850 return MMSYSERR_INVALHANDLE;
1852 if (!(lpWaveHdr->dwFlags & WHDR_PREPARED)) {
1853 TRACE("never been prepared !\n");
1854 return WAVERR_UNPREPARED;
1856 if (lpWaveHdr->dwFlags & WHDR_INQUEUE) {
1857 TRACE("header already in use !\n");
1858 return WAVERR_STILLPLAYING;
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;
1868 /**************************************************************************
1869 * widPrepare [internal]
1871 static DWORD widPrepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1873 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1875 if (wDevID >= MAX_WAVEINDRV) return MMSYSERR_INVALHANDLE;
1877 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1878 return WAVERR_STILLPLAYING;
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;
1887 /**************************************************************************
1888 * widUnprepare [internal]
1890 static DWORD widUnprepare(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
1892 TRACE("(%u, %p, %08lX);\n", wDevID, lpWaveHdr, dwSize);
1893 if (wDevID >= MAX_WAVEINDRV) return MMSYSERR_INVALHANDLE;
1895 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
1896 return WAVERR_STILLPLAYING;
1898 lpWaveHdr->dwFlags &= ~(WHDR_PREPARED|WHDR_INQUEUE);
1899 lpWaveHdr->dwFlags |= WHDR_DONE;
1901 return MMSYSERR_NOERROR;
1904 /**************************************************************************
1905 * widStart [internal]
1907 static DWORD widStart(WORD wDevID)
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;
1915 PostThreadMessageA(WInDev[wDevID].dwThreadID, WINE_WM_RESTARTING, 0, 0);
1916 WaitForSingleObject(WInDev[wDevID].hEvent, INFINITE);
1918 return MMSYSERR_NOERROR;
1921 /**************************************************************************
1922 * widStop [internal]
1924 static DWORD widStop(WORD wDevID)
1926 TRACE("(%u);\n", wDevID);
1927 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
1928 WARN("can't stop !\n");
1929 return MMSYSERR_INVALHANDLE;
1931 /* FIXME: reset aint stop */
1932 PostThreadMessageA(WInDev[wDevID].dwThreadID, WINE_WM_RESETTING, 0, 0);
1933 WaitForSingleObject(WInDev[wDevID].hEvent, INFINITE);
1935 return MMSYSERR_NOERROR;
1938 /**************************************************************************
1939 * widReset [internal]
1941 static DWORD widReset(WORD wDevID)
1943 TRACE("(%u);\n", wDevID);
1944 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
1945 WARN("can't reset !\n");
1946 return MMSYSERR_INVALHANDLE;
1948 PostThreadMessageA(WInDev[wDevID].dwThreadID, WINE_WM_RESETTING, 0, 0);
1949 WaitForSingleObject(WInDev[wDevID].hEvent, INFINITE);
1950 return MMSYSERR_NOERROR;
1953 /**************************************************************************
1954 * widGetPosition [internal]
1956 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
1961 TRACE("(%u, %p, %lu);\n", wDevID, lpTime, uSize);
1963 if (wDevID >= MAX_WAVEINDRV || WInDev[wDevID].unixdev == -1) {
1964 WARN("can't get pos !\n");
1965 return MMSYSERR_INVALHANDLE;
1967 if (lpTime == NULL) return MMSYSERR_INVALPARAM;
1969 wwi = &WInDev[wDevID];
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) {
1978 lpTime->u.cb = wwi->dwTotalRecorded;
1979 TRACE("TIME_BYTES=%lu\n", lpTime->u.cb);
1982 lpTime->u.sample = wwi->dwTotalRecorded * 8 /
1983 wwi->format.wBitsPerSample;
1984 TRACE("TIME_SAMPLES=%lu\n", lpTime->u.sample);
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);
2002 lpTime->u.ms = wwi->dwTotalRecorded /
2003 (wwi->format.wf.nAvgBytesPerSec / 1000);
2004 TRACE("TIME_MS=%lu\n", lpTime->u.ms);
2007 FIXME("format not supported (%u) ! use TIME_MS !\n", lpTime->wType);
2008 lpTime->wType = TIME_MS;
2010 return MMSYSERR_NOERROR;
2013 /**************************************************************************
2014 * OSS_widMessage [sample driver]
2016 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2017 DWORD dwParam1, DWORD dwParam2)
2019 TRACE("(%u, %04X, %08lX, %08lX, %08lX);\n",
2020 wDevID, wMsg, dwUser, dwParam1, dwParam2);
2027 /* FIXME: Pretend this is supported */
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);
2041 FIXME("unknown message %u!\n", wMsg);
2043 return MMSYSERR_NOTSUPPORTED;
2046 #else /* !HAVE_OSS */
2048 /**************************************************************************
2049 * OSS_wodMessage [sample driver]
2051 DWORD WINAPI OSS_wodMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2052 DWORD dwParam1, DWORD dwParam2)
2054 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2055 return MMSYSERR_NOTENABLED;
2058 /**************************************************************************
2059 * OSS_widMessage [sample driver]
2061 DWORD WINAPI OSS_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
2062 DWORD dwParam1, DWORD dwParam2)
2064 FIXME("(%u, %04X, %08lX, %08lX, %08lX):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
2065 return MMSYSERR_NOTENABLED;
2068 #endif /* HAVE_OSS */