winealsa: Only linear PCM is supported.
[wine/hacks.git] / dlls / winealsa.drv / alsa.c
blobb242cfa4cd6996423a2057a3896e71bc6c8f100c
1 /*
2 * Wine Driver for ALSA
4 * Copyright 2002 Eric Pouech
5 * Copyright 2006 Jaroslav Kysela
6 * Copyright 2007 Maarten Lankhorst
8 * This file has a few shared generic subroutines shared among the alsa
9 * implementation.
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "config.h"
28 #include <stdarg.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "mmddk.h"
36 #include "ks.h"
37 #include "guiddef.h"
38 #include "ksmedia.h"
40 #include "alsa.h"
42 #ifdef HAVE_ALSA
44 #include "wine/library.h"
45 #include "wine/unicode.h"
46 #include "wine/debug.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(alsa);
49 /* unless someone makes a wineserver kernel module, Unix pipes are faster than win32 events */
50 #define USE_PIPE_SYNC
52 #ifdef USE_PIPE_SYNC
53 #define INIT_OMR(omr) do { if (pipe(omr->msg_pipe) < 0) { omr->msg_pipe[0] = omr->msg_pipe[1] = -1; } } while (0)
54 #define CLOSE_OMR(omr) do { close(omr->msg_pipe[0]); close(omr->msg_pipe[1]); } while (0)
55 #define SIGNAL_OMR(omr) do { int x = 0; write((omr)->msg_pipe[1], &x, sizeof(x)); } while (0)
56 #define CLEAR_OMR(omr) do { int x = 0; read((omr)->msg_pipe[0], &x, sizeof(x)); } while (0)
57 #define RESET_OMR(omr) do { } while (0)
58 #define WAIT_OMR(omr, sleep) \
59 do { struct pollfd pfd; pfd.fd = (omr)->msg_pipe[0]; \
60 pfd.events = POLLIN; poll(&pfd, 1, sleep); } while (0)
61 #else
62 #define INIT_OMR(omr) do { omr->msg_event = CreateEventW(NULL, FALSE, FALSE, NULL); } while (0)
63 #define CLOSE_OMR(omr) do { CloseHandle(omr->msg_event); } while (0)
64 #define SIGNAL_OMR(omr) do { SetEvent((omr)->msg_event); } while (0)
65 #define CLEAR_OMR(omr) do { } while (0)
66 #define RESET_OMR(omr) do { ResetEvent((omr)->msg_event); } while (0)
67 #define WAIT_OMR(omr, sleep) \
68 do { WaitForSingleObject((omr)->msg_event, sleep); } while (0)
69 #endif
71 #define ALSA_RING_BUFFER_INCREMENT 64
73 /******************************************************************
74 * ALSA_InitRingMessage
76 * Initialize the ring of messages for passing between driver's caller and playback/record
77 * thread
79 int ALSA_InitRingMessage(ALSA_MSG_RING* omr)
81 omr->msg_toget = 0;
82 omr->msg_tosave = 0;
83 INIT_OMR(omr);
84 omr->ring_buffer_size = ALSA_RING_BUFFER_INCREMENT;
85 omr->messages = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,omr->ring_buffer_size * sizeof(ALSA_MSG));
87 InitializeCriticalSection(&omr->msg_crst);
88 omr->msg_crst.DebugInfo->Spare[0] = (DWORD_PTR)(__FILE__ ": ALSA_MSG_RING.msg_crst");
89 return 0;
92 /******************************************************************
93 * ALSA_DestroyRingMessage
96 int ALSA_DestroyRingMessage(ALSA_MSG_RING* omr)
98 CLOSE_OMR(omr);
99 HeapFree(GetProcessHeap(),0,omr->messages);
100 omr->ring_buffer_size = 0;
101 omr->msg_crst.DebugInfo->Spare[0] = 0;
102 DeleteCriticalSection(&omr->msg_crst);
103 return 0;
105 /******************************************************************
106 * ALSA_ResetRingMessage
109 void ALSA_ResetRingMessage(ALSA_MSG_RING* omr)
111 RESET_OMR(omr);
114 /******************************************************************
115 * ALSA_WaitRingMessage
118 void ALSA_WaitRingMessage(ALSA_MSG_RING* omr, DWORD sleep)
120 WAIT_OMR(omr, sleep);
123 /******************************************************************
124 * ALSA_AddRingMessage
126 * Inserts a new message into the ring (should be called from DriverProc derived routines)
128 int ALSA_AddRingMessage(ALSA_MSG_RING* omr, enum win_wm_message msg, DWORD_PTR param, BOOL wait)
130 HANDLE hEvent = INVALID_HANDLE_VALUE;
132 EnterCriticalSection(&omr->msg_crst);
133 if ((omr->msg_toget == ((omr->msg_tosave + 1) % omr->ring_buffer_size)))
135 int old_ring_buffer_size = omr->ring_buffer_size;
136 omr->ring_buffer_size += ALSA_RING_BUFFER_INCREMENT;
137 omr->messages = HeapReAlloc(GetProcessHeap(),0,omr->messages, omr->ring_buffer_size * sizeof(ALSA_MSG));
138 /* Now we need to rearrange the ring buffer so that the new
139 buffers just allocated are in between omr->msg_tosave and
140 omr->msg_toget.
142 if (omr->msg_tosave < omr->msg_toget)
144 memmove(&(omr->messages[omr->msg_toget + ALSA_RING_BUFFER_INCREMENT]),
145 &(omr->messages[omr->msg_toget]),
146 sizeof(ALSA_MSG)*(old_ring_buffer_size - omr->msg_toget)
148 omr->msg_toget += ALSA_RING_BUFFER_INCREMENT;
151 if (wait)
153 hEvent = CreateEventW(NULL, FALSE, FALSE, NULL);
154 if (hEvent == INVALID_HANDLE_VALUE)
156 ERR("can't create event !?\n");
157 LeaveCriticalSection(&omr->msg_crst);
158 return 0;
160 if (omr->msg_toget != omr->msg_tosave && omr->messages[omr->msg_toget].msg != WINE_WM_HEADER)
161 FIXME("two fast messages in the queue!!!! toget = %d(%s), tosave=%d(%s)\n",
162 omr->msg_toget,ALSA_getCmdString(omr->messages[omr->msg_toget].msg),
163 omr->msg_tosave,ALSA_getCmdString(omr->messages[omr->msg_tosave].msg));
165 /* fast messages have to be added at the start of the queue */
166 omr->msg_toget = (omr->msg_toget + omr->ring_buffer_size - 1) % omr->ring_buffer_size;
168 omr->messages[omr->msg_toget].msg = msg;
169 omr->messages[omr->msg_toget].param = param;
170 omr->messages[omr->msg_toget].hEvent = hEvent;
172 else
174 omr->messages[omr->msg_tosave].msg = msg;
175 omr->messages[omr->msg_tosave].param = param;
176 omr->messages[omr->msg_tosave].hEvent = INVALID_HANDLE_VALUE;
177 omr->msg_tosave = (omr->msg_tosave + 1) % omr->ring_buffer_size;
179 LeaveCriticalSection(&omr->msg_crst);
180 /* signal a new message */
181 SIGNAL_OMR(omr);
182 if (wait)
184 /* wait for playback/record thread to have processed the message */
185 WaitForSingleObject(hEvent, INFINITE);
186 CloseHandle(hEvent);
188 return 1;
191 /******************************************************************
192 * ALSA_RetrieveRingMessage
194 * Get a message from the ring. Should be called by the playback/record thread.
196 int ALSA_RetrieveRingMessage(ALSA_MSG_RING* omr, enum win_wm_message *msg,
197 DWORD_PTR *param, HANDLE *hEvent)
199 EnterCriticalSection(&omr->msg_crst);
201 if (omr->msg_toget == omr->msg_tosave) /* buffer empty ? */
203 LeaveCriticalSection(&omr->msg_crst);
204 return 0;
207 *msg = omr->messages[omr->msg_toget].msg;
208 omr->messages[omr->msg_toget].msg = 0;
209 *param = omr->messages[omr->msg_toget].param;
210 *hEvent = omr->messages[omr->msg_toget].hEvent;
211 omr->msg_toget = (omr->msg_toget + 1) % omr->ring_buffer_size;
212 CLEAR_OMR(omr);
213 LeaveCriticalSection(&omr->msg_crst);
214 return 1;
217 /*======================================================================*
218 * Utility functions *
219 *======================================================================*/
221 /* These strings used only for tracing */
222 const char * ALSA_getCmdString(enum win_wm_message msg)
224 #define MSG_TO_STR(x) case x: return #x
225 switch(msg) {
226 MSG_TO_STR(WINE_WM_PAUSING);
227 MSG_TO_STR(WINE_WM_RESTARTING);
228 MSG_TO_STR(WINE_WM_RESETTING);
229 MSG_TO_STR(WINE_WM_HEADER);
230 MSG_TO_STR(WINE_WM_UPDATE);
231 MSG_TO_STR(WINE_WM_BREAKLOOP);
232 MSG_TO_STR(WINE_WM_CLOSING);
233 MSG_TO_STR(WINE_WM_STARTING);
234 MSG_TO_STR(WINE_WM_STOPPING);
236 #undef MSG_TO_STR
237 return wine_dbg_sprintf("UNKNOWN(0x%08x)", msg);
240 const char * ALSA_getMessage(UINT msg)
242 #define MSG_TO_STR(x) case x: return #x
243 switch(msg) {
244 MSG_TO_STR(DRVM_INIT);
245 MSG_TO_STR(DRVM_EXIT);
246 MSG_TO_STR(DRVM_ENABLE);
247 MSG_TO_STR(DRVM_DISABLE);
248 MSG_TO_STR(WIDM_OPEN);
249 MSG_TO_STR(WIDM_CLOSE);
250 MSG_TO_STR(WIDM_ADDBUFFER);
251 MSG_TO_STR(WIDM_PREPARE);
252 MSG_TO_STR(WIDM_UNPREPARE);
253 MSG_TO_STR(WIDM_GETDEVCAPS);
254 MSG_TO_STR(WIDM_GETNUMDEVS);
255 MSG_TO_STR(WIDM_GETPOS);
256 MSG_TO_STR(WIDM_RESET);
257 MSG_TO_STR(WIDM_START);
258 MSG_TO_STR(WIDM_STOP);
259 MSG_TO_STR(WODM_OPEN);
260 MSG_TO_STR(WODM_CLOSE);
261 MSG_TO_STR(WODM_WRITE);
262 MSG_TO_STR(WODM_PAUSE);
263 MSG_TO_STR(WODM_GETPOS);
264 MSG_TO_STR(WODM_BREAKLOOP);
265 MSG_TO_STR(WODM_PREPARE);
266 MSG_TO_STR(WODM_UNPREPARE);
267 MSG_TO_STR(WODM_GETDEVCAPS);
268 MSG_TO_STR(WODM_GETNUMDEVS);
269 MSG_TO_STR(WODM_GETPITCH);
270 MSG_TO_STR(WODM_SETPITCH);
271 MSG_TO_STR(WODM_GETPLAYBACKRATE);
272 MSG_TO_STR(WODM_SETPLAYBACKRATE);
273 MSG_TO_STR(WODM_GETVOLUME);
274 MSG_TO_STR(WODM_SETVOLUME);
275 MSG_TO_STR(WODM_RESTART);
276 MSG_TO_STR(WODM_RESET);
277 MSG_TO_STR(DRV_QUERYDEVICEINTERFACESIZE);
278 MSG_TO_STR(DRV_QUERYDEVICEINTERFACE);
279 MSG_TO_STR(DRV_QUERYDSOUNDIFACE);
280 MSG_TO_STR(DRV_QUERYDSOUNDDESC);
282 #undef MSG_TO_STR
283 return wine_dbg_sprintf("UNKNOWN(0x%04x)", msg);
286 const char * ALSA_getFormat(WORD wFormatTag)
288 #define FMT_TO_STR(x) case x: return #x
289 switch(wFormatTag) {
290 FMT_TO_STR(WAVE_FORMAT_PCM);
291 FMT_TO_STR(WAVE_FORMAT_EXTENSIBLE);
292 FMT_TO_STR(WAVE_FORMAT_MULAW);
293 FMT_TO_STR(WAVE_FORMAT_ALAW);
294 FMT_TO_STR(WAVE_FORMAT_ADPCM);
296 #undef FMT_TO_STR
297 return wine_dbg_sprintf("UNKNOWN(0x%04x)", wFormatTag);
300 /* Allow 1% deviation for sample rates (some ES137x cards) */
301 BOOL ALSA_NearMatch(int rate1, int rate2)
303 return (((100 * (rate1 - rate2)) / rate1) == 0);
306 DWORD ALSA_bytes_to_mmtime(LPMMTIME lpTime, DWORD position, WAVEFORMATPCMEX* format)
308 TRACE("wType=%04X wBitsPerSample=%u nSamplesPerSec=%u nChannels=%u nAvgBytesPerSec=%u\n",
309 lpTime->wType, format->Format.wBitsPerSample, format->Format.nSamplesPerSec,
310 format->Format.nChannels, format->Format.nAvgBytesPerSec);
311 TRACE("Position in bytes=%u\n", position);
313 switch (lpTime->wType) {
314 case TIME_SAMPLES:
315 lpTime->u.sample = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
316 TRACE("TIME_SAMPLES=%u\n", lpTime->u.sample);
317 break;
318 case TIME_MS:
319 lpTime->u.ms = 1000.0 * position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels * format->Format.nSamplesPerSec);
320 TRACE("TIME_MS=%u\n", lpTime->u.ms);
321 break;
322 case TIME_SMPTE:
323 lpTime->u.smpte.fps = 30;
324 position = position / (format->Format.wBitsPerSample / 8 * format->Format.nChannels);
325 position += (format->Format.nSamplesPerSec / lpTime->u.smpte.fps) - 1; /* round up */
326 lpTime->u.smpte.sec = position / format->Format.nSamplesPerSec;
327 position -= lpTime->u.smpte.sec * format->Format.nSamplesPerSec;
328 lpTime->u.smpte.min = lpTime->u.smpte.sec / 60;
329 lpTime->u.smpte.sec -= 60 * lpTime->u.smpte.min;
330 lpTime->u.smpte.hour = lpTime->u.smpte.min / 60;
331 lpTime->u.smpte.min -= 60 * lpTime->u.smpte.hour;
332 lpTime->u.smpte.fps = 30;
333 lpTime->u.smpte.frame = position * lpTime->u.smpte.fps / format->Format.nSamplesPerSec;
334 TRACE("TIME_SMPTE=%02u:%02u:%02u:%02u\n",
335 lpTime->u.smpte.hour, lpTime->u.smpte.min,
336 lpTime->u.smpte.sec, lpTime->u.smpte.frame);
337 break;
338 default:
339 WARN("Format %d not supported, using TIME_BYTES !\n", lpTime->wType);
340 lpTime->wType = TIME_BYTES;
341 /* fall through */
342 case TIME_BYTES:
343 lpTime->u.cb = position;
344 TRACE("TIME_BYTES=%u\n", lpTime->u.cb);
345 break;
347 return MMSYSERR_NOERROR;
350 void ALSA_copyFormat(LPWAVEFORMATEX wf1, LPWAVEFORMATPCMEX wf2)
352 unsigned int iLength;
354 ZeroMemory(wf2, sizeof(wf2));
355 if (wf1->wFormatTag == WAVE_FORMAT_PCM)
356 iLength = sizeof(PCMWAVEFORMAT);
357 else if (wf1->wFormatTag == WAVE_FORMAT_EXTENSIBLE)
358 iLength = sizeof(WAVEFORMATPCMEX);
359 else
360 iLength = sizeof(WAVEFORMATEX) + wf1->cbSize;
361 memcpy(wf2, wf1, iLength);
364 BOOL ALSA_supportedFormat(LPWAVEFORMATEX wf)
366 TRACE("(%p)\n",wf);
368 if (wf->nSamplesPerSec<DSBFREQUENCY_MIN||wf->nSamplesPerSec>DSBFREQUENCY_MAX)
369 return FALSE;
371 if (wf->wFormatTag == WAVE_FORMAT_PCM) {
372 if (wf->nChannels==1||wf->nChannels==2) {
373 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16)
374 return TRUE;
376 } else if (wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE) {
377 WAVEFORMATEXTENSIBLE * wfex = (WAVEFORMATEXTENSIBLE *)wf;
379 if (wf->cbSize == 22 &&
380 (IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_PCM) ||
381 IsEqualGUID(&wfex->SubFormat, &KSDATAFORMAT_SUBTYPE_IEEE_FLOAT))) {
382 if (wf->nChannels>=1 && wf->nChannels<=6) {
383 if (wf->wBitsPerSample==wfex->Samples.wValidBitsPerSample) {
384 if (wf->wBitsPerSample==8||wf->wBitsPerSample==16||
385 wf->wBitsPerSample==24||wf->wBitsPerSample==32) {
386 return TRUE;
388 } else
389 WARN("wBitsPerSample != wValidBitsPerSample not supported yet\n");
391 } else
392 WARN("only KSDATAFORMAT_SUBTYPE_PCM and KSDATAFORMAT_SUBTYPE_IEEE_FLOAT "
393 "supported\n");
394 } else
395 WARN("only WAVE_FORMAT_PCM and WAVE_FORMAT_EXTENSIBLE supported\n");
397 return FALSE;
400 /*======================================================================*
401 * Low level WAVE implementation *
402 *======================================================================*/
404 /**************************************************************************
405 * ALSA_CheckSetVolume [internal]
407 * Helper function for Alsa volume queries. This tries to simplify
408 * the process of managing the volume. All parameters are optional
409 * (pass NULL to ignore or not use).
410 * Return values are MMSYSERR_NOERROR on success, or !0 on failure;
411 * error codes are normalized into the possible documented return
412 * values from waveOutGetVolume.
414 int ALSA_CheckSetVolume(snd_hctl_t *hctl, int *out_left, int *out_right,
415 int *out_min, int *out_max, int *out_step,
416 int *new_left, int *new_right)
418 int rc = MMSYSERR_NOERROR;
419 int value_count = 0;
420 snd_hctl_elem_t * elem = NULL;
421 snd_ctl_elem_info_t * eleminfop = NULL;
422 snd_ctl_elem_value_t * elemvaluep = NULL;
423 snd_ctl_elem_id_t * elemidp = NULL;
426 #define EXIT_ON_ERROR(f,txt,exitcode) do \
428 int err; \
429 if ( (err = (f) ) < 0) \
431 ERR(txt " failed: %s\n", snd_strerror(err)); \
432 rc = exitcode; \
433 goto out; \
435 } while(0)
437 if (! hctl)
438 return MMSYSERR_NOTSUPPORTED;
440 /* Allocate areas to return information about the volume */
441 EXIT_ON_ERROR(snd_ctl_elem_id_malloc(&elemidp), "snd_ctl_elem_id_malloc", MMSYSERR_NOMEM);
442 EXIT_ON_ERROR(snd_ctl_elem_value_malloc (&elemvaluep), "snd_ctl_elem_value_malloc", MMSYSERR_NOMEM);
443 EXIT_ON_ERROR(snd_ctl_elem_info_malloc (&eleminfop), "snd_ctl_elem_info_malloc", MMSYSERR_NOMEM);
444 snd_ctl_elem_id_clear(elemidp);
445 snd_ctl_elem_value_clear(elemvaluep);
446 snd_ctl_elem_info_clear(eleminfop);
448 /* Setup and find an element id that exactly matches the characteristic we want
449 ** FIXME: It is probably short sighted to hard code and fixate on PCM Playback Volume */
450 snd_ctl_elem_id_set_name(elemidp, "PCM Playback Volume");
451 snd_ctl_elem_id_set_interface(elemidp, SND_CTL_ELEM_IFACE_MIXER);
452 elem = snd_hctl_find_elem(hctl, elemidp);
453 if (elem)
455 /* Read and return volume information */
456 EXIT_ON_ERROR(snd_hctl_elem_info(elem, eleminfop), "snd_hctl_elem_info", MMSYSERR_NOTSUPPORTED);
457 value_count = snd_ctl_elem_info_get_count(eleminfop);
458 if (out_min || out_max || out_step)
460 if (!snd_ctl_elem_info_is_readable(eleminfop))
462 ERR("snd_ctl_elem_info_is_readable returned false; cannot return info\n");
463 rc = MMSYSERR_NOTSUPPORTED;
464 goto out;
467 if (out_min)
468 *out_min = snd_ctl_elem_info_get_min(eleminfop);
470 if (out_max)
471 *out_max = snd_ctl_elem_info_get_max(eleminfop);
473 if (out_step)
474 *out_step = snd_ctl_elem_info_get_step(eleminfop);
477 if (out_left || out_right)
479 EXIT_ON_ERROR(snd_hctl_elem_read(elem, elemvaluep), "snd_hctl_elem_read", MMSYSERR_NOTSUPPORTED);
481 if (out_left)
482 *out_left = snd_ctl_elem_value_get_integer(elemvaluep, 0);
484 if (out_right)
486 if (value_count == 1)
487 *out_right = snd_ctl_elem_value_get_integer(elemvaluep, 0);
488 else if (value_count == 2)
489 *out_right = snd_ctl_elem_value_get_integer(elemvaluep, 1);
490 else
492 ERR("Unexpected value count %d from snd_ctl_elem_info_get_count while getting volume info\n", value_count);
493 rc = -1;
494 goto out;
499 /* Set the volume */
500 if (new_left || new_right)
502 EXIT_ON_ERROR(snd_hctl_elem_read(elem, elemvaluep), "snd_hctl_elem_read", MMSYSERR_NOTSUPPORTED);
503 if (new_left)
504 snd_ctl_elem_value_set_integer(elemvaluep, 0, *new_left);
505 if (new_right)
507 if (value_count == 1)
508 snd_ctl_elem_value_set_integer(elemvaluep, 0, *new_right);
509 else if (value_count == 2)
510 snd_ctl_elem_value_set_integer(elemvaluep, 1, *new_right);
511 else
513 ERR("Unexpected value count %d from snd_ctl_elem_info_get_count while setting volume info\n", value_count);
514 rc = -1;
515 goto out;
519 EXIT_ON_ERROR(snd_hctl_elem_write(elem, elemvaluep), "snd_hctl_elem_write", MMSYSERR_NOTSUPPORTED);
522 else
524 ERR("Could not find 'PCM Playback Volume' element\n");
525 rc = MMSYSERR_NOTSUPPORTED;
529 #undef EXIT_ON_ERROR
531 out:
533 if (elemvaluep)
534 snd_ctl_elem_value_free(elemvaluep);
535 if (eleminfop)
536 snd_ctl_elem_info_free(eleminfop);
537 if (elemidp)
538 snd_ctl_elem_id_free(elemidp);
540 return rc;
544 /**************************************************************************
545 * wine_snd_pcm_recover [internal]
547 * Code slightly modified from alsa-lib v1.0.23 snd_pcm_recover implementation.
548 * used to recover from XRUN errors (buffer underflow/overflow)
550 int wine_snd_pcm_recover(snd_pcm_t *pcm, int err, int silent)
552 if (err > 0)
553 err = -err;
554 if (err == -EINTR) /* nothing to do, continue */
555 return 0;
556 if (err == -EPIPE) {
557 const char *s;
558 if (snd_pcm_stream(pcm) == SND_PCM_STREAM_PLAYBACK)
559 s = "underrun";
560 else
561 s = "overrun";
562 if (!silent)
563 ERR("%s occurred\n", s);
564 err = snd_pcm_prepare(pcm);
565 if (err < 0) {
566 ERR("cannot recover from %s, prepare failed: %s\n", s, snd_strerror(err));
567 return err;
569 return 0;
571 if (err == -ESTRPIPE) {
572 while ((err = snd_pcm_resume(pcm)) == -EAGAIN)
573 /* wait until suspend flag is released */
574 poll(NULL, 0, 1000);
575 if (err < 0) {
576 err = snd_pcm_prepare(pcm);
577 if (err < 0) {
578 ERR("cannot recover from suspend, prepare failed: %s\n", snd_strerror(err));
579 return err;
582 return 0;
584 return err;
587 /**************************************************************************
588 * ALSA_TraceParameters [internal]
590 * used to trace format changes, hw and sw parameters
592 void ALSA_TraceParameters(snd_pcm_hw_params_t * hw_params, snd_pcm_sw_params_t * sw, int full)
594 int err;
595 snd_pcm_format_t format;
596 snd_pcm_access_t access;
598 #define X(x) ((x)? "true" : "false")
599 if (full)
600 TRACE("FLAGS: sampleres=%s overrng=%s pause=%s resume=%s syncstart=%s batch=%s block=%s double=%s "
601 "halfd=%s joint=%s\n",
602 X(snd_pcm_hw_params_can_mmap_sample_resolution(hw_params)),
603 X(snd_pcm_hw_params_can_overrange(hw_params)),
604 X(snd_pcm_hw_params_can_pause(hw_params)),
605 X(snd_pcm_hw_params_can_resume(hw_params)),
606 X(snd_pcm_hw_params_can_sync_start(hw_params)),
607 X(snd_pcm_hw_params_is_batch(hw_params)),
608 X(snd_pcm_hw_params_is_block_transfer(hw_params)),
609 X(snd_pcm_hw_params_is_double(hw_params)),
610 X(snd_pcm_hw_params_is_half_duplex(hw_params)),
611 X(snd_pcm_hw_params_is_joint_duplex(hw_params)));
612 #undef X
614 err = snd_pcm_hw_params_get_access(hw_params, &access);
615 if (err >= 0)
617 TRACE("access=%s\n", snd_pcm_access_name(access));
619 else
621 snd_pcm_access_mask_t * acmask;
623 acmask = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_access_mask_sizeof());
624 snd_pcm_hw_params_get_access_mask(hw_params, acmask);
625 for ( access = SND_PCM_ACCESS_MMAP_INTERLEAVED; access <= SND_PCM_ACCESS_LAST; access++)
626 if (snd_pcm_access_mask_test(acmask, access))
627 TRACE("access=%s\n", snd_pcm_access_name(access));
628 HeapFree( GetProcessHeap(), 0, acmask );
631 err = snd_pcm_hw_params_get_format(hw_params, &format);
632 if (err >= 0)
634 TRACE("format=%s\n", snd_pcm_format_name(format));
637 else
639 snd_pcm_format_mask_t * fmask;
641 fmask = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, snd_pcm_format_mask_sizeof());
642 snd_pcm_hw_params_get_format_mask(hw_params, fmask);
643 for ( format = SND_PCM_FORMAT_S8; format <= SND_PCM_FORMAT_LAST ; format++)
644 if ( snd_pcm_format_mask_test(fmask, format) )
645 TRACE("format=%s\n", snd_pcm_format_name(format));
646 HeapFree( GetProcessHeap(), 0, fmask );
649 do {
650 int err=0;
651 unsigned int val=0;
652 err = snd_pcm_hw_params_get_channels(hw_params, &val);
653 if (err<0) {
654 unsigned int min = 0;
655 unsigned int max = 0;
656 err = snd_pcm_hw_params_get_channels_min(hw_params, &min),
657 err = snd_pcm_hw_params_get_channels_max(hw_params, &max);
658 TRACE("channels_min=%u, channels_min_max=%u\n", min, max);
659 } else {
660 TRACE("channels=%d\n", val);
662 } while(0);
663 do {
664 int err=0;
665 snd_pcm_uframes_t val=0;
666 err = snd_pcm_hw_params_get_buffer_size(hw_params, &val);
667 if (err<0) {
668 snd_pcm_uframes_t min = 0;
669 snd_pcm_uframes_t max = 0;
670 err = snd_pcm_hw_params_get_buffer_size_min(hw_params, &min),
671 err = snd_pcm_hw_params_get_buffer_size_max(hw_params, &max);
672 TRACE("buffer_size_min=%lu, buffer_size_min_max=%lu\n", min, max);
673 } else {
674 TRACE("buffer_size=%lu\n", val);
676 } while(0);
678 #define X(x) do { \
679 int err=0; \
680 int dir=0; \
681 unsigned int val=0; \
682 err = snd_pcm_hw_params_get_##x(hw_params,&val, &dir); \
683 if (err<0) { \
684 unsigned int min = 0; \
685 unsigned int max = 0; \
686 err = snd_pcm_hw_params_get_##x##_min(hw_params, &min, &dir); \
687 err = snd_pcm_hw_params_get_##x##_max(hw_params, &max, &dir); \
688 TRACE(#x "_min=%u " #x "_max=%u\n", min, max); \
689 } else \
690 TRACE(#x "=%d\n", val); \
691 } while(0)
693 X(rate);
694 X(buffer_time);
695 X(periods);
696 do {
697 int err=0;
698 int dir=0;
699 snd_pcm_uframes_t val=0;
700 err = snd_pcm_hw_params_get_period_size(hw_params, &val, &dir);
701 if (err<0) {
702 snd_pcm_uframes_t min = 0;
703 snd_pcm_uframes_t max = 0;
704 err = snd_pcm_hw_params_get_period_size_min(hw_params, &min, &dir),
705 err = snd_pcm_hw_params_get_period_size_max(hw_params, &max, &dir);
706 TRACE("period_size_min=%lu, period_size_min_max=%lu\n", min, max);
707 } else {
708 TRACE("period_size=%lu\n", val);
710 } while(0);
712 X(period_time);
713 #undef X
715 if (!sw)
716 return;
719 #endif
721 /**************************************************************************
722 * DriverProc (WINEALSA.@)
724 LRESULT CALLBACK ALSA_DriverProc(DWORD_PTR dwDevID, HDRVR hDriv, UINT wMsg,
725 LPARAM dwParam1, LPARAM dwParam2)
727 /* EPP TRACE("(%08lX, %04X, %08lX, %08lX, %08lX)\n", */
728 /* EPP dwDevID, hDriv, wMsg, dwParam1, dwParam2); */
730 switch(wMsg) {
731 #ifdef HAVE_ALSA
732 case DRV_LOAD:
733 case DRV_FREE:
734 case DRV_OPEN:
735 case DRV_CLOSE:
736 case DRV_ENABLE:
737 case DRV_DISABLE:
738 case DRV_QUERYCONFIGURE:
739 return 1;
740 case DRV_CONFIGURE: MessageBoxA(0, "ALSA MultiMedia Driver !", "ALSA Driver", MB_OK); return 1;
741 case DRV_INSTALL:
742 case DRV_REMOVE:
743 return DRV_SUCCESS;
744 #endif
745 default:
746 return 0;