2 * \file control/cards.c
3 * \brief Basic Soundcard Operations
4 * \author Jaroslav Kysela <perex@perex.cz>
8 * Soundcard Operations - main file
9 * Copyright (c) 1998 by Jaroslav Kysela <perex@perex.cz>
12 * This library is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License as
14 * published by the Free Software Foundation; either version 2.1 of
15 * the License, or (at your option) any later version.
17 * This program 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
20 * GNU 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 #include <sys/ioctl.h>
35 #include "control_local.h"
38 #define SND_FILE_CONTROL ALSA_DEVICE_DIRECTORY "controlC%i"
39 #define SND_FILE_LOAD ALOAD_DEVICE_DIRECTORY "aloadC%i"
43 * \brief Try to load the driver for a card.
44 * \param card Card number.
45 * \return 1 if driver is present, zero if driver is not present
47 int snd_card_load(int card)
50 char control[sizeof(SND_FILE_CONTROL) + 10];
52 sprintf(control, SND_FILE_CONTROL, card);
54 open_dev = snd_open_device(control, O_RDONLY);
57 char aload[sizeof(SND_FILE_LOAD) + 10];
58 sprintf(aload, SND_FILE_LOAD, card);
59 open_dev = snd_open_device(aload, O_RDONLY);
70 * \brief Try to determine the next card.
71 * \param rcard pointer to card number
72 * \result zero if success, otherwise a negative error code
74 * Tries to determine the next card from given card number.
75 * If card number is -1, then the first available card is
76 * returned. If the result card number is -1, no more cards
79 int snd_card_next(int *rcard)
86 card = card < 0 ? 0 : card + 1;
87 for (; card < 32; card++) {
88 if (snd_card_load(card)) {
98 * \brief Convert card string to an integer value.
99 * \param string String containing card identifier
100 * \return zero if success, otherwise a negative error code
102 * The accepted format is an integer value in ASCII representation
103 * or the card identifier (the id parameter for sound-card drivers).
105 int snd_card_get_index(const char *string)
109 snd_ctl_card_info_t info;
111 if (!string || *string == '\0')
113 if ((isdigit(*string) && *(string + 1) == 0) ||
114 (isdigit(*string) && isdigit(*(string + 1)) && *(string + 2) == 0)) {
115 if (sscanf(string, "%i", &card) != 1)
117 if (card < 0 || card > 31)
119 if (snd_card_load(card))
123 for (card = 0; card < 32; card++) {
124 if (! snd_card_load(card))
126 if (snd_ctl_hw_open(&handle, NULL, card, 0) < 0)
128 if (snd_ctl_card_info(handle, &info) < 0) {
129 snd_ctl_close(handle);
132 snd_ctl_close(handle);
133 if (!strcmp((const char *)info.id, string))
140 * \brief Obtain the card name.
141 * \param card Card number
142 * \param name Result - card name corresponding to card number
143 * \result zero if success, otherwise a negative error code
145 int snd_card_get_name(int card, char **name)
148 snd_ctl_card_info_t info;
153 if ((err = snd_ctl_hw_open(&handle, NULL, card, 0)) < 0)
155 if ((err = snd_ctl_card_info(handle, &info)) < 0) {
156 snd_ctl_close(handle);
159 snd_ctl_close(handle);
160 *name = strdup((const char *)info.name);
167 * \brief Obtain the card long name.
168 * \param card Card number
169 * \param name Result - card long name corresponding to card number
170 * \result zero if success, otherwise a negative error code
172 int snd_card_get_longname(int card, char **name)
175 snd_ctl_card_info_t info;
180 if ((err = snd_ctl_hw_open(&handle, NULL, card, 0)) < 0)
182 if ((err = snd_ctl_card_info(handle, &info)) < 0) {
183 snd_ctl_close(handle);
186 snd_ctl_close(handle);
187 *name = strdup((const char *)info.longname);