1998-08-13 15:42:56 +00:00
|
|
|
/*
|
|
|
|
|
* Control Interface - main file
|
1999-05-11 22:15:16 +00:00
|
|
|
* Copyright (c) 1998 by Jaroslav Kysela <perex@suse.cz>
|
1998-08-13 15:42:56 +00:00
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* This library is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
|
* published by the Free Software Foundation; either version 2 of
|
|
|
|
|
* the License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Library General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <unistd.h>
|
1999-12-11 10:50:39 +00:00
|
|
|
#include <string.h>
|
1998-08-13 15:42:56 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
1998-08-30 21:08:44 +00:00
|
|
|
#include "asoundlib.h"
|
1998-08-13 15:42:56 +00:00
|
|
|
|
1999-02-01 17:57:08 +00:00
|
|
|
#define SND_FILE_CONTROL "/dev/snd/controlC%i"
|
1999-02-03 11:10:30 +00:00
|
|
|
#define SND_FILE_LOAD "/dev/aloadC%i"
|
1998-08-13 15:42:56 +00:00
|
|
|
|
1999-01-30 18:35:52 +00:00
|
|
|
int snd_card_load(int card)
|
|
|
|
|
{
|
|
|
|
|
int open_dev;
|
|
|
|
|
char control[32];
|
|
|
|
|
|
1999-03-08 16:51:36 +00:00
|
|
|
sprintf(control, SND_FILE_CONTROL, card);
|
1999-01-30 18:35:52 +00:00
|
|
|
|
|
|
|
|
if ((open_dev=open(control, O_RDONLY)) < 0) {
|
2000-08-25 14:33:53 +00:00
|
|
|
char aload[32];
|
|
|
|
|
sprintf(aload, SND_FILE_LOAD, card);
|
|
|
|
|
open_dev = open(aload, O_RDONLY);
|
|
|
|
|
}
|
|
|
|
|
if (open_dev >= 0) {
|
1999-01-30 18:35:52 +00:00
|
|
|
close (open_dev);
|
2000-08-25 14:33:53 +00:00
|
|
|
return 0;
|
1999-01-30 18:35:52 +00:00
|
|
|
}
|
2000-08-25 14:33:53 +00:00
|
|
|
return open_dev;
|
1999-01-30 18:35:52 +00:00
|
|
|
}
|
|
|
|
|
|
1998-11-27 14:53:44 +00:00
|
|
|
int snd_cards(void)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
1998-11-27 14:53:44 +00:00
|
|
|
int idx, count;
|
|
|
|
|
unsigned int mask;
|
|
|
|
|
|
|
|
|
|
mask = snd_cards_mask();
|
|
|
|
|
for (idx = 0, count = 0; idx < SND_CARDS; idx++) {
|
|
|
|
|
if (mask & (1 << idx))
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
return count;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* this routine uses very ugly method...
|
1998-11-27 14:53:44 +00:00
|
|
|
* need to do... (use only stat on /proc/asound?)
|
|
|
|
|
* now is information cached over static variable
|
1998-08-13 15:42:56 +00:00
|
|
|
*/
|
|
|
|
|
|
1998-11-27 14:53:44 +00:00
|
|
|
unsigned int snd_cards_mask(void)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
2000-08-25 14:33:53 +00:00
|
|
|
int idx;
|
1998-11-27 14:53:44 +00:00
|
|
|
unsigned int mask;
|
|
|
|
|
static unsigned int save_mask = 0;
|
|
|
|
|
|
|
|
|
|
if (save_mask)
|
|
|
|
|
return save_mask;
|
|
|
|
|
for (idx = 0, mask = 0; idx < SND_CARDS; idx++) {
|
2000-08-25 14:33:53 +00:00
|
|
|
if (snd_card_load(idx) >= 0)
|
|
|
|
|
mask |= 1 << idx;
|
1998-11-27 14:53:44 +00:00
|
|
|
}
|
|
|
|
|
save_mask = mask;
|
|
|
|
|
return mask;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
|
|
|
|
|
2000-08-25 14:33:53 +00:00
|
|
|
int snd_card_get_index(const char *string)
|
1998-08-13 15:42:56 +00:00
|
|
|
{
|
2000-08-25 14:33:53 +00:00
|
|
|
int card;
|
1999-06-02 00:40:30 +00:00
|
|
|
snd_ctl_t *handle;
|
2000-08-25 14:33:53 +00:00
|
|
|
snd_ctl_hw_info_t info;
|
1998-08-13 15:42:56 +00:00
|
|
|
|
1999-03-08 16:51:36 +00:00
|
|
|
if (!string || *string == '\0')
|
1998-12-27 01:01:47 +00:00
|
|
|
return -EINVAL;
|
1998-11-27 14:53:44 +00:00
|
|
|
if ((isdigit(*string) && *(string + 1) == 0) ||
|
|
|
|
|
(isdigit(*string) && isdigit(*(string + 1)) && *(string + 2) == 0)) {
|
|
|
|
|
sscanf(string, "%i", &card);
|
|
|
|
|
if (card < 0 || card > 31)
|
|
|
|
|
return -EINVAL;
|
2000-08-25 14:33:53 +00:00
|
|
|
if (snd_card_load(card) >= 0)
|
|
|
|
|
return card;
|
|
|
|
|
return -EINVAL;
|
1998-11-27 14:53:44 +00:00
|
|
|
}
|
|
|
|
|
for (card = 0; card < 32; card++) {
|
2000-08-25 14:33:53 +00:00
|
|
|
if (snd_card_load(card) < 0)
|
1998-11-27 14:53:44 +00:00
|
|
|
continue;
|
|
|
|
|
if (snd_ctl_open(&handle, card) < 0)
|
|
|
|
|
continue;
|
|
|
|
|
if (snd_ctl_hw_info(handle, &info) < 0) {
|
|
|
|
|
snd_ctl_close(handle);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
snd_ctl_close(handle);
|
|
|
|
|
if (!strcmp(info.id, string))
|
|
|
|
|
return card;
|
|
|
|
|
}
|
|
|
|
|
return -ENODEV;
|
1998-08-13 15:42:56 +00:00
|
|
|
}
|
1999-12-11 10:50:39 +00:00
|
|
|
|
|
|
|
|
int snd_card_get_name(int card, char **name)
|
|
|
|
|
{
|
|
|
|
|
snd_ctl_t *handle;
|
2000-08-25 14:33:53 +00:00
|
|
|
snd_ctl_hw_info_t info;
|
1999-12-11 10:50:39 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
if (name == NULL)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if ((err = snd_ctl_open(&handle, card)) < 0)
|
|
|
|
|
return err;
|
|
|
|
|
if ((err = snd_ctl_hw_info(handle, &info)) < 0) {
|
|
|
|
|
snd_ctl_close(handle);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
snd_ctl_close(handle);
|
|
|
|
|
*name = strdup(info.name);
|
|
|
|
|
if (*name == NULL)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int snd_card_get_longname(int card, char **name)
|
|
|
|
|
{
|
|
|
|
|
snd_ctl_t *handle;
|
2000-08-25 14:33:53 +00:00
|
|
|
snd_ctl_hw_info_t info;
|
1999-12-11 10:50:39 +00:00
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
if (name == NULL)
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
if ((err = snd_ctl_open(&handle, card)) < 0)
|
|
|
|
|
return err;
|
|
|
|
|
if ((err = snd_ctl_hw_info(handle, &info)) < 0) {
|
|
|
|
|
snd_ctl_close(handle);
|
|
|
|
|
return err;
|
|
|
|
|
}
|
|
|
|
|
snd_ctl_close(handle);
|
|
|
|
|
*name = strdup(info.longname);
|
|
|
|
|
if (*name == NULL)
|
|
|
|
|
return -ENOMEM;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|