Blame src/control/cards.c

Packit 4a16fb
/**
Packit 4a16fb
 * \file control/cards.c
Packit 4a16fb
 * \brief Basic Soundcard Operations
Packit 4a16fb
 * \author Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 * \date 1998-2001
Packit 4a16fb
 */
Packit 4a16fb
/*
Packit 4a16fb
 *  Soundcard Operations - main file
Packit 4a16fb
 *  Copyright (c) 1998 by Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 *
Packit 4a16fb
 *
Packit 4a16fb
 *   This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 *   published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 *   the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *   License along with this library; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#include <stdio.h>
Packit 4a16fb
#include <stdlib.h>
Packit 4a16fb
#include <unistd.h>
Packit 4a16fb
#include <string.h>
Packit 4a16fb
#include <ctype.h>
Packit 4a16fb
#include <fcntl.h>
Packit 4a16fb
#include <sys/ioctl.h>
Packit 4a16fb
#include "control_local.h"
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
#define SND_FILE_CONTROL	ALSA_DEVICE_DIRECTORY "controlC%i"
Packit 4a16fb
#define SND_FILE_LOAD		ALOAD_DEVICE_DIRECTORY "aloadC%i"
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
static int snd_card_load2(const char *control)
Packit 4a16fb
{
Packit 4a16fb
	int open_dev;
Packit 4a16fb
	snd_ctl_card_info_t info;
Packit 4a16fb
Packit 4a16fb
	open_dev = snd_open_device(control, O_RDONLY);
Packit 4a16fb
	if (open_dev >= 0) {
Packit 4a16fb
		if (ioctl(open_dev, SNDRV_CTL_IOCTL_CARD_INFO, &info) < 0) {
Packit 4a16fb
			int err = -errno;
Packit 4a16fb
			close(open_dev);
Packit 4a16fb
			return err;
Packit 4a16fb
		}
Packit 4a16fb
		close(open_dev);
Packit 4a16fb
		return info.card;
Packit 4a16fb
	} else {
Packit 4a16fb
		return -errno;
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int snd_card_load1(int card)
Packit 4a16fb
{
Packit 4a16fb
	int res;
Packit 4a16fb
	char control[sizeof(SND_FILE_CONTROL) + 10];
Packit 4a16fb
Packit 4a16fb
	sprintf(control, SND_FILE_CONTROL, card);
Packit 4a16fb
	res = snd_card_load2(control);
Packit 4a16fb
#ifdef SUPPORT_ALOAD
Packit 4a16fb
	if (res < 0) {
Packit 4a16fb
		char aload[sizeof(SND_FILE_LOAD) + 10];
Packit 4a16fb
		sprintf(aload, SND_FILE_LOAD, card);
Packit 4a16fb
		res = snd_card_load2(aload);
Packit 4a16fb
	}
Packit 4a16fb
#endif
Packit 4a16fb
	return res;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Try to load the driver for a card.
Packit 4a16fb
 * \param card Card number.
Packit 4a16fb
 * \return 1 if driver is present, zero if driver is not present
Packit 4a16fb
 */
Packit 4a16fb
int snd_card_load(int card)
Packit 4a16fb
{
Packit 4a16fb
	return !!(snd_card_load1(card) >= 0);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Try to determine the next card.
Packit 4a16fb
 * \param rcard pointer to card number
Packit 4a16fb
 * \result zero if success, otherwise a negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * Tries to determine the next card from given card number.
Packit 4a16fb
 * If card number is -1, then the first available card is
Packit 4a16fb
 * returned. If the result card number is -1, no more cards
Packit 4a16fb
 * are available.
Packit 4a16fb
 */
Packit 4a16fb
int snd_card_next(int *rcard)
Packit 4a16fb
{
Packit 4a16fb
	int card;
Packit 4a16fb
	
Packit 4a16fb
	if (rcard == NULL)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	card = *rcard;
Packit 4a16fb
	card = card < 0 ? 0 : card + 1;
Packit 4a16fb
	for (; card < SND_MAX_CARDS; card++) {
Packit 4a16fb
		if (snd_card_load(card)) {
Packit 4a16fb
			*rcard = card;
Packit 4a16fb
			return 0;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	*rcard = -1;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Convert card string to an integer value.
Packit 4a16fb
 * \param string String containing card identifier
Packit 4a16fb
 * \return zero if success, otherwise a negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * The accepted format is an integer value in ASCII representation
Packit 4a16fb
 * or the card identifier (the id parameter for sound-card drivers).
Packit 4a16fb
 * The control device name like /dev/snd/controlC0 is accepted, too.
Packit 4a16fb
 */
Packit 4a16fb
int snd_card_get_index(const char *string)
Packit 4a16fb
{
Packit 4a16fb
	int card, err;
Packit 4a16fb
	snd_ctl_t *handle;
Packit 4a16fb
	snd_ctl_card_info_t info;
Packit 4a16fb
Packit 4a16fb
	if (!string || *string == '\0')
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	if ((isdigit(*string) && *(string + 1) == 0) ||
Packit 4a16fb
	    (isdigit(*string) && isdigit(*(string + 1)) && *(string + 2) == 0)) {
Packit 4a16fb
		if (sscanf(string, "%i", &card) != 1)
Packit 4a16fb
			return -EINVAL;
Packit 4a16fb
		if (card < 0 || card >= SND_MAX_CARDS)
Packit 4a16fb
			return -EINVAL;
Packit 4a16fb
		err = snd_card_load1(card);
Packit 4a16fb
		if (err >= 0)
Packit 4a16fb
			return card;
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	if (string[0] == '/')	/* device name */
Packit 4a16fb
		return snd_card_load2(string);
Packit 4a16fb
	for (card = 0; card < SND_MAX_CARDS; card++) {
Packit 4a16fb
#ifdef SUPPORT_ALOAD
Packit 4a16fb
		if (! snd_card_load(card))
Packit 4a16fb
			continue;
Packit 4a16fb
#endif
Packit 4a16fb
		if (snd_ctl_hw_open(&handle, NULL, card, 0) < 0)
Packit 4a16fb
			continue;
Packit 4a16fb
		if (snd_ctl_card_info(handle, &info) < 0) {
Packit 4a16fb
			snd_ctl_close(handle);
Packit 4a16fb
			continue;
Packit 4a16fb
		}
Packit 4a16fb
		snd_ctl_close(handle);
Packit 4a16fb
		if (!strcmp((const char *)info.id, string))
Packit 4a16fb
			return card;
Packit 4a16fb
	}
Packit 4a16fb
	return -ENODEV;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Obtain the card name.
Packit 4a16fb
 * \param card Card number
Packit 4a16fb
 * \param name Result - card name corresponding to card number
Packit 4a16fb
 * \result zero if success, otherwise a negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * The value returned in name is allocated with strdup and should be
Packit 4a16fb
 * freed when no longer used.
Packit 4a16fb
 */
Packit 4a16fb
int snd_card_get_name(int card, char **name)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_t *handle;
Packit 4a16fb
	snd_ctl_card_info_t info;
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	if (name == NULL)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	if ((err = snd_ctl_hw_open(&handle, NULL, card, 0)) < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	if ((err = snd_ctl_card_info(handle, &info)) < 0) {
Packit 4a16fb
		snd_ctl_close(handle);
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	snd_ctl_close(handle);
Packit 4a16fb
	*name = strdup((const char *)info.name);
Packit 4a16fb
	if (*name == NULL)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Obtain the card long name.
Packit 4a16fb
 * \param card Card number
Packit 4a16fb
 * \param name Result - card long name corresponding to card number
Packit 4a16fb
 * \result zero if success, otherwise a negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * The value returned in name is allocated with strdup and should be
Packit 4a16fb
 * freed when no longer used.
Packit 4a16fb
 */
Packit 4a16fb
int snd_card_get_longname(int card, char **name)
Packit 4a16fb
{
Packit 4a16fb
	snd_ctl_t *handle;
Packit 4a16fb
	snd_ctl_card_info_t info;
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	if (name == NULL)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	if ((err = snd_ctl_hw_open(&handle, NULL, card, 0)) < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	if ((err = snd_ctl_card_info(handle, &info)) < 0) {
Packit 4a16fb
		snd_ctl_close(handle);
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	snd_ctl_close(handle);
Packit 4a16fb
	*name = strdup((const char *)info.longname);
Packit 4a16fb
	if (*name == NULL)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	return 0;
Packit 4a16fb
}