2 * Sample Wine Driver for Advanced Linux Sound System (ALSA)
3 * Based on version <final> of the ALSA API
5 * This file performs the initialisation and scanning of the sound subsystem.
7 * Copyright 2002 Eric Pouech
8 * 2002 Marco Pietrobono
9 * 2003 Christian Costa : WaveIn support
10 * 2006-2007 Maarten Lankhorst
12 * This library is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License as published by the Free Software Foundation; either
15 * version 2.1 of the License, or (at your option) any later version.
17 * This library is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with this library; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
28 #include "wine/port.h"
40 #ifdef HAVE_SYS_IOCTL_H
41 # include <sys/ioctl.h>
43 #ifdef HAVE_SYS_MMAN_H
44 # include <sys/mman.h>
55 /* ksmedia.h defines KSDATAFORMAT_SUBTYPE_PCM and KSDATAFORMAT_SUBTYPE_IEEE_FLOAT
56 * However either all files that use it will define it, or no files will
57 * The only way to solve this is by adding initguid.h here, and include the guid that way
62 #include "wine/library.h"
63 #include "wine/unicode.h"
64 #include "wine/debug.h"
66 WINE_DEFAULT_DEBUG_CHANNEL(wave);
70 /*----------------------------------------------------------------------------
71 ** ALSA_TestDeviceForWine
73 ** Test to see if a given device is sufficient for Wine.
75 static int ALSA_TestDeviceForWine(int card, int device, snd_pcm_stream_t streamtype)
77 snd_pcm_t *pcm = NULL;
80 snd_pcm_hw_params_t *hwparams;
81 const char *reason = NULL;
84 /* Note that the plug: device masks out a lot of info, we want to avoid that */
85 sprintf(pcmname, "hw:%d,%d", card, device);
86 retcode = snd_pcm_open(&pcm, pcmname, streamtype, SND_PCM_NONBLOCK);
89 /* Note that a busy device isn't automatically disqualified */
90 if (retcode == (-1 * EBUSY))
95 snd_pcm_hw_params_alloca(&hwparams);
97 retcode = snd_pcm_hw_params_any(pcm, hwparams);
100 reason = "Could not retrieve hw_params";
104 /* set the count of channels */
105 retcode = snd_pcm_hw_params_set_channels(pcm, hwparams, 2);
108 reason = "Could not set channels";
113 retcode = snd_pcm_hw_params_set_rate_near(pcm, hwparams, &rrate, 0);
116 reason = "Could not set rate";
122 reason = "Rate came back as 0";
126 /* write the parameters to device */
127 retcode = snd_pcm_hw_params(pcm, hwparams);
130 reason = "Could not set hwparams";
140 if (retcode != 0 && retcode != (-1 * ENOENT))
141 TRACE("Discarding card %d/device %d: %s [%d(%s)]\n", card, device, reason, retcode, snd_strerror(retcode));
146 /*----------------------------------------------------------------------------
148 ** Retrieve a string from a registry key
150 static int ALSA_RegGetString(HKEY key, const char *value, char **bufp)
157 rc = RegQueryValueExA(key, value, NULL, &type, NULL, &bufsize);
158 if (rc != ERROR_SUCCESS)
164 *bufp = HeapAlloc(GetProcessHeap(), 0, bufsize);
168 rc = RegQueryValueExA(key, value, NULL, NULL, (LPBYTE)*bufp, &bufsize);
172 /*----------------------------------------------------------------------------
173 ** ALSA_RegGetBoolean
174 ** Get a string and interpret it as a boolean
178 Y(es), T(rue), 1, E(nabled) */
180 #define IS_OPTION_TRUE(ch) ((ch) == 'y' || (ch) == 'Y' || (ch) == 't' || (ch) == 'T' || (ch) == '1' || (ch) == 'e' || (ch) == 'E')
181 static int ALSA_RegGetBoolean(HKEY key, const char *value, BOOL *answer)
186 rc = ALSA_RegGetString(key, value, &buf);
190 if (IS_OPTION_TRUE(*buf))
193 HeapFree(GetProcessHeap(), 0, buf);
199 /*----------------------------------------------------------------------------
200 ** ALSA_RegGetBoolean
201 ** Get a string and interpret it as a DWORD
203 static int ALSA_RegGetInt(HKEY key, const char *value, DWORD *answer)
208 rc = ALSA_RegGetString(key, value, &buf);
212 HeapFree(GetProcessHeap(), 0, buf);
218 /* return a string duplicated on the win32 process heap, free with HeapFree */
219 static char* ALSA_strdup(const char *s) {
220 char *result = HeapAlloc(GetProcessHeap(), 0, strlen(s)+1);
227 #define ALSA_RETURN_ONFAIL(mycall) \
233 ERR("%s failed: %s(%d)\n", #mycall, snd_strerror(rc), rc); \
238 /*----------------------------------------------------------------------------
241 ** Given an ALSA PCM, figure out our HW CAPS structure info.
242 ** ctl can be null, pcm is required, as is all output parms.
245 static int ALSA_ComputeCaps(snd_ctl_t *ctl, snd_pcm_t *pcm,
246 WORD *channels, DWORD *flags, DWORD *formats, DWORD *supports)
248 snd_pcm_hw_params_t *hw_params;
249 snd_pcm_format_mask_t *fmask;
250 snd_pcm_access_mask_t *acmask;
251 unsigned int ratemin = 0;
252 unsigned int ratemax = 0;
253 unsigned int chmin = 0;
254 unsigned int chmax = 0;
257 snd_pcm_hw_params_alloca(&hw_params);
258 ALSA_RETURN_ONFAIL(snd_pcm_hw_params_any(pcm, hw_params));
260 snd_pcm_format_mask_alloca(&fmask);
261 snd_pcm_hw_params_get_format_mask(hw_params, fmask);
263 snd_pcm_access_mask_alloca(&acmask);
264 ALSA_RETURN_ONFAIL(snd_pcm_hw_params_get_access_mask(hw_params, acmask));
266 ALSA_RETURN_ONFAIL(snd_pcm_hw_params_get_rate_min(hw_params, &ratemin, &dir));
267 ALSA_RETURN_ONFAIL(snd_pcm_hw_params_get_rate_max(hw_params, &ratemax, &dir));
268 ALSA_RETURN_ONFAIL(snd_pcm_hw_params_get_channels_min(hw_params, &chmin));
269 ALSA_RETURN_ONFAIL(snd_pcm_hw_params_get_channels_max(hw_params, &chmax));
272 if ( (r) >= ratemin && ( (r) <= ratemax || ratemax == -1) ) \
274 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_U8)) \
276 if (chmin <= 1 && 1 <= chmax) \
277 *formats |= WAVE_FORMAT_##v##M08; \
278 if (chmin <= 2 && 2 <= chmax) \
279 *formats |= WAVE_FORMAT_##v##S08; \
281 if (snd_pcm_format_mask_test( fmask, SND_PCM_FORMAT_S16_LE)) \
283 if (chmin <= 1 && 1 <= chmax) \
284 *formats |= WAVE_FORMAT_##v##M16; \
285 if (chmin <= 2 && 2 <= chmax) \
286 *formats |= WAVE_FORMAT_##v##S16; \
297 FIXME("Device has a minimum of %d channels\n", chmin);
300 /* FIXME: is sample accurate always true ?
301 ** Can we do WAVECAPS_PITCH, WAVECAPS_SYNC, or WAVECAPS_PLAYBACKRATE? */
302 *supports |= WAVECAPS_SAMPLEACCURATE;
304 /* FIXME: NONITERLEAVED and COMPLEX are not supported right now */
305 if ( snd_pcm_access_mask_test( acmask, SND_PCM_ACCESS_MMAP_INTERLEAVED ) )
306 *supports |= WAVECAPS_DIRECTSOUND;
308 /* check for volume control support */
310 if (snd_ctl_name(ctl))
313 if (snd_hctl_open(&hctl, snd_ctl_name(ctl), 0) >= 0)
316 if (!ALSA_CheckSetVolume( hctl, NULL, NULL, NULL, NULL, NULL, NULL, NULL ))
318 *supports |= WAVECAPS_VOLUME;
319 if (chmin <= 2 && 2 <= chmax)
320 *supports |= WAVECAPS_LRVOLUME;
323 snd_hctl_close(hctl);
328 if (*formats & (WAVE_FORMAT_1M08 | WAVE_FORMAT_2M08 |
329 WAVE_FORMAT_4M08 | WAVE_FORMAT_48M08 |
330 WAVE_FORMAT_96M08 | WAVE_FORMAT_1M16 |
331 WAVE_FORMAT_2M16 | WAVE_FORMAT_4M16 |
332 WAVE_FORMAT_48M16 | WAVE_FORMAT_96M16) )
333 *flags |= DSCAPS_PRIMARYMONO;
335 if (*formats & (WAVE_FORMAT_1S08 | WAVE_FORMAT_2S08 |
336 WAVE_FORMAT_4S08 | WAVE_FORMAT_48S08 |
337 WAVE_FORMAT_96S08 | WAVE_FORMAT_1S16 |
338 WAVE_FORMAT_2S16 | WAVE_FORMAT_4S16 |
339 WAVE_FORMAT_48S16 | WAVE_FORMAT_96S16) )
340 *flags |= DSCAPS_PRIMARYSTEREO;
342 if (*formats & (WAVE_FORMAT_1M08 | WAVE_FORMAT_2M08 |
343 WAVE_FORMAT_4M08 | WAVE_FORMAT_48M08 |
344 WAVE_FORMAT_96M08 | WAVE_FORMAT_1S08 |
345 WAVE_FORMAT_2S08 | WAVE_FORMAT_4S08 |
346 WAVE_FORMAT_48S08 | WAVE_FORMAT_96S08) )
347 *flags |= DSCAPS_PRIMARY8BIT;
349 if (*formats & (WAVE_FORMAT_1M16 | WAVE_FORMAT_2M16 |
350 WAVE_FORMAT_4M16 | WAVE_FORMAT_48M16 |
351 WAVE_FORMAT_96M16 | WAVE_FORMAT_1S16 |
352 WAVE_FORMAT_2S16 | WAVE_FORMAT_4S16 |
353 WAVE_FORMAT_48S16 | WAVE_FORMAT_96S16) )
354 *flags |= DSCAPS_PRIMARY16BIT;
359 /*----------------------------------------------------------------------------
360 ** ALSA_AddCommonDevice
362 ** Perform Alsa initialization common to both capture and playback
364 ** Side Effect: ww->pcname and ww->ctlname may need to be freed.
366 ** Note: this was originally coded by using snd_pcm_name(pcm), until
367 ** I discovered that with at least one version of alsa lib,
368 ** the use of a pcm named default:0 would cause snd_pcm_name() to fail.
369 ** So passing the name in is logically extraneous. Sigh.
371 static int ALSA_AddCommonDevice(snd_ctl_t *ctl, snd_pcm_t *pcm, const char *pcmname, WINE_WAVEDEV *ww)
373 snd_pcm_info_t *infop;
375 snd_pcm_info_alloca(&infop);
376 ALSA_RETURN_ONFAIL(snd_pcm_info(pcm, infop));
379 ww->pcmname = ALSA_strdup(pcmname);
383 if (ctl && snd_ctl_name(ctl))
384 ww->ctlname = ALSA_strdup(snd_ctl_name(ctl));
386 strcpy(ww->interface_name, "winealsa: ");
387 memcpy(ww->interface_name + strlen(ww->interface_name),
389 min(strlen(ww->pcmname), sizeof(ww->interface_name) - strlen("winealsa: ")));
391 strcpy(ww->ds_desc.szDrvname, "winealsa.drv");
393 memcpy(ww->ds_desc.szDesc, snd_pcm_info_get_name(infop),
394 min( (sizeof(ww->ds_desc.szDesc) - 1), strlen(snd_pcm_info_get_name(infop))) );
396 ww->ds_caps.dwMinSecondarySampleRate = DSBFREQUENCY_MIN;
397 ww->ds_caps.dwMaxSecondarySampleRate = DSBFREQUENCY_MAX;
398 ww->ds_caps.dwPrimaryBuffers = 1;
403 /*----------------------------------------------------------------------------
406 static void ALSA_FreeDevice(WINE_WAVEDEV *ww)
408 HeapFree(GetProcessHeap(), 0, ww->pcmname);
411 HeapFree(GetProcessHeap(), 0, ww->ctlname);
415 /*----------------------------------------------------------------------------
416 ** ALSA_AddDeviceToArray
418 ** Dynamically size one of the wavein or waveout arrays of devices,
419 ** and add a fully configured device node to the array.
422 static int ALSA_AddDeviceToArray(WINE_WAVEDEV *ww, WINE_WAVEDEV **array,
423 DWORD *count, DWORD *alloced, int isdefault)
427 if (*count >= *alloced)
429 (*alloced) += WAVEDEV_ALLOC_EXTENT_SIZE;
431 *array = HeapAlloc(GetProcessHeap(), 0, sizeof(*ww) * (*alloced));
433 *array = HeapReAlloc(GetProcessHeap(), 0, *array, sizeof(*ww) * (*alloced));
441 /* If this is the default, arrange for it to be the first element */
442 if (isdefault && i > 0)
444 (*array)[*count] = (*array)[0];
454 /*----------------------------------------------------------------------------
455 ** ALSA_AddPlaybackDevice
457 ** Add a given Alsa device to Wine's internal list of Playback
460 static int ALSA_AddPlaybackDevice(snd_ctl_t *ctl, snd_pcm_t *pcm, const char *pcmname, int isdefault)
465 memset(&wwo, '\0', sizeof(wwo));
467 rc = ALSA_AddCommonDevice(ctl, pcm, pcmname, &wwo);
471 MultiByteToWideChar(CP_ACP, 0, wwo.ds_desc.szDesc, -1,
472 wwo.outcaps.szPname, sizeof(wwo.outcaps.szPname)/sizeof(WCHAR));
473 wwo.outcaps.szPname[sizeof(wwo.outcaps.szPname)/sizeof(WCHAR) - 1] = '\0';
475 wwo.outcaps.wMid = MM_CREATIVE;
476 wwo.outcaps.wPid = MM_CREATIVE_SBP16_WAVEOUT;
477 wwo.outcaps.vDriverVersion = 0x0100;
479 rc = ALSA_ComputeCaps(ctl, pcm, &wwo.outcaps.wChannels, &wwo.ds_caps.dwFlags,
480 &wwo.outcaps.dwFormats, &wwo.outcaps.dwSupport);
483 WARN("Error calculating device caps for pcm [%s]\n", wwo.pcmname);
484 ALSA_FreeDevice(&wwo);
488 rc = ALSA_AddDeviceToArray(&wwo, &WOutDev, &ALSA_WodNumDevs, &ALSA_WodNumMallocedDevs, isdefault);
490 ALSA_FreeDevice(&wwo);
494 /*----------------------------------------------------------------------------
495 ** ALSA_AddCaptureDevice
497 ** Add a given Alsa device to Wine's internal list of Capture
500 static int ALSA_AddCaptureDevice(snd_ctl_t *ctl, snd_pcm_t *pcm, const char *pcmname, int isdefault)
505 memset(&wwi, '\0', sizeof(wwi));
507 rc = ALSA_AddCommonDevice(ctl, pcm, pcmname, &wwi);
511 MultiByteToWideChar(CP_ACP, 0, wwi.ds_desc.szDesc, -1,
512 wwi.incaps.szPname, sizeof(wwi.incaps.szPname) / sizeof(WCHAR));
513 wwi.incaps.szPname[sizeof(wwi.incaps.szPname)/sizeof(WCHAR) - 1] = '\0';
515 wwi.incaps.wMid = MM_CREATIVE;
516 wwi.incaps.wPid = MM_CREATIVE_SBP16_WAVEOUT;
517 wwi.incaps.vDriverVersion = 0x0100;
519 rc = ALSA_ComputeCaps(ctl, pcm, &wwi.incaps.wChannels, &wwi.ds_caps.dwFlags,
520 &wwi.incaps.dwFormats, &wwi.dwSupport);
523 WARN("Error calculating device caps for pcm [%s]\n", wwi.pcmname);
524 ALSA_FreeDevice(&wwi);
528 if (wwi.dwSupport & WAVECAPS_DIRECTSOUND)
530 FIXME("Add support for DSCapture\n");
531 wwi.dwSupport &= ~WAVECAPS_DIRECTSOUND;
534 rc = ALSA_AddDeviceToArray(&wwi, &WInDev, &ALSA_WidNumDevs, &ALSA_WidNumMallocedDevs, isdefault);
536 ALSA_FreeDevice(&wwi);
540 /*----------------------------------------------------------------------------
541 ** ALSA_CheckEnvironment
543 ** Given an Alsa style configuration node, scan its subitems
544 ** for environment variable names, and use them to find an override,
546 ** This is essentially a long and convolunted way of doing:
547 ** getenv("ALSA_CARD")
548 ** getenv("ALSA_CTL_CARD")
549 ** getenv("ALSA_PCM_CARD")
550 ** getenv("ALSA_PCM_DEVICE")
552 ** The output value is set with the atoi() of the first environment
553 ** variable found to be set, if any; otherwise, it is left alone
555 static void ALSA_CheckEnvironment(snd_config_t *node, int *outvalue)
557 snd_config_iterator_t iter;
559 for (iter = snd_config_iterator_first(node);
560 iter != snd_config_iterator_end(node);
561 iter = snd_config_iterator_next(iter))
563 snd_config_t *leaf = snd_config_iterator_entry(iter);
564 if (snd_config_get_type(leaf) == SND_CONFIG_TYPE_STRING)
567 if (snd_config_get_string(leaf, &value) >= 0)
569 char *p = getenv(value);
580 /*----------------------------------------------------------------------------
581 ** ALSA_DefaultDevices
583 ** Jump through Alsa style hoops to (hopefully) properly determine
584 ** Alsa defaults for CTL Card #, as well as for PCM Card + Device #.
585 ** We'll also find out if the user has set any of the environment
586 ** variables that specify we're to use a specific card or device.
589 ** directhw Whether to use a direct hardware device or not;
590 ** essentially switches the pcm device name from
591 ** one of 'default:X' or 'plughw:X' to "hw:X"
592 ** defctlcard If !NULL, will hold the ctl card number given
593 ** by the ALSA config as the default
594 ** defpcmcard If !NULL, default pcm card #
595 ** defpcmdev If !NULL, default pcm device #
596 ** fixedctlcard If !NULL, and the user set the appropriate
597 ** environment variable, we'll set to the
598 ** card the user specified.
599 ** fixedpcmcard If !NULL, and the user set the appropriate
600 ** environment variable, we'll set to the
601 ** card the user specified.
602 ** fixedpcmdev If !NULL, and the user set the appropriate
603 ** environment variable, we'll set to the
604 ** device the user specified.
606 ** Returns: 0 on success, < 0 on failiure
608 static int ALSA_DefaultDevices(int directhw,
610 long *defpcmcard, long *defpcmdev,
612 int *fixedpcmcard, int *fixedpcmdev)
614 snd_config_t *configp;
617 ALSA_RETURN_ONFAIL(snd_config_update());
620 if (snd_config_search(snd_config, "defaults.ctl.card", &configp) >= 0)
621 snd_config_get_integer(configp, defctlcard);
624 if (snd_config_search(snd_config, "defaults.pcm.card", &configp) >= 0)
625 snd_config_get_integer(configp, defpcmcard);
628 if (snd_config_search(snd_config, "defaults.pcm.device", &configp) >= 0)
629 snd_config_get_integer(configp, defpcmdev);
634 if (snd_config_search(snd_config, "ctl.hw.@args.CARD.default.vars", &configp) >= 0)
635 ALSA_CheckEnvironment(configp, fixedctlcard);
640 sprintf(pcmsearch, "pcm.%s.@args.CARD.default.vars", directhw ? "hw" : "plughw");
641 if (snd_config_search(snd_config, pcmsearch, &configp) >= 0)
642 ALSA_CheckEnvironment(configp, fixedpcmcard);
647 sprintf(pcmsearch, "pcm.%s.@args.DEV.default.vars", directhw ? "hw" : "plughw");
648 if (snd_config_search(snd_config, pcmsearch, &configp) >= 0)
649 ALSA_CheckEnvironment(configp, fixedpcmdev);
656 /*----------------------------------------------------------------------------
659 ** Iterate through all discoverable ALSA cards, searching
660 ** for usable PCM devices.
663 ** directhw Whether to use a direct hardware device or not;
664 ** essentially switches the pcm device name from
665 ** one of 'default:X' or 'plughw:X' to "hw:X"
666 ** defctlcard Alsa's notion of the default ctl card.
667 ** defpcmcard . pcm card
668 ** defpcmdev . pcm device
669 ** fixedctlcard If not -1, then gives the value of ALSA_CTL_CARD
670 ** or equivalent environment variable
671 ** fixedpcmcard If not -1, then gives the value of ALSA_PCM_CARD
672 ** or equivalent environment variable
673 ** fixedpcmdev If not -1, then gives the value of ALSA_PCM_DEVICE
674 ** or equivalent environment variable
676 ** Returns: 0 on success, < 0 on failiure
678 static int ALSA_ScanDevices(int directhw,
679 long defctlcard, long defpcmcard, long defpcmdev,
680 int fixedctlcard, int fixedpcmcard, int fixedpcmdev)
682 int card = fixedpcmcard;
683 int scan_devices = (fixedpcmdev == -1);
685 /*------------------------------------------------------------------------
686 ** Loop through all available cards
687 **----------------------------------------------------------------------*/
689 snd_card_next(&card);
691 for (; card != -1; snd_card_next(&card))
698 /*--------------------------------------------------------------------
699 ** Try to open a ctl handle; Wine doesn't absolutely require one,
700 ** but it does allow for volume control and for device scanning
701 **------------------------------------------------------------------*/
702 sprintf(ctlname, "default:%d", fixedctlcard == -1 ? card : fixedctlcard);
703 rc = snd_ctl_open(&ctl, ctlname, SND_CTL_NONBLOCK);
706 sprintf(ctlname, "hw:%d", fixedctlcard == -1 ? card : fixedctlcard);
707 rc = snd_ctl_open(&ctl, ctlname, SND_CTL_NONBLOCK);
712 WARN("Unable to open an alsa ctl for [%s] (pcm card %d): %s; not scanning devices\n",
713 ctlname, card, snd_strerror(rc));
714 if (fixedpcmdev == -1)
718 /*--------------------------------------------------------------------
719 ** Loop through all available devices on this card
720 **------------------------------------------------------------------*/
721 device = fixedpcmdev;
723 snd_ctl_pcm_next_device(ctl, &device);
725 for (; device != -1; snd_ctl_pcm_next_device(ctl, &device))
727 char defaultpcmname[256];
728 char plugpcmname[256];
730 char *pcmname = NULL;
733 sprintf(defaultpcmname, "default:%d", card);
734 sprintf(plugpcmname, "plughw:%d,%d", card, device);
735 sprintf(hwpcmname, "hw:%d,%d", card, device);
737 /*----------------------------------------------------------------
738 ** See if it's a valid playback device
739 **--------------------------------------------------------------*/
740 if (ALSA_TestDeviceForWine(card, device, SND_PCM_STREAM_PLAYBACK) == 0)
742 /* If we can, try the default:X device name first */
743 if (! scan_devices && ! directhw)
745 pcmname = defaultpcmname;
746 rc = snd_pcm_open(&pcm, pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
753 pcmname = directhw ? hwpcmname : plugpcmname;
754 rc = snd_pcm_open(&pcm, pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
759 if (defctlcard == card && defpcmcard == card && defpcmdev == device)
760 ALSA_AddPlaybackDevice(ctl, pcm, pcmname, TRUE);
762 ALSA_AddPlaybackDevice(ctl, pcm, pcmname, FALSE);
767 TRACE("Device [%s/%s] failed to open for playback: %s\n",
768 directhw || scan_devices ? "(N/A)" : defaultpcmname,
769 directhw ? hwpcmname : plugpcmname,
774 /*----------------------------------------------------------------
775 ** See if it's a valid capture device
776 **--------------------------------------------------------------*/
777 if (ALSA_TestDeviceForWine(card, device, SND_PCM_STREAM_CAPTURE) == 0)
779 /* If we can, try the default:X device name first */
780 if (! scan_devices && ! directhw)
782 pcmname = defaultpcmname;
783 rc = snd_pcm_open(&pcm, pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
790 pcmname = directhw ? hwpcmname : plugpcmname;
791 rc = snd_pcm_open(&pcm, pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
796 if (defctlcard == card && defpcmcard == card && defpcmdev == device)
797 ALSA_AddCaptureDevice(ctl, pcm, pcmname, TRUE);
799 ALSA_AddCaptureDevice(ctl, pcm, pcmname, FALSE);
805 TRACE("Device [%s/%s] failed to open for capture: %s\n",
806 directhw || scan_devices ? "(N/A)" : defaultpcmname,
807 directhw ? hwpcmname : plugpcmname,
819 /*--------------------------------------------------------------------
820 ** If the user has set env variables such that we're pegged to
821 ** a specific card, then break after we've examined it
822 **------------------------------------------------------------------*/
823 if (fixedpcmcard != -1)
831 /*----------------------------------------------------------------------------
832 ** ALSA_PerformDefaultScan
833 ** Perform the basic default scanning for devices within ALSA.
834 ** The hope is that this routine implements a 'correct'
835 ** scanning algorithm from the Alsalib point of view.
837 ** Note that Wine, overall, has other mechanisms to
838 ** override and specify exact CTL and PCM device names,
839 ** but this routine is imagined as the default that
840 ** 99% of users will use.
842 ** The basic algorithm is simple:
843 ** Use snd_card_next to iterate cards; within cards, use
844 ** snd_ctl_pcm_next_device to iterate through devices.
846 ** We add a little complexity by taking into consideration
847 ** environment variables such as ALSA_CARD (et all), and by
848 ** detecting when a given device matches the default specified
852 ** directhw If !0, indicates we should use the hw:X
853 ** PCM interface, rather than first try
854 ** the 'default' device followed by the plughw
855 ** device. (default and plughw do fancy mixing
856 ** and audio scaling, if they are available).
857 ** devscan If TRUE, we should scan all devices, not
858 ** juse use device 0 on each card
864 ** Invokes the ALSA_AddXXXDevice functions on valid
867 static int ALSA_PerformDefaultScan(int directhw, BOOL devscan)
869 long defctlcard = -1, defpcmcard = -1, defpcmdev = -1;
870 int fixedctlcard = -1, fixedpcmcard = -1, fixedpcmdev = -1;
873 /* FIXME: We should dlsym the new snd_names_list/snd_names_list_free 1.0.9 apis,
874 ** and use them instead of this scan mechanism if they are present */
876 rc = ALSA_DefaultDevices(directhw, &defctlcard, &defpcmcard, &defpcmdev,
877 &fixedctlcard, &fixedpcmcard, &fixedpcmdev);
881 if (fixedpcmdev == -1 && ! devscan)
884 return(ALSA_ScanDevices(directhw, defctlcard, defpcmcard, defpcmdev, fixedctlcard, fixedpcmcard, fixedpcmdev));
888 /*----------------------------------------------------------------------------
889 ** ALSA_AddUserSpecifiedDevice
890 ** Add a device given from the registry
892 static int ALSA_AddUserSpecifiedDevice(const char *ctlname, const char *pcmname)
896 snd_ctl_t *ctl = NULL;
897 snd_pcm_t *pcm = NULL;
901 rc = snd_ctl_open(&ctl, ctlname, SND_CTL_NONBLOCK);
906 rc = snd_pcm_open(&pcm, pcmname, SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
909 ALSA_AddPlaybackDevice(ctl, pcm, pcmname, FALSE);
914 rc = snd_pcm_open(&pcm, pcmname, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK);
917 ALSA_AddCaptureDevice(ctl, pcm, pcmname, FALSE);
929 /*----------------------------------------------------------------------------
931 ** Initialize the Wine Alsa sub system.
932 ** The main task is to probe for and store a list of all appropriate playback
933 ** and capture devices.
934 ** Key control points are from the registry key:
935 ** [Software\Wine\Alsa Driver]
936 ** AutoScanCards Whether or not to scan all known sound cards
937 ** and add them to Wine's list (default yes)
938 ** AutoScanDevices Whether or not to scan all known PCM devices
939 ** on each card (default no)
940 ** UseDirectHW Whether or not to use the hw:X device,
941 ** instead of the fancy default:X or plughw:X device.
942 ** The hw:X device goes straight to the hardware
943 ** without any fancy mixing or audio scaling in between.
944 ** DeviceCount If present, specifies the number of hard coded
945 ** Alsa devices to add to Wine's list; default 0
946 ** DevicePCMn Specifies the Alsa PCM devices to open for
947 ** Device n (where n goes from 1 to DeviceCount)
948 ** DeviceCTLn Specifies the Alsa control devices to open for
949 ** Device n (where n goes from 1 to DeviceCount)
951 ** Using AutoScanCards no, and then Devicexxx info
952 ** is a way to exactly specify the devices used by Wine.
955 LONG ALSA_WaveInit(void)
958 BOOL AutoScanCards = TRUE;
959 BOOL AutoScanDevices = FALSE;
960 BOOL UseDirectHW = FALSE;
961 DWORD DeviceCount = 0;
965 if (!wine_dlopen("libasound.so.2", RTLD_LAZY|RTLD_GLOBAL, NULL, 0))
967 ERR("Error: ALSA lib needs to be loaded with flags RTLD_LAZY and RTLD_GLOBAL.\n");
971 /* @@ Wine registry key: HKCU\Software\Wine\Alsa Driver */
972 rc = RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Wine\\Alsa Driver", 0, KEY_QUERY_VALUE, &key);
973 if (rc == ERROR_SUCCESS)
975 ALSA_RegGetBoolean(key, "AutoScanCards", &AutoScanCards);
976 ALSA_RegGetBoolean(key, "AutoScanDevices", &AutoScanDevices);
977 ALSA_RegGetBoolean(key, "UseDirectHW", &UseDirectHW);
978 ALSA_RegGetInt(key, "DeviceCount", &DeviceCount);
982 rc = ALSA_PerformDefaultScan(UseDirectHW, AutoScanDevices);
984 for (i = 0; i < DeviceCount; i++)
986 char *ctl_name = NULL;
987 char *pcm_name = NULL;
990 sprintf(value, "DevicePCM%d", i + 1);
991 if (ALSA_RegGetString(key, value, &pcm_name) == ERROR_SUCCESS)
993 sprintf(value, "DeviceCTL%d", i + 1);
994 ALSA_RegGetString(key, value, &ctl_name);
995 ALSA_AddUserSpecifiedDevice(ctl_name, pcm_name);
998 HeapFree(GetProcessHeap(), 0, ctl_name);
999 HeapFree(GetProcessHeap(), 0, pcm_name);
1008 #endif /* HAVE_ALSA */