2 * Sample Wine Driver for Advanced Linux Sound System (ALSA)
3 * Based on version <final> of the ALSA API
5 * Copyright 2002 Eric Pouech
6 * 2002 Marco Pietrobono
7 * 2003 Christian Costa : WaveIn support
8 * 2006-2007 Maarten Lankhorst
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 /*======================================================================*
26 * Low level WAVE IN implementation *
27 *======================================================================*/
30 #include "wine/port.h"
42 #ifdef HAVE_SYS_IOCTL_H
43 # include <sys/ioctl.h>
45 #ifdef HAVE_SYS_MMAN_H
46 # include <sys/mman.h>
56 #include "wine/library.h"
57 #include "wine/unicode.h"
58 #include "wine/debug.h"
60 WINE_DEFAULT_DEBUG_CHANNEL(wave);
65 DWORD ALSA_WidNumMallocedDevs;
66 DWORD ALSA_WidNumDevs;
68 /**************************************************************************
69 * widNotifyClient [internal]
71 static DWORD widNotifyClient(WINE_WAVEDEV* wwi, WORD wMsg, DWORD dwParam1, DWORD dwParam2)
73 TRACE("wMsg = 0x%04x dwParm1 = %04X dwParam2 = %04X\n", wMsg, dwParam1, dwParam2);
79 if (wwi->wFlags != DCB_NULL &&
80 !DriverCallback(wwi->waveDesc.dwCallback, wwi->wFlags, (HDRVR)wwi->waveDesc.hWave,
81 wMsg, wwi->waveDesc.dwInstance, dwParam1, dwParam2)) {
82 WARN("can't notify client !\n");
83 return MMSYSERR_ERROR;
87 FIXME("Unknown callback message %u\n", wMsg);
88 return MMSYSERR_INVALPARAM;
90 return MMSYSERR_NOERROR;
93 /**************************************************************************
94 * widGetDevCaps [internal]
96 static DWORD widGetDevCaps(WORD wDevID, LPWAVEINCAPSW lpCaps, DWORD dwSize)
98 TRACE("(%u, %p, %u);\n", wDevID, lpCaps, dwSize);
100 if (lpCaps == NULL) return MMSYSERR_NOTENABLED;
102 if (wDevID >= ALSA_WidNumDevs) {
103 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
104 return MMSYSERR_BADDEVICEID;
107 memcpy(lpCaps, &WInDev[wDevID].incaps, min(dwSize, sizeof(*lpCaps)));
108 return MMSYSERR_NOERROR;
111 /**************************************************************************
112 * widRecorder_ReadHeaders [internal]
114 static void widRecorder_ReadHeaders(WINE_WAVEDEV * wwi)
116 enum win_wm_message tmp_msg;
121 while (ALSA_RetrieveRingMessage(&wwi->msgRing, &tmp_msg, &tmp_param, &tmp_ev)) {
122 if (tmp_msg == WINE_WM_HEADER) {
124 lpWaveHdr = (LPWAVEHDR)tmp_param;
125 lpWaveHdr->lpNext = 0;
127 if (wwi->lpQueuePtr == 0)
128 wwi->lpQueuePtr = lpWaveHdr;
130 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
134 ERR("should only have headers left\n");
139 /**************************************************************************
140 * widRecorder [internal]
142 static DWORD CALLBACK widRecorder(LPVOID pmt)
144 WORD uDevID = (DWORD)pmt;
145 WINE_WAVEDEV* wwi = (WINE_WAVEDEV*)&WInDev[uDevID];
149 enum win_wm_message msg;
152 DWORD frames_per_period;
154 wwi->state = WINE_WS_STOPPED;
155 InterlockedExchange((LONG*)&wwi->dwTotalRecorded, 0);
156 wwi->lpQueuePtr = NULL;
158 SetEvent(wwi->hStartUpEvent);
160 /* make sleep time to be # of ms to output a period */
161 dwSleepTime = (wwi->dwPeriodSize * 1000) / wwi->format.Format.nAvgBytesPerSec;
162 frames_per_period = snd_pcm_bytes_to_frames(wwi->pcm, wwi->dwPeriodSize);
163 TRACE("sleeptime=%d ms, total buffer length=%d ms (%d bytes)\n", dwSleepTime, wwi->dwBufferSize * 1000 / wwi->format.Format.nAvgBytesPerSec, wwi->dwBufferSize);
166 /* wait for dwSleepTime or an event in thread's queue */
167 if (wwi->lpQueuePtr != NULL && wwi->state == WINE_WS_PLAYING)
173 lpWaveHdr = wwi->lpQueuePtr;
174 /* read all the fragments accumulated so far */
175 frames = snd_pcm_avail_update(wwi->pcm);
176 bytes = snd_pcm_frames_to_bytes(wwi->pcm, frames);
178 TRACE("frames = %d bytes = %d state=%d\n", frames, bytes, snd_pcm_state(wwi->pcm));
179 if (snd_pcm_state(wwi->pcm) == SND_PCM_STATE_XRUN)
181 FIXME("Recovering from XRUN!\n");
182 snd_pcm_prepare(wwi->pcm);
183 frames = snd_pcm_avail_update(wwi->pcm);
184 bytes = snd_pcm_frames_to_bytes(wwi->pcm, frames);
185 snd_pcm_start(wwi->pcm);
186 snd_pcm_forward(wwi->pcm, frames - snd_pcm_bytes_to_frames(wwi->pcm, wwi->dwPeriodSize));
189 while (frames > 0 && wwi->lpQueuePtr)
191 TRACE("bytes = %d\n", bytes);
192 if (lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded < bytes)
194 bytes = lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded;
195 frames = snd_pcm_bytes_to_frames(wwi->pcm, bytes);
197 /* directly read fragment in wavehdr */
198 read = wwi->read(wwi->pcm, lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded, frames);
199 bytesRead = snd_pcm_frames_to_bytes(wwi->pcm, read);
201 TRACE("bytesRead=(%d(%d)/(%d)) -> (%d/%d)\n", bytesRead, read, frames, lpWaveHdr->dwBufferLength, lpWaveHdr->dwBufferLength - lpWaveHdr->dwBytesRecorded);
202 if (read != (DWORD) -1)
204 /* update number of bytes recorded in current buffer and by this device */
205 lpWaveHdr->dwBytesRecorded += bytesRead;
206 InterlockedExchangeAdd((LONG*)&wwi->dwTotalRecorded, bytesRead);
210 /* buffer is full. notify client */
211 if (!snd_pcm_bytes_to_frames(wwi->pcm, lpWaveHdr->dwBytesRecorded - lpWaveHdr->dwBufferLength))
213 /* must copy the value of next waveHdr, because we have no idea of what
214 * will be done with the content of lpWaveHdr in callback
216 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
218 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
219 lpWaveHdr->dwFlags |= WHDR_DONE;
221 wwi->lpQueuePtr = lpNext;
222 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
226 WARN("read(%s, %p, %d) failed (%d/%s)\n", wwi->pcmname,
227 lpWaveHdr->lpData + lpWaveHdr->dwBytesRecorded,
228 frames, frames, snd_strerror(read));
233 ALSA_WaitRingMessage(&wwi->msgRing, dwSleepTime);
235 while (ALSA_RetrieveRingMessage(&wwi->msgRing, &msg, ¶m, &ev))
237 TRACE("msg=%s param=0x%x\n", ALSA_getCmdString(msg), param);
239 case WINE_WM_PAUSING:
240 wwi->state = WINE_WS_PAUSED;
241 /*FIXME("Device should stop recording\n");*/
244 case WINE_WM_STARTING:
245 wwi->state = WINE_WS_PLAYING;
246 snd_pcm_start(wwi->pcm);
250 lpWaveHdr = (LPWAVEHDR)param;
251 lpWaveHdr->lpNext = 0;
253 /* insert buffer at the end of queue */
256 for (wh = &(wwi->lpQueuePtr); *wh; wh = &((*wh)->lpNext));
260 case WINE_WM_STOPPING:
261 if (wwi->state != WINE_WS_STOPPED)
263 snd_pcm_drain(wwi->pcm);
265 /* read any headers in queue */
266 widRecorder_ReadHeaders(wwi);
268 /* return current buffer to app */
269 lpWaveHdr = wwi->lpQueuePtr;
272 LPWAVEHDR lpNext = lpWaveHdr->lpNext;
273 TRACE("stop %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
274 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
275 lpWaveHdr->dwFlags |= WHDR_DONE;
276 wwi->lpQueuePtr = lpNext;
277 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
280 wwi->state = WINE_WS_STOPPED;
283 case WINE_WM_RESETTING:
284 if (wwi->state != WINE_WS_STOPPED)
286 snd_pcm_drain(wwi->pcm);
288 wwi->state = WINE_WS_STOPPED;
289 wwi->dwTotalRecorded = 0;
291 /* read any headers in queue */
292 widRecorder_ReadHeaders(wwi);
294 /* return all buffers to the app */
295 for (lpWaveHdr = wwi->lpQueuePtr; lpWaveHdr; lpWaveHdr = lpWaveHdr->lpNext) {
296 TRACE("reset %p %p\n", lpWaveHdr, lpWaveHdr->lpNext);
297 lpWaveHdr->dwFlags &= ~WHDR_INQUEUE;
298 lpWaveHdr->dwFlags |= WHDR_DONE;
299 wwi->lpQueuePtr = lpWaveHdr->lpNext;
300 widNotifyClient(wwi, WIM_DATA, (DWORD)lpWaveHdr, 0);
303 wwi->lpQueuePtr = NULL;
306 case WINE_WM_CLOSING:
308 wwi->state = WINE_WS_CLOSED;
311 /* shouldn't go here */
313 FIXME("unknown message %d\n", msg);
319 /* just for not generating compilation warnings... should never be executed */
323 /**************************************************************************
326 static DWORD widOpen(WORD wDevID, LPWAVEOPENDESC lpDesc, DWORD dwFlags)
329 snd_pcm_hw_params_t * hw_params;
330 snd_pcm_sw_params_t * sw_params;
331 snd_pcm_access_t access;
332 snd_pcm_format_t format;
334 unsigned int buffer_time = 500000;
335 unsigned int period_time = 10000;
336 snd_pcm_uframes_t buffer_size;
337 snd_pcm_uframes_t period_size;
344 /* JPW TODO - review this code */
345 TRACE("(%u, %p, %08X);\n", wDevID, lpDesc, dwFlags);
346 if (lpDesc == NULL) {
347 WARN("Invalid Parameter !\n");
348 return MMSYSERR_INVALPARAM;
350 if (wDevID >= ALSA_WidNumDevs) {
351 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
352 return MMSYSERR_BADDEVICEID;
355 /* only PCM format is supported so far... */
356 if (!ALSA_supportedFormat(lpDesc->lpFormat)) {
357 WARN("Bad format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
358 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
359 lpDesc->lpFormat->nSamplesPerSec);
360 return WAVERR_BADFORMAT;
363 if (dwFlags & WAVE_FORMAT_QUERY) {
364 TRACE("Query format: tag=%04X nChannels=%d nSamplesPerSec=%d !\n",
365 lpDesc->lpFormat->wFormatTag, lpDesc->lpFormat->nChannels,
366 lpDesc->lpFormat->nSamplesPerSec);
367 return MMSYSERR_NOERROR;
370 wwi = &WInDev[wDevID];
372 if (wwi->pcm != NULL) {
373 WARN("already allocated\n");
374 return MMSYSERR_ALLOCATED;
378 flags = SND_PCM_NONBLOCK;
380 if ( (err=snd_pcm_open(&pcm, wwi->pcmname, SND_PCM_STREAM_CAPTURE, flags)) < 0 )
382 ERR("Error open: %s\n", snd_strerror(err));
383 return MMSYSERR_NOTENABLED;
386 wwi->wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
388 wwi->waveDesc = *lpDesc;
389 ALSA_copyFormat(lpDesc->lpFormat, &wwi->format);
391 if (wwi->format.Format.wBitsPerSample == 0) {
392 WARN("Resetting zeroed wBitsPerSample\n");
393 wwi->format.Format.wBitsPerSample = 8 *
394 (wwi->format.Format.nAvgBytesPerSec /
395 wwi->format.Format.nSamplesPerSec) /
396 wwi->format.Format.nChannels;
399 hw_params = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_hw_params_sizeof() );
400 sw_params = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_sw_params_sizeof() );
402 snd_pcm_hw_params_any(pcm, hw_params);
404 #define EXIT_ON_ERROR(f,e,txt) do \
407 if ( (err = (f) ) < 0) \
409 WARN(txt ": %s\n", snd_strerror(err)); \
415 access = SND_PCM_ACCESS_MMAP_INTERLEAVED;
416 if ( ( err = snd_pcm_hw_params_set_access(pcm, hw_params, access ) ) < 0) {
417 WARN("mmap not available. switching to standard write.\n");
418 access = SND_PCM_ACCESS_RW_INTERLEAVED;
419 EXIT_ON_ERROR( snd_pcm_hw_params_set_access(pcm, hw_params, access ), MMSYSERR_INVALPARAM, "unable to set access for playback");
420 wwi->read = snd_pcm_readi;
423 wwi->read = snd_pcm_mmap_readi;
425 EXIT_ON_ERROR( snd_pcm_hw_params_set_channels(pcm, hw_params, wwi->format.Format.nChannels), WAVERR_BADFORMAT, "unable to set required channels");
427 if ((wwi->format.Format.wFormatTag == WAVE_FORMAT_PCM) ||
428 ((wwi->format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
429 IsEqualGUID(&wwi->format.SubFormat, &KSDATAFORMAT_SUBTYPE_PCM))) {
430 format = (wwi->format.Format.wBitsPerSample == 8) ? SND_PCM_FORMAT_U8 :
431 (wwi->format.Format.wBitsPerSample == 16) ? SND_PCM_FORMAT_S16_LE :
432 (wwi->format.Format.wBitsPerSample == 24) ? SND_PCM_FORMAT_S24_3LE :
433 (wwi->format.Format.wBitsPerSample == 32) ? SND_PCM_FORMAT_S32_LE : -1;
434 } else if ((wwi->format.Format.wFormatTag == WAVE_FORMAT_EXTENSIBLE) &&
435 IsEqualGUID(&wwi->format.SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT)){
436 format = (wwi->format.Format.wBitsPerSample == 32) ? SND_PCM_FORMAT_FLOAT_LE : -1;
437 } else if (wwi->format.Format.wFormatTag == WAVE_FORMAT_MULAW) {
438 FIXME("unimplemented format: WAVE_FORMAT_MULAW\n");
439 ret = WAVERR_BADFORMAT;
441 } else if (wwi->format.Format.wFormatTag == WAVE_FORMAT_ALAW) {
442 FIXME("unimplemented format: WAVE_FORMAT_ALAW\n");
443 ret = WAVERR_BADFORMAT;
445 } else if (wwi->format.Format.wFormatTag == WAVE_FORMAT_ADPCM) {
446 FIXME("unimplemented format: WAVE_FORMAT_ADPCM\n");
447 ret = WAVERR_BADFORMAT;
450 ERR("invalid format: %0x04x\n", wwi->format.Format.wFormatTag);
451 ret = WAVERR_BADFORMAT;
455 EXIT_ON_ERROR( snd_pcm_hw_params_set_format(pcm, hw_params, format), WAVERR_BADFORMAT, "unable to set required format");
457 rate = wwi->format.Format.nSamplesPerSec;
459 err = snd_pcm_hw_params_set_rate_near(pcm, hw_params, &rate, &dir);
461 WARN("Rate %d Hz not available for playback: %s\n", wwi->format.Format.nSamplesPerSec, snd_strerror(rate));
462 ret = WAVERR_BADFORMAT;
465 if (!ALSA_NearMatch(rate, wwi->format.Format.nSamplesPerSec)) {
466 WARN("Rate doesn't match (requested %d Hz, got %d Hz)\n", wwi->format.Format.nSamplesPerSec, rate);
467 ret = WAVERR_BADFORMAT;
472 EXIT_ON_ERROR( snd_pcm_hw_params_set_buffer_time_near(pcm, hw_params, &buffer_time, &dir), MMSYSERR_INVALPARAM, "unable to set buffer time");
474 EXIT_ON_ERROR( snd_pcm_hw_params_set_period_time_near(pcm, hw_params, &period_time, &dir), MMSYSERR_INVALPARAM, "unable to set period time");
476 EXIT_ON_ERROR( snd_pcm_hw_params(pcm, hw_params), MMSYSERR_INVALPARAM, "unable to set hw params for playback");
479 err = snd_pcm_hw_params_get_period_size(hw_params, &period_size, &dir);
480 err = snd_pcm_hw_params_get_buffer_size(hw_params, &buffer_size);
482 snd_pcm_sw_params_current(pcm, sw_params);
483 EXIT_ON_ERROR( snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 1), MMSYSERR_ERROR, "unable to set start threshold");
484 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_size(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence size");
485 EXIT_ON_ERROR( snd_pcm_sw_params_set_avail_min(pcm, sw_params, period_size), MMSYSERR_ERROR, "unable to set avail min");
486 EXIT_ON_ERROR( snd_pcm_sw_params_set_xfer_align(pcm, sw_params, 1), MMSYSERR_ERROR, "unable to set xfer align");
487 EXIT_ON_ERROR( snd_pcm_sw_params_set_silence_threshold(pcm, sw_params, 0), MMSYSERR_ERROR, "unable to set silence threshold");
488 EXIT_ON_ERROR( snd_pcm_sw_params(pcm, sw_params), MMSYSERR_ERROR, "unable to set sw params for playback");
491 snd_pcm_prepare(pcm);
494 ALSA_TraceParameters(hw_params, sw_params, FALSE);
496 /* now, we can save all required data for later use... */
497 if ( wwi->hw_params )
498 snd_pcm_hw_params_free(wwi->hw_params);
499 snd_pcm_hw_params_malloc(&(wwi->hw_params));
500 snd_pcm_hw_params_copy(wwi->hw_params, hw_params);
502 wwi->dwBufferSize = snd_pcm_frames_to_bytes(pcm, buffer_size);
503 wwi->lpQueuePtr = wwi->lpPlayPtr = wwi->lpLoopPtr = NULL;
506 ALSA_InitRingMessage(&wwi->msgRing);
508 wwi->dwPeriodSize = snd_pcm_frames_to_bytes(pcm, period_size);
509 TRACE("dwPeriodSize=%u\n", wwi->dwPeriodSize);
510 TRACE("wBitsPerSample=%u, nAvgBytesPerSec=%u, nSamplesPerSec=%u, nChannels=%u nBlockAlign=%u!\n",
511 wwi->format.Format.wBitsPerSample, wwi->format.Format.nAvgBytesPerSec,
512 wwi->format.Format.nSamplesPerSec, wwi->format.Format.nChannels,
513 wwi->format.Format.nBlockAlign);
515 wwi->hStartUpEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
516 wwi->hThread = CreateThread(NULL, 0, widRecorder, (LPVOID)(DWORD)wDevID, 0, &(wwi->dwThreadID));
518 SetThreadPriority(wwi->hThread, THREAD_PRIORITY_TIME_CRITICAL);
519 WaitForSingleObject(wwi->hStartUpEvent, INFINITE);
520 CloseHandle(wwi->hStartUpEvent);
521 wwi->hStartUpEvent = INVALID_HANDLE_VALUE;
523 HeapFree( GetProcessHeap(), 0, hw_params );
524 HeapFree( GetProcessHeap(), 0, sw_params );
525 return widNotifyClient(wwi, WIM_OPEN, 0L, 0L);
529 HeapFree( GetProcessHeap(), 0, hw_params );
530 HeapFree( GetProcessHeap(), 0, sw_params );
535 /**************************************************************************
536 * widClose [internal]
538 static DWORD widClose(WORD wDevID)
540 DWORD ret = MMSYSERR_NOERROR;
543 TRACE("(%u);\n", wDevID);
545 if (wDevID >= ALSA_WidNumDevs) {
546 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
547 return MMSYSERR_BADDEVICEID;
550 if (WInDev[wDevID].pcm == NULL) {
551 WARN("Requested to close already closed device %d!\n", wDevID);
552 return MMSYSERR_BADDEVICEID;
555 wwi = &WInDev[wDevID];
556 if (wwi->lpQueuePtr) {
557 WARN("buffers still playing !\n");
558 ret = WAVERR_STILLPLAYING;
560 if (wwi->hThread != INVALID_HANDLE_VALUE) {
561 ALSA_AddRingMessage(&wwi->msgRing, WINE_WM_CLOSING, 0, TRUE);
563 ALSA_DestroyRingMessage(&wwi->msgRing);
565 snd_pcm_hw_params_free(wwi->hw_params);
566 wwi->hw_params = NULL;
568 snd_pcm_close(wwi->pcm);
571 ret = widNotifyClient(wwi, WIM_CLOSE, 0L, 0L);
577 /**************************************************************************
578 * widAddBuffer [internal]
581 static DWORD widAddBuffer(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
583 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
585 /* first, do the sanity checks... */
586 if (wDevID >= ALSA_WidNumDevs) {
587 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
588 return MMSYSERR_BADDEVICEID;
591 if (WInDev[wDevID].pcm == NULL) {
592 WARN("Requested to add buffer to already closed device %d!\n", wDevID);
593 return MMSYSERR_BADDEVICEID;
596 if (lpWaveHdr->lpData == NULL || !(lpWaveHdr->dwFlags & WHDR_PREPARED))
597 return WAVERR_UNPREPARED;
599 if (lpWaveHdr->dwFlags & WHDR_INQUEUE)
600 return WAVERR_STILLPLAYING;
602 lpWaveHdr->dwFlags &= ~WHDR_DONE;
603 lpWaveHdr->dwFlags |= WHDR_INQUEUE;
604 lpWaveHdr->dwBytesRecorded = 0;
605 lpWaveHdr->lpNext = 0;
607 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_HEADER, (DWORD)lpWaveHdr, FALSE);
609 return MMSYSERR_NOERROR;
612 /**************************************************************************
613 * widStart [internal]
616 static DWORD widStart(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
618 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
620 /* first, do the sanity checks... */
621 if (wDevID >= ALSA_WidNumDevs) {
622 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
623 return MMSYSERR_BADDEVICEID;
626 if (WInDev[wDevID].pcm == NULL) {
627 WARN("Requested to start closed device %d!\n", wDevID);
628 return MMSYSERR_BADDEVICEID;
631 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STARTING, 0, TRUE);
633 return MMSYSERR_NOERROR;
636 /**************************************************************************
640 static DWORD widStop(WORD wDevID, LPWAVEHDR lpWaveHdr, DWORD dwSize)
642 TRACE("(%u, %p, %08X);\n", wDevID, lpWaveHdr, dwSize);
644 /* first, do the sanity checks... */
645 if (wDevID >= ALSA_WidNumDevs) {
646 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
647 return MMSYSERR_BADDEVICEID;
650 if (WInDev[wDevID].pcm == NULL) {
651 WARN("Requested to stop closed device %d!\n", wDevID);
652 return MMSYSERR_BADDEVICEID;
655 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_STOPPING, 0, TRUE);
657 return MMSYSERR_NOERROR;
660 /**************************************************************************
661 * widReset [internal]
663 static DWORD widReset(WORD wDevID)
665 TRACE("(%u);\n", wDevID);
666 if (wDevID >= ALSA_WidNumDevs) {
667 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
668 return MMSYSERR_BADDEVICEID;
671 if (WInDev[wDevID].pcm == NULL) {
672 WARN("Requested to reset closed device %d!\n", wDevID);
673 return MMSYSERR_BADDEVICEID;
676 ALSA_AddRingMessage(&WInDev[wDevID].msgRing, WINE_WM_RESETTING, 0, TRUE);
677 return MMSYSERR_NOERROR;
680 /**************************************************************************
681 * widGetPosition [internal]
683 static DWORD widGetPosition(WORD wDevID, LPMMTIME lpTime, DWORD uSize)
687 TRACE("(%u, %p, %u);\n", wDevID, lpTime, uSize);
689 if (wDevID >= ALSA_WidNumDevs) {
690 TRACE("Requested device %d, but only %d are known!\n", wDevID, ALSA_WidNumDevs);
691 return MMSYSERR_BADDEVICEID;
694 if (WInDev[wDevID].state == WINE_WS_CLOSED) {
695 WARN("Requested position of closed device %d!\n", wDevID);
696 return MMSYSERR_BADDEVICEID;
699 if (lpTime == NULL) {
700 WARN("invalid parameter: lpTime = NULL\n");
701 return MMSYSERR_INVALPARAM;
704 wwi = &WInDev[wDevID];
705 return ALSA_bytes_to_mmtime(lpTime, wwi->dwTotalRecorded, &wwi->format);
708 /**************************************************************************
709 * widGetNumDevs [internal]
711 static DWORD widGetNumDevs(void)
713 return ALSA_WidNumDevs;
716 /**************************************************************************
717 * widDevInterfaceSize [internal]
719 static DWORD widDevInterfaceSize(UINT wDevID, LPDWORD dwParam1)
721 TRACE("(%u, %p)\n", wDevID, dwParam1);
723 *dwParam1 = MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
724 NULL, 0 ) * sizeof(WCHAR);
725 return MMSYSERR_NOERROR;
728 /**************************************************************************
729 * widDevInterface [internal]
731 static DWORD widDevInterface(UINT wDevID, PWCHAR dwParam1, DWORD dwParam2)
733 if (dwParam2 >= MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
734 NULL, 0 ) * sizeof(WCHAR))
736 MultiByteToWideChar(CP_ACP, 0, WInDev[wDevID].interface_name, -1,
737 dwParam1, dwParam2 / sizeof(WCHAR));
738 return MMSYSERR_NOERROR;
740 return MMSYSERR_INVALPARAM;
743 /**************************************************************************
744 * widMessage (WINEALSA.@)
746 DWORD WINAPI ALSA_widMessage(UINT wDevID, UINT wMsg, DWORD dwUser,
747 DWORD dwParam1, DWORD dwParam2)
749 TRACE("(%u, %s, %08X, %08X, %08X);\n",
750 wDevID, ALSA_getMessage(wMsg), dwUser, dwParam1, dwParam2);
757 /* FIXME: Pretend this is supported */
759 case WIDM_OPEN: return widOpen (wDevID, (LPWAVEOPENDESC)dwParam1, dwParam2);
760 case WIDM_CLOSE: return widClose (wDevID);
761 case WIDM_ADDBUFFER: return widAddBuffer (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
762 case WIDM_PREPARE: return MMSYSERR_NOTSUPPORTED;
763 case WIDM_UNPREPARE: return MMSYSERR_NOTSUPPORTED;
764 case WIDM_GETDEVCAPS: return widGetDevCaps (wDevID, (LPWAVEINCAPSW)dwParam1, dwParam2);
765 case WIDM_GETNUMDEVS: return widGetNumDevs ();
766 case WIDM_GETPOS: return widGetPosition (wDevID, (LPMMTIME)dwParam1, dwParam2);
767 case WIDM_RESET: return widReset (wDevID);
768 case WIDM_START: return widStart (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
769 case WIDM_STOP: return widStop (wDevID, (LPWAVEHDR)dwParam1, dwParam2);
770 case DRV_QUERYDEVICEINTERFACESIZE: return widDevInterfaceSize (wDevID, (LPDWORD)dwParam1);
771 case DRV_QUERYDEVICEINTERFACE: return widDevInterface (wDevID, (PWCHAR)dwParam1, dwParam2);
772 case DRV_QUERYDSOUNDIFACE: return widDsCreate (wDevID, (PIDSCDRIVER*)dwParam1);
773 case DRV_QUERYDSOUNDDESC: return widDsDesc (wDevID, (PDSDRIVERDESC)dwParam1);
775 FIXME("unknown message %d!\n", wMsg);
777 return MMSYSERR_NOTSUPPORTED;
780 #else /* HAVE_ALSA */
782 /**************************************************************************
783 * widMessage (WINEALSA.@)
785 DWORD WINAPI ALSA_widMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
786 DWORD dwParam1, DWORD dwParam2)
788 FIXME("(%u, %04X, %08X, %08X, %08X):stub\n", wDevID, wMsg, dwUser, dwParam1, dwParam2);
789 return MMSYSERR_NOTENABLED;
792 #endif /* HAVE_ALSA */