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"
42 static int snd_card_load2(const char *control)
45 snd_ctl_card_info_t info;
47 open_dev = snd_open_device(control, O_RDONLY);
49 if (ioctl(open_dev, SNDRV_CTL_IOCTL_CARD_INFO, &info) < 0) {
61 static int snd_card_load1(int card)
64 char control[sizeof(SND_FILE_CONTROL) + 10];
66 sprintf(control, SND_FILE_CONTROL, card);
67 res = snd_card_load2(control);
70 char aload[sizeof(SND_FILE_LOAD) + 10];
71 sprintf(aload, SND_FILE_LOAD, card);
72 res = snd_card_load2(aload);
79 * \brief Try to load the driver for a card.
80 * \param card Card number.
81 * \return 1 if driver is present, zero if driver is not present
83 int snd_card_load(int card)
85 return !!(snd_card_load1(card) >= 0);
89 * \brief Try to determine the next card.
90 * \param rcard pointer to card number
91 * \result zero if success, otherwise a negative error code
93 * Tries to determine the next card from given card number.
94 * If card number is -1, then the first available card is
95 * returned. If the result card number is -1, no more cards
98 int snd_card_next(int *rcard)
105 card = card < 0 ? 0 : card + 1;
106 for (; card < 32; card++) {
107 if (snd_card_load(card)) {
117 * \brief Convert card string to an integer value.
118 * \param string String containing card identifier
119 * \return zero if success, otherwise a negative error code
121 * The accepted format is an integer value in ASCII representation
122 * or the card identifier (the id parameter for sound-card drivers).
123 * The control device name like /dev/snd/controlC0 is accepted, too.
125 int snd_card_get_index(const char *string)
129 snd_ctl_card_info_t info;
131 if (!string || *string == '\0')
133 if ((isdigit(*string) && *(string + 1) == 0) ||
134 (isdigit(*string) && isdigit(*(string + 1)) && *(string + 2) == 0)) {
135 if (sscanf(string, "%i", &card) != 1)
137 if (card < 0 || card > 31)
139 err = snd_card_load1(card);
144 if (string[0] == '/') /* device name */
145 return snd_card_load2(string);
146 for (card = 0; card < 32; card++) {
148 if (! snd_card_load(card))
151 if (snd_ctl_hw_open(&handle, NULL, card, 0) < 0)
153 if (snd_ctl_card_info(handle, &info) < 0) {
154 snd_ctl_close(handle);
157 snd_ctl_close(handle);
158 if (!strcmp((const char *)info.id, string))
165 * \brief Obtain the card name.
166 * \param card Card number
167 * \param name Result - card name corresponding to card number
168 * \result zero if success, otherwise a negative error code
170 * The value returned in name is allocated with strdup and should be
171 * freed when no longer used.
173 int snd_card_get_name(int card, char **name)
176 snd_ctl_card_info_t info;
181 if ((err = snd_ctl_hw_open(&handle, NULL, card, 0)) < 0)
183 if ((err = snd_ctl_card_info(handle, &info)) < 0) {
184 snd_ctl_close(handle);
187 snd_ctl_close(handle);
188 *name = strdup((const char *)info.name);
195 * \brief Obtain the card long name.
196 * \param card Card number
197 * \param name Result - card long name corresponding to card number
198 * \result zero if success, otherwise a negative error code
200 * The value returned in name is allocated with strdup and should be
201 * freed when no longer used.
203 int snd_card_get_longname(int card, char **name)
206 snd_ctl_card_info_t info;
211 if ((err = snd_ctl_hw_open(&handle, NULL, card, 0)) < 0)
213 if ((err = snd_ctl_card_info(handle, &info)) < 0) {
214 snd_ctl_close(handle);
217 snd_ctl_close(handle);
218 *name = strdup((const char *)info.longname);