Added abstraction layer to controls. Added client/server support to controls. Cleaned private_data use for PCMs. Cleaned aserver

This commit is contained in:
Abramo Bagnara 2000-09-11 15:49:10 +00:00
parent 1acf1f3fb9
commit df35e8457a
18 changed files with 1623 additions and 778 deletions

View file

@ -1,6 +1,7 @@
EXTRA_LTLIBRARIES = libcontrol.la
libcontrol_la_SOURCES = cards.c control.c controls.c bag.c defaults.c
libcontrol_la_SOURCES = cards.c controls.c bag.c defaults.c \
control.c control_hw.c control_client.c
all: libcontrol.la

View file

@ -106,7 +106,7 @@ int snd_card_get_index(const char *string)
for (card = 0; card < 32; card++) {
if (snd_card_load(card) < 0)
continue;
if (snd_ctl_open(&handle, card) < 0)
if (snd_ctl_hw_open(&handle, card) < 0)
continue;
if (snd_ctl_hw_info(handle, &info) < 0) {
snd_ctl_close(handle);
@ -127,7 +127,7 @@ int snd_card_get_name(int card, char **name)
if (name == NULL)
return -EINVAL;
if ((err = snd_ctl_open(&handle, card)) < 0)
if ((err = snd_ctl_hw_open(&handle, card)) < 0)
return err;
if ((err = snd_ctl_hw_info(handle, &info)) < 0) {
snd_ctl_close(handle);
@ -148,7 +148,7 @@ int snd_card_get_longname(int card, char **name)
if (name == NULL)
return -EINVAL;
if ((err = snd_ctl_open(&handle, card)) < 0)
if ((err = snd_ctl_hw_open(&handle, card)) < 0)
return err;
if ((err = snd_ctl_hw_info(handle, &info)) < 0) {
snd_ctl_close(handle);

View file

@ -1,6 +1,6 @@
/*
* Control Interface - main file
* Copyright (c) 1998,1999,2000 by Jaroslav Kysela <perex@suse.cz>
* Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
*
*
* This library is free software; you can redistribute it and/or modify
@ -25,145 +25,98 @@
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <assert.h>
#include "asoundlib.h"
#include "control_local.h"
#define SND_FILE_CONTROL "/dev/snd/controlC%i"
#define SND_CTL_VERSION_MAX SND_PROTOCOL_VERSION(2, 0, 0)
int snd_ctl_open(snd_ctl_t **handle, int card)
snd_ctl_type_t snd_ctl_type(snd_ctl_t *ctl)
{
int fd, ver;
char filename[32];
snd_ctl_t *ctl;
*handle = NULL;
assert(card >= 0 && card < SND_CARDS);
sprintf(filename, SND_FILE_CONTROL, card);
if ((fd = open(filename, O_RDWR)) < 0) {
snd_card_load(card);
if ((fd = open(filename, O_RDWR)) < 0)
return -errno;
}
if (ioctl(fd, SND_CTL_IOCTL_PVERSION, &ver) < 0) {
close(fd);
return -errno;
}
if (SND_PROTOCOL_INCOMPATIBLE(ver, SND_CTL_VERSION_MAX)) {
close(fd);
return -SND_ERROR_INCOMPATIBLE_VERSION;
}
ctl = (snd_ctl_t *) calloc(1, sizeof(snd_ctl_t));
if (ctl == NULL) {
close(fd);
return -ENOMEM;
}
ctl->card = card;
ctl->fd = fd;
INIT_LIST_HEAD(&ctl->hlist);
*handle = ctl;
return 0;
return ctl->type;
}
int snd_ctl_close(snd_ctl_t *handle)
int snd_ctl_close(snd_ctl_t *ctl)
{
int res;
assert(handle);
res = close(handle->fd) < 0 ? -errno : 0;
free(handle);
assert(ctl);
res = ctl->ops->close(ctl);
free(ctl);
return res;
}
int snd_ctl_file_descriptor(snd_ctl_t *handle)
int snd_ctl_file_descriptor(snd_ctl_t *ctl)
{
assert(handle);
return handle->fd;
assert(ctl);
return ctl->ops->file_descriptor(ctl);
}
int snd_ctl_hw_info(snd_ctl_t *handle, snd_ctl_hw_info_t *info)
int snd_ctl_hw_info(snd_ctl_t *ctl, snd_ctl_hw_info_t *info)
{
assert(handle && info);
if (ioctl(handle->fd, SND_CTL_IOCTL_HW_INFO, info) < 0)
return -errno;
return 0;
assert(ctl && info);
return ctl->ops->hw_info(ctl, info);
}
int snd_ctl_clist(snd_ctl_t *handle, snd_control_list_t *list)
int snd_ctl_clist(snd_ctl_t *ctl, snd_control_list_t *list)
{
assert(handle && list);
if (ioctl(handle->fd, SND_CTL_IOCTL_CONTROL_LIST, list) < 0)
return -errno;
return 0;
assert(ctl && list);
return ctl->ops->clist(ctl, list);
}
int snd_ctl_cinfo(snd_ctl_t *handle, snd_control_info_t *info)
int snd_ctl_cinfo(snd_ctl_t *ctl, snd_control_info_t *info)
{
assert(handle && info && (info->id.name[0] || info->id.numid));
if (ioctl(handle->fd, SND_CTL_IOCTL_CONTROL_INFO, info) < 0)
return -errno;
return 0;
assert(ctl && info && (info->id.name[0] || info->id.numid));
return ctl->ops->cinfo(ctl, info);
}
int snd_ctl_cread(snd_ctl_t *handle, snd_control_t *control)
int snd_ctl_cread(snd_ctl_t *ctl, snd_control_t *control)
{
assert(handle && control && (control->id.name[0] || control->id.numid));
if (ioctl(handle->fd, SND_CTL_IOCTL_CONTROL_READ, control) < 0)
return -errno;
return 0;
assert(ctl && control && (control->id.name[0] || control->id.numid));
return ctl->ops->cread(ctl, control);
}
int snd_ctl_cwrite(snd_ctl_t *handle, snd_control_t *control)
int snd_ctl_cwrite(snd_ctl_t *ctl, snd_control_t *control)
{
assert(handle && control && (control->id.name[0] || control->id.numid));
if (ioctl(handle->fd, SND_CTL_IOCTL_CONTROL_WRITE, control) < 0)
return -errno;
return 0;
assert(ctl && control && (control->id.name[0] || control->id.numid));
return ctl->ops->cwrite(ctl, control);
}
int snd_ctl_hwdep_info(snd_ctl_t *handle, snd_hwdep_info_t * info)
int snd_ctl_hwdep_info(snd_ctl_t *ctl, snd_hwdep_info_t * info)
{
assert(handle && info);
if (ioctl(handle->fd, SND_CTL_IOCTL_HWDEP_INFO, info) < 0)
return -errno;
return 0;
assert(ctl && info);
return ctl->ops->hwdep_info(ctl, info);
}
int snd_ctl_pcm_info(snd_ctl_t *handle, snd_pcm_info_t * info)
int snd_ctl_pcm_info(snd_ctl_t *ctl, snd_pcm_info_t * info)
{
assert(handle && info);
if (ioctl(handle->fd, SND_CTL_IOCTL_PCM_INFO, info) < 0)
return -errno;
return 0;
assert(ctl && info);
return ctl->ops->pcm_info(ctl, info);
}
int snd_ctl_pcm_prefer_subdevice(snd_ctl_t *handle, int subdev)
int snd_ctl_pcm_prefer_subdevice(snd_ctl_t *ctl, int subdev)
{
assert(handle);
if (ioctl(handle->fd, SND_CTL_IOCTL_PCM_PREFER_SUBDEVICE, &subdev) < 0)
return -errno;
return 0;
assert(ctl);
return ctl->ops->pcm_prefer_subdevice(ctl, subdev);
}
int snd_ctl_rawmidi_info(snd_ctl_t *handle, snd_rawmidi_info_t * info)
int snd_ctl_rawmidi_info(snd_ctl_t *ctl, snd_rawmidi_info_t * info)
{
assert(handle && info);
if (ioctl(handle->fd, SND_CTL_IOCTL_RAWMIDI_INFO, info) < 0)
return -errno;
return 0;
assert(ctl && info);
return ctl->ops->rawmidi_info(ctl, info);
}
int snd_ctl_read(snd_ctl_t *handle, snd_ctl_callbacks_t * callbacks)
int snd_ctl_read1(snd_ctl_t *ctl, snd_ctl_event_t *event)
{
assert(ctl && event);
return ctl->ops->read(ctl, event);
}
int snd_ctl_read(snd_ctl_t *ctl, snd_ctl_callbacks_t * callbacks)
{
int result, count;
snd_ctl_event_t r;
assert(handle);
assert(ctl);
count = 0;
while ((result = read(handle->fd, &r, sizeof(r))) > 0) {
while ((result = snd_ctl_read1(ctl, &r)) > 0) {
if (result != sizeof(r))
return -EIO;
if (!callbacks)
@ -171,26 +124,142 @@ int snd_ctl_read(snd_ctl_t *handle, snd_ctl_callbacks_t * callbacks)
switch (r.type) {
case SND_CTL_EVENT_REBUILD:
if (callbacks->rebuild)
callbacks->rebuild(handle, callbacks->private_data);
callbacks->rebuild(ctl, callbacks->private_data);
break;
case SND_CTL_EVENT_VALUE:
if (callbacks->value)
callbacks->value(handle, callbacks->private_data, &r.data.id);
callbacks->value(ctl, callbacks->private_data, &r.data.id);
break;
case SND_CTL_EVENT_CHANGE:
if (callbacks->change)
callbacks->change(handle, callbacks->private_data, &r.data.id);
callbacks->change(ctl, callbacks->private_data, &r.data.id);
break;
case SND_CTL_EVENT_ADD:
if (callbacks->add)
callbacks->add(handle, callbacks->private_data, &r.data.id);
callbacks->add(ctl, callbacks->private_data, &r.data.id);
break;
case SND_CTL_EVENT_REMOVE:
if (callbacks->remove)
callbacks->remove(handle, callbacks->private_data, &r.data.id);
callbacks->remove(ctl, callbacks->private_data, &r.data.id);
break;
}
count++;
}
return result >= 0 ? count : -errno;
}
static int _snd_ctl_open_hw(snd_ctl_t **handlep, snd_config_t *conf)
{
snd_config_iterator_t i;
long card = -1;
char *str;
int err;
snd_config_foreach(i, conf) {
snd_config_t *n = snd_config_entry(i);
if (strcmp(n->id, "comment") == 0)
continue;
if (strcmp(n->id, "type") == 0)
continue;
if (strcmp(n->id, "card") == 0) {
err = snd_config_integer_get(n, &card);
if (err < 0) {
err = snd_config_string_get(n, &str);
if (err < 0)
return -EINVAL;
card = snd_card_get_index(str);
if (card < 0)
return card;
}
continue;
}
return -EINVAL;
}
if (card < 0)
return -EINVAL;
return snd_ctl_hw_open(handlep, card);
}
static int _snd_ctl_open_client(snd_ctl_t **handlep, snd_config_t *conf)
{
snd_config_iterator_t i;
char *socket = NULL;
char *name = NULL;
char *host = NULL;
long port = -1;
int err;
snd_config_foreach(i, conf) {
snd_config_t *n = snd_config_entry(i);
if (strcmp(n->id, "comment") == 0)
continue;
if (strcmp(n->id, "type") == 0)
continue;
if (strcmp(n->id, "socket") == 0) {
err = snd_config_string_get(n, &socket);
if (err < 0)
return -EINVAL;
continue;
}
if (strcmp(n->id, "host") == 0) {
err = snd_config_string_get(n, &host);
if (err < 0)
return -EINVAL;
continue;
}
if (strcmp(n->id, "port") == 0) {
err = snd_config_integer_get(n, &port);
if (err < 0)
return -EINVAL;
continue;
}
if (strcmp(n->id, "name") == 0) {
err = snd_config_string_get(n, &name);
if (err < 0)
return -EINVAL;
continue;
}
return -EINVAL;
}
if (!name)
return -EINVAL;
if (socket) {
if (port >= 0 || host)
return -EINVAL;
return snd_ctl_client_open(handlep, socket, -1, SND_TRANSPORT_TYPE_SHM, name);
} else {
if (port < 0 || !name)
return -EINVAL;
return snd_ctl_client_open(handlep, host, port, SND_TRANSPORT_TYPE_TCP, name);
}
}
int snd_ctl_open(snd_ctl_t **handlep, char *name)
{
char *str;
int err;
snd_config_t *ctl_conf, *conf;
assert(handlep && name);
err = snd_config_update();
if (err < 0)
return err;
err = snd_config_searchv(snd_config, &ctl_conf, "ctl", name, 0);
if (err < 0) {
int idx = snd_card_get_index(name);
if (idx < 0)
return idx;
return snd_ctl_hw_open(handlep, idx);
}
if (snd_config_type(ctl_conf) != SND_CONFIG_TYPE_COMPOUND)
return -EINVAL;
err = snd_config_search(ctl_conf, "type", &conf);
if (err < 0)
return err;
err = snd_config_string_get(conf, &str);
if (err < 0)
return err;
if (strcmp(str, "hw") == 0)
return _snd_ctl_open_hw(handlep, ctl_conf);
else if (strcmp(str, "client") == 0)
return _snd_ctl_open_client(handlep, ctl_conf);
else
return -EINVAL;
}

View file

@ -0,0 +1,433 @@
/*
* Control - Client
* Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
*
*
* 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>
#include <stddef.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <sys/poll.h>
#include <sys/un.h>
#include <sys/uio.h>
#include <sys/mman.h>
#include <netinet/in.h>
#include <netdb.h>
#include "control_local.h"
#include "aserver.h"
typedef struct {
int data_fd;
int ctrl_fd;
union {
struct {
void *ctrl;
} shm;
} u;
} snd_ctl_client_t;
static void clean_poll(snd_ctl_t *ctl)
{
snd_ctl_client_t *client = ctl->private;
struct pollfd pfd;
int err;
char buf[1];
pfd.fd = client->data_fd;
pfd.events = POLLIN;
while (1) {
err = poll(&pfd, 1, 0);
if (err == 0)
break;
assert(err > 0);
err = read(client->data_fd, buf, 1);
assert(err == 1);
}
}
static int snd_ctl_client_shm_action(snd_ctl_t *ctl)
{
snd_ctl_client_t *client = ctl->private;
int err;
char buf[1];
snd_ctl_client_shm_t *ctrl = client->u.shm.ctrl;
clean_poll(ctl);
err = write(client->ctrl_fd, buf, 1);
if (err != 1)
return -EBADFD;
err = read(client->ctrl_fd, buf, 1);
if (err != 1)
return -EBADFD;
if (ctrl->cmd) {
fprintf(stderr, "Server has not done the cmd\n");
return -EBADFD;
}
return 0;
}
static int snd_ctl_client_shm_close(snd_ctl_t *ctl)
{
snd_ctl_client_t *client = ctl->private;
snd_ctl_client_shm_t *ctrl = client->u.shm.ctrl;
int result;
ctrl->cmd = SND_CTL_IOCTL_CLOSE;
result = snd_ctl_client_shm_action(ctl);
if (result >= 0)
result = ctrl->result;
shmdt((void *)ctrl);
close(client->data_fd);
close(client->ctrl_fd);
free(client);
return result;
}
static int snd_ctl_client_file_descriptor(snd_ctl_t *ctl)
{
snd_ctl_client_t *client = ctl->private;
return client->data_fd;
}
static int snd_ctl_client_shm_hw_info(snd_ctl_t *ctl, snd_ctl_hw_info_t *info)
{
snd_ctl_client_t *client = ctl->private;
snd_ctl_client_shm_t *ctrl = client->u.shm.ctrl;
int err;
// ctrl->u.hw_info = *info;
ctrl->cmd = SND_CTL_IOCTL_HW_INFO;
err = snd_ctl_client_shm_action(ctl);
if (err < 0)
return err;
*info = ctrl->u.hw_info;
return ctrl->result;
}
static int snd_ctl_client_shm_clist(snd_ctl_t *ctl, snd_control_list_t *list)
{
snd_ctl_client_t *client = ctl->private;
snd_ctl_client_shm_t *ctrl = client->u.shm.ctrl;
size_t maxsize = CTL_SHM_DATA_MAXLEN;
size_t bytes = list->controls_request * sizeof(*list->pids);
int err;
snd_control_id_t *pids = list->pids;
if (bytes > maxsize)
return -EINVAL;
ctrl->u.clist = *list;
ctrl->cmd = SND_CTL_IOCTL_CONTROL_LIST;
err = snd_ctl_client_shm_action(ctl);
if (err < 0)
return err;
*list = ctrl->u.clist;
list->pids = pids;
memcpy(pids, ctrl->data, bytes);
return ctrl->result;
}
static int snd_ctl_client_shm_cinfo(snd_ctl_t *ctl, snd_control_info_t *info)
{
snd_ctl_client_t *client = ctl->private;
snd_ctl_client_shm_t *ctrl = client->u.shm.ctrl;
int err;
ctrl->u.cinfo = *info;
ctrl->cmd = SND_CTL_IOCTL_CONTROL_INFO;
err = snd_ctl_client_shm_action(ctl);
if (err < 0)
return err;
*info = ctrl->u.cinfo;
return ctrl->result;
}
static int snd_ctl_client_shm_cread(snd_ctl_t *ctl, snd_control_t *control)
{
snd_ctl_client_t *client = ctl->private;
snd_ctl_client_shm_t *ctrl = client->u.shm.ctrl;
int err;
ctrl->u.cread = *control;
ctrl->cmd = SND_CTL_IOCTL_CONTROL_READ;
err = snd_ctl_client_shm_action(ctl);
if (err < 0)
return err;
*control = ctrl->u.cread;
return ctrl->result;
}
static int snd_ctl_client_shm_cwrite(snd_ctl_t *ctl, snd_control_t *control)
{
snd_ctl_client_t *client = ctl->private;
snd_ctl_client_shm_t *ctrl = client->u.shm.ctrl;
int err;
ctrl->u.cwrite = *control;
ctrl->cmd = SND_CTL_IOCTL_CONTROL_WRITE;
err = snd_ctl_client_shm_action(ctl);
if (err < 0)
return err;
*control = ctrl->u.cwrite;
return ctrl->result;
}
static int snd_ctl_client_shm_hwdep_info(snd_ctl_t *ctl, snd_hwdep_info_t * info)
{
snd_ctl_client_t *client = ctl->private;
snd_ctl_client_shm_t *ctrl = client->u.shm.ctrl;
int err;
ctrl->u.hwdep_info = *info;
ctrl->cmd = SND_CTL_IOCTL_HWDEP_INFO;
err = snd_ctl_client_shm_action(ctl);
if (err < 0)
return err;
*info = ctrl->u.hwdep_info;
return ctrl->result;
}
static int snd_ctl_client_shm_pcm_info(snd_ctl_t *ctl, snd_pcm_info_t * info)
{
snd_ctl_client_t *client = ctl->private;
snd_ctl_client_shm_t *ctrl = client->u.shm.ctrl;
int err;
ctrl->u.pcm_info = *info;
ctrl->cmd = SND_CTL_IOCTL_PCM_INFO;
err = snd_ctl_client_shm_action(ctl);
if (err < 0)
return err;
*info = ctrl->u.pcm_info;
return ctrl->result;
}
static int snd_ctl_client_shm_pcm_prefer_subdevice(snd_ctl_t *ctl, int subdev)
{
snd_ctl_client_t *client = ctl->private;
snd_ctl_client_shm_t *ctrl = client->u.shm.ctrl;
int err;
ctrl->u.pcm_prefer_subdevice = subdev;
ctrl->cmd = SND_CTL_IOCTL_PCM_PREFER_SUBDEVICE;
err = snd_ctl_client_shm_action(ctl);
if (err < 0)
return err;
return ctrl->result;
}
static int snd_ctl_client_shm_rawmidi_info(snd_ctl_t *ctl, snd_rawmidi_info_t * info)
{
snd_ctl_client_t *client = ctl->private;
snd_ctl_client_shm_t *ctrl = client->u.shm.ctrl;
int err;
ctrl->u.rawmidi_info = *info;
ctrl->cmd = SND_CTL_IOCTL_RAWMIDI_INFO;
err = snd_ctl_client_shm_action(ctl);
if (err < 0)
return err;
*info = ctrl->u.rawmidi_info;
return ctrl->result;
}
static int snd_ctl_client_shm_read(snd_ctl_t *ctl, snd_ctl_event_t *event)
{
snd_ctl_client_t *client = ctl->private;
snd_ctl_client_shm_t *ctrl = client->u.shm.ctrl;
int err;
ctrl->u.read = *event;
ctrl->cmd = SND_CTL_IOCTL_READ;
err = snd_ctl_client_shm_action(ctl);
if (err < 0)
return err;
*event = ctrl->u.read;
return ctrl->result;
}
struct snd_ctl_ops snd_ctl_client_ops = {
close: snd_ctl_client_shm_close,
file_descriptor: snd_ctl_client_file_descriptor,
hw_info: snd_ctl_client_shm_hw_info,
clist: snd_ctl_client_shm_clist,
cinfo: snd_ctl_client_shm_cinfo,
cread: snd_ctl_client_shm_cread,
cwrite: snd_ctl_client_shm_cwrite,
hwdep_info: snd_ctl_client_shm_hwdep_info,
pcm_info: snd_ctl_client_shm_pcm_info,
pcm_prefer_subdevice: snd_ctl_client_shm_pcm_prefer_subdevice,
rawmidi_info: snd_ctl_client_shm_rawmidi_info,
read: snd_ctl_client_shm_read,
};
static int make_local_socket(const char *filename)
{
size_t l = strlen(filename);
size_t size = offsetof(struct sockaddr_un, sun_path) + l;
struct sockaddr_un *addr = alloca(size);
int sock;
sock = socket(PF_LOCAL, SOCK_STREAM, 0);
if (sock < 0)
return -errno;
addr->sun_family = AF_LOCAL;
memcpy(addr->sun_path, filename, l);
if (connect(sock, (struct sockaddr *) addr, size) < 0)
return -errno;
return sock;
}
static int make_inet_socket(const char *host, int port)
{
struct sockaddr_in addr;
int sock;
struct hostent *h = gethostbyname(host);
if (!h)
return -ENOENT;
sock = socket(PF_INET, SOCK_STREAM, 0);
if (sock < 0)
return -errno;
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
memcpy(&addr.sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
if (connect(sock, (struct sockaddr *) &addr, sizeof(addr)) < 0)
return -errno;
return sock;
}
/* port == -1 -> PF_LOCAL and host is the socket name */
int snd_ctl_client_open(snd_ctl_t **handlep, char *host, int port, int transport, char *name)
{
snd_ctl_t *ctl;
snd_ctl_client_t *client;
snd_client_open_request_t *req;
snd_client_open_answer_t ans;
size_t namelen, reqlen;
int err;
int result;
int fds[2] = {-1, -1};
int k;
snd_ctl_client_shm_t *ctrl = NULL;
uint32_t rcookie, scookie = getpid();
namelen = strlen(name);
if (namelen > 255)
return -EINVAL;
for (k = 0; k < 2; ++k) {
if (port == -1)
fds[k] = make_local_socket(host);
else
fds[k] = make_inet_socket(host, port);
if (fds[k] < 0) {
result = fds[k];
goto _err;
}
err = write(fds[k], &scookie, sizeof(scookie));
if (err != sizeof(scookie)) {
result = -EBADFD;
goto _err;
}
err = read(fds[k], &rcookie, sizeof(rcookie));
if (err != sizeof(rcookie) ||
rcookie != scookie) {
result = -EBADFD;
goto _err;
}
}
reqlen = sizeof(*req) + namelen;
req = alloca(reqlen);
memcpy(req->name, name, namelen);
req->dev_type = SND_DEV_TYPE_CONTROL;
req->transport_type = transport;
req->stream = 0;
req->mode = 0;
req->namelen = namelen;
err = write(fds[1], req, reqlen);
if (err < 0) {
result = -errno;
goto _err;
}
if ((size_t) err != reqlen) {
result = -EINVAL;
goto _err;
}
err = read(fds[1], &ans, sizeof(ans));
if (err < 0) {
result = -errno;
goto _err;
}
if (err != sizeof(ans)) {
result = -EINVAL;
goto _err;
}
result = ans.result;
if (result < 0)
goto _err;
switch (transport) {
case SND_TRANSPORT_TYPE_SHM:
ctrl = shmat(ans.cookie, 0, 0);
if (!ctrl) {
result = -errno;
goto _err;
}
break;
default:
result = -ENOSYS;
goto _err;
}
ctl = calloc(1, sizeof(snd_ctl_t));
if (!ctl) {
result = -ENOMEM;
goto _err;
}
client = calloc(1, sizeof(snd_ctl_client_t));
if (!ctl) {
free(ctl);
result = -ENOMEM;
goto _err;
}
client->data_fd = fds[0];
client->ctrl_fd = fds[1];
switch (transport) {
case SND_TRANSPORT_TYPE_SHM:
client->u.shm.ctrl = ctrl;
break;
}
ctl->type = SND_CTL_TYPE_CLIENT;
ctl->ops = &snd_ctl_client_ops;
ctl->private = client;
INIT_LIST_HEAD(&ctl->hlist);
*handlep = ctl;
return 0;
_err:
if (fds[0] >= 0)
close(fds[0]);
if (fds[1] >= 0)
close(fds[1]);
switch (transport) {
case SND_TRANSPORT_TYPE_SHM:
if (ctrl)
shmdt(ctrl);
break;
}
return result;
}

194
src/control/control_hw.c Normal file
View file

@ -0,0 +1,194 @@
/*
* Control Interface - Hardware
* Copyright (c) 1998,1999,2000 by Jaroslav Kysela <perex@suse.cz>
* Copyright (c) 2000 by Abramo Bagnara <abramo@alsa-project.org>
*
*
* 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>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <assert.h>
#include "asoundlib.h"
#include "control_local.h"
#define SND_FILE_CONTROL "/dev/snd/controlC%i"
#define SND_CTL_VERSION_MAX SND_PROTOCOL_VERSION(2, 0, 0)
typedef struct {
int card;
int fd;
} snd_ctl_hw_t;
static int snd_ctl_hw_close(snd_ctl_t *handle)
{
snd_ctl_hw_t *hw = handle->private;
int res;
res = close(hw->fd) < 0 ? -errno : 0;
free(hw);
return res;
}
static int snd_ctl_hw_file_descriptor(snd_ctl_t *handle)
{
snd_ctl_hw_t *hw = handle->private;
return hw->fd;
}
static int snd_ctl_hw_hw_info(snd_ctl_t *handle, snd_ctl_hw_info_t *info)
{
snd_ctl_hw_t *hw = handle->private;
if (ioctl(hw->fd, SND_CTL_IOCTL_HW_INFO, info) < 0)
return -errno;
return 0;
}
static int snd_ctl_hw_clist(snd_ctl_t *handle, snd_control_list_t *list)
{
snd_ctl_hw_t *hw = handle->private;
if (ioctl(hw->fd, SND_CTL_IOCTL_CONTROL_LIST, list) < 0)
return -errno;
return 0;
}
static int snd_ctl_hw_cinfo(snd_ctl_t *handle, snd_control_info_t *info)
{
snd_ctl_hw_t *hw = handle->private;
if (ioctl(hw->fd, SND_CTL_IOCTL_CONTROL_INFO, info) < 0)
return -errno;
return 0;
}
static int snd_ctl_hw_cread(snd_ctl_t *handle, snd_control_t *control)
{
snd_ctl_hw_t *hw = handle->private;
if (ioctl(hw->fd, SND_CTL_IOCTL_CONTROL_READ, control) < 0)
return -errno;
return 0;
}
static int snd_ctl_hw_cwrite(snd_ctl_t *handle, snd_control_t *control)
{
snd_ctl_hw_t *hw = handle->private;
if (ioctl(hw->fd, SND_CTL_IOCTL_CONTROL_WRITE, control) < 0)
return -errno;
return 0;
}
static int snd_ctl_hw_hwdep_info(snd_ctl_t *handle, snd_hwdep_info_t * info)
{
snd_ctl_hw_t *hw = handle->private;
if (ioctl(hw->fd, SND_CTL_IOCTL_HWDEP_INFO, info) < 0)
return -errno;
return 0;
}
static int snd_ctl_hw_pcm_info(snd_ctl_t *handle, snd_pcm_info_t * info)
{
snd_ctl_hw_t *hw = handle->private;
if (ioctl(hw->fd, SND_CTL_IOCTL_PCM_INFO, info) < 0)
return -errno;
return 0;
}
static int snd_ctl_hw_pcm_prefer_subdevice(snd_ctl_t *handle, int subdev)
{
snd_ctl_hw_t *hw = handle->private;
if (ioctl(hw->fd, SND_CTL_IOCTL_PCM_PREFER_SUBDEVICE, &subdev) < 0)
return -errno;
return 0;
}
static int snd_ctl_hw_rawmidi_info(snd_ctl_t *handle, snd_rawmidi_info_t * info)
{
snd_ctl_hw_t *hw = handle->private;
if (ioctl(hw->fd, SND_CTL_IOCTL_RAWMIDI_INFO, info) < 0)
return -errno;
return 0;
}
static int snd_ctl_hw_read(snd_ctl_t *handle, snd_ctl_event_t *event)
{
snd_ctl_hw_t *hw = handle->private;
return read(hw->fd, event, sizeof(*event));
}
struct snd_ctl_ops snd_ctl_hw_ops = {
close: snd_ctl_hw_close,
file_descriptor: snd_ctl_hw_file_descriptor,
hw_info: snd_ctl_hw_hw_info,
clist: snd_ctl_hw_clist,
cinfo: snd_ctl_hw_cinfo,
cread: snd_ctl_hw_cread,
cwrite: snd_ctl_hw_cwrite,
hwdep_info: snd_ctl_hw_hwdep_info,
pcm_info: snd_ctl_hw_pcm_info,
pcm_prefer_subdevice: snd_ctl_hw_pcm_prefer_subdevice,
rawmidi_info: snd_ctl_hw_rawmidi_info,
read: snd_ctl_hw_read,
};
int snd_ctl_hw_open(snd_ctl_t **handle, int card)
{
int fd, ver;
char filename[32];
snd_ctl_t *ctl;
snd_ctl_hw_t *hw;
*handle = NULL;
assert(card >= 0 && card < SND_CARDS);
sprintf(filename, SND_FILE_CONTROL, card);
if ((fd = open(filename, O_RDWR)) < 0) {
snd_card_load(card);
if ((fd = open(filename, O_RDWR)) < 0)
return -errno;
}
if (ioctl(fd, SND_CTL_IOCTL_PVERSION, &ver) < 0) {
close(fd);
return -errno;
}
if (SND_PROTOCOL_INCOMPATIBLE(ver, SND_CTL_VERSION_MAX)) {
close(fd);
return -SND_ERROR_INCOMPATIBLE_VERSION;
}
ctl = calloc(1, sizeof(snd_ctl_t));
if (ctl == NULL) {
close(fd);
return -ENOMEM;
}
hw = calloc(1, sizeof(snd_ctl_hw_t));
if (hw == NULL) {
close(fd);
free(ctl);
return -ENOMEM;
}
hw->card = card;
hw->fd = fd;
ctl->ops = &snd_ctl_hw_ops;
ctl->private = hw;
INIT_LIST_HEAD(&ctl->hlist);
*handle = ctl;
return 0;
}

View file

@ -19,11 +19,30 @@
*
*/
#include <assert.h>
#include "asoundlib.h"
#include "list.h"
struct snd_ctl_ops {
int (*close)(snd_ctl_t *handle);
int (*file_descriptor)(snd_ctl_t *handle);
int (*hw_info)(snd_ctl_t *handle, snd_ctl_hw_info_t *info);
int (*clist)(snd_ctl_t *handle, snd_control_list_t *list);
int (*cinfo)(snd_ctl_t *handle, snd_control_info_t *info);
int (*cread)(snd_ctl_t *handle, snd_control_t *control);
int (*cwrite)(snd_ctl_t *handle, snd_control_t *control);
int (*hwdep_info)(snd_ctl_t *handle, snd_hwdep_info_t * info);
int (*pcm_info)(snd_ctl_t *handle, snd_pcm_info_t * info);
int (*pcm_prefer_subdevice)(snd_ctl_t *handle, int subdev);
int (*rawmidi_info)(snd_ctl_t *handle, snd_rawmidi_info_t * info);
int (*read)(snd_ctl_t *handle, snd_ctl_event_t *event);
};
struct snd_ctl {
int card;
int fd;
snd_ctl_type_t type;
struct snd_ctl_ops *ops;
void *private;
int hcount;
int herr;
struct list_head hlist; /* list of all controls */