mirror of
https://github.com/alsa-project/alsa-lib.git
synced 2025-10-29 05:40:25 -04:00
963 lines
23 KiB
C
963 lines
23 KiB
C
/**
|
|
* \file confmisc.c
|
|
* \ingroup Configuration
|
|
* \brief Configuration helper functions
|
|
* \author Abramo Bagnara <abramo@alsa-project.org>
|
|
* \author Jaroslav Kysela <perex@suse.cz>
|
|
* \date 2000-2001
|
|
*
|
|
* Configuration helper functions.
|
|
*
|
|
* See the \ref conffunc page for more details.
|
|
*/
|
|
/*
|
|
* Miscellaneous configuration helper functions
|
|
* Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>,
|
|
* Jaroslav Kysela <perex@suse.cz>
|
|
*
|
|
*
|
|
* This library is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU Lesser General Public License as
|
|
* published by the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with this library; if not, write to the Free Software
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
*
|
|
*/
|
|
|
|
/*! \page conffunc
|
|
|
|
\section conffunc_ref Function reference
|
|
|
|
<UL>
|
|
<LI>The getenv function - snd_func_getenv() - allows to obtain
|
|
an environment value. The result is string.
|
|
<LI>The igetenv function - snd_func_igetenv() - allows to obtain
|
|
an environment value. The result is integer.
|
|
<LI>The concat function - snd_func_concat() - merges all specified
|
|
strings. The result is string.
|
|
<LI>The datadir function - snd_func_datadir() - returns the
|
|
data directory. The result is string.
|
|
<LI>The refer function - snd_func_refer() - copies the referred
|
|
configuration. The result is same as the referred node.
|
|
<LI>The card_driver function - snd_func_card_driver() - returns
|
|
the driver identification. The result is string.
|
|
<LI>The card_id function - snd_func_card_id() - returns
|
|
the card identification. The result is string.
|
|
<LI>The pcm_id function - snd_func_pcm_id() - returns
|
|
the pcm identification. The result is string.
|
|
<LI>The private_string - snd_func_private_string() - returns
|
|
string using private_data node.
|
|
<LI>The private_card_driver - snd_func_private_card_driver() -
|
|
returns the driver identification using private_data node.
|
|
The result is string.
|
|
<LI>The private_pcm_subdevice - snd_func_private_pcm_subdevice() -
|
|
returns the PCM subdevice number using the private_data node.
|
|
The result is string.
|
|
</UL>
|
|
|
|
*/
|
|
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
#include "local.h"
|
|
|
|
/**
|
|
* \brief Get the boolean value from given ASCII string
|
|
* \param ascii The ASCII string to be parsed
|
|
* \return a positive value when success otherwise a negative error number
|
|
*/
|
|
int snd_config_get_bool_ascii(const char *ascii)
|
|
{
|
|
unsigned int k;
|
|
static struct {
|
|
const char *str;
|
|
int val;
|
|
} b[] = {
|
|
{ "0", 0 },
|
|
{ "1", 1 },
|
|
{ "false", 0 },
|
|
{ "true", 1 },
|
|
{ "no", 0 },
|
|
{ "yes", 1 },
|
|
{ "off", 0 },
|
|
{ "on", 1 },
|
|
};
|
|
for (k = 0; k < sizeof(b) / sizeof(*b); k++) {
|
|
if (strcasecmp(b[k].str, ascii) == 0)
|
|
return b[k].val;
|
|
}
|
|
return -EINVAL;
|
|
}
|
|
|
|
/**
|
|
* \brief Get the boolean value
|
|
* \param conf The configuration node to be parsed
|
|
* \return a positive value when success otherwise a negative error number
|
|
*/
|
|
int snd_config_get_bool(const snd_config_t *conf)
|
|
{
|
|
long v;
|
|
const char *str, *id;
|
|
int err;
|
|
|
|
err = snd_config_get_id(conf, &id);
|
|
if (err < 0)
|
|
return err;
|
|
err = snd_config_get_integer(conf, &v);
|
|
if (err >= 0) {
|
|
if (v < 0 || v > 1) {
|
|
_invalid_value:
|
|
SNDERR("Invalid value for %s", id);
|
|
return -EINVAL;
|
|
}
|
|
return v;
|
|
}
|
|
err = snd_config_get_string(conf, &str);
|
|
if (err < 0) {
|
|
SNDERR("Invalid type for %s", id);
|
|
return -EINVAL;
|
|
}
|
|
err = snd_config_get_bool_ascii(str);
|
|
if (err < 0)
|
|
goto _invalid_value;
|
|
return err;
|
|
}
|
|
|
|
/**
|
|
* \brief Get the control interface index from given ASCII string
|
|
* \param ascii The ASCII string to be parsed
|
|
* \return a positive value when success otherwise a negative error number
|
|
*/
|
|
int snd_config_get_ctl_iface_ascii(const char *ascii)
|
|
{
|
|
long v;
|
|
snd_ctl_elem_iface_t idx;
|
|
if (isdigit(ascii[0])) {
|
|
if (safe_strtol(ascii, &v) >= 0) {
|
|
if (v < 0 || v > SND_CTL_ELEM_IFACE_LAST)
|
|
return -EINVAL;
|
|
return v;
|
|
}
|
|
}
|
|
for (idx = 0; idx <= SND_CTL_ELEM_IFACE_LAST; idx++) {
|
|
if (strcasecmp(snd_ctl_elem_iface_name(idx), ascii) == 0)
|
|
return idx;
|
|
}
|
|
return -EINVAL;
|
|
}
|
|
|
|
/**
|
|
* \brief Get the control interface index
|
|
* \param conf The configuration node to be parsed
|
|
* \return a positive value when success otherwise a negative error number
|
|
*/
|
|
int snd_config_get_ctl_iface(const snd_config_t *conf)
|
|
{
|
|
long v;
|
|
const char *str, *id;
|
|
int err;
|
|
|
|
err = snd_config_get_id(conf, &id);
|
|
if (err < 0)
|
|
return err;
|
|
err = snd_config_get_integer(conf, &v);
|
|
if (err >= 0) {
|
|
if (v < 0 || v > SND_CTL_ELEM_IFACE_LAST) {
|
|
_invalid_value:
|
|
SNDERR("Invalid value for %s", id);
|
|
return -EINVAL;
|
|
}
|
|
return v;
|
|
}
|
|
err = snd_config_get_string(conf, &str);
|
|
if (err < 0) {
|
|
SNDERR("Invalid type for %s", id);
|
|
return -EINVAL;
|
|
}
|
|
err = snd_config_get_ctl_iface_ascii(str);
|
|
if (err < 0)
|
|
goto _invalid_value;
|
|
return err;
|
|
}
|
|
|
|
/*
|
|
* Helper functions for the configuration file
|
|
*/
|
|
|
|
/**
|
|
* \brief Get environment value
|
|
* \param dst The destination node (result type is string)
|
|
* \param root The root source node
|
|
* \param src The source node, with vars and default definition
|
|
* \param private_data The private_data node
|
|
* \return a positive value when success otherwise a negative error number
|
|
*
|
|
* Example:
|
|
\code
|
|
{
|
|
@func getenv
|
|
vars [ MY_CARD CARD C ]
|
|
default 0
|
|
}
|
|
\endcode
|
|
*/
|
|
int snd_func_getenv(snd_config_t **dst, snd_config_t *root, snd_config_t *src,
|
|
snd_config_t *private_data)
|
|
{
|
|
snd_config_t *n, *d;
|
|
snd_config_iterator_t i, next;
|
|
char *res, *def = NULL;
|
|
int idx = 0, err, hit;
|
|
|
|
err = snd_config_search(src, "vars", &n);
|
|
if (err < 0) {
|
|
SNDERR("field vars not found");
|
|
goto __error;
|
|
}
|
|
err = snd_config_evaluate(n, root, private_data, NULL);
|
|
if (err < 0) {
|
|
SNDERR("error evaluating vars");
|
|
goto __error;
|
|
}
|
|
err = snd_config_search(src, "default", &d);
|
|
if (err < 0) {
|
|
SNDERR("field default not found");
|
|
goto __error;
|
|
}
|
|
err = snd_config_evaluate(d, root, private_data, NULL);
|
|
if (err < 0) {
|
|
SNDERR("error evaluating default");
|
|
goto __error;
|
|
}
|
|
err = snd_config_get_ascii(d, &def);
|
|
if (err < 0) {
|
|
SNDERR("error getting field default");
|
|
goto __error;
|
|
}
|
|
do {
|
|
hit = 0;
|
|
snd_config_for_each(i, next, n) {
|
|
snd_config_t *n = snd_config_iterator_entry(i);
|
|
const char *id, *ptr, *env;
|
|
long i;
|
|
if (snd_config_get_id(n, &id) < 0)
|
|
continue;
|
|
if (snd_config_get_type(n) != SND_CONFIG_TYPE_STRING) {
|
|
SNDERR("field %s is not a string", id);
|
|
err = -EINVAL;
|
|
goto __error;
|
|
}
|
|
err = safe_strtol(id, &i);
|
|
if (err < 0) {
|
|
SNDERR("id of field %s is not an integer", id);
|
|
err = -EINVAL;
|
|
goto __error;
|
|
}
|
|
if (i == idx) {
|
|
idx++;
|
|
snd_config_get_string(n, &ptr);
|
|
env = getenv(ptr);
|
|
if (env != NULL && *env != '\0') {
|
|
res = strdup(env);
|
|
goto __ok;
|
|
}
|
|
hit = 1;
|
|
}
|
|
}
|
|
} while (hit);
|
|
res = def;
|
|
def = NULL;
|
|
__ok:
|
|
err = res == NULL ? -ENOMEM : 0;
|
|
if (err >= 0) {
|
|
const char *id;
|
|
err = snd_config_get_id(src, &id);
|
|
if (err >= 0)
|
|
err = snd_config_imake_string(dst, id, res);
|
|
free(res);
|
|
}
|
|
__error:
|
|
if (def)
|
|
free(def);
|
|
return err;
|
|
}
|
|
#ifndef DOC_HIDDEN
|
|
SND_DLSYM_BUILD_VERSION(snd_func_getenv, SND_CONFIG_DLSYM_VERSION_EVALUATE);
|
|
#endif
|
|
|
|
/**
|
|
* \brief Get integer environment value
|
|
* \param dst The destination node (result type is integer)
|
|
* \param root The root source node
|
|
* \param src The source node, with vars and default definition
|
|
* \param private_data The private_data node
|
|
* \return a positive value when success otherwise a negative error number
|
|
*
|
|
* Example:
|
|
\code
|
|
{
|
|
@func getenv
|
|
vars [ MY_DEVICE DEVICE D ]
|
|
default 0
|
|
}
|
|
\endcode
|
|
*/
|
|
int snd_func_igetenv(snd_config_t **dst, snd_config_t *root, snd_config_t *src,
|
|
snd_config_t *private_data)
|
|
{
|
|
snd_config_t *d;
|
|
const char *str, *id;
|
|
int err;
|
|
long v;
|
|
|
|
err = snd_func_getenv(&d, root, src, private_data);
|
|
if (err < 0)
|
|
return err;
|
|
err = snd_config_get_string(d, &str);
|
|
if (err < 0) {
|
|
snd_config_delete(d);
|
|
return err;
|
|
}
|
|
err = safe_strtol(str, &v);
|
|
if (err < 0) {
|
|
snd_config_delete(d);
|
|
return err;
|
|
}
|
|
snd_config_delete(d);
|
|
err = snd_config_get_id(src, &id);
|
|
if (err < 0)
|
|
return err;
|
|
err = snd_config_imake_integer(dst, id, v);
|
|
if (err < 0)
|
|
return err;
|
|
return 0;
|
|
}
|
|
#ifndef DOC_HIDDEN
|
|
SND_DLSYM_BUILD_VERSION(snd_func_igetenv, SND_CONFIG_DLSYM_VERSION_EVALUATE);
|
|
#endif
|
|
|
|
/**
|
|
* \brief Merge given strings
|
|
* \param dst The destination node (result type is string)
|
|
* \param root The root source node
|
|
* \param src The source node, with strings definition
|
|
* \param private_data The private_data node
|
|
* \return a positive value when success otherwise a negative error number
|
|
*
|
|
* Example (result is string "a1b2c3" ]:
|
|
\code
|
|
{
|
|
@func concat
|
|
strings [ "a1" "b2" "c3" ]
|
|
default 0
|
|
}
|
|
\endcode
|
|
*/
|
|
int snd_func_concat(snd_config_t **dst, snd_config_t *root, snd_config_t *src,
|
|
snd_config_t *private_data)
|
|
{
|
|
snd_config_t *n;
|
|
snd_config_iterator_t i, next;
|
|
const char *id;
|
|
char *res = NULL, *tmp;
|
|
int idx = 0, len = 0, len1, err, hit;
|
|
|
|
err = snd_config_search(src, "strings", &n);
|
|
if (err < 0) {
|
|
SNDERR("field strings not found");
|
|
goto __error;
|
|
}
|
|
err = snd_config_evaluate(n, root, private_data, NULL);
|
|
if (err < 0) {
|
|
SNDERR("error evaluating strings");
|
|
goto __error;
|
|
}
|
|
do {
|
|
hit = 0;
|
|
snd_config_for_each(i, next, n) {
|
|
snd_config_t *n = snd_config_iterator_entry(i);
|
|
char *ptr;
|
|
const char *id;
|
|
long i;
|
|
if (snd_config_get_id(n, &id) < 0)
|
|
continue;
|
|
err = safe_strtol(id, &i);
|
|
if (err < 0) {
|
|
SNDERR("id of field %s is not an integer", id);
|
|
err = -EINVAL;
|
|
goto __error;
|
|
}
|
|
if (i == idx) {
|
|
idx++;
|
|
snd_config_get_ascii(n, &ptr);
|
|
len1 = strlen(ptr);
|
|
tmp = realloc(res, len + len1 + 1);
|
|
if (tmp == NULL) {
|
|
free(ptr);
|
|
if (res)
|
|
free(res);
|
|
err = -ENOMEM;
|
|
goto __error;
|
|
}
|
|
memcpy(tmp + len, ptr, len1);
|
|
free(ptr);
|
|
len += len1;
|
|
tmp[len] = '\0';
|
|
res = tmp;
|
|
hit = 1;
|
|
}
|
|
}
|
|
} while (hit);
|
|
if (res == NULL) {
|
|
SNDERR("empty string is not accepted");
|
|
err = -EINVAL;
|
|
goto __error;
|
|
}
|
|
err = snd_config_get_id(src, &id);
|
|
if (err >= 0)
|
|
err = snd_config_imake_string(dst, id, res);
|
|
free(res);
|
|
__error:
|
|
return err;
|
|
}
|
|
#ifndef DOC_HIDDEN
|
|
SND_DLSYM_BUILD_VERSION(snd_func_concat, SND_CONFIG_DLSYM_VERSION_EVALUATE);
|
|
#endif
|
|
|
|
/**
|
|
* \brief Get data directory
|
|
* \param dst The destination node (result type is string)
|
|
* \param root The root source node
|
|
* \param src The source node
|
|
* \param private_data The private_data node (unused)
|
|
* \return a positive value when success otherwise a negative error number
|
|
*
|
|
* Example (result is "/usr/share/alsa" using default paths):
|
|
\code
|
|
{
|
|
@func datadir
|
|
}
|
|
\endcode
|
|
*/
|
|
int snd_func_datadir(snd_config_t **dst, snd_config_t *root ATTRIBUTE_UNUSED,
|
|
snd_config_t *src, snd_config_t *private_data ATTRIBUTE_UNUSED)
|
|
{
|
|
int err;
|
|
const char *id;
|
|
|
|
err = snd_config_get_id(src, &id);
|
|
if (err < 0)
|
|
return err;
|
|
return snd_config_imake_string(dst, id, DATADIR "/alsa");
|
|
}
|
|
#ifndef DOC_HIDDEN
|
|
SND_DLSYM_BUILD_VERSION(snd_func_datadir, SND_CONFIG_DLSYM_VERSION_EVALUATE);
|
|
#endif
|
|
|
|
static int open_ctl(long card, snd_ctl_t **ctl)
|
|
{
|
|
char name[16];
|
|
snprintf(name, sizeof(name), "hw:%li", card);
|
|
name[sizeof(name)-1] = '\0';
|
|
return snd_ctl_open(ctl, name, 0);
|
|
}
|
|
|
|
#if 0
|
|
static int string_from_integer(char **dst, long v)
|
|
{
|
|
char str[32];
|
|
char *res;
|
|
sprintf(str, "%li", v);
|
|
res = strdup(str);
|
|
if (res == NULL)
|
|
return -ENOMEM;
|
|
*dst = res;
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
/**
|
|
* \brief Get string from private_data
|
|
* \param dst The destination node (result type is string)
|
|
* \param root The root source node
|
|
* \param src The source node
|
|
* \param private_data The private_data node (type string, id == "string")
|
|
* \return a positive value when success otherwise a negative error number
|
|
*
|
|
* Example:
|
|
\code
|
|
{
|
|
@func private_string
|
|
}
|
|
\endcode
|
|
*/
|
|
int snd_func_private_string(snd_config_t **dst, snd_config_t *root ATTRIBUTE_UNUSED,
|
|
snd_config_t *src, snd_config_t *private_data)
|
|
{
|
|
int err;
|
|
const char *str, *id;
|
|
|
|
if (private_data == NULL)
|
|
return snd_config_copy(dst, src);
|
|
err = snd_config_test_id(private_data, "string");
|
|
if (err) {
|
|
SNDERR("field string not found");
|
|
return -EINVAL;
|
|
}
|
|
err = snd_config_get_string(private_data, &str);
|
|
if (err < 0) {
|
|
SNDERR("field string is not a string");
|
|
return err;
|
|
}
|
|
err = snd_config_get_id(src, &id);
|
|
if (err >= 0)
|
|
err = snd_config_imake_string(dst, id, str);
|
|
return err;
|
|
}
|
|
#ifndef DOC_HIDDEN
|
|
SND_DLSYM_BUILD_VERSION(snd_func_private_string, SND_CONFIG_DLSYM_VERSION_EVALUATE);
|
|
#endif
|
|
|
|
#ifndef DOC_HIDDEN
|
|
int snd_determine_driver(int card, char **driver)
|
|
{
|
|
snd_ctl_t *ctl = NULL;
|
|
snd_ctl_card_info_t *info;
|
|
char *res = NULL;
|
|
int err;
|
|
|
|
assert(card >= 0 && card <= 32);
|
|
err = open_ctl(card, &ctl);
|
|
if (err < 0) {
|
|
SNDERR("could not open control for card %li", card);
|
|
goto __error;
|
|
}
|
|
snd_ctl_card_info_alloca(&info);
|
|
err = snd_ctl_card_info(ctl, info);
|
|
if (err < 0) {
|
|
SNDERR("snd_ctl_card_info error: %s", snd_strerror(err));
|
|
goto __error;
|
|
}
|
|
res = strdup(snd_ctl_card_info_get_driver(info));
|
|
if (res == NULL)
|
|
err = -ENOMEM;
|
|
else {
|
|
*driver = res;
|
|
err = 0;
|
|
}
|
|
__error:
|
|
if (ctl)
|
|
snd_ctl_close(ctl);
|
|
return err;
|
|
}
|
|
#endif
|
|
|
|
/**
|
|
* \brief Get driver identification using private_data
|
|
* \param dst The destination node (result type is string)
|
|
* \param root The root source node
|
|
* \param src The source node
|
|
* \param private_data The private_data node (type = integer, id = "card")
|
|
* \return a positive value when success otherwise a negative error number
|
|
*
|
|
* Example:
|
|
\code
|
|
{
|
|
@func private_card_driver
|
|
}
|
|
\endcode
|
|
*/
|
|
int snd_func_private_card_driver(snd_config_t **dst, snd_config_t *root ATTRIBUTE_UNUSED, snd_config_t *src,
|
|
snd_config_t *private_data)
|
|
{
|
|
char *driver;
|
|
const char *id;
|
|
int err;
|
|
long card;
|
|
|
|
err = snd_config_test_id(private_data, "card");
|
|
if (err) {
|
|
SNDERR("field card not found");
|
|
return -EINVAL;
|
|
}
|
|
err = snd_config_get_integer(private_data, &card);
|
|
if (err < 0) {
|
|
SNDERR("field card is not an integer");
|
|
return err;
|
|
}
|
|
if ((err = snd_determine_driver(card, &driver)) < 0)
|
|
return err;
|
|
err = snd_config_get_id(src, &id);
|
|
if (err >= 0)
|
|
err = snd_config_imake_string(dst, id, driver);
|
|
free(driver);
|
|
return err;
|
|
}
|
|
#ifndef DOC_HIDDEN
|
|
SND_DLSYM_BUILD_VERSION(snd_func_private_card_driver, SND_CONFIG_DLSYM_VERSION_EVALUATE);
|
|
#endif
|
|
|
|
/**
|
|
* \brief Get driver identification
|
|
* \param dst The destination node (result type is string)
|
|
* \param root The root source node
|
|
* \param src The source node
|
|
* \param private_data The private_data node
|
|
* \return a positive value when success otherwise a negative error number
|
|
*
|
|
* Example:
|
|
\code
|
|
{
|
|
@func card_driver
|
|
card 0
|
|
}
|
|
\endcode
|
|
*/
|
|
int snd_func_card_driver(snd_config_t **dst, snd_config_t *root, snd_config_t *src,
|
|
snd_config_t *private_data)
|
|
{
|
|
snd_config_t *n, *val;
|
|
char *str;
|
|
long v;
|
|
int err;
|
|
|
|
err = snd_config_search(src, "card", &n);
|
|
if (err < 0) {
|
|
SNDERR("field card not found");
|
|
return err;
|
|
}
|
|
err = snd_config_evaluate(n, root, private_data, NULL);
|
|
if (err < 0) {
|
|
SNDERR("error evaluating card");
|
|
return err;
|
|
}
|
|
err = snd_config_get_ascii(n, &str);
|
|
if (err < 0) {
|
|
SNDERR("field card is not an integer or a string");
|
|
return err;
|
|
}
|
|
v = snd_card_get_index(str);
|
|
if (v < 0) {
|
|
SNDERR("cannot find card '%s'", str);
|
|
free(str);
|
|
return v;
|
|
}
|
|
free(str);
|
|
err = snd_config_imake_integer(&val, "card", v);
|
|
if (err < 0)
|
|
return err;
|
|
err = snd_func_private_card_driver(dst, root, src, val);
|
|
snd_config_delete(val);
|
|
return err;
|
|
}
|
|
#ifndef DOC_HIDDEN
|
|
SND_DLSYM_BUILD_VERSION(snd_func_card_driver, SND_CONFIG_DLSYM_VERSION_EVALUATE);
|
|
#endif
|
|
|
|
/**
|
|
* \brief Get card identification
|
|
* \param dst The destination node (result type is string)
|
|
* \param root The root source node
|
|
* \param src The source node
|
|
* \param private_data The private_data node
|
|
* \return a positive value when success otherwise a negative error number
|
|
*
|
|
* Example:
|
|
\code
|
|
{
|
|
@func card_id
|
|
card 0
|
|
}
|
|
\endcode
|
|
*/
|
|
int snd_func_card_id(snd_config_t **dst, snd_config_t *root, snd_config_t *src,
|
|
snd_config_t *private_data)
|
|
{
|
|
snd_config_t *n;
|
|
char *res = NULL;
|
|
snd_ctl_t *ctl = NULL;
|
|
snd_ctl_card_info_t *info;
|
|
const char *id;
|
|
long v;
|
|
int err;
|
|
|
|
err = snd_config_search(src, "card", &n);
|
|
if (err < 0) {
|
|
SNDERR("field card not found");
|
|
goto __error;
|
|
}
|
|
err = snd_config_evaluate(n, root, private_data, NULL);
|
|
if (err < 0) {
|
|
SNDERR("error evaluating card");
|
|
goto __error;
|
|
}
|
|
err = snd_config_get_integer(n, &v);
|
|
if (err < 0) {
|
|
SNDERR("field card is not an integer");
|
|
goto __error;
|
|
}
|
|
err = open_ctl(v, &ctl);
|
|
if (err < 0) {
|
|
SNDERR("could not open control for card %li", v);
|
|
goto __error;
|
|
}
|
|
snd_ctl_card_info_alloca(&info);
|
|
err = snd_ctl_card_info(ctl, info);
|
|
if (err < 0) {
|
|
SNDERR("snd_ctl_card_info error: %s", snd_strerror(err));
|
|
goto __error;
|
|
}
|
|
res = strdup(snd_ctl_card_info_get_id(info));
|
|
if (res == NULL) {
|
|
err = -ENOMEM;
|
|
goto __error;
|
|
}
|
|
err = snd_config_get_id(src, &id);
|
|
if (err >= 0)
|
|
err = snd_config_imake_string(dst, id, res);
|
|
free(res);
|
|
__error:
|
|
if (ctl)
|
|
snd_ctl_close(ctl);
|
|
return err;
|
|
}
|
|
#ifndef DOC_HIDDEN
|
|
SND_DLSYM_BUILD_VERSION(snd_func_card_id, SND_CONFIG_DLSYM_VERSION_EVALUATE);
|
|
#endif
|
|
|
|
/**
|
|
* \brief Get pcm identification
|
|
* \param dst The destination node (result type is string)
|
|
* \param root The root source node
|
|
* \param src The source node
|
|
* \param private_data The private_data node
|
|
* \return a positive value when success otherwise a negative error number
|
|
*
|
|
* Example:
|
|
\code
|
|
{
|
|
@func pcm_id
|
|
card 0
|
|
device 0
|
|
subdevice 0 # optional
|
|
}
|
|
\endcode
|
|
*/
|
|
int snd_func_pcm_id(snd_config_t **dst, snd_config_t *root, snd_config_t *src, void *private_data)
|
|
{
|
|
snd_config_t *n;
|
|
snd_ctl_t *ctl = NULL;
|
|
snd_pcm_info_t *info;
|
|
const char *id;
|
|
long card, device, subdevice = 0;
|
|
int err;
|
|
|
|
err = snd_config_search(src, "card", &n);
|
|
if (err < 0) {
|
|
SNDERR("field card not found");
|
|
goto __error;
|
|
}
|
|
err = snd_config_evaluate(n, root, private_data, NULL);
|
|
if (err < 0) {
|
|
SNDERR("error evaluating card");
|
|
goto __error;
|
|
}
|
|
err = snd_config_get_integer(n, &card);
|
|
if (err < 0) {
|
|
SNDERR("field card is not an integer");
|
|
goto __error;
|
|
}
|
|
err = snd_config_search(src, "device", &n);
|
|
if (err < 0) {
|
|
SNDERR("field device not found");
|
|
goto __error;
|
|
}
|
|
err = snd_config_evaluate(n, root, private_data, NULL);
|
|
if (err < 0) {
|
|
SNDERR("error evaluating device");
|
|
goto __error;
|
|
}
|
|
err = snd_config_get_integer(n, &device);
|
|
if (err < 0) {
|
|
SNDERR("field device is not an integer");
|
|
goto __error;
|
|
}
|
|
if (snd_config_search(src, "subdevice", &n) >= 0) {
|
|
err = snd_config_evaluate(n, root, private_data, NULL);
|
|
if (err < 0) {
|
|
SNDERR("error evaluating subdevice");
|
|
goto __error;
|
|
}
|
|
err = snd_config_get_integer(n, &subdevice);
|
|
if (err < 0) {
|
|
SNDERR("field subdevice is not an integer");
|
|
goto __error;
|
|
}
|
|
}
|
|
err = open_ctl(card, &ctl);
|
|
if (err < 0) {
|
|
SNDERR("could not open control for card %li", card);
|
|
goto __error;
|
|
}
|
|
snd_pcm_info_alloca(&info);
|
|
snd_pcm_info_set_device(info, device);
|
|
snd_pcm_info_set_subdevice(info, subdevice);
|
|
err = snd_ctl_pcm_info(ctl, info);
|
|
if (err < 0) {
|
|
SNDERR("snd_ctl_pcm_info error: %s", snd_strerror(err));
|
|
goto __error;
|
|
}
|
|
err = snd_config_get_id(src, &id);
|
|
if (err >= 0)
|
|
err = snd_config_imake_string(dst, id, snd_pcm_info_get_id(info));
|
|
__error:
|
|
if (ctl)
|
|
snd_ctl_close(ctl);
|
|
return err;
|
|
}
|
|
#ifndef DOC_HIDDEN
|
|
SND_DLSYM_BUILD_VERSION(snd_func_pcm_id, SND_CONFIG_DLSYM_VERSION_EVALUATE);
|
|
#endif
|
|
|
|
/**
|
|
* \brief Get pcm subdevice using private_data
|
|
* \param dst The destination node (result type is integer)
|
|
* \param root The root source node
|
|
* \param src The source node
|
|
* \param private_data The private_data node (type = pointer, id = "pcm_handle")
|
|
* \return a positive value when success otherwise a negative error number
|
|
*
|
|
* Example:
|
|
\code
|
|
{
|
|
@func private_pcm_subdevice
|
|
}
|
|
\endcode
|
|
*/
|
|
int snd_func_private_pcm_subdevice(snd_config_t **dst, snd_config_t *root ATTRIBUTE_UNUSED,
|
|
snd_config_t *src, snd_config_t *private_data)
|
|
{
|
|
snd_pcm_info_t *info;
|
|
const char *id;
|
|
snd_pcm_t *pcm;
|
|
int err;
|
|
|
|
if (private_data == NULL)
|
|
return snd_config_copy(dst, src);
|
|
err = snd_config_test_id(private_data, "pcm_handle");
|
|
if (err) {
|
|
SNDERR("field pcm_handle not found");
|
|
return -EINVAL;
|
|
}
|
|
err = snd_config_get_pointer(private_data, (const void **)&pcm);
|
|
if (err < 0) {
|
|
SNDERR("field pcm_handle is not a pointer");
|
|
return err;
|
|
}
|
|
snd_pcm_info_alloca(&info);
|
|
err = snd_pcm_info(pcm, info);
|
|
if (err < 0) {
|
|
SNDERR("snd_ctl_pcm_info error: %s", snd_strerror(err));
|
|
return err;
|
|
}
|
|
err = snd_config_get_id(src, &id);
|
|
if (err >= 0)
|
|
err = snd_config_imake_integer(dst, id, snd_pcm_info_get_subdevice(info));
|
|
return err;
|
|
}
|
|
#ifndef DOC_HIDDEN
|
|
SND_DLSYM_BUILD_VERSION(snd_func_private_pcm_subdevice, SND_CONFIG_DLSYM_VERSION_EVALUATE);
|
|
#endif
|
|
|
|
/**
|
|
* \brief Copy the referred configuration node
|
|
* \param dst The destination node (result type is same as referred node)
|
|
* \param root The root source node (can be modified!!!)
|
|
* \param src The source node
|
|
* \param private_data The private_data node
|
|
* \return a positive value when success otherwise a negative error number
|
|
*
|
|
* Example:
|
|
\code
|
|
{
|
|
@func refer
|
|
file "/etc/myconf.conf" # optional
|
|
name "id1.id2.id3"
|
|
}
|
|
\endcode
|
|
*/
|
|
int snd_func_refer(snd_config_t **dst, snd_config_t *root, snd_config_t *src,
|
|
snd_config_t *private_data)
|
|
{
|
|
snd_config_t *n;
|
|
const char *file = NULL, *name = NULL;
|
|
int err;
|
|
|
|
err = snd_config_search(src, "file", &n);
|
|
if (err >= 0) {
|
|
err = snd_config_evaluate(n, root, private_data, NULL);
|
|
if (err < 0) {
|
|
SNDERR("error evaluating file");
|
|
goto _end;
|
|
}
|
|
err = snd_config_get_string(n, &file);
|
|
if (err < 0) {
|
|
SNDERR("file is not a string");
|
|
goto _end;
|
|
}
|
|
}
|
|
err = snd_config_search(src, "name", &n);
|
|
if (err >= 0) {
|
|
err = snd_config_evaluate(n, root, private_data, NULL);
|
|
if (err < 0) {
|
|
SNDERR("error evaluating name");
|
|
goto _end;
|
|
}
|
|
err = snd_config_get_string(n, &name);
|
|
if (err < 0) {
|
|
SNDERR("name is not a string");
|
|
goto _end;
|
|
}
|
|
}
|
|
if (!name) {
|
|
err = -EINVAL;
|
|
SNDERR("name is not specified");
|
|
goto _end;
|
|
}
|
|
if (file) {
|
|
snd_input_t *input;
|
|
err = snd_input_stdio_open(&input, file, "r");
|
|
if (err < 0) {
|
|
SNDERR("Unable to open file %s: %s", file, snd_strerror(err));
|
|
goto _end;
|
|
}
|
|
err = snd_config_load(root, input);
|
|
snd_input_close(input);
|
|
if (err < 0)
|
|
goto _end;
|
|
}
|
|
err = snd_config_search_definition(root, NULL, name, dst);
|
|
if (err >= 0) {
|
|
const char *id;
|
|
err = snd_config_get_id(src, &id);
|
|
if (err >= 0)
|
|
err = snd_config_set_id(*dst, id);
|
|
} else
|
|
SNDERR("Unable to find definition '%s'", name);
|
|
_end:
|
|
return err;
|
|
}
|
|
#ifndef DOC_HIDDEN
|
|
SND_DLSYM_BUILD_VERSION(snd_func_refer, SND_CONFIG_DLSYM_VERSION_EVALUATE);
|
|
#endif
|