• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * \file control/cards.c
3  * \brief Basic Soundcard Operations
4  * \author Jaroslav Kysela <perex@perex.cz>
5  * \date 1998-2001
6  */
7 /*
8  *  Soundcard Operations - main file
9  *  Copyright (c) 1998 by Jaroslav Kysela <perex@perex.cz>
10  *
11  *
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.
16  *
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.
21  *
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 Street, Fifth Floor, Boston, MA  02110-1301  USA
25  *
26  */
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <fcntl.h>
34 #include <sys/ioctl.h>
35 #include "control_local.h"
36 
37 #ifndef DOC_HIDDEN
38 #define SND_FILE_CONTROL	ALSA_DEVICE_DIRECTORY "controlC%i"
39 #define SND_FILE_LOAD		ALOAD_DEVICE_DIRECTORY "aloadC%i"
40 #endif
41 
snd_card_load2(const char * control)42 static int snd_card_load2(const char *control)
43 {
44 	int open_dev;
45 	snd_ctl_card_info_t info;
46 
47 	open_dev = snd_open_device(control, O_RDONLY);
48 	if (open_dev >= 0) {
49 		if (ioctl(open_dev, SNDRV_CTL_IOCTL_CARD_INFO, &info) < 0) {
50 			int err = -errno;
51 			close(open_dev);
52 			return err;
53 		}
54 		close(open_dev);
55 		return info.card;
56 	} else {
57 		return -errno;
58 	}
59 }
60 
snd_card_load1(int card)61 static int snd_card_load1(int card)
62 {
63 	int res;
64 	char control[sizeof(SND_FILE_CONTROL) + 10];
65 
66 	sprintf(control, SND_FILE_CONTROL, card);
67 	res = snd_card_load2(control);
68 #ifdef SUPPORT_ALOAD
69 	if (res < 0) {
70 		char aload[sizeof(SND_FILE_LOAD) + 10];
71 		sprintf(aload, SND_FILE_LOAD, card);
72 		res = snd_card_load2(aload);
73 	}
74 #endif
75 	return res;
76 }
77 
78 /**
79  * \brief Try to load the driver for a card.
80  * \param card Card index.
81  * \return 1 if driver is present, zero if driver is not present.
82  */
snd_card_load(int card)83 int snd_card_load(int card)
84 {
85 	return !!(snd_card_load1(card) >= 0);
86 }
87 
88 /**
89  * \brief Iterate over physical sound cards.
90  *
91  * This function takes the index of a physical sound card and sets it to the
92  * index of the next card. If index is -1, it is set to the index of the first
93  * card. After the last card, the index is set to -1.
94  *
95  * For example, if you have 2 sound cards (with index 0 and 1), the index will
96  * be modified as follows:
97  *
98  * - -1 --> 0
99  * - 0 --> 1
100  * - 1 --> -1
101  *
102  * This does not work for virtual sound cards.
103  *
104  * \param rcard Index of current card. The index of the next card is stored
105  *        here.
106  * \result zero if success, otherwise a negative error code.
107  */
snd_card_next(int * rcard)108 int snd_card_next(int *rcard)
109 {
110 	int card;
111 
112 	if (rcard == NULL)
113 		return -EINVAL;
114 	card = *rcard;
115 	card = card < 0 ? 0 : card + 1;
116 	for (; card < SND_MAX_CARDS; card++) {
117 		if (snd_card_load(card)) {
118 			*rcard = card;
119 			return 0;
120 		}
121 	}
122 	*rcard = -1;
123 	return 0;
124 }
125 
126 /**
127  * \brief Convert a card string to the card index.
128  *
129  * This works only for physical sound cards, not for virtual cards.
130  *
131  * \param string A string identifying the card.
132  * \return The index of the card. On error, a a negative error code
133  *         is returned.
134  *
135  * The accepted formats for "string" are:
136  * - The index of the card (as listed in /proc/asound/cards), given as string
137  * - The ID of the card (as listed in /proc/asound/cards)
138  * - The control device name (like /dev/snd/controlC0)
139  */
snd_card_get_index(const char * string)140 int snd_card_get_index(const char *string)
141 {
142 	int card, err;
143 	snd_ctl_t *handle;
144 	snd_ctl_card_info_t info;
145 
146 	if (!string || *string == '\0')
147 		return -EINVAL;
148 	if ((isdigit(*string) && *(string + 1) == 0) ||
149 	    (isdigit(*string) && isdigit(*(string + 1)) && *(string + 2) == 0)) {
150 		/* We got an index */
151 		if (sscanf(string, "%i", &card) != 1)
152 			return -EINVAL;
153 		if (card < 0 || card >= SND_MAX_CARDS)
154 			return -EINVAL;
155 		err = snd_card_load1(card);
156 		if (err >= 0)
157 			return card;
158 		return err;
159 	}
160 	if (string[0] == '/')
161 		/* We got a device name */
162 		return snd_card_load2(string);
163 	/* We got in ID */
164 	for (card = 0; card < SND_MAX_CARDS; card++) {
165 #ifdef SUPPORT_ALOAD
166 		if (! snd_card_load(card))
167 			continue;
168 #endif
169 		if (snd_ctl_hw_open(&handle, NULL, card, 0) < 0)
170 			continue;
171 		if (snd_ctl_card_info(handle, &info) < 0) {
172 			snd_ctl_close(handle);
173 			continue;
174 		}
175 		snd_ctl_close(handle);
176 		if (!strcmp((const char *)info.id, string))
177 			return card;
178 	}
179 	return -ENODEV;
180 }
181 
182 /**
183  * \brief Obtain the card name.
184  *
185  * \param card The index of the card.
186  * \param name Result - card name corresponding to card index.
187  * \result zero if success, otherwise a negative error code
188  *
189  * The value returned in name is allocated with strdup and should be
190  * freed when no longer used.
191  */
snd_card_get_name(int card,char ** name)192 int snd_card_get_name(int card, char **name)
193 {
194 	snd_ctl_t *handle;
195 	snd_ctl_card_info_t info;
196 	int err;
197 
198 	if (name == NULL)
199 		return -EINVAL;
200 	if ((err = snd_ctl_hw_open(&handle, NULL, card, 0)) < 0)
201 		return err;
202 	if ((err = snd_ctl_card_info(handle, &info)) < 0) {
203 		snd_ctl_close(handle);
204 		return err;
205 	}
206 	snd_ctl_close(handle);
207 	*name = strdup((const char *)info.name);
208 	if (*name == NULL)
209 		return -ENOMEM;
210 	return 0;
211 }
212 
213 /**
214  * \brief Obtain the card long name.
215  * \param card Index of the card.
216  * \param name Result - card long name corresponding to card index.
217  * \result Zero if success, otherwise a negative error code.
218  *
219  * The value returned in name is allocated with strdup and should be
220  * freed when no longer used.
221  */
snd_card_get_longname(int card,char ** name)222 int snd_card_get_longname(int card, char **name)
223 {
224 	snd_ctl_t *handle;
225 	snd_ctl_card_info_t info;
226 	int err;
227 
228 	if (name == NULL)
229 		return -EINVAL;
230 	if ((err = snd_ctl_hw_open(&handle, NULL, card, 0)) < 0)
231 		return err;
232 	if ((err = snd_ctl_card_info(handle, &info)) < 0) {
233 		snd_ctl_close(handle);
234 		return err;
235 	}
236 	snd_ctl_close(handle);
237 	*name = strdup((const char *)info.longname);
238 	if (*name == NULL)
239 		return -ENOMEM;
240 	return 0;
241 }
242