mirror of
				https://github.com/alsa-project/alsa-lib.git
				synced 2025-11-03 09:01:52 -05:00 
			
		
		
		
	Fixed mmap wrt shm. Renamed pcm_client, control_client to shm. More error messages. Implemented asoundrc as documented.
This commit is contained in:
		
							parent
							
								
									dcc88ffaa7
								
							
						
					
					
						commit
						e5e1ca14d4
					
				
					 30 changed files with 1642 additions and 2555 deletions
				
			
		| 
						 | 
				
			
			@ -1,7 +1,7 @@
 | 
			
		|||
EXTRA_LTLIBRARIES = libcontrol.la
 | 
			
		||||
 | 
			
		||||
libcontrol_la_SOURCES = cards.c controls.c bag.c defaults.c \
 | 
			
		||||
                        control.c control_hw.c control_client.c
 | 
			
		||||
                        control.c control_hw.c control_shm.c
 | 
			
		||||
 | 
			
		||||
all: libcontrol.la
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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_hw_open(&handle, card) < 0)
 | 
			
		||||
		if (snd_ctl_hw_open(&handle, NULL, 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_hw_open(&handle, card)) < 0)
 | 
			
		||||
	if ((err = snd_ctl_hw_open(&handle, NULL, 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_hw_open(&handle, card)) < 0)
 | 
			
		||||
	if ((err = snd_ctl_hw_open(&handle, NULL, card)) < 0)
 | 
			
		||||
		return err;
 | 
			
		||||
	if ((err = snd_ctl_hw_info(handle, &info)) < 0) {
 | 
			
		||||
		snd_ctl_close(handle);
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,6 +26,7 @@
 | 
			
		|||
#include <errno.h>
 | 
			
		||||
#include <fcntl.h>
 | 
			
		||||
#include <assert.h>
 | 
			
		||||
#include <dlfcn.h>
 | 
			
		||||
#include "asoundlib.h"
 | 
			
		||||
#include "control_local.h"
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -148,106 +149,22 @@ int snd_ctl_read(snd_ctl_t *ctl, snd_ctl_callbacks_t * callbacks)
 | 
			
		|||
	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)
 | 
			
		||||
int snd_ctl_open(snd_ctl_t **ctlp, char *name)
 | 
			
		||||
{
 | 
			
		||||
	char *str;
 | 
			
		||||
	int err;
 | 
			
		||||
	snd_config_t *ctl_conf, *conf;
 | 
			
		||||
	assert(handlep && name);
 | 
			
		||||
	snd_config_t *ctl_conf, *conf, *type_conf;
 | 
			
		||||
	snd_config_iterator_t i;
 | 
			
		||||
	char *lib = NULL, *open = NULL;
 | 
			
		||||
	int (*open_func)(snd_ctl_t **ctlp, 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) {
 | 
			
		||||
		int idx = snd_card_get_index(name);
 | 
			
		||||
		if (idx < 0)
 | 
			
		||||
			return idx;
 | 
			
		||||
		return snd_ctl_hw_open(handlep, idx);
 | 
			
		||||
	}
 | 
			
		||||
	if (err < 0)
 | 
			
		||||
		return err;
 | 
			
		||||
	if (snd_config_type(ctl_conf) != SND_CONFIG_TYPE_COMPOUND)
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	err = snd_config_search(ctl_conf, "type", &conf);
 | 
			
		||||
| 
						 | 
				
			
			@ -256,10 +173,37 @@ int snd_ctl_open(snd_ctl_t **handlep, char *name)
 | 
			
		|||
	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
 | 
			
		||||
	err = snd_config_searchv(snd_config, &type_conf, "ctltype", str, 0);
 | 
			
		||||
	if (err < 0)
 | 
			
		||||
		return err;
 | 
			
		||||
	snd_config_foreach(i, type_conf) {
 | 
			
		||||
		snd_config_t *n = snd_config_entry(i);
 | 
			
		||||
		if (strcmp(n->id, "comment") == 0)
 | 
			
		||||
			continue;
 | 
			
		||||
		if (strcmp(n->id, "lib") == 0) {
 | 
			
		||||
			err = snd_config_string_get(n, &lib);
 | 
			
		||||
			if (err < 0)
 | 
			
		||||
				return -EINVAL;
 | 
			
		||||
			continue;
 | 
			
		||||
		}
 | 
			
		||||
		if (strcmp(n->id, "open") == 0) {
 | 
			
		||||
			err = snd_config_string_get(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);
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,433 +0,0 @@
 | 
			
		|||
/*
 | 
			
		||||
 *  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_poll_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,
 | 
			
		||||
	poll_descriptor: snd_ctl_client_poll_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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -147,7 +147,7 @@ struct snd_ctl_ops snd_ctl_hw_ops = {
 | 
			
		|||
	read: snd_ctl_hw_read,
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
int snd_ctl_hw_open(snd_ctl_t **handle, int card)
 | 
			
		||||
int snd_ctl_hw_open(snd_ctl_t **handle, char *name, int card)
 | 
			
		||||
{
 | 
			
		||||
	int fd, ver;
 | 
			
		||||
	char filename[32];
 | 
			
		||||
| 
						 | 
				
			
			@ -185,6 +185,9 @@ int snd_ctl_hw_open(snd_ctl_t **handle, int card)
 | 
			
		|||
	}
 | 
			
		||||
	hw->card = card;
 | 
			
		||||
	hw->fd = fd;
 | 
			
		||||
	if (name)
 | 
			
		||||
		ctl->name = strdup(name);
 | 
			
		||||
	ctl->type = SND_CTL_TYPE_HW;
 | 
			
		||||
	ctl->ops = &snd_ctl_hw_ops;
 | 
			
		||||
	ctl->private = hw;
 | 
			
		||||
	INIT_LIST_HEAD(&ctl->hlist);
 | 
			
		||||
| 
						 | 
				
			
			@ -192,3 +195,34 @@ int snd_ctl_hw_open(snd_ctl_t **handle, int card)
 | 
			
		|||
	return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int _snd_ctl_hw_open(snd_ctl_t **handlep, char *name, 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, name, card);
 | 
			
		||||
}
 | 
			
		||||
				
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -40,6 +40,7 @@ struct snd_ctl_ops {
 | 
			
		|||
 | 
			
		||||
 | 
			
		||||
struct snd_ctl {
 | 
			
		||||
	char *name;
 | 
			
		||||
	snd_ctl_type_t type;
 | 
			
		||||
	struct snd_ctl_ops *ops;
 | 
			
		||||
	void *private;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										512
									
								
								src/control/control_shm.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										512
									
								
								src/control/control_shm.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,512 @@
 | 
			
		|||
/*
 | 
			
		||||
 *  Control - SHM 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 socket;
 | 
			
		||||
	void *ctrl;
 | 
			
		||||
} snd_ctl_shm_t;
 | 
			
		||||
 | 
			
		||||
extern int receive_fd(int socket, void *data, size_t len, int *fd);
 | 
			
		||||
 | 
			
		||||
static int snd_ctl_shm_action(snd_ctl_t *ctl)
 | 
			
		||||
{
 | 
			
		||||
	snd_ctl_shm_t *shm = ctl->private;
 | 
			
		||||
	int err;
 | 
			
		||||
	char buf[1];
 | 
			
		||||
	snd_ctl_shm_ctrl_t *ctrl = shm->ctrl;
 | 
			
		||||
	err = write(shm->socket, buf, 1);
 | 
			
		||||
	if (err != 1)
 | 
			
		||||
		return -EBADFD;
 | 
			
		||||
	err = read(shm->socket, buf, 1);
 | 
			
		||||
	if (err != 1)
 | 
			
		||||
		return -EBADFD;
 | 
			
		||||
	if (ctrl->cmd) {
 | 
			
		||||
		fprintf(stderr, "Server has not done the cmd\n");
 | 
			
		||||
		return -EBADFD;
 | 
			
		||||
	}
 | 
			
		||||
	return ctrl->result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int snd_ctl_shm_action_fd(snd_ctl_t *ctl, int *fd)
 | 
			
		||||
{
 | 
			
		||||
	snd_ctl_shm_t *shm = ctl->private;
 | 
			
		||||
	int err;
 | 
			
		||||
	char buf[1];
 | 
			
		||||
	snd_ctl_shm_ctrl_t *ctrl = shm->ctrl;
 | 
			
		||||
	err = write(shm->socket, buf, 1);
 | 
			
		||||
	if (err != 1)
 | 
			
		||||
		return -EBADFD;
 | 
			
		||||
	err = receive_fd(shm->socket, buf, 1, fd);
 | 
			
		||||
	if (err != 1)
 | 
			
		||||
		return -EBADFD;
 | 
			
		||||
	if (ctrl->cmd) {
 | 
			
		||||
		fprintf(stderr, "Server has not done the cmd\n");
 | 
			
		||||
		return -EBADFD;
 | 
			
		||||
	}
 | 
			
		||||
	return ctrl->result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int snd_ctl_shm_close(snd_ctl_t *ctl)
 | 
			
		||||
{
 | 
			
		||||
	snd_ctl_shm_t *shm = ctl->private;
 | 
			
		||||
	snd_ctl_shm_ctrl_t *ctrl = shm->ctrl;
 | 
			
		||||
	int result;
 | 
			
		||||
	ctrl->cmd = SND_CTL_IOCTL_CLOSE;
 | 
			
		||||
	result = snd_ctl_shm_action(ctl);
 | 
			
		||||
	shmdt((void *)ctrl);
 | 
			
		||||
	close(shm->socket);
 | 
			
		||||
	free(shm);
 | 
			
		||||
	return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int snd_ctl_shm_poll_descriptor(snd_ctl_t *ctl)
 | 
			
		||||
{
 | 
			
		||||
	snd_ctl_shm_t *shm = ctl->private;
 | 
			
		||||
	snd_ctl_shm_ctrl_t *ctrl = shm->ctrl;
 | 
			
		||||
	int fd, err;
 | 
			
		||||
	ctrl->cmd = SND_CTL_IOCTL_POLL_DESCRIPTOR;
 | 
			
		||||
	err = snd_ctl_shm_action_fd(ctl, &fd);
 | 
			
		||||
	if (err < 0)
 | 
			
		||||
		return err;
 | 
			
		||||
	return fd;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int snd_ctl_shm_hw_info(snd_ctl_t *ctl, snd_ctl_hw_info_t *info)
 | 
			
		||||
{
 | 
			
		||||
	snd_ctl_shm_t *shm = ctl->private;
 | 
			
		||||
	snd_ctl_shm_ctrl_t *ctrl = shm->ctrl;
 | 
			
		||||
	int err;
 | 
			
		||||
//	ctrl->u.hw_info = *info;
 | 
			
		||||
	ctrl->cmd = SND_CTL_IOCTL_HW_INFO;
 | 
			
		||||
	err = snd_ctl_shm_action(ctl);
 | 
			
		||||
	if (err < 0)
 | 
			
		||||
		return err;
 | 
			
		||||
	*info = ctrl->u.hw_info;
 | 
			
		||||
	return err;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int snd_ctl_shm_clist(snd_ctl_t *ctl, snd_control_list_t *list)
 | 
			
		||||
{
 | 
			
		||||
	snd_ctl_shm_t *shm = ctl->private;
 | 
			
		||||
	snd_ctl_shm_ctrl_t *ctrl = 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_shm_action(ctl);
 | 
			
		||||
	if (err < 0)
 | 
			
		||||
		return err;
 | 
			
		||||
	*list = ctrl->u.clist;
 | 
			
		||||
	list->pids = pids;
 | 
			
		||||
	memcpy(pids, ctrl->data, bytes);
 | 
			
		||||
	return err;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int snd_ctl_shm_cinfo(snd_ctl_t *ctl, snd_control_info_t *info)
 | 
			
		||||
{
 | 
			
		||||
	snd_ctl_shm_t *shm = ctl->private;
 | 
			
		||||
	snd_ctl_shm_ctrl_t *ctrl = shm->ctrl;
 | 
			
		||||
	int err;
 | 
			
		||||
	ctrl->u.cinfo = *info;
 | 
			
		||||
	ctrl->cmd = SND_CTL_IOCTL_CONTROL_INFO;
 | 
			
		||||
	err = snd_ctl_shm_action(ctl);
 | 
			
		||||
	if (err < 0)
 | 
			
		||||
		return err;
 | 
			
		||||
	*info = ctrl->u.cinfo;
 | 
			
		||||
	return err;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int snd_ctl_shm_cread(snd_ctl_t *ctl, snd_control_t *control)
 | 
			
		||||
{
 | 
			
		||||
	snd_ctl_shm_t *shm = ctl->private;
 | 
			
		||||
	snd_ctl_shm_ctrl_t *ctrl = shm->ctrl;
 | 
			
		||||
	int err;
 | 
			
		||||
	ctrl->u.cread = *control;
 | 
			
		||||
	ctrl->cmd = SND_CTL_IOCTL_CONTROL_READ;
 | 
			
		||||
	err = snd_ctl_shm_action(ctl);
 | 
			
		||||
	if (err < 0)
 | 
			
		||||
		return err;
 | 
			
		||||
	*control = ctrl->u.cread;
 | 
			
		||||
	return err;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int snd_ctl_shm_cwrite(snd_ctl_t *ctl, snd_control_t *control)
 | 
			
		||||
{
 | 
			
		||||
	snd_ctl_shm_t *shm = ctl->private;
 | 
			
		||||
	snd_ctl_shm_ctrl_t *ctrl = shm->ctrl;
 | 
			
		||||
	int err;
 | 
			
		||||
	ctrl->u.cwrite = *control;
 | 
			
		||||
	ctrl->cmd = SND_CTL_IOCTL_CONTROL_WRITE;
 | 
			
		||||
	err = snd_ctl_shm_action(ctl);
 | 
			
		||||
	if (err < 0)
 | 
			
		||||
		return err;
 | 
			
		||||
	*control = ctrl->u.cwrite;
 | 
			
		||||
	return err;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int snd_ctl_shm_hwdep_info(snd_ctl_t *ctl, snd_hwdep_info_t * info)
 | 
			
		||||
{
 | 
			
		||||
	snd_ctl_shm_t *shm = ctl->private;
 | 
			
		||||
	snd_ctl_shm_ctrl_t *ctrl = shm->ctrl;
 | 
			
		||||
	int err;
 | 
			
		||||
	ctrl->u.hwdep_info = *info;
 | 
			
		||||
	ctrl->cmd = SND_CTL_IOCTL_HWDEP_INFO;
 | 
			
		||||
	err = snd_ctl_shm_action(ctl);
 | 
			
		||||
	if (err < 0)
 | 
			
		||||
		return err;
 | 
			
		||||
	*info = ctrl->u.hwdep_info;
 | 
			
		||||
	return err;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int snd_ctl_shm_pcm_info(snd_ctl_t *ctl, snd_pcm_info_t * info)
 | 
			
		||||
{
 | 
			
		||||
	snd_ctl_shm_t *shm = ctl->private;
 | 
			
		||||
	snd_ctl_shm_ctrl_t *ctrl = shm->ctrl;
 | 
			
		||||
	int err;
 | 
			
		||||
	ctrl->u.pcm_info = *info;
 | 
			
		||||
	ctrl->cmd = SND_CTL_IOCTL_PCM_INFO;
 | 
			
		||||
	err = snd_ctl_shm_action(ctl);
 | 
			
		||||
	if (err < 0)
 | 
			
		||||
		return err;
 | 
			
		||||
	*info = ctrl->u.pcm_info;
 | 
			
		||||
	return err;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int snd_ctl_shm_pcm_prefer_subdevice(snd_ctl_t *ctl, int subdev)
 | 
			
		||||
{
 | 
			
		||||
	snd_ctl_shm_t *shm = ctl->private;
 | 
			
		||||
	snd_ctl_shm_ctrl_t *ctrl = shm->ctrl;
 | 
			
		||||
	int err;
 | 
			
		||||
	ctrl->u.pcm_prefer_subdevice = subdev;
 | 
			
		||||
	ctrl->cmd = SND_CTL_IOCTL_PCM_PREFER_SUBDEVICE;
 | 
			
		||||
	err = snd_ctl_shm_action(ctl);
 | 
			
		||||
	if (err < 0)
 | 
			
		||||
		return err;
 | 
			
		||||
	return err;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int snd_ctl_shm_rawmidi_info(snd_ctl_t *ctl, snd_rawmidi_info_t * info)
 | 
			
		||||
{
 | 
			
		||||
	snd_ctl_shm_t *shm = ctl->private;
 | 
			
		||||
	snd_ctl_shm_ctrl_t *ctrl = shm->ctrl;
 | 
			
		||||
	int err;
 | 
			
		||||
	ctrl->u.rawmidi_info = *info;
 | 
			
		||||
	ctrl->cmd = SND_CTL_IOCTL_RAWMIDI_INFO;
 | 
			
		||||
	err = snd_ctl_shm_action(ctl);
 | 
			
		||||
	if (err < 0)
 | 
			
		||||
		return err;
 | 
			
		||||
	*info = ctrl->u.rawmidi_info;
 | 
			
		||||
	return err;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
static int snd_ctl_shm_read(snd_ctl_t *ctl, snd_ctl_event_t *event)
 | 
			
		||||
{
 | 
			
		||||
	snd_ctl_shm_t *shm = ctl->private;
 | 
			
		||||
	snd_ctl_shm_ctrl_t *ctrl = shm->ctrl;
 | 
			
		||||
	int err;
 | 
			
		||||
	ctrl->u.read = *event;
 | 
			
		||||
	ctrl->cmd = SND_CTL_IOCTL_READ;
 | 
			
		||||
	err = snd_ctl_shm_action(ctl);
 | 
			
		||||
	if (err < 0)
 | 
			
		||||
		return err;
 | 
			
		||||
	*event = ctrl->u.read;
 | 
			
		||||
	return err;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
struct snd_ctl_ops snd_ctl_shm_ops = {
 | 
			
		||||
	close: snd_ctl_shm_close,
 | 
			
		||||
	poll_descriptor: snd_ctl_shm_poll_descriptor,
 | 
			
		||||
	hw_info: snd_ctl_shm_hw_info,
 | 
			
		||||
	clist: snd_ctl_shm_clist,
 | 
			
		||||
	cinfo: snd_ctl_shm_cinfo,
 | 
			
		||||
	cread: snd_ctl_shm_cread,
 | 
			
		||||
	cwrite: snd_ctl_shm_cwrite,
 | 
			
		||||
	hwdep_info: snd_ctl_shm_hwdep_info,
 | 
			
		||||
	pcm_info: snd_ctl_shm_pcm_info,
 | 
			
		||||
	pcm_prefer_subdevice: snd_ctl_shm_pcm_prefer_subdevice,
 | 
			
		||||
	rawmidi_info: snd_ctl_shm_rawmidi_info,
 | 
			
		||||
	read: snd_ctl_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;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#if 0
 | 
			
		||||
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;
 | 
			
		||||
}
 | 
			
		||||
#endif
 | 
			
		||||
 | 
			
		||||
int snd_ctl_shm_open(snd_ctl_t **handlep, char *name, char *socket, char *sname)
 | 
			
		||||
{
 | 
			
		||||
	snd_ctl_t *ctl;
 | 
			
		||||
	snd_ctl_shm_t *shm = NULL;
 | 
			
		||||
	snd_client_open_request_t *req;
 | 
			
		||||
	snd_client_open_answer_t ans;
 | 
			
		||||
	size_t snamelen, reqlen;
 | 
			
		||||
	int err;
 | 
			
		||||
	int result;
 | 
			
		||||
	int sock = -1;
 | 
			
		||||
	snd_ctl_shm_ctrl_t *ctrl = NULL;
 | 
			
		||||
	snamelen = strlen(sname);
 | 
			
		||||
	if (snamelen > 255)
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
 | 
			
		||||
	result = make_local_socket(socket);
 | 
			
		||||
	if (result < 0) {
 | 
			
		||||
		ERR("server for socket %s is not running", socket);
 | 
			
		||||
		goto _err;
 | 
			
		||||
	}
 | 
			
		||||
	sock = result;
 | 
			
		||||
 | 
			
		||||
	reqlen = sizeof(*req) + snamelen;
 | 
			
		||||
	req = alloca(reqlen);
 | 
			
		||||
	memcpy(req->name, sname, snamelen);
 | 
			
		||||
	req->dev_type = SND_DEV_TYPE_CONTROL;
 | 
			
		||||
	req->transport_type = SND_TRANSPORT_TYPE_SHM;
 | 
			
		||||
	req->stream = 0;
 | 
			
		||||
	req->mode = 0;
 | 
			
		||||
	req->namelen = snamelen;
 | 
			
		||||
	err = write(sock, req, reqlen);
 | 
			
		||||
	if (err < 0) {
 | 
			
		||||
		ERR("write error");
 | 
			
		||||
		result = -errno;
 | 
			
		||||
		goto _err;
 | 
			
		||||
	}
 | 
			
		||||
	if ((size_t) err != reqlen) {
 | 
			
		||||
		ERR("write size error");
 | 
			
		||||
		result = -EINVAL;
 | 
			
		||||
		goto _err;
 | 
			
		||||
	}
 | 
			
		||||
	err = read(sock, &ans, sizeof(ans));
 | 
			
		||||
	if (err < 0) {
 | 
			
		||||
		ERR("read error");
 | 
			
		||||
		result = -errno;
 | 
			
		||||
		goto _err;
 | 
			
		||||
	}
 | 
			
		||||
	if (err != sizeof(ans)) {
 | 
			
		||||
		ERR("read size error");
 | 
			
		||||
		result = -EINVAL;
 | 
			
		||||
		goto _err;
 | 
			
		||||
	}
 | 
			
		||||
	result = ans.result;
 | 
			
		||||
	if (result < 0)
 | 
			
		||||
		goto _err;
 | 
			
		||||
 | 
			
		||||
	ctrl = shmat(ans.cookie, 0, 0);
 | 
			
		||||
	if (!ctrl) {
 | 
			
		||||
		result = -errno;
 | 
			
		||||
		goto _err;
 | 
			
		||||
	}
 | 
			
		||||
		
 | 
			
		||||
	ctl = calloc(1, sizeof(snd_ctl_t));
 | 
			
		||||
	if (!ctl) {
 | 
			
		||||
		result = -ENOMEM;
 | 
			
		||||
		goto _err;
 | 
			
		||||
	}
 | 
			
		||||
	shm = calloc(1, sizeof(snd_ctl_shm_t));
 | 
			
		||||
	if (!ctl) {
 | 
			
		||||
		free(ctl);
 | 
			
		||||
		result = -ENOMEM;
 | 
			
		||||
		goto _err;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	shm->socket = sock;
 | 
			
		||||
	shm->ctrl = ctrl;
 | 
			
		||||
 | 
			
		||||
	if (name)
 | 
			
		||||
		ctl->name = strdup(name);
 | 
			
		||||
	ctl->type = SND_CTL_TYPE_SHM;
 | 
			
		||||
	ctl->ops = &snd_ctl_shm_ops;
 | 
			
		||||
	ctl->private = shm;
 | 
			
		||||
	INIT_LIST_HEAD(&ctl->hlist);
 | 
			
		||||
	*handlep = ctl;
 | 
			
		||||
	return 0;
 | 
			
		||||
 | 
			
		||||
 _err:
 | 
			
		||||
	close(sock);
 | 
			
		||||
	if (ctrl)
 | 
			
		||||
		shmdt(ctrl);
 | 
			
		||||
	if (shm)
 | 
			
		||||
		free(shm);
 | 
			
		||||
	return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
extern int is_local(struct hostent *hent);
 | 
			
		||||
 | 
			
		||||
int _snd_ctl_shm_open(snd_ctl_t **handlep, char *name, snd_config_t *conf)
 | 
			
		||||
{
 | 
			
		||||
	snd_config_iterator_t i;
 | 
			
		||||
	char *server = NULL;
 | 
			
		||||
	char *sname = NULL;
 | 
			
		||||
	snd_config_t *sconfig;
 | 
			
		||||
	char *host = NULL;
 | 
			
		||||
	char *socket = NULL;
 | 
			
		||||
	long port = -1;
 | 
			
		||||
	int err;
 | 
			
		||||
	int local;
 | 
			
		||||
	struct hostent *h;
 | 
			
		||||
	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, "server") == 0) {
 | 
			
		||||
			err = snd_config_string_get(n, &server);
 | 
			
		||||
			if (err < 0) {
 | 
			
		||||
				ERR("Invalid type for server");
 | 
			
		||||
				return -EINVAL;
 | 
			
		||||
			}
 | 
			
		||||
			continue;
 | 
			
		||||
		}
 | 
			
		||||
		if (strcmp(n->id, "sname") == 0) {
 | 
			
		||||
			err = snd_config_string_get(n, &sname);
 | 
			
		||||
			if (err < 0) {
 | 
			
		||||
				ERR("Invalid type for sname");
 | 
			
		||||
				return -EINVAL;
 | 
			
		||||
			}
 | 
			
		||||
			continue;
 | 
			
		||||
		}
 | 
			
		||||
		ERR("Unknown field: %s", n->id);
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	}
 | 
			
		||||
	if (!sname) {
 | 
			
		||||
		ERR("sname is not defined");
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	}
 | 
			
		||||
	if (!server) {
 | 
			
		||||
		ERR("server is not defined");
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	}
 | 
			
		||||
	err = snd_config_searchv(snd_config, &sconfig, "server", server, 0);
 | 
			
		||||
	if (err < 0) {
 | 
			
		||||
		ERR("Unknown server %s", server);
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	}
 | 
			
		||||
	snd_config_foreach(i, conf) {
 | 
			
		||||
		snd_config_t *n = snd_config_entry(i);
 | 
			
		||||
		if (strcmp(n->id, "comment") == 0)
 | 
			
		||||
			continue;
 | 
			
		||||
		if (strcmp(n->id, "host") == 0) {
 | 
			
		||||
			err = snd_config_string_get(n, &host);
 | 
			
		||||
			if (err < 0) {
 | 
			
		||||
				ERR("Invalid type for host");
 | 
			
		||||
				return -EINVAL;
 | 
			
		||||
			}
 | 
			
		||||
			continue;
 | 
			
		||||
		}
 | 
			
		||||
		if (strcmp(n->id, "socket") == 0) {
 | 
			
		||||
			err = snd_config_string_get(n, &socket);
 | 
			
		||||
			if (err < 0) {
 | 
			
		||||
				ERR("Invalid type for socket");
 | 
			
		||||
				return -EINVAL;
 | 
			
		||||
			}
 | 
			
		||||
			continue;
 | 
			
		||||
		}
 | 
			
		||||
		if (strcmp(n->id, "port") == 0) {
 | 
			
		||||
			err = snd_config_integer_get(n, &port);
 | 
			
		||||
			if (err < 0) {
 | 
			
		||||
				ERR("Invalid type for port");
 | 
			
		||||
				return -EINVAL;
 | 
			
		||||
			}
 | 
			
		||||
			continue;
 | 
			
		||||
		}
 | 
			
		||||
		ERR("Unknown field: %s", n->id);
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if (!host) {
 | 
			
		||||
		ERR("host is not defined");
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	}
 | 
			
		||||
	if (!socket) {
 | 
			
		||||
		ERR("socket is not defined");
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	}
 | 
			
		||||
	h = gethostbyname(host);
 | 
			
		||||
	if (!h) {
 | 
			
		||||
		ERR("Cannot resolve %s", host);
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	}
 | 
			
		||||
	local = is_local(h);
 | 
			
		||||
	if (!local) {
 | 
			
		||||
		ERR("%s is not the local host", host);
 | 
			
		||||
		return -EINVAL;
 | 
			
		||||
	}
 | 
			
		||||
	return snd_ctl_shm_open(handlep, name, socket, sname);
 | 
			
		||||
}
 | 
			
		||||
				
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue