OSDN Git Service

Changed Jaroslav Kysela's e-mail from perex@suse.cz to perex@perex.cz
[android-x86/external-alsa-lib.git] / src / control / cards.c
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307 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
42 /**
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
46  */
47 int snd_card_load(int card)
48 {
49         int open_dev;
50         char control[sizeof(SND_FILE_CONTROL) + 10];
51
52         sprintf(control, SND_FILE_CONTROL, card);
53
54         open_dev = snd_open_device(control, O_RDONLY);
55 #ifdef SUPPORT_ALOAD
56         if (open_dev < 0) {
57                 char aload[sizeof(SND_FILE_LOAD) + 10];
58                 sprintf(aload, SND_FILE_LOAD, card);
59                 open_dev = snd_open_device(aload, O_RDONLY);
60         }
61 #endif
62         if (open_dev >= 0) {
63                 close (open_dev);
64                 return 1;
65         }
66         return 0;
67 }
68
69 /**
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
73  *
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
77  * are available.
78  */
79 int snd_card_next(int *rcard)
80 {
81         int card;
82         
83         if (rcard == NULL)
84                 return -EINVAL;
85         card = *rcard;
86         card = card < 0 ? 0 : card + 1;
87         for (; card < 32; card++) {
88                 if (snd_card_load(card)) {
89                         *rcard = card;
90                         return 0;
91                 }
92         }
93         *rcard = -1;
94         return 0;
95 }
96
97 /**
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
101  *
102  * The accepted format is an integer value in ASCII representation
103  * or the card identifier (the id parameter for sound-card drivers).
104  */
105 int snd_card_get_index(const char *string)
106 {
107         int card;
108         snd_ctl_t *handle;
109         snd_ctl_card_info_t info;
110
111         if (!string || *string == '\0')
112                 return -EINVAL;
113         if ((isdigit(*string) && *(string + 1) == 0) ||
114             (isdigit(*string) && isdigit(*(string + 1)) && *(string + 2) == 0)) {
115                 if (sscanf(string, "%i", &card) != 1)
116                         return -EINVAL;
117                 if (card < 0 || card > 31)
118                         return -EINVAL;
119                 if (snd_card_load(card))
120                         return card;
121                 return -ENODEV;
122         }
123         for (card = 0; card < 32; card++) {
124                 if (! snd_card_load(card))
125                         continue;
126                 if (snd_ctl_hw_open(&handle, NULL, card, 0) < 0)
127                         continue;
128                 if (snd_ctl_card_info(handle, &info) < 0) {
129                         snd_ctl_close(handle);
130                         continue;
131                 }
132                 snd_ctl_close(handle);
133                 if (!strcmp((const char *)info.id, string))
134                         return card;
135         }
136         return -ENODEV;
137 }
138
139 /**
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
144  */
145 int snd_card_get_name(int card, char **name)
146 {
147         snd_ctl_t *handle;
148         snd_ctl_card_info_t info;
149         int err;
150         
151         if (name == NULL)
152                 return -EINVAL;
153         if ((err = snd_ctl_hw_open(&handle, NULL, card, 0)) < 0)
154                 return err;
155         if ((err = snd_ctl_card_info(handle, &info)) < 0) {
156                 snd_ctl_close(handle);
157                 return err;
158         }
159         snd_ctl_close(handle);
160         *name = strdup((const char *)info.name);
161         if (*name == NULL)
162                 return -ENOMEM;
163         return 0;
164 }
165
166 /**
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
171  */
172 int snd_card_get_longname(int card, char **name)
173 {
174         snd_ctl_t *handle;
175         snd_ctl_card_info_t info;
176         int err;
177         
178         if (name == NULL)
179                 return -EINVAL;
180         if ((err = snd_ctl_hw_open(&handle, NULL, card, 0)) < 0)
181                 return err;
182         if ((err = snd_ctl_card_info(handle, &info)) < 0) {
183                 snd_ctl_close(handle);
184                 return err;
185         }
186         snd_ctl_close(handle);
187         *name = strdup((const char *)info.longname);
188         if (*name == NULL)
189                 return -ENOMEM;
190         return 0;
191 }