Coding style and fixes in snd_card_name() function...

This commit is contained in:
Jaroslav Kysela 1998-11-27 14:53:44 +00:00
parent ee5a757438
commit 2605e4d7dd

View file

@ -30,65 +30,78 @@
#define SND_FILE_CONTROL "/dev/snd/control%i" #define SND_FILE_CONTROL "/dev/snd/control%i"
int snd_cards( void ) int snd_cards(void)
{ {
int idx, count; int idx, count;
unsigned int mask; unsigned int mask;
mask = snd_cards_mask(); mask = snd_cards_mask();
for ( idx = 0, count = 0; idx < SND_CARDS; idx++ ) { for (idx = 0, count = 0; idx < SND_CARDS; idx++) {
if ( mask & (1 << idx) ) count++; if (mask & (1 << idx))
} count++;
return count; }
return count;
} }
/* /*
* this routine uses very ugly method... * this routine uses very ugly method...
* need to do... * need to do... (use only stat on /proc/asound?)
* now is information cached over static variable
*/ */
unsigned int snd_cards_mask( void ) unsigned int snd_cards_mask(void)
{ {
int fd, idx; int fd, idx;
unsigned int mask; unsigned int mask;
char filename[32]; char filename[32];
static unsigned int save_mask = 0;
for ( idx = 0, mask = 0; idx < SND_CARDS; idx++ ) { if (save_mask)
sprintf( filename, SND_FILE_CONTROL, idx ); return save_mask;
if ( (fd = open( filename, O_RDWR )) < 0 ) continue; for (idx = 0, mask = 0; idx < SND_CARDS; idx++) {
close( fd ); sprintf(filename, SND_FILE_CONTROL, idx);
mask |= 1 << idx; if ((fd = open(filename, O_RDWR)) < 0)
} continue;
return mask; close(fd);
mask |= 1 << idx;
}
save_mask = mask;
return mask;
} }
int snd_card_name( const char *string ) int snd_card_name(const char *string)
{ {
int card, cards; int card, bitmask;
void *handle; void *handle;
struct snd_ctl_hw_info info; struct snd_ctl_hw_info info;
cards = snd_cards(); bitmask = snd_cards_mask();
if ( cards <= 0 ) return -ENODEV; if (!bitmask)
if ( !string ) return -EINVAL; return -ENODEV;
if ( (isdigit( *string ) && *(string+1) == 0) || if (!string)
(isdigit( *string ) && isdigit( *(string+1) ) && *(string+2) == 0) ) { return -EINVAL;
sscanf( string, "%i", &card ); if ((isdigit(*string) && *(string + 1) == 0) ||
card--; (isdigit(*string) && isdigit(*(string + 1)) && *(string + 2) == 0)) {
if ( card < 0 || card >= cards ) sscanf(string, "%i", &card);
return -EINVAL; card--;
return card; if (card < 0 || card > 31)
} return -EINVAL;
for ( card = 0; card < cards; card++ ) { if (card < 0 || !((1 << card) & bitmask))
if ( snd_ctl_open( &handle, card ) < 0 ) return -EINVAL;
continue; return card;
if ( snd_ctl_hw_info( handle, &info ) < 0 ) { }
snd_ctl_close( handle ); for (card = 0; card < 32; card++) {
continue; if (!((1 << card) & bitmask))
} continue;
snd_ctl_close( handle ); if (snd_ctl_open(&handle, card) < 0)
if ( !strcmp( info.id, string ) ) continue;
return card; if (snd_ctl_hw_info(handle, &info) < 0) {
} snd_ctl_close(handle);
return -ENODEV; continue;
}
snd_ctl_close(handle);
if (!strcmp(info.id, string))
return card;
}
return -ENODEV;
} }