Blame modules/mixer/simple/sbasedl.c

Packit 4a16fb
/*
Packit 4a16fb
 *  Mixer Interface - simple abstact module - base library (dlopen function)
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
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 "config.h"
Packit 4a16fb
#include "asoundlib.h"
Packit 4a16fb
#include "mixer_abst.h"
Packit 4a16fb
#include "sbase.h"
Packit 4a16fb
Packit 4a16fb
#define SO_PATH ALSA_PLUGIN_DIR "/smixer"
Packit 4a16fb
Packit 4a16fb
int mixer_simple_basic_dlopen(snd_mixer_class_t *class,
Packit 4a16fb
			      bclass_base_ops_t **ops)
Packit 4a16fb
{
Packit 4a16fb
	struct bclass_private *priv = snd_mixer_sbasic_get_private(class);
Packit 4a16fb
	const char *lib = "smixer-sbase.so";
Packit 4a16fb
	void (*initpriv)(snd_mixer_class_t *class, struct bclass_private *priv);
Packit 4a16fb
	char *xlib, *path, errbuf[256];
Packit 4a16fb
	void *h;
Packit 4a16fb
	int initflag = 0;
Packit 4a16fb
Packit 4a16fb
	if (priv == NULL) {
Packit 4a16fb
		priv = calloc(1, sizeof(*priv));
Packit 4a16fb
		if (priv == NULL)
Packit 4a16fb
			return -ENOMEM;
Packit 4a16fb
		initflag = 1;
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
		if (initflag)
Packit 4a16fb
			free(priv);
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	}
Packit 4a16fb
	strcpy(xlib, path);
Packit 4a16fb
	strcat(xlib, "/");
Packit 4a16fb
	strcat(xlib, lib);
Packit 4a16fb
	h = 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
		goto __error;
Packit 4a16fb
	}
Packit 4a16fb
	initpriv = dlsym(h, "alsa_mixer_sbasic_initpriv");
Packit 4a16fb
	if (initpriv == NULL) {
Packit 4a16fb
		SNDERR("Symbol 'alsa_mixer_sbasic_initpriv' was not found in '%s'", xlib);
Packit 4a16fb
		goto __error;
Packit 4a16fb
	}
Packit 4a16fb
	priv->ops.event = dlsym(h, "alsa_mixer_sbasic_event");
Packit 4a16fb
	if (priv->ops.event == NULL) {
Packit 4a16fb
		SNDERR("Symbol 'alsa_mixer_sbasic_event' was not found in '%s'", xlib);
Packit 4a16fb
		goto __error;
Packit 4a16fb
	}
Packit 4a16fb
	priv->ops.selreg = dlsym(h, "alsa_mixer_sbasic_selreg");
Packit 4a16fb
	if (priv->ops.selreg == NULL) {
Packit 4a16fb
		SNDERR("Symbol 'alsa_mixer_sbasic_selreg' was not found in '%s'", xlib);
Packit 4a16fb
		goto __error;
Packit 4a16fb
	}
Packit 4a16fb
	priv->ops.sidreg = dlsym(h, "alsa_mixer_sbasic_sidreg");
Packit 4a16fb
	if (priv->ops.sidreg == NULL) {
Packit 4a16fb
		SNDERR("Symbol 'alsa_mixer_sbasic_sidreg' was not found in '%s'", xlib);
Packit 4a16fb
		goto __error;
Packit 4a16fb
	}
Packit 4a16fb
	free(xlib);
Packit 4a16fb
	if (initflag)
Packit 4a16fb
		initpriv(class, priv);
Packit 4a16fb
	priv->dl_sbase = h;
Packit 4a16fb
	if (ops)
Packit 4a16fb
		*ops = &priv->ops;
Packit 4a16fb
	return 1;
Packit 4a16fb
Packit 4a16fb
      __error:
Packit 4a16fb
      	if (initflag)
Packit 4a16fb
      		free(priv);
Packit 4a16fb
	if (h)
Packit 4a16fb
		snd_dlclose(h);
Packit 4a16fb
	free(xlib);
Packit 4a16fb
	return -ENXIO;
Packit 4a16fb
}