Blame src/mixer/simple_abst.c

Packit 4a16fb
/**
Packit 4a16fb
 * \file mixer/simple_abst.c
Packit 4a16fb
 * \brief Mixer Simple Element Class Interface - Module Abstraction
Packit 4a16fb
 * \author Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 * \date 2005
Packit 4a16fb
 *
Packit 4a16fb
 * Mixer simple element class interface.
Packit 4a16fb
 */
Packit 4a16fb
/*
Packit 4a16fb
 *  Mixer Interface - simple controls - abstraction module
Packit 4a16fb
 *  Copyright (c) 2005 by Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 *
Packit 4a16fb
 *
Packit 4a16fb
 *   This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 *   published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 *   the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *   License along with this library; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#include <stdio.h>
Packit 4a16fb
#include <stdlib.h>
Packit 4a16fb
#include <unistd.h>
Packit 4a16fb
#include <string.h>
Packit 4a16fb
#include <fcntl.h>
Packit 4a16fb
#include <sys/ioctl.h>
Packit 4a16fb
#include <math.h>
Packit 4a16fb
#include <dlfcn.h>
Packit 4a16fb
#include "mixer_local.h"
Packit 4a16fb
#include "mixer_simple.h"
Packit 4a16fb
Packit 4a16fb
#ifndef DOC_HIDDEN
Packit 4a16fb
Packit 4a16fb
#define SO_PATH ALSA_PLUGIN_DIR "/smixer"
Packit 4a16fb
Packit 4a16fb
typedef struct _class_priv {
Packit 4a16fb
	char *device;
Packit 4a16fb
	snd_ctl_t *ctl;
Packit 4a16fb
	snd_hctl_t *hctl;
Packit 4a16fb
	int attach_flag;
Packit 4a16fb
	snd_ctl_card_info_t *info;
Packit 4a16fb
	void *dlhandle;
Packit 4a16fb
	void *private_data;
Packit 4a16fb
	void (*private_free)(snd_mixer_class_t *class);
Packit 4a16fb
} class_priv_t;
Packit 4a16fb
Packit 4a16fb
typedef int (*snd_mixer_sbasic_init_t)(snd_mixer_class_t *class);
Packit 4a16fb
typedef int (*snd_mixer_sfbasic_init_t)(snd_mixer_class_t *class,
Packit 4a16fb
					snd_mixer_t *mixer,
Packit 4a16fb
					const char *device);
Packit 4a16fb
Packit 4a16fb
#endif /* !DOC_HIDDEN */
Packit 4a16fb
Packit 4a16fb
static int try_open(snd_mixer_class_t *class, const char *lib)
Packit 4a16fb
{
Packit 4a16fb
	class_priv_t *priv = snd_mixer_class_get_private(class);
Packit 4a16fb
	snd_mixer_event_t event_func;
Packit 4a16fb
	snd_mixer_sbasic_init_t init_func = NULL;
Packit 4a16fb
	char *xlib, *path, errbuf[256];
Packit 4a16fb
	void *h;
Packit 4a16fb
	int err = 0;
Packit 4a16fb
Packit 4a16fb
	if (!lib)
Packit 4a16fb
		return -ENXIO;
Packit 4a16fb
	path = getenv("ALSA_MIXER_SIMPLE_MODULES");
Packit 4a16fb
	if (!path)
Packit 4a16fb
		path = SO_PATH;
Packit 4a16fb
	xlib = malloc(strlen(lib) + strlen(path) + 1 + 1);
Packit 4a16fb
	if (xlib == NULL)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	strcpy(xlib, path);
Packit 4a16fb
	strcat(xlib, "/");
Packit 4a16fb
	strcat(xlib, lib);
Packit 4a16fb
	h = INTERNAL(snd_dlopen)(xlib, RTLD_NOW, errbuf, sizeof(errbuf));
Packit 4a16fb
	if (h == NULL) {
Packit 4a16fb
		SNDERR("Unable to open library '%s' (%s)", xlib, errbuf);
Packit 4a16fb
		free(xlib);
Packit 4a16fb
		return -ENXIO;
Packit 4a16fb
	}
Packit 4a16fb
	priv->dlhandle = h;
Packit 4a16fb
	event_func = snd_dlsym(h, "alsa_mixer_simple_event", NULL);
Packit 4a16fb
	if (event_func == NULL) {
Packit 4a16fb
		SNDERR("Symbol 'alsa_mixer_simple_event' was not found in '%s'", xlib);
Packit 4a16fb
		err = -ENXIO;
Packit 4a16fb
	}
Packit 4a16fb
	if (err == 0) {
Packit 4a16fb
		init_func = snd_dlsym(h, "alsa_mixer_simple_init", NULL);
Packit 4a16fb
		if (init_func == NULL) {
Packit 4a16fb
			SNDERR("Symbol 'alsa_mixer_simple_init' was not found in '%s'", xlib);
Packit 4a16fb
			err = -ENXIO;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	free(xlib);
Packit 4a16fb
	err = err == 0 ? init_func(class) : err;
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	snd_mixer_class_set_event(class, event_func);
Packit 4a16fb
	return 1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int try_open_full(snd_mixer_class_t *class, snd_mixer_t *mixer,
Packit 4a16fb
			 const char *lib, const char *device)
Packit 4a16fb
{
Packit 4a16fb
	class_priv_t *priv = snd_mixer_class_get_private(class);
Packit 4a16fb
	snd_mixer_event_t event_func;
Packit 4a16fb
	snd_mixer_sfbasic_init_t init_func = NULL;
Packit 4a16fb
	char *xlib, *path, errbuf[256];
Packit 4a16fb
	void *h;
Packit 4a16fb
	int err = 0;
Packit 4a16fb
Packit 4a16fb
	path = getenv("ALSA_MIXER_SIMPLE_MODULES");
Packit 4a16fb
	if (!path)
Packit 4a16fb
		path = SO_PATH;
Packit 4a16fb
	xlib = malloc(strlen(lib) + strlen(path) + 1 + 1);
Packit 4a16fb
	if (xlib == NULL)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	strcpy(xlib, path);
Packit 4a16fb
	strcat(xlib, "/");
Packit 4a16fb
	strcat(xlib, lib);
Packit 4a16fb
	/* note python modules requires RTLD_GLOBAL */
Packit 4a16fb
	h = INTERNAL(snd_dlopen)(xlib, RTLD_NOW|RTLD_GLOBAL, errbuf, sizeof(errbuf));
Packit 4a16fb
	if (h == NULL) {
Packit 4a16fb
		SNDERR("Unable to open library '%s'", xlib);
Packit 4a16fb
		free(xlib);
Packit 4a16fb
		return -ENXIO;
Packit 4a16fb
	}
Packit 4a16fb
	priv->dlhandle = h;
Packit 4a16fb
	event_func = snd_dlsym(h, "alsa_mixer_simple_event", NULL);
Packit 4a16fb
	if (event_func == NULL) {
Packit 4a16fb
		SNDERR("Symbol 'alsa_mixer_simple_event' was not found in '%s'", xlib);
Packit 4a16fb
		err = -ENXIO;
Packit 4a16fb
	}
Packit 4a16fb
	if (err == 0) {
Packit 4a16fb
		init_func = snd_dlsym(h, "alsa_mixer_simple_finit", NULL);
Packit 4a16fb
		if (init_func == NULL) {
Packit 4a16fb
			SNDERR("Symbol 'alsa_mixer_simple_finit' was not found in '%s'", xlib);
Packit 4a16fb
			err = -ENXIO;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	free(xlib);
Packit 4a16fb
	err = err == 0 ? init_func(class, mixer, device) : err;
Packit 4a16fb
	if (err < 0)
Packit 4a16fb
		return err;
Packit 4a16fb
	snd_mixer_class_set_event(class, event_func);
Packit 4a16fb
	return 1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int match(snd_mixer_class_t *class, const char *lib, const char *searchl)
Packit 4a16fb
{
Packit 4a16fb
	class_priv_t *priv = snd_mixer_class_get_private(class);
Packit 4a16fb
	const char *components;
Packit 4a16fb
Packit 4a16fb
	if (searchl == NULL)
Packit 4a16fb
		return try_open(class, lib);
Packit 4a16fb
	components = snd_ctl_card_info_get_components(priv->info);
Packit 4a16fb
	while (*components != '\0') {
Packit 4a16fb
		if (!strncmp(components, searchl, strlen(searchl)))
Packit 4a16fb
			return try_open(class, lib);
Packit 4a16fb
		while (*components != ' ' && *components != '\0')
Packit 4a16fb
			components++;
Packit 4a16fb
		while (*components == ' ' && *components != '\0')
Packit 4a16fb
			components++;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int find_full(snd_mixer_class_t *class, snd_mixer_t *mixer,
Packit 4a16fb
		     snd_config_t *top, const char *device)
Packit 4a16fb
{
Packit 4a16fb
	snd_config_iterator_t i, next;
Packit 4a16fb
	char *lib;
Packit 4a16fb
	const char *id;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	snd_config_for_each(i, next, top) {
Packit 4a16fb
		snd_config_t *n = snd_config_iterator_entry(i);
Packit 4a16fb
		if (snd_config_get_id(n, &id) < 0)
Packit 4a16fb
			continue;
Packit 4a16fb
		if (strcmp(id, "_full"))
Packit 4a16fb
			continue;
Packit 4a16fb
		err = snd_config_get_string(n, (const char **)&lib);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			return err;
Packit 4a16fb
		err = try_open_full(class, mixer, lib, device);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			return err;
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
	return -ENOENT;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int find_module(snd_mixer_class_t *class, snd_config_t *top)
Packit 4a16fb
{
Packit 4a16fb
	snd_config_iterator_t i, next;
Packit 4a16fb
	snd_config_iterator_t j, jnext;
Packit 4a16fb
	char *lib, *searchl;
Packit 4a16fb
	const char *id;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	snd_config_for_each(i, next, top) {
Packit 4a16fb
		snd_config_t *n = snd_config_iterator_entry(i);
Packit 4a16fb
		if (snd_config_get_id(n, &id) < 0)
Packit 4a16fb
			continue;
Packit 4a16fb
		if (*id == '_')
Packit 4a16fb
			continue;
Packit 4a16fb
		searchl = NULL;
Packit 4a16fb
		lib = NULL;
Packit 4a16fb
		snd_config_for_each(j, jnext, n) {
Packit 4a16fb
			snd_config_t *m = snd_config_iterator_entry(j);
Packit 4a16fb
			if (snd_config_get_id(m, &id) < 0)
Packit 4a16fb
				continue;
Packit 4a16fb
			if (!strcmp(id, "searchl")) {
Packit 4a16fb
				err = snd_config_get_string(m, (const char **)&searchl);
Packit 4a16fb
				if (err < 0)
Packit 4a16fb
					return err;
Packit 4a16fb
				continue;
Packit 4a16fb
			}
Packit 4a16fb
			if (!strcmp(id, "lib")) {
Packit 4a16fb
				err = snd_config_get_string(m, (const char **)&lib);
Packit 4a16fb
				if (err < 0)
Packit 4a16fb
					return err;
Packit 4a16fb
				continue;
Packit 4a16fb
			}
Packit 4a16fb
		}
Packit 4a16fb
		err = match(class, lib, searchl);
Packit 4a16fb
		if (err == 1)
Packit 4a16fb
			return 0;
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			return err;
Packit 4a16fb
	}
Packit 4a16fb
	return -ENOENT;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void private_free(snd_mixer_class_t *class)
Packit 4a16fb
{
Packit 4a16fb
	class_priv_t *priv = snd_mixer_class_get_private(class);
Packit 4a16fb
	
Packit 4a16fb
	if (priv->private_free)
Packit 4a16fb
		priv->private_free(class);
Packit 4a16fb
	if (priv->dlhandle)
Packit 4a16fb
		snd_dlclose(priv->dlhandle);
Packit 4a16fb
	if (priv->info)
Packit 4a16fb
		snd_ctl_card_info_free(priv->info);
Packit 4a16fb
	if (priv->hctl) {
Packit 4a16fb
		if (priv->attach_flag)
Packit 4a16fb
			snd_mixer_detach_hctl(snd_mixer_class_get_mixer(class), priv->hctl);
Packit 4a16fb
		snd_hctl_close(priv->hctl);
Packit 4a16fb
	} else if (priv->ctl)
Packit 4a16fb
		snd_ctl_close(priv->ctl);
Packit 4a16fb
	free(priv->device);
Packit 4a16fb
	free(priv);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Register mixer simple element class - basic abstraction
Packit 4a16fb
 * \param mixer Mixer handle
Packit 4a16fb
 * \param options Options container
Packit 4a16fb
 * \param classp Pointer to returned mixer simple element class handle (or NULL
Packit 4a16fb
 * \return 0 on success otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_mixer_simple_basic_register(snd_mixer_t *mixer,
Packit 4a16fb
				    struct snd_mixer_selem_regopt *options,
Packit 4a16fb
				    snd_mixer_class_t **classp)
Packit 4a16fb
{
Packit 4a16fb
	snd_mixer_class_t *class;
Packit 4a16fb
	class_priv_t *priv = calloc(1, sizeof(*priv));
Packit 4a16fb
	const char *file;
Packit 4a16fb
	snd_input_t *input;
Packit 4a16fb
	snd_config_t *top = NULL;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	if (priv == NULL)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	if (options->device == NULL) {
Packit 4a16fb
		free(priv);
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
	if (snd_mixer_class_malloc(&class)) {
Packit 4a16fb
		free(priv);
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	priv->device = strdup(options->device);
Packit 4a16fb
	if (priv->device == NULL) {
Packit 4a16fb
		free(priv);
Packit 4a16fb
		snd_mixer_class_free(class);
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	snd_mixer_class_set_compare(class, snd_mixer_selem_compare);
Packit 4a16fb
	snd_mixer_class_set_private(class, priv);
Packit 4a16fb
	snd_mixer_class_set_private_free(class, private_free);
Packit 4a16fb
	file = getenv("ALSA_MIXER_SIMPLE");
Packit 4a16fb
	if (!file) {
Packit 4a16fb
		const char *topdir = snd_config_topdir();
Packit 4a16fb
		char *s = alloca(strlen(topdir) + strlen("smixer.conf") + 2);
Packit 4a16fb
		sprintf(s, "%s/smixer.conf", topdir);
Packit 4a16fb
		file = s;
Packit 4a16fb
	}
Packit 4a16fb
	err = snd_config_top(&top);
Packit 4a16fb
	if (err >= 0) {
Packit 4a16fb
		err = snd_input_stdio_open(&input, file, "r");
Packit 4a16fb
		if (err < 0) {
Packit 4a16fb
			SNDERR("unable to open simple mixer configuration file '%s'", file);
Packit 4a16fb
			goto __error;
Packit 4a16fb
		}
Packit 4a16fb
		err = snd_config_load(top, input);
Packit 4a16fb
		snd_input_close(input);
Packit 4a16fb
		if (err < 0) {
Packit 4a16fb
			SNDERR("%s may be old or corrupted: consider to remove or fix it", file);
Packit 4a16fb
			goto __error;
Packit 4a16fb
		}
Packit 4a16fb
		err = find_full(class, mixer, top, priv->device);
Packit 4a16fb
		if (err >= 0)
Packit 4a16fb
			goto __full;
Packit 4a16fb
	}
Packit 4a16fb
	if (err >= 0) {
Packit 4a16fb
		err = snd_ctl_open(&priv->ctl, priv->device, 0);
Packit 4a16fb
		if (err < 0) {
Packit 4a16fb
			SNDERR("unable to open control device '%s': %s", priv->device, snd_strerror(err));
Packit 4a16fb
			goto __error;
Packit 4a16fb
		}
Packit 4a16fb
		err = snd_hctl_open_ctl(&priv->hctl, priv->ctl);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			goto __error;
Packit 4a16fb
		err = snd_ctl_card_info_malloc(&priv->info);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			goto __error;
Packit 4a16fb
		err = snd_ctl_card_info(priv->ctl, priv->info);
Packit 4a16fb
		if (err < 0)
Packit 4a16fb
			goto __error;
Packit 4a16fb
	}
Packit 4a16fb
	if (err >= 0)
Packit 4a16fb
		err = find_module(class, top);
Packit 4a16fb
	if (err >= 0)
Packit 4a16fb
		err = snd_mixer_attach_hctl(mixer, priv->hctl);
Packit 4a16fb
	if (err >= 0) {
Packit 4a16fb
		priv->attach_flag = 1;
Packit 4a16fb
		err = snd_mixer_class_register(class, mixer);
Packit 4a16fb
	}
Packit 4a16fb
      __full:
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
	      __error:
Packit 4a16fb
		if (top)
Packit 4a16fb
			snd_config_delete(top);
Packit 4a16fb
	      	if (class)
Packit 4a16fb
			snd_mixer_class_free(class);
Packit 4a16fb
		return err;
Packit 4a16fb
	}
Packit 4a16fb
	if (top)
Packit 4a16fb
		snd_config_delete(top);
Packit 4a16fb
	if (classp)
Packit 4a16fb
		*classp = class;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Basic Mixer Abstraction - Get information about device
Packit 4a16fb
 * \param class Mixer class
Packit 4a16fb
 * \param info Info structure
Packit 4a16fb
 * \return 0 on success otherwise a negative error code
Packit 4a16fb
 */
Packit 4a16fb
int snd_mixer_sbasic_info(const snd_mixer_class_t *class, sm_class_basic_t *info)
Packit 4a16fb
{
Packit 4a16fb
	class_priv_t *priv = snd_mixer_class_get_private(class);
Packit 4a16fb
Packit 4a16fb
	if (class == NULL || info == NULL)
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	info->device = priv->device;
Packit 4a16fb
	info->ctl = priv->ctl;
Packit 4a16fb
	info->hctl = priv->hctl;
Packit 4a16fb
	info->info = priv->info;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get private data for basic abstraction
Packit 4a16fb
 * \param class Mixer class
Packit 4a16fb
 * \return private data
Packit 4a16fb
 */
Packit 4a16fb
void *snd_mixer_sbasic_get_private(const snd_mixer_class_t *class)
Packit 4a16fb
{
Packit 4a16fb
	class_priv_t *priv = snd_mixer_class_get_private(class);
Packit 4a16fb
Packit 4a16fb
	if (class == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	return priv->private_data;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Set private data for basic abstraction
Packit 4a16fb
 * \param class Mixer class
Packit 4a16fb
 * \param private_data Private data
Packit 4a16fb
 */
Packit 4a16fb
void snd_mixer_sbasic_set_private(const snd_mixer_class_t *class, void *private_data)
Packit 4a16fb
{
Packit 4a16fb
	class_priv_t *priv;
Packit 4a16fb
Packit 4a16fb
	if (class == NULL)
Packit 4a16fb
		return;
Packit 4a16fb
	priv = snd_mixer_class_get_private(class);
Packit 4a16fb
	priv->private_data = private_data;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Set private data free callback for basic abstraction
Packit 4a16fb
 * \param class Mixer class
Packit 4a16fb
 * \param private_free free callback for private data
Packit 4a16fb
 */
Packit 4a16fb
void snd_mixer_sbasic_set_private_free(const snd_mixer_class_t *class, void (*private_free)(snd_mixer_class_t *class))
Packit 4a16fb
{
Packit 4a16fb
	class_priv_t *priv;
Packit 4a16fb
Packit 4a16fb
	if (class == NULL)
Packit 4a16fb
		return;
Packit 4a16fb
	priv = snd_mixer_class_get_private(class);
Packit 4a16fb
	priv->private_free = private_free;
Packit 4a16fb
}