|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \file control/setup.c
|
|
Packit Service |
db8eaa |
* \brief Routines to setup control primitives from configuration
|
|
Packit Service |
db8eaa |
* \author Abramo Bagnara <abramo@alsa-project.org>
|
|
Packit Service |
db8eaa |
* \author Jaroslav Kysela <perex@perex.cz>
|
|
Packit Service |
db8eaa |
* \date 2001
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* Routines to setup control primitives from configuration
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
/*
|
|
Packit Service |
db8eaa |
* Control Interface - routines for setup from configuration
|
|
Packit Service |
db8eaa |
* Copyright (c) 2001 by Abramo Bagnara <abramo@alsa-project.org>
|
|
Packit Service |
db8eaa |
* Jaroslav Kysela <perex@perex.cz>
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* This library is free software; you can redistribute it and/or modify
|
|
Packit Service |
db8eaa |
* it under the terms of the GNU Lesser General Public License as
|
|
Packit Service |
db8eaa |
* published by the Free Software Foundation; either version 2.1 of
|
|
Packit Service |
db8eaa |
* the License, or (at your option) any later version.
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* This program is distributed in the hope that it will be useful,
|
|
Packit Service |
db8eaa |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
db8eaa |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit Service |
db8eaa |
* GNU Lesser General Public License for more details.
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
db8eaa |
* License along with this library; if not, write to the Free Software
|
|
Packit Service |
db8eaa |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit Service |
db8eaa |
*
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
#include <stdio.h>
|
|
Packit Service |
db8eaa |
#include <stdlib.h>
|
|
Packit Service |
db8eaa |
#include <stdarg.h>
|
|
Packit Service |
db8eaa |
#include <unistd.h>
|
|
Packit Service |
db8eaa |
#include <string.h>
|
|
Packit Service |
db8eaa |
#include <ctype.h>
|
|
Packit Service |
db8eaa |
#include "local.h"
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
#ifndef DOC_HIDDEN
|
|
Packit Service |
db8eaa |
typedef struct {
|
|
Packit Service |
db8eaa |
unsigned int lock: 1;
|
|
Packit Service |
db8eaa |
unsigned int preserve: 1;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_id_t *id;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_info_t *info;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_t *val;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_t *mask;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_t *old;
|
|
Packit Service |
db8eaa |
struct list_head list;
|
|
Packit Service |
db8eaa |
} snd_sctl_elem_t;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
struct _snd_sctl {
|
|
Packit Service |
db8eaa |
int mode;
|
|
Packit Service |
db8eaa |
snd_ctl_t *ctl;
|
|
Packit Service |
db8eaa |
struct list_head elems;
|
|
Packit Service |
db8eaa |
};
|
|
Packit Service |
db8eaa |
#endif /* DOC_HIDDEN */
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int free_elems(snd_sctl_t *h)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
int err = 0;
|
|
Packit Service |
db8eaa |
while (!list_empty(&h->elems)) {
|
|
Packit Service |
db8eaa |
snd_sctl_elem_t *elem = list_entry(h->elems.next, snd_sctl_elem_t, list);
|
|
Packit Service |
db8eaa |
snd_ctl_elem_id_free(elem->id);
|
|
Packit Service |
db8eaa |
snd_ctl_elem_info_free(elem->info);
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_free(elem->val);
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_free(elem->mask);
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_free(elem->old);
|
|
Packit Service |
db8eaa |
list_del(&elem->list);
|
|
Packit Service |
db8eaa |
free(elem);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if ((h->mode & SND_SCTL_NOFREE) == 0)
|
|
Packit Service |
db8eaa |
err = snd_ctl_close(h->ctl);
|
|
Packit Service |
db8eaa |
free(h);
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \brief Install given values to control elements
|
|
Packit Service |
db8eaa |
* \param h Setup control handle
|
|
Packit Service |
db8eaa |
* \result zero if success, otherwise a negative error code
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
int snd_sctl_install(snd_sctl_t *h)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
struct list_head *pos;
|
|
Packit Service |
db8eaa |
int err;
|
|
Packit Service |
db8eaa |
unsigned int k;
|
|
Packit Service |
db8eaa |
assert(h);
|
|
Packit Service |
db8eaa |
list_for_each(pos, &h->elems) {
|
|
Packit Service |
db8eaa |
snd_sctl_elem_t *elem = list_entry(pos, snd_sctl_elem_t, list);
|
|
Packit Service |
db8eaa |
unsigned int count;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_type_t type;
|
|
Packit Service |
db8eaa |
if (elem->lock) {
|
|
Packit Service |
db8eaa |
err = snd_ctl_elem_lock(h->ctl, elem->id);
|
|
Packit Service |
db8eaa |
if (err < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("Cannot lock ctl elem");
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
err = snd_ctl_elem_read(h->ctl, elem->old);
|
|
Packit Service |
db8eaa |
if (err < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("Cannot read ctl elem");
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
count = snd_ctl_elem_info_get_count(elem->info);
|
|
Packit Service |
db8eaa |
type = snd_ctl_elem_info_get_type(elem->info);
|
|
Packit Service |
db8eaa |
switch (type) {
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_BOOLEAN:
|
|
Packit Service |
db8eaa |
for (k = 0; k < count; ++k) {
|
|
Packit Service |
db8eaa |
int old, val, mask;
|
|
Packit Service |
db8eaa |
old = snd_ctl_elem_value_get_boolean(elem->old, k);
|
|
Packit Service |
db8eaa |
mask = snd_ctl_elem_value_get_boolean(elem->mask, k);
|
|
Packit Service |
db8eaa |
old &= ~mask;
|
|
Packit Service |
db8eaa |
if (old) {
|
|
Packit Service |
db8eaa |
val = snd_ctl_elem_value_get_boolean(elem->val, k);
|
|
Packit Service |
db8eaa |
val |= old;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_boolean(elem->val, k, val);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_INTEGER:
|
|
Packit Service |
db8eaa |
for (k = 0; k < count; ++k) {
|
|
Packit Service |
db8eaa |
long old, val, mask;
|
|
Packit Service |
db8eaa |
old = snd_ctl_elem_value_get_integer(elem->old, k);
|
|
Packit Service |
db8eaa |
mask = snd_ctl_elem_value_get_integer(elem->mask, k);
|
|
Packit Service |
db8eaa |
old &= ~mask;
|
|
Packit Service |
db8eaa |
if (old) {
|
|
Packit Service |
db8eaa |
val = snd_ctl_elem_value_get_integer(elem->val, k);
|
|
Packit Service |
db8eaa |
val |= old;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_integer(elem->val, k, val);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_ENUMERATED:
|
|
Packit Service |
db8eaa |
for (k = 0; k < count; ++k) {
|
|
Packit Service |
db8eaa |
unsigned int old, val, mask;
|
|
Packit Service |
db8eaa |
old = snd_ctl_elem_value_get_enumerated(elem->old, k);
|
|
Packit Service |
db8eaa |
mask = snd_ctl_elem_value_get_enumerated(elem->mask, k);
|
|
Packit Service |
db8eaa |
old &= ~mask;
|
|
Packit Service |
db8eaa |
if (old) {
|
|
Packit Service |
db8eaa |
val = snd_ctl_elem_value_get_enumerated(elem->val, k);
|
|
Packit Service |
db8eaa |
val |= old;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_enumerated(elem->val, k, val);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_IEC958:
|
|
Packit Service |
db8eaa |
count = sizeof(snd_aes_iec958_t);
|
|
Packit Service |
db8eaa |
/* Fall through */
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_BYTES:
|
|
Packit Service |
db8eaa |
for (k = 0; k < count; ++k) {
|
|
Packit Service |
db8eaa |
unsigned char old, val, mask;
|
|
Packit Service |
db8eaa |
old = snd_ctl_elem_value_get_byte(elem->old, k);
|
|
Packit Service |
db8eaa |
mask = snd_ctl_elem_value_get_byte(elem->mask, k);
|
|
Packit Service |
db8eaa |
old &= ~mask;
|
|
Packit Service |
db8eaa |
if (old) {
|
|
Packit Service |
db8eaa |
val = snd_ctl_elem_value_get_byte(elem->val, k);
|
|
Packit Service |
db8eaa |
val |= old;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_byte(elem->val, k, val);
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
default:
|
|
Packit Service |
db8eaa |
assert(0);
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
err = snd_ctl_elem_write(h->ctl, elem->val);
|
|
Packit Service |
db8eaa |
if (err < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("Cannot write ctl elem");
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \brief Remove (restore) previous values from control elements
|
|
Packit Service |
db8eaa |
* \param h Setup control handle
|
|
Packit Service |
db8eaa |
* \result zero if success, otherwise a negative error code
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
int snd_sctl_remove(snd_sctl_t *h)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
struct list_head *pos;
|
|
Packit Service |
db8eaa |
int err;
|
|
Packit Service |
db8eaa |
assert(h);
|
|
Packit Service |
db8eaa |
list_for_each(pos, &h->elems) {
|
|
Packit Service |
db8eaa |
snd_sctl_elem_t *elem = list_entry(pos, snd_sctl_elem_t, list);
|
|
Packit Service |
db8eaa |
if (elem->lock) {
|
|
Packit Service |
db8eaa |
err = snd_ctl_elem_unlock(h->ctl, elem->id);
|
|
Packit Service |
db8eaa |
if (err < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("Cannot unlock ctl elem");
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
/* Only restore the old value if it differs from the requested
|
|
Packit Service |
db8eaa |
* value, because if it has changed restoring the old value
|
|
Packit Service |
db8eaa |
* overrides the change. Take for example, a voice modem with
|
|
Packit Service |
db8eaa |
* a .conf that sets preserve off-hook. Start playback (on-hook
|
|
Packit Service |
db8eaa |
* to off-hook), start record (off-hook to off-hook), stop
|
|
Packit Service |
db8eaa |
* playback (off-hook to restore on-hook), stop record (on-hook
|
|
Packit Service |
db8eaa |
* to restore off-hook), Clearly you don't want to leave the
|
|
Packit Service |
db8eaa |
* modem "on the phone" now that there isn't any playback or
|
|
Packit Service |
db8eaa |
* recording active.
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
if (elem->preserve && snd_ctl_elem_value_compare(elem->val, elem->old)) {
|
|
Packit Service |
db8eaa |
err = snd_ctl_elem_write(h->ctl, elem->old);
|
|
Packit Service |
db8eaa |
if (err < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("Cannot restore ctl elem");
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_config_get_ctl_elem_enumerated(snd_config_t *n, snd_ctl_t *ctl,
|
|
Packit Service |
db8eaa |
snd_ctl_elem_info_t *info)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
const char *str;
|
|
Packit Service |
db8eaa |
long val;
|
|
Packit Service |
db8eaa |
unsigned int idx, items;
|
|
Packit Service |
db8eaa |
switch (snd_config_get_type(n)) {
|
|
Packit Service |
db8eaa |
case SND_CONFIG_TYPE_INTEGER:
|
|
Packit Service |
db8eaa |
snd_config_get_integer(n, &val;;
|
|
Packit Service |
db8eaa |
return val;
|
|
Packit Service |
db8eaa |
case SND_CONFIG_TYPE_STRING:
|
|
Packit Service |
db8eaa |
snd_config_get_string(n, &str);
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
default:
|
|
Packit Service |
db8eaa |
return -1;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
items = snd_ctl_elem_info_get_items(info);
|
|
Packit Service |
db8eaa |
for (idx = 0; idx < items; idx++) {
|
|
Packit Service |
db8eaa |
int err;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_info_set_item(info, idx);
|
|
Packit Service |
db8eaa |
err = snd_ctl_elem_info(ctl, info);
|
|
Packit Service |
db8eaa |
if (err < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("Cannot obtain info for CTL elem");
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (strcmp(str, snd_ctl_elem_info_get_item_name(info)) == 0)
|
|
Packit Service |
db8eaa |
return idx;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
return -1;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int snd_config_get_ctl_elem_value(snd_config_t *conf,
|
|
Packit Service |
db8eaa |
snd_ctl_t *ctl,
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_t *val,
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_t *mask,
|
|
Packit Service |
db8eaa |
snd_ctl_elem_info_t *info)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
int err;
|
|
Packit Service |
db8eaa |
snd_config_iterator_t i, next;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_id_t id = {0};
|
|
Packit Service |
db8eaa |
snd_ctl_elem_type_t type;
|
|
Packit Service |
db8eaa |
unsigned int count;
|
|
Packit Service |
db8eaa |
long v;
|
|
Packit Service |
db8eaa |
long idx;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_get_id(val, &id;;
|
|
Packit Service |
db8eaa |
count = snd_ctl_elem_info_get_count(info);
|
|
Packit Service |
db8eaa |
type = snd_ctl_elem_info_get_type(info);
|
|
Packit Service |
db8eaa |
if (count == 1) {
|
|
Packit Service |
db8eaa |
switch (type) {
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_BOOLEAN:
|
|
Packit Service |
db8eaa |
v = snd_config_get_bool(conf);
|
|
Packit Service |
db8eaa |
if (v >= 0) {
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_boolean(val, 0, v);
|
|
Packit Service |
db8eaa |
if (mask)
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_boolean(mask, 0, 1);
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_INTEGER:
|
|
Packit Service |
db8eaa |
err = snd_config_get_integer(conf, &v);
|
|
Packit Service |
db8eaa |
if (err == 0) {
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_integer(val, 0, v);
|
|
Packit Service |
db8eaa |
if (mask)
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_integer(mask, 0, ~0L);
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_ENUMERATED:
|
|
Packit Service |
db8eaa |
v = snd_config_get_ctl_elem_enumerated(conf, ctl, info);
|
|
Packit Service |
db8eaa |
if (v >= 0) {
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_enumerated(val, 0, v);
|
|
Packit Service |
db8eaa |
if (mask)
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_enumerated(mask, 0, ~0);
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_BYTES:
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_IEC958:
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
default:
|
|
Packit Service |
db8eaa |
SNDERR("Unknown control type: %d", type);
|
|
Packit Service |
db8eaa |
return -EINVAL;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
switch (type) {
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_IEC958:
|
|
Packit Service |
db8eaa |
count = sizeof(snd_aes_iec958_t);
|
|
Packit Service |
db8eaa |
/* Fall through */
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_BYTES:
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
const char *buf;
|
|
Packit Service |
db8eaa |
err = snd_config_get_string(conf, &buf;;
|
|
Packit Service |
db8eaa |
if (err >= 0) {
|
|
Packit Service |
db8eaa |
int c1 = 0;
|
|
Packit Service |
db8eaa |
unsigned int len = strlen(buf);
|
|
Packit Service |
db8eaa |
unsigned int idx = 0;
|
|
Packit Service |
db8eaa |
if (len % 2 != 0 || len > count * 2) {
|
|
Packit Service |
db8eaa |
_bad_content:
|
|
Packit Service |
db8eaa |
SNDERR("bad value content\n");
|
|
Packit Service |
db8eaa |
return -EINVAL;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
while (*buf) {
|
|
Packit Service |
db8eaa |
int c = *buf++;
|
|
Packit Service |
db8eaa |
if (c >= '0' && c <= '9')
|
|
Packit Service |
db8eaa |
c -= '0';
|
|
Packit Service |
db8eaa |
else if (c >= 'a' && c <= 'f')
|
|
Packit Service |
db8eaa |
c = c - 'a' + 10;
|
|
Packit Service |
db8eaa |
else if (c >= 'A' && c <= 'F')
|
|
Packit Service |
db8eaa |
c = c - 'A' + 10;
|
|
Packit Service |
db8eaa |
else {
|
|
Packit Service |
db8eaa |
goto _bad_content;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (idx % 2 == 1) {
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_byte(val, idx / 2, c1 << 4 | c);
|
|
Packit Service |
db8eaa |
if (mask)
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_byte(mask, idx / 2, 0xff);
|
|
Packit Service |
db8eaa |
} else
|
|
Packit Service |
db8eaa |
c1 = c;
|
|
Packit Service |
db8eaa |
idx++;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
default:
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (snd_config_get_type(conf) != SND_CONFIG_TYPE_COMPOUND) {
|
|
Packit Service |
db8eaa |
SNDERR("bad value type");
|
|
Packit Service |
db8eaa |
return -EINVAL;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
snd_config_for_each(i, next, conf) {
|
|
Packit Service |
db8eaa |
snd_config_t *n = snd_config_iterator_entry(i);
|
|
Packit Service |
db8eaa |
const char *id;
|
|
Packit Service |
db8eaa |
if (snd_config_get_id(n, &id) < 0)
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
err = safe_strtol(id, &idx);
|
|
Packit Service |
db8eaa |
if (err < 0 || idx < 0 || (unsigned int) idx >= count) {
|
|
Packit Service |
db8eaa |
SNDERR("bad value index");
|
|
Packit Service |
db8eaa |
return -EINVAL;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
switch (type) {
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_BOOLEAN:
|
|
Packit Service |
db8eaa |
v = snd_config_get_bool(n);
|
|
Packit Service |
db8eaa |
if (v < 0)
|
|
Packit Service |
db8eaa |
goto _bad_content;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_boolean(val, idx, v);
|
|
Packit Service |
db8eaa |
if (mask)
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_boolean(mask, idx, 1);
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_INTEGER:
|
|
Packit Service |
db8eaa |
err = snd_config_get_integer(n, &v);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
goto _bad_content;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_integer(val, idx, v);
|
|
Packit Service |
db8eaa |
if (mask)
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_integer(mask, idx, ~0L);
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_ENUMERATED:
|
|
Packit Service |
db8eaa |
v = snd_config_get_ctl_elem_enumerated(n, ctl, info);
|
|
Packit Service |
db8eaa |
if (v < 0)
|
|
Packit Service |
db8eaa |
goto _bad_content;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_enumerated(val, idx, v);
|
|
Packit Service |
db8eaa |
if (mask)
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_enumerated(mask, idx, ~0);
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_BYTES:
|
|
Packit Service |
db8eaa |
case SND_CTL_ELEM_TYPE_IEC958:
|
|
Packit Service |
db8eaa |
err = snd_config_get_integer(n, &v);
|
|
Packit Service |
db8eaa |
if (err < 0 || v < 0 || v > 255)
|
|
Packit Service |
db8eaa |
goto _bad_content;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_byte(val, idx, v);
|
|
Packit Service |
db8eaa |
if (mask)
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_byte(mask, idx, 0xff);
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
default:
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
static int add_elem(snd_sctl_t *h, snd_config_t *_conf, snd_config_t *private_data, int *quit)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_config_t *conf;
|
|
Packit Service |
db8eaa |
snd_config_iterator_t i, next;
|
|
Packit Service |
db8eaa |
int iface = SND_CTL_ELEM_IFACE_MIXER;
|
|
Packit Service |
db8eaa |
const char *name = NULL;
|
|
Packit Service |
db8eaa |
long index = 0;
|
|
Packit Service |
db8eaa |
long device = -1;
|
|
Packit Service |
db8eaa |
long subdevice = -1;
|
|
Packit Service |
db8eaa |
int lock = 0;
|
|
Packit Service |
db8eaa |
int preserve = 0;
|
|
Packit Service |
db8eaa |
int optional = 0;
|
|
Packit Service |
db8eaa |
int skip_rest = 0;
|
|
Packit Service |
db8eaa |
snd_config_t *value = NULL, *mask = NULL;
|
|
Packit Service |
db8eaa |
snd_sctl_elem_t *elem = NULL;
|
|
Packit Service |
db8eaa |
int err;
|
|
Packit Service |
db8eaa |
err = snd_config_expand(_conf, _conf, NULL, private_data, &conf;;
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
snd_config_for_each(i, next, conf) {
|
|
Packit Service |
db8eaa |
snd_config_t *n = snd_config_iterator_entry(i);
|
|
Packit Service |
db8eaa |
const char *id;
|
|
Packit Service |
db8eaa |
if (snd_config_get_id(n, &id) < 0)
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
if (strcmp(id, "comment") == 0)
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
if (strcmp(id, "iface") == 0 || strcmp(id, "interface") == 0) {
|
|
Packit Service |
db8eaa |
const char *ptr;
|
|
Packit Service |
db8eaa |
if ((err = snd_config_get_string(n, &ptr)) < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("field %s is not a string", id);
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if ((err = snd_config_get_ctl_iface_ascii(ptr)) < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("Invalid value for '%s'", id);
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
iface = err;
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (strcmp(id, "name") == 0) {
|
|
Packit Service |
db8eaa |
if ((err = snd_config_get_string(n, &name)) < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("field %s is not a string", id);
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (strcmp(id, "index") == 0) {
|
|
Packit Service |
db8eaa |
if ((err = snd_config_get_integer(n, &index)) < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("field %s is not an integer", id);
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (strcmp(id, "device") == 0) {
|
|
Packit Service |
db8eaa |
if ((err = snd_config_get_integer(n, &device)) < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("field %s is not an integer", id);
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (strcmp(id, "subdevice") == 0) {
|
|
Packit Service |
db8eaa |
if ((err = snd_config_get_integer(n, &subdevice)) < 0) {
|
|
Packit Service |
db8eaa |
SNDERR("field %s is not an integer", id);
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (strcmp(id, "lock") == 0) {
|
|
Packit Service |
db8eaa |
err = snd_config_get_bool(n);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
lock = err;
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (strcmp(id, "preserve") == 0) {
|
|
Packit Service |
db8eaa |
err = snd_config_get_bool(n);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
preserve = err;
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (strcmp(id, "value") == 0) {
|
|
Packit Service |
db8eaa |
value = n;
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (strcmp(id, "mask") == 0) {
|
|
Packit Service |
db8eaa |
mask = n;
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (strcmp(id, "optional") == 0) {
|
|
Packit Service |
db8eaa |
err = snd_config_get_bool(n);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
optional = err;
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (strcmp(id, "skip_rest") == 0) {
|
|
Packit Service |
db8eaa |
err = snd_config_get_bool(n);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
skip_rest = err;
|
|
Packit Service |
db8eaa |
continue;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
SNDERR("Unknown field %s", id);
|
|
Packit Service |
db8eaa |
return -EINVAL;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (name == NULL) {
|
|
Packit Service |
db8eaa |
SNDERR("Missing control name");
|
|
Packit Service |
db8eaa |
err = -EINVAL;
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (value == NULL) {
|
|
Packit Service |
db8eaa |
SNDERR("Missing control value");
|
|
Packit Service |
db8eaa |
err = -EINVAL;
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (device < 0)
|
|
Packit Service |
db8eaa |
device = 0;
|
|
Packit Service |
db8eaa |
if (subdevice < 0)
|
|
Packit Service |
db8eaa |
subdevice = 0;
|
|
Packit Service |
db8eaa |
elem = calloc(1, sizeof(*elem));
|
|
Packit Service |
db8eaa |
if (!elem)
|
|
Packit Service |
db8eaa |
return -ENOMEM;
|
|
Packit Service |
db8eaa |
err = snd_ctl_elem_id_malloc(&elem->id);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
err = snd_ctl_elem_info_malloc(&elem->info);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
err = snd_ctl_elem_value_malloc(&elem->val);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
err = snd_ctl_elem_value_malloc(&elem->mask);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
err = snd_ctl_elem_value_malloc(&elem->old);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
elem->lock = lock;
|
|
Packit Service |
db8eaa |
elem->preserve = preserve;
|
|
Packit Service |
db8eaa |
snd_ctl_elem_id_set_interface(elem->id, iface);
|
|
Packit Service |
db8eaa |
snd_ctl_elem_id_set_name(elem->id, name);
|
|
Packit Service |
db8eaa |
snd_ctl_elem_id_set_index(elem->id, index);
|
|
Packit Service |
db8eaa |
snd_ctl_elem_id_set_device(elem->id, device);
|
|
Packit Service |
db8eaa |
snd_ctl_elem_id_set_subdevice(elem->id, subdevice);
|
|
Packit Service |
db8eaa |
snd_ctl_elem_info_set_id(elem->info, elem->id);
|
|
Packit Service |
db8eaa |
err = snd_ctl_elem_info(h->ctl, elem->info);
|
|
Packit Service |
db8eaa |
if (err < 0) {
|
|
Packit Service |
db8eaa |
if (! optional)
|
|
Packit Service |
db8eaa |
SNDERR("Cannot obtain info for CTL elem (%s,'%s',%li,%li,%li): %s", snd_ctl_elem_iface_name(iface), name, index, device, subdevice, snd_strerror(err));
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
} else {
|
|
Packit Service |
db8eaa |
if (skip_rest)
|
|
Packit Service |
db8eaa |
*quit = 1;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_id(elem->val, elem->id);
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_set_id(elem->old, elem->id);
|
|
Packit Service |
db8eaa |
if (mask) {
|
|
Packit Service |
db8eaa |
err = snd_config_get_ctl_elem_value(value, h->ctl, elem->val, NULL, elem->info);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
err = snd_config_get_ctl_elem_value(mask, h->ctl, elem->mask, NULL, elem->info);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
} else {
|
|
Packit Service |
db8eaa |
err = snd_config_get_ctl_elem_value(value, h->ctl, elem->val, elem->mask, elem->info);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
err = snd_config_get_ctl_elem_value(value, h->ctl, elem->val, elem->mask, elem->info);
|
|
Packit Service |
db8eaa |
if (err < 0)
|
|
Packit Service |
db8eaa |
goto _err;
|
|
Packit Service |
db8eaa |
list_add_tail(&elem->list, &h->elems);
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
_err:
|
|
Packit Service |
db8eaa |
if (err < 0 && elem) {
|
|
Packit Service |
db8eaa |
if (elem->id)
|
|
Packit Service |
db8eaa |
snd_ctl_elem_id_free(elem->id);
|
|
Packit Service |
db8eaa |
if (elem->info)
|
|
Packit Service |
db8eaa |
snd_ctl_elem_info_free(elem->info);
|
|
Packit Service |
db8eaa |
if (elem->val)
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_free(elem->val);
|
|
Packit Service |
db8eaa |
if (elem->mask)
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_free(elem->mask);
|
|
Packit Service |
db8eaa |
if (elem->old)
|
|
Packit Service |
db8eaa |
snd_ctl_elem_value_free(elem->old);
|
|
Packit Service |
db8eaa |
free(elem);
|
|
Packit Service |
db8eaa |
if (err != -ENOMEM && optional)
|
|
Packit Service |
db8eaa |
err = 0; /* ignore the error */
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (conf)
|
|
Packit Service |
db8eaa |
snd_config_delete(conf);
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \brief Build setup control handle
|
|
Packit Service |
db8eaa |
* \param sctl Result - setup control handle
|
|
Packit Service |
db8eaa |
* \param handle Master control handle
|
|
Packit Service |
db8eaa |
* \param conf Setup configuration
|
|
Packit Service |
db8eaa |
* \param private_data Private data for runtime evaluation
|
|
Packit Service |
db8eaa |
* \param mode Build mode - SND_SCTL_xxxx
|
|
Packit Service |
db8eaa |
* \result zero if success, otherwise a negative error code
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
int snd_sctl_build(snd_sctl_t **sctl, snd_ctl_t *handle, snd_config_t *conf, snd_config_t *private_data, int mode)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
snd_sctl_t *h;
|
|
Packit Service |
db8eaa |
snd_config_iterator_t i, next;
|
|
Packit Service |
db8eaa |
int err, quit = 0;
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
assert(sctl);
|
|
Packit Service |
db8eaa |
assert(handle);
|
|
Packit Service |
db8eaa |
assert(conf);
|
|
Packit Service |
db8eaa |
*sctl = NULL;
|
|
Packit Service |
db8eaa |
if (snd_config_get_type(conf) != SND_CONFIG_TYPE_COMPOUND)
|
|
Packit Service |
db8eaa |
return -EINVAL;
|
|
Packit Service |
db8eaa |
h = calloc(1, sizeof(*h));
|
|
Packit Service |
db8eaa |
if (!h) {
|
|
Packit Service |
db8eaa |
if (mode & SND_SCTL_NOFREE)
|
|
Packit Service |
db8eaa |
return -ENOMEM;
|
|
Packit Service |
db8eaa |
snd_ctl_close(handle);
|
|
Packit Service |
db8eaa |
return -ENOMEM;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
h->mode = mode;
|
|
Packit Service |
db8eaa |
h->ctl = handle;
|
|
Packit Service |
db8eaa |
INIT_LIST_HEAD(&h->elems);
|
|
Packit Service |
db8eaa |
snd_config_for_each(i, next, conf) {
|
|
Packit Service |
db8eaa |
snd_config_t *n = snd_config_iterator_entry(i);
|
|
Packit Service |
db8eaa |
err = add_elem(h, n, private_data, &quit);
|
|
Packit Service |
db8eaa |
if (err < 0) {
|
|
Packit Service |
db8eaa |
free_elems(h);
|
|
Packit Service |
db8eaa |
return err;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
if (quit)
|
|
Packit Service |
db8eaa |
break;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
*sctl = h;
|
|
Packit Service |
db8eaa |
return 0;
|
|
Packit Service |
db8eaa |
}
|
|
Packit Service |
db8eaa |
|
|
Packit Service |
db8eaa |
/**
|
|
Packit Service |
db8eaa |
* \brief Free setup control handle
|
|
Packit Service |
db8eaa |
* \param sctl Setup control handle
|
|
Packit Service |
db8eaa |
* \result zero if success, otherwise a negative error code
|
|
Packit Service |
db8eaa |
*/
|
|
Packit Service |
db8eaa |
int snd_sctl_free(snd_sctl_t *sctl)
|
|
Packit Service |
db8eaa |
{
|
|
Packit Service |
db8eaa |
assert(sctl);
|
|
Packit Service |
db8eaa |
return free_elems(sctl);
|
|
Packit Service |
db8eaa |
}
|