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 42static 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 61static 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 number. 81 * \return 1 if driver is present, zero if driver is not present 82 */ 83int snd_card_load(int card) 84{ 85 return !!(snd_card_load1(card) >= 0); 86} 87 88/** 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 92 * 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 96 * are available. 97 */ 98int snd_card_next(int *rcard) 99{ 100 int card; 101 102 if (rcard == NULL) 103 return -EINVAL; 104 card = *rcard; 105 card = card < 0 ? 0 : card + 1; 106 for (; card < 32; card++) { 107 if (snd_card_load(card)) { 108 *rcard = card; 109 return 0; 110 } 111 } 112 *rcard = -1; 113 return 0; 114} 115 116/** 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 120 * 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. 124 */ 125int snd_card_get_index(const char *string) 126{ 127 int card, err; 128 snd_ctl_t *handle; 129 snd_ctl_card_info_t info; 130 131 if (!string || *string == '\0') 132 return -EINVAL; 133 if ((isdigit(*string) && *(string + 1) == 0) || 134 (isdigit(*string) && isdigit(*(string + 1)) && *(string + 2) == 0)) { 135 if (sscanf(string, "%i", &card) != 1) 136 return -EINVAL; 137 if (card < 0 || card > 31) 138 return -EINVAL; 139 err = snd_card_load1(card); 140 if (err >= 0) 141 return card; 142 return err; 143 } 144 if (string[0] == '/') /* device name */ 145 return snd_card_load2(string); 146 for (card = 0; card < 32; card++) { 147#ifdef SUPPORT_ALOAD 148 if (! snd_card_load(card)) 149 continue; 150#endif 151 if (snd_ctl_hw_open(&handle, NULL, card, 0) < 0) 152 continue; 153 if (snd_ctl_card_info(handle, &info) < 0) { 154 snd_ctl_close(handle); 155 continue; 156 } 157 snd_ctl_close(handle); 158 if (!strcmp((const char *)info.id, string)) 159 return card; 160 } 161 return -ENODEV; 162} 163 164/** 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 169 * 170 * The value returned in name is allocated with strdup and should be 171 * freed when no longer used. 172 */ 173int snd_card_get_name(int card, char **name) 174{ 175 snd_ctl_t *handle; 176 snd_ctl_card_info_t info; 177 int err; 178 179 if (name == NULL) 180 return -EINVAL; 181 if ((err = snd_ctl_hw_open(&handle, NULL, card, 0)) < 0) 182 return err; 183 if ((err = snd_ctl_card_info(handle, &info)) < 0) { 184 snd_ctl_close(handle); 185 return err; 186 } 187 snd_ctl_close(handle); 188 *name = strdup((const char *)info.name); 189 if (*name == NULL) 190 return -ENOMEM; 191 return 0; 192} 193 194/** 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 199 * 200 * The value returned in name is allocated with strdup and should be 201 * freed when no longer used. 202 */ 203int snd_card_get_longname(int card, char **name) 204{ 205 snd_ctl_t *handle; 206 snd_ctl_card_info_t info; 207 int err; 208 209 if (name == NULL) 210 return -EINVAL; 211 if ((err = snd_ctl_hw_open(&handle, NULL, card, 0)) < 0) 212 return err; 213 if ((err = snd_ctl_card_info(handle, &info)) < 0) { 214 snd_ctl_close(handle); 215 return err; 216 } 217 snd_ctl_close(handle); 218 *name = strdup((const char *)info.longname); 219 if (*name == NULL) 220 return -ENOMEM; 221 return 0; 222} 223