alsa-lib/src/control/control.c

394 lines
9 KiB
C
Raw Normal View History

1998-08-13 15:42:56 +00:00
/*
* Control Interface - main file
* Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
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>
2000-11-20 20:10:46 +00:00
#include <stdarg.h>
1998-08-13 15:42:56 +00:00
#include <unistd.h>
1998-08-27 20:47:51 +00:00
#include <string.h>
1998-08-13 15:42:56 +00:00
#include <fcntl.h>
#include <dlfcn.h>
2001-02-09 11:20:31 +00:00
#include <sys/poll.h>
#include "control_local.h"
1998-08-13 15:42:56 +00:00
const char *snd_ctl_name(snd_ctl_t *ctl)
{
assert(ctl);
return ctl->name;
}
snd_ctl_type_t snd_ctl_type(snd_ctl_t *ctl)
1998-11-27 14:57:39 +00:00
{
assert(ctl);
return ctl->type;
1998-08-27 20:47:51 +00:00
}
int snd_ctl_close(snd_ctl_t *ctl)
1998-08-13 15:42:56 +00:00
{
1998-11-27 14:57:39 +00:00
int res;
res = ctl->ops->close(ctl);
if (ctl->name)
free(ctl->name);
free(ctl);
1998-11-27 14:57:39 +00:00
return res;
1998-08-13 15:42:56 +00:00
}
2001-02-09 11:20:31 +00:00
int snd_ctl_nonblock(snd_ctl_t *ctl, int nonblock)
{
int err;
assert(ctl);
err = ctl->ops->nonblock(ctl, nonblock);
if (err < 0)
return err;
ctl->nonblock = nonblock;
return 0;
}
int snd_ctl_async(snd_ctl_t *ctl, int sig, pid_t pid)
{
assert(ctl);
return ctl->ops->async(ctl, sig, pid);
}
int _snd_ctl_poll_descriptor(snd_ctl_t *ctl)
{
assert(ctl);
2000-09-24 09:57:26 +00:00
return ctl->ops->poll_descriptor(ctl);
}
int snd_ctl_poll_descriptors_count(snd_ctl_t *ctl, unsigned int *count)
{
assert(ctl);
assert(count);
*count = 1;
return 0;
}
int snd_ctl_poll_descriptors(snd_ctl_t *ctl, struct pollfd *pfds, unsigned int space)
{
assert(ctl);
if (space > 0) {
pfds->fd = ctl->ops->poll_descriptor(ctl);
pfds->events = POLLIN;
return 1;
}
return 0;
}
int snd_ctl_subscribe_events(snd_ctl_t *ctl, int subscribe)
{
assert(ctl);
return ctl->ops->subscribe_events(ctl, subscribe);
}
2001-02-07 15:13:15 +00:00
int snd_ctl_card_info(snd_ctl_t *ctl, snd_ctl_card_info_t *info)
1998-08-13 15:42:56 +00:00
{
assert(ctl && info);
return ctl->ops->card_info(ctl, info);
1998-08-13 15:42:56 +00:00
}
2001-02-09 11:20:31 +00:00
int snd_ctl_elem_list(snd_ctl_t *ctl, snd_ctl_elem_list_t *list)
1998-08-13 15:42:56 +00:00
{
assert(ctl && list);
2001-02-09 11:20:31 +00:00
assert(list->space == 0 || list->pids);
return ctl->ops->element_list(ctl, list);
1998-08-13 15:42:56 +00:00
}
2001-02-09 11:20:31 +00:00
int snd_ctl_elem_info(snd_ctl_t *ctl, snd_ctl_elem_info_t *info)
{
assert(ctl && info && (info->id.name[0] || info->id.numid));
2001-02-09 11:20:31 +00:00
return ctl->ops->element_info(ctl, info);
}
int snd_ctl_elem_read(snd_ctl_t *ctl, snd_ctl_elem_value_t *control)
1998-11-27 14:57:39 +00:00
{
assert(ctl && control && (control->id.name[0] || control->id.numid));
2001-02-09 11:20:31 +00:00
return ctl->ops->element_read(ctl, control);
}
int snd_ctl_elem_write(snd_ctl_t *ctl, snd_ctl_elem_value_t *control)
{
assert(ctl && control && (control->id.name[0] || control->id.numid));
2001-02-09 11:20:31 +00:00
return ctl->ops->element_write(ctl, control);
}
2001-02-05 16:04:15 +00:00
int snd_ctl_hwdep_next_device(snd_ctl_t *ctl, int *device)
{
assert(ctl && device);
return ctl->ops->hwdep_next_device(ctl, device);
}
int snd_ctl_hwdep_info(snd_ctl_t *ctl, snd_hwdep_info_t * info)
{
assert(ctl && info);
return ctl->ops->hwdep_info(ctl, info);
1999-06-22 13:18:24 +00:00
}
int snd_ctl_pcm_next_device(snd_ctl_t *ctl, int * device)
{
assert(ctl && device);
return ctl->ops->pcm_next_device(ctl, device);
}
int snd_ctl_pcm_info(snd_ctl_t *ctl, snd_pcm_info_t * info)
{
assert(ctl && info);
return ctl->ops->pcm_info(ctl, info);
}
int snd_ctl_pcm_prefer_subdevice(snd_ctl_t *ctl, int subdev)
{
assert(ctl);
return ctl->ops->pcm_prefer_subdevice(ctl, subdev);
}
int snd_ctl_rawmidi_next_device(snd_ctl_t *ctl, int * device)
{
assert(ctl && device);
return ctl->ops->rawmidi_next_device(ctl, device);
}
int snd_ctl_rawmidi_info(snd_ctl_t *ctl, snd_rawmidi_info_t * info)
{
assert(ctl && info);
return ctl->ops->rawmidi_info(ctl, info);
1998-11-27 14:57:39 +00:00
}
int snd_ctl_rawmidi_prefer_subdevice(snd_ctl_t *ctl, int subdev)
{
assert(ctl);
return ctl->ops->rawmidi_prefer_subdevice(ctl, subdev);
}
2001-02-09 11:20:31 +00:00
int snd_ctl_read(snd_ctl_t *ctl, snd_ctl_event_t *event)
1998-11-27 14:57:39 +00:00
{
assert(ctl && event);
return ctl->ops->read(ctl, event);
}
2001-02-09 11:20:31 +00:00
int snd_ctl_wait(snd_ctl_t *ctl, int timeout)
{
2001-02-09 11:20:31 +00:00
struct pollfd pfd;
int err;
err = snd_ctl_poll_descriptors(ctl, &pfd, 1);
assert(err == 1);
2001-02-09 11:20:31 +00:00
err = poll(&pfd, 1, timeout);
if (err < 0)
return -errno;
return 0;
}
int snd_ctl_open(snd_ctl_t **ctlp, const char *name)
{
const char *str;
int err;
snd_config_t *ctl_conf, *conf, *type_conf;
snd_config_iterator_t i, next;
const char *lib = NULL, *open = NULL;
int (*open_func)(snd_ctl_t **ctlp, const char *name, snd_config_t *conf);
void *h;
assert(ctlp && name);
err = snd_config_update();
if (err < 0)
return err;
err = snd_config_searchv(snd_config, &ctl_conf, "ctl", name, 0);
if (err < 0) {
2000-11-20 20:10:46 +00:00
int card;
char socket[256], sname[256];
err = sscanf(name, "hw:%d", &card);
if (err == 1)
return snd_ctl_hw_open(ctlp, name, card);
2000-12-23 16:13:38 +00:00
err = sscanf(name, "shm:%256[^,],%256[^,]", socket, sname);
2000-11-20 20:10:46 +00:00
if (err == 2)
return snd_ctl_shm_open(ctlp, name, socket, sname);
2000-11-20 20:10:46 +00:00
ERR("Unknown control %s", name);
return -ENOENT;
}
2001-02-07 11:34:33 +00:00
if (snd_config_get_type(ctl_conf) != SND_CONFIG_TYPE_COMPOUND)
return -EINVAL;
err = snd_config_search(ctl_conf, "type", &conf);
if (err < 0)
return err;
2001-02-07 11:34:33 +00:00
err = snd_config_get_string(conf, &str);
if (err < 0)
return err;
err = snd_config_searchv(snd_config, &type_conf, "ctltype", str, 0);
snd_config_for_each(i, next, type_conf) {
2001-02-07 11:34:33 +00:00
snd_config_t *n = snd_config_iterator_entry(i);
const char *id = snd_config_get_id(n);
if (strcmp(id, "comment") == 0)
continue;
2001-02-07 11:34:33 +00:00
if (strcmp(id, "lib") == 0) {
err = snd_config_get_string(n, &lib);
if (err < 0)
return -EINVAL;
continue;
}
2001-02-07 11:34:33 +00:00
if (strcmp(id, "open") == 0) {
err = snd_config_get_string(n, &open);
if (err < 0)
return -EINVAL;
continue;
return -EINVAL;
}
}
if (!open)
return -EINVAL;
if (!lib)
lib = "libasound.so";
h = dlopen(lib, RTLD_NOW);
if (!h)
return -ENOENT;
open_func = dlsym(h, open);
dlclose(h);
if (!open_func)
return -ENXIO;
return open_func(ctlp, name, ctl_conf);
}
2000-11-20 20:10:46 +00:00
void snd_ctl_elem_set_bytes(snd_ctl_elem_value_t *obj, void *data, size_t size)
{
assert(obj);
assert(size <= sizeof(obj->value.bytes.data));
memcpy(obj->value.bytes.data, data, size);
}
2001-02-09 11:20:31 +00:00
#define TYPE(v) [SND_CTL_ELEM_TYPE_##v] = #v
#define IFACE(v) [SND_CTL_ELEM_IFACE_##v] = #v
#define EVENT(v) [SND_CTL_EVENT_##v] = #v
2001-02-09 11:20:31 +00:00
const char *snd_ctl_elem_type_names[] = {
TYPE(NONE),
TYPE(BOOLEAN),
TYPE(INTEGER),
TYPE(ENUMERATED),
TYPE(BYTES),
TYPE(IEC958),
};
2001-02-09 11:20:31 +00:00
const char *snd_ctl_elem_iface_names[] = {
IFACE(CARD),
IFACE(HWDEP),
IFACE(MIXER),
IFACE(PCM),
IFACE(RAWMIDI),
IFACE(TIMER),
IFACE(SEQUENCER),
};
const char *snd_ctl_event_type_names[] = {
EVENT(ELEM),
};
2001-02-09 11:20:31 +00:00
const char *snd_ctl_elem_type_name(snd_ctl_elem_type_t type)
{
2001-02-09 11:20:31 +00:00
assert(type <= SND_CTL_ELEM_TYPE_LAST);
return snd_ctl_elem_type_names[snd_enum_to_int(type)];
}
2001-02-09 11:20:31 +00:00
const char *snd_ctl_elem_iface_name(snd_ctl_elem_iface_t iface)
{
2001-02-09 11:20:31 +00:00
assert(iface <= SND_CTL_ELEM_IFACE_LAST);
return snd_ctl_elem_iface_names[snd_enum_to_int(iface)];
}
const char *snd_ctl_event_type_name(snd_ctl_event_type_t type)
{
assert(type <= SND_CTL_EVENT_LAST);
return snd_ctl_event_type_names[snd_enum_to_int(type)];
}
2001-02-09 11:20:31 +00:00
int snd_ctl_elem_list_alloc_space(snd_ctl_elem_list_t *obj, unsigned int entries)
{
2001-02-09 11:20:31 +00:00
if (obj->pids)
free(obj->pids);
obj->pids = calloc(entries, sizeof(*obj->pids));
if (!obj->pids) {
obj->space = 0;
return -ENOMEM;
}
obj->space = entries;
return 0;
}
2001-02-09 11:20:31 +00:00
void snd_ctl_elem_list_free_space(snd_ctl_elem_list_t *obj)
{
free(obj->pids);
obj->pids = NULL;
}
unsigned int snd_ctl_event_elem_get_mask(const snd_ctl_event_t *obj)
{
assert(obj);
assert(obj->type == SND_CTL_EVENT_ELEM);
return obj->data.elem.mask;
}
unsigned int snd_ctl_event_elem_get_numid(const snd_ctl_event_t *obj)
{
assert(obj);
assert(obj->type == SND_CTL_EVENT_ELEM);
return obj->data.elem.id.numid;
}
void snd_ctl_event_elem_get_id(const snd_ctl_event_t *obj, snd_ctl_elem_id_t *ptr)
{
assert(obj && ptr);
assert(obj->type == SND_CTL_EVENT_ELEM);
*ptr = obj->data.elem.id;
}
snd_ctl_elem_iface_t snd_ctl_event_elem_get_interface(const snd_ctl_event_t *obj)
{
assert(obj);
assert(obj->type == SND_CTL_EVENT_ELEM);
return snd_int_to_enum(obj->data.elem.id.iface);
}
unsigned int snd_ctl_event_elem_get_device(const snd_ctl_event_t *obj)
{
assert(obj);
assert(obj->type == SND_CTL_EVENT_ELEM);
return obj->data.elem.id.device;
}
unsigned int snd_ctl_event_elem_get_subdevice(const snd_ctl_event_t *obj)
{
assert(obj);
assert(obj->type == SND_CTL_EVENT_ELEM);
return obj->data.elem.id.subdevice;
}
const char *snd_ctl_event_elem_get_name(const snd_ctl_event_t *obj)
{
assert(obj);
assert(obj->type == SND_CTL_EVENT_ELEM);
return obj->data.elem.id.name;
}
unsigned int snd_ctl_event_elem_get_index(const snd_ctl_event_t *obj)
{
assert(obj);
assert(obj->type == SND_CTL_EVENT_ELEM);
return obj->data.elem.id.index;
}