Blame modules/mixer/simple/python.c

Packit 4a16fb
/*
Packit 4a16fb
 *  Mixer Interface - python binding simple abstact module
Packit 4a16fb
 *  Copyright (c) 2007 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 "Python.h"
Packit 4a16fb
#include <stddef.h>
Packit 4a16fb
#include "config.h"
Packit 4a16fb
#include "asoundlib.h"
Packit 4a16fb
#include "mixer_abst.h"
Packit 4a16fb
Packit 4a16fb
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
Packit 4a16fb
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
struct python_priv {
Packit 4a16fb
	int py_initialized;
Packit 4a16fb
	PyObject *py_event_func;
Packit 4a16fb
	PyObject *py_mdict;
Packit 4a16fb
	PyObject *py_mixer;
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
#define SCRIPT ALSA_PLUGIN_DIR "/smixer/python/main.py"
Packit 4a16fb
Packit 4a16fb
struct pymelem {
Packit 4a16fb
	PyObject_HEAD
Packit 4a16fb
	sm_selem_t selem;
Packit 4a16fb
	PyObject *py_mixer;
Packit 4a16fb
	snd_mixer_elem_t *melem;
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
struct pymixer {
Packit 4a16fb
	PyObject_HEAD
Packit 4a16fb
	snd_mixer_class_t *class;
Packit 4a16fb
	snd_mixer_t *mixer;
Packit 4a16fb
	PyObject *mdict;
Packit 4a16fb
	int hctl_count;
Packit 4a16fb
	void **hctl;
Packit 4a16fb
	int helem_count;
Packit 4a16fb
	void **helem;
Packit 4a16fb
	int melem_count;
Packit 4a16fb
	void **melem;
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
static PyInterpreterState *main_interpreter;
Packit 4a16fb
Packit 4a16fb
#if PY_MAJOR_VERSION >= 3
Packit 4a16fb
  #define PyInt_FromLong PyLong_FromLong
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
static inline int get_long(PyObject *o, long *val)
Packit 4a16fb
{
Packit 4a16fb
#if PY_MAJOR_VERSION < 3
Packit 4a16fb
        if (PyInt_Check(o)) {
Packit 4a16fb
                *val = PyInt_AsLong(o);
Packit 4a16fb
                return 0;
Packit 4a16fb
        }
Packit 4a16fb
#endif
Packit 4a16fb
        if (PyLong_Check(o)) {
Packit 4a16fb
                *val = PyLong_AsLong(o);
Packit 4a16fb
                return 0;
Packit 4a16fb
        }
Packit 4a16fb
        return 1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static inline PyObject *InternFromString(const char *name)
Packit 4a16fb
{
Packit 4a16fb
#if PY_MAJOR_VERSION < 3
Packit 4a16fb
        return PyString_InternFromString(name);
Packit 4a16fb
#else
Packit 4a16fb
        return PyUnicode_InternFromString(name);
Packit 4a16fb
#endif
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void *get_C_ptr(PyObject *obj, const char *attr)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *o;
Packit 4a16fb
	long val;
Packit 4a16fb
Packit 4a16fb
	o = PyObject_GetAttr(obj, InternFromString(attr));
Packit 4a16fb
	if (!o) {
Packit 4a16fb
		PyErr_Format(PyExc_TypeError, "missing '%s' attribute", attr);
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	if (get_long(o, &val)) {
Packit 4a16fb
		PyErr_Format(PyExc_TypeError, "'%s' attribute is not Int or Long", attr);
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	return (void *)val;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct pymelem *melem_to_pymelem(snd_mixer_elem_t *elem)
Packit 4a16fb
{
Packit 4a16fb
	return (struct pymelem *)((char *)snd_mixer_elem_get_private(elem) - offsetof(struct pymelem, selem));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int pcall(struct pymelem *pymelem, const char *attr, PyObject *args, PyObject **_res)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj = (PyObject *)pymelem, *res;
Packit 4a16fb
	long xres = 0;
Packit 4a16fb
Packit 4a16fb
	if (_res)
Packit 4a16fb
		*_res = NULL;
Packit 4a16fb
	obj = PyObject_GetAttr(obj, InternFromString(attr));
Packit 4a16fb
	if (!obj) {
Packit 4a16fb
		PyErr_Format(PyExc_TypeError, "missing '%s' attribute", attr);
Packit 4a16fb
		PyErr_Print();
Packit 4a16fb
		PyErr_Clear();
Packit 4a16fb
		Py_DECREF(args);
Packit 4a16fb
		return -EIO;
Packit 4a16fb
	}
Packit 4a16fb
	res = PyObject_CallObject(obj, args);
Packit 4a16fb
	Py_XDECREF(args);
Packit 4a16fb
	if (res == NULL) {
Packit 4a16fb
		PyErr_Print();
Packit 4a16fb
		PyErr_Clear();
Packit 4a16fb
		return -EIO;
Packit 4a16fb
	}
Packit 4a16fb
	if (_res && PyTuple_Check(res)) {
Packit 4a16fb
		*_res = res;
Packit 4a16fb
		res = PyTuple_GetItem(res, 0);
Packit 4a16fb
	}
Packit 4a16fb
	if (PyLong_Check(res)) {
Packit 4a16fb
		xres = PyLong_AsLong(res);
Packit 4a16fb
#if PY_MAJOR_VERSION < 3
Packit 4a16fb
	} else if (PyInt_Check(res)) {
Packit 4a16fb
		xres = PyInt_AsLong(res);
Packit 4a16fb
#endif
Packit 4a16fb
	} else if (res == Py_None) {
Packit 4a16fb
		xres = 0;
Packit 4a16fb
	} else if (PyBool_Check(res)) {
Packit 4a16fb
		xres = res == Py_True;
Packit 4a16fb
	} else {
Packit 4a16fb
		PyErr_Format(PyExc_TypeError, "wrong result from '%s'!", attr);
Packit 4a16fb
		PyErr_Print();
Packit 4a16fb
		PyErr_Clear();
Packit 4a16fb
		Py_DECREF(res);
Packit 4a16fb
		if (_res)
Packit 4a16fb
			*_res = NULL;
Packit 4a16fb
		return -EIO;
Packit 4a16fb
	}
Packit 4a16fb
	if (_res && *_res)
Packit 4a16fb
		return xres;
Packit 4a16fb
	Py_DECREF(res);
Packit 4a16fb
	return xres;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int is_ops(snd_mixer_elem_t *elem, int dir, int cmd, int val)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj1;
Packit 4a16fb
	struct pymelem *pymelem = melem_to_pymelem(elem);
Packit 4a16fb
	char *s, fcn[32] = "opsIs";
Packit 4a16fb
	int res, xdir = 1, xval = 0;
Packit 4a16fb
Packit 4a16fb
	switch (cmd) {
Packit 4a16fb
	case SM_OPS_IS_ACTIVE: 	s = "Active"; xdir = 0; break;
Packit 4a16fb
	case SM_OPS_IS_MONO:	s = "Mono"; break;
Packit 4a16fb
	case SM_OPS_IS_CHANNEL:	s = "Channel"; xval = 1; break;
Packit 4a16fb
	case SM_OPS_IS_ENUMERATED: s = "Enumerated"; xdir = val == 1; break;
Packit 4a16fb
	case SM_OPS_IS_ENUMCNT:	s = "EnumCnt"; break;
Packit 4a16fb
	default:
Packit 4a16fb
		return 1;
Packit 4a16fb
	}
Packit 4a16fb
	strcat(fcn, s);
Packit 4a16fb
Packit 4a16fb
	obj1 = PyTuple_New(xdir + xval);
Packit 4a16fb
	if (xdir) {
Packit 4a16fb
		PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(dir));
Packit 4a16fb
		if (xval)
Packit 4a16fb
			PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong(val));
Packit 4a16fb
	}
Packit 4a16fb
	res = pcall(pymelem, fcn, obj1, NULL);
Packit 4a16fb
	return res < 0 ? 0 : res;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int get_x_range_ops(snd_mixer_elem_t *elem, int dir,
Packit 4a16fb
                           long *min, long *max, const char *attr)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj1, *t1, *t2, *res;
Packit 4a16fb
	struct pymelem *pymelem = melem_to_pymelem(elem);
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	obj1 = PyTuple_New(1);
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(dir));
Packit 4a16fb
	err = pcall(pymelem, attr, obj1, &res;;
Packit 4a16fb
	if (err >= 0) {
Packit 4a16fb
		t1 = PyTuple_GetItem(res, 1);
Packit 4a16fb
		t2 = PyTuple_GetItem(res, 2);
Packit 4a16fb
		if (PyLong_Check(t1) && PyLong_Check(t2)) {
Packit 4a16fb
			*min = PyLong_AsLong(PyTuple_GetItem(res, 1));
Packit 4a16fb
			*max = PyLong_AsLong(PyTuple_GetItem(res, 2));
Packit 4a16fb
			err = 0;
Packit 4a16fb
#if PY_MAJOR_VERSION < 3
Packit 4a16fb
		} else if (PyInt_Check(t1) && PyInt_Check(t2)) {
Packit 4a16fb
			*min = PyInt_AsLong(PyTuple_GetItem(res, 1));
Packit 4a16fb
			*max = PyInt_AsLong(PyTuple_GetItem(res, 2));
Packit 4a16fb
			err = 0;
Packit 4a16fb
#endif
Packit 4a16fb
		} else {
Packit 4a16fb
			PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)");
Packit 4a16fb
			PyErr_Print();
Packit 4a16fb
			PyErr_Clear();
Packit 4a16fb
			err = -EIO;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	Py_XDECREF(res);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int get_range_ops(snd_mixer_elem_t *elem, int dir,
Packit 4a16fb
                         long *min, long *max)
Packit 4a16fb
{
Packit 4a16fb
	return get_x_range_ops(elem, dir, min, max, "opsGetRange");
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int set_range_ops(snd_mixer_elem_t *elem, int dir,
Packit 4a16fb
                         long min, long max)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj1;
Packit 4a16fb
	struct pymelem *pymelem = melem_to_pymelem(elem);
Packit 4a16fb
Packit 4a16fb
	obj1 = PyTuple_New(3);
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(dir));
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong(min));
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 2, PyInt_FromLong(max));
Packit 4a16fb
	return pcall(pymelem, "opsGetRange", obj1, NULL);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int get_x_ops(snd_mixer_elem_t *elem, int dir,
Packit 4a16fb
                     long channel, long *value,
Packit 4a16fb
                     const char *attr)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj1, *t1, *res;
Packit 4a16fb
	struct pymelem *pymelem = melem_to_pymelem(elem);
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	obj1 = PyTuple_New(2);
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(dir));
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong(channel));
Packit 4a16fb
	err = pcall(pymelem, attr, obj1, &res;;
Packit 4a16fb
	if (err >= 0) {
Packit 4a16fb
		t1 = PyTuple_GetItem(res, 1);
Packit 4a16fb
		if (PyLong_Check(t1)) {
Packit 4a16fb
			*value = PyLong_AsLong(t1);
Packit 4a16fb
			err = 0;
Packit 4a16fb
#if PY_MAJOR_VERSION < 3
Packit 4a16fb
		} else if (PyInt_Check(t1)) {
Packit 4a16fb
			*value = PyInt_AsLong(t1);
Packit 4a16fb
			err = 0;
Packit 4a16fb
#endif
Packit 4a16fb
		} else {
Packit 4a16fb
			PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)");
Packit 4a16fb
			PyErr_Print();
Packit 4a16fb
			PyErr_Clear();
Packit 4a16fb
			err = -EIO;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	Py_XDECREF(res);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int get_volume_ops(snd_mixer_elem_t *elem, int dir,
Packit 4a16fb
			  snd_mixer_selem_channel_id_t channel, long *value)
Packit 4a16fb
{
Packit 4a16fb
	return get_x_ops(elem, dir, channel, value, "opsGetVolume");
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int get_switch_ops(snd_mixer_elem_t *elem, int dir,
Packit 4a16fb
                          snd_mixer_selem_channel_id_t channel, int *value)
Packit 4a16fb
{
Packit 4a16fb
	long value1;
Packit 4a16fb
	int res;
Packit 4a16fb
	res = get_x_ops(elem, dir, channel, &value1, "opsGetSwitch");
Packit 4a16fb
	*value = value1;
Packit 4a16fb
	return res;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int ask_vol_dB_ops(snd_mixer_elem_t *elem,
Packit 4a16fb
			  int dir,
Packit 4a16fb
			  long value,
Packit 4a16fb
			  long *dbValue)
Packit 4a16fb
{
Packit 4a16fb
	return get_x_ops(elem, dir, value, dbValue, "opsGetVolDB");
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int ask_dB_vol_ops(snd_mixer_elem_t *elem,
Packit 4a16fb
			  int dir,
Packit 4a16fb
			  long value,
Packit 4a16fb
			  long *dbValue,
Packit 4a16fb
			  int xdir)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj1, *t1, *res;
Packit 4a16fb
	struct pymelem *pymelem = melem_to_pymelem(elem);
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	obj1 = PyTuple_New(3);
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(dir));
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong(value));
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 2, PyInt_FromLong(xdir));
Packit 4a16fb
	err = pcall(pymelem, "opsGetDBVol", obj1, &res;;
Packit 4a16fb
	if (err >= 0) {
Packit 4a16fb
		t1 = PyTuple_GetItem(res, 1);
Packit 4a16fb
		if (PyLong_Check(t1)) {
Packit 4a16fb
			*dbValue = PyLong_AsLong(t1);
Packit 4a16fb
			err = 0;
Packit 4a16fb
#if PY_MAJOR_VERSION < 3
Packit 4a16fb
		} else if (PyInt_Check(t1)) {
Packit 4a16fb
			*dbValue = PyInt_AsLong(t1);
Packit 4a16fb
			err = 0;
Packit 4a16fb
#endif
Packit 4a16fb
		} else {
Packit 4a16fb
			PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)");
Packit 4a16fb
			PyErr_Print();
Packit 4a16fb
			PyErr_Clear();
Packit 4a16fb
			err = -EIO;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	Py_XDECREF(res);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int get_dB_ops(snd_mixer_elem_t *elem,
Packit 4a16fb
                      int dir,
Packit 4a16fb
                      snd_mixer_selem_channel_id_t channel,
Packit 4a16fb
                      long *value)
Packit 4a16fb
{
Packit 4a16fb
	return get_x_ops(elem, dir, channel, value, "opsGetDB");
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int get_dB_range_ops(snd_mixer_elem_t *elem, int dir,
Packit 4a16fb
                            long *min, long *max)
Packit 4a16fb
{
Packit 4a16fb
	return get_x_range_ops(elem, dir, min, max, "opsGetDBRange");
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int set_volume_ops(snd_mixer_elem_t *elem, int dir,
Packit 4a16fb
                          snd_mixer_selem_channel_id_t channel, long value)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj1;
Packit 4a16fb
	struct pymelem *pymelem = melem_to_pymelem(elem);
Packit 4a16fb
Packit 4a16fb
	obj1 = PyTuple_New(3);
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(dir));
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong(channel));
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 2, PyInt_FromLong(value));
Packit 4a16fb
	return pcall(pymelem, "opsSetVolume", obj1, NULL);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int set_switch_ops(snd_mixer_elem_t *elem, int dir,
Packit 4a16fb
                          snd_mixer_selem_channel_id_t channel, int value)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj1;
Packit 4a16fb
	struct pymelem *pymelem = melem_to_pymelem(elem);
Packit 4a16fb
Packit 4a16fb
	obj1 = PyTuple_New(3);
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(dir));
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong(channel));
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 2, PyInt_FromLong(value));
Packit 4a16fb
	return pcall(pymelem, "opsSetSwitch", obj1, NULL);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int set_dB_ops(snd_mixer_elem_t *elem, int dir,
Packit 4a16fb
                      snd_mixer_selem_channel_id_t channel,
Packit 4a16fb
                      long db_gain, int xdir)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj1;
Packit 4a16fb
	struct pymelem *pymelem = melem_to_pymelem(elem);
Packit 4a16fb
Packit 4a16fb
	obj1 = PyTuple_New(4);
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(dir));
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong(channel));
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 2, PyInt_FromLong(db_gain));
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 3, PyInt_FromLong(xdir));
Packit 4a16fb
	return pcall(pymelem, "opsSetDB", obj1, NULL);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int enum_item_name_ops(snd_mixer_elem_t *elem,
Packit 4a16fb
                              unsigned int item,
Packit 4a16fb
                              size_t maxlen, char *buf)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj1, *obj2, *t1, *res;
Packit 4a16fb
	struct pymelem *pymelem = melem_to_pymelem(elem);
Packit 4a16fb
	int err;
Packit 4a16fb
	unsigned int len;
Packit 4a16fb
	char *s;
Packit 4a16fb
	
Packit 4a16fb
	obj1 = PyTuple_New(1);
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(item));
Packit 4a16fb
	err = pcall(pymelem, "opsGetEnumItemName", obj1, &res;;
Packit 4a16fb
	if (err >= 0) {
Packit 4a16fb
		t1 = PyTuple_GetItem(res, 1);
Packit 4a16fb
		if (PyUnicode_Check(t1)) {
Packit 4a16fb
			obj2 = PyUnicode_AsEncodedString(t1, "utf-8", "strict");
Packit 4a16fb
			if (obj2) {
Packit 4a16fb
				s = PyBytes_AsString(obj2);
Packit 4a16fb
				len = strlen(s);
Packit 4a16fb
				if (maxlen - 1 > len)
Packit 4a16fb
					len = maxlen - 1;
Packit 4a16fb
				memcpy(buf, s, len);
Packit 4a16fb
				buf[len] = '\0';
Packit 4a16fb
				Py_DECREF(obj2);
Packit 4a16fb
			} else {
Packit 4a16fb
				goto errlbl;
Packit 4a16fb
			}
Packit 4a16fb
#if PY_MAJOR_VERSION < 3
Packit 4a16fb
		} else if (PyString_Check(t1)) {
Packit 4a16fb
			s = PyString_AsString(t1);
Packit 4a16fb
			len = strlen(s);
Packit 4a16fb
			if (maxlen - 1 > len)
Packit 4a16fb
				len = maxlen - 1;
Packit 4a16fb
			memcpy(buf, s, len);
Packit 4a16fb
			buf[len] = '\0';
Packit 4a16fb
#endif
Packit 4a16fb
		} else {
Packit 4a16fb
errlbl:
Packit 4a16fb
			PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)");
Packit 4a16fb
			PyErr_Print();
Packit 4a16fb
			PyErr_Clear();
Packit 4a16fb
			err = -EIO;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	Py_XDECREF(res);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int get_enum_item_ops(snd_mixer_elem_t *elem,
Packit 4a16fb
                             snd_mixer_selem_channel_id_t channel,
Packit 4a16fb
                             unsigned int *itemp)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj1, *t1, *res;
Packit 4a16fb
	struct pymelem *pymelem = melem_to_pymelem(elem);
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	obj1 = PyTuple_New(1);
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(channel));
Packit 4a16fb
	err = pcall(pymelem, "opsGetEnumItem", obj1, &res;;
Packit 4a16fb
	if (err >= 0) {
Packit 4a16fb
		t1 = PyTuple_GetItem(res, 1);
Packit 4a16fb
		if (PyLong_Check(t1)) {
Packit 4a16fb
			*itemp = PyLong_AsLong(t1);
Packit 4a16fb
			err = 0;
Packit 4a16fb
#if PY_MAJOR_VERSION < 3
Packit 4a16fb
		} else if (PyInt_Check(t1)) {
Packit 4a16fb
			*itemp = PyInt_AsLong(t1);
Packit 4a16fb
			err = 0;
Packit 4a16fb
#endif
Packit 4a16fb
		} else {
Packit 4a16fb
			PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)");
Packit 4a16fb
			PyErr_Print();
Packit 4a16fb
			PyErr_Clear();
Packit 4a16fb
			err = -EIO;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	Py_XDECREF(res);
Packit 4a16fb
	return err;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int set_enum_item_ops(snd_mixer_elem_t *elem,
Packit 4a16fb
                             snd_mixer_selem_channel_id_t channel,
Packit 4a16fb
                             unsigned int item)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj1;
Packit 4a16fb
	struct pymelem *pymelem = melem_to_pymelem(elem);
Packit 4a16fb
Packit 4a16fb
	obj1 = PyTuple_New(2);
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(channel));
Packit 4a16fb
	PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong(item));
Packit 4a16fb
	return pcall(pymelem, "opsSetEnumItem", obj1, NULL);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static struct sm_elem_ops simple_python_ops = {
Packit 4a16fb
        .is             = is_ops,
Packit 4a16fb
        .get_range      = get_range_ops,
Packit 4a16fb
        .get_dB_range   = get_dB_range_ops,
Packit 4a16fb
        .set_range      = set_range_ops,
Packit 4a16fb
        .ask_vol_dB	= ask_vol_dB_ops,
Packit 4a16fb
        .ask_dB_vol	= ask_dB_vol_ops,
Packit 4a16fb
        .get_volume     = get_volume_ops,
Packit 4a16fb
        .get_dB         = get_dB_ops,
Packit 4a16fb
        .set_volume     = set_volume_ops,
Packit 4a16fb
        .set_dB         = set_dB_ops,
Packit 4a16fb
        .get_switch     = get_switch_ops,
Packit 4a16fb
        .set_switch     = set_switch_ops,
Packit 4a16fb
        .enum_item_name = enum_item_name_ops,
Packit 4a16fb
        .get_enum_item  = get_enum_item_ops,
Packit 4a16fb
        .set_enum_item  = set_enum_item_ops
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
static void selem_free(snd_mixer_elem_t *elem)
Packit 4a16fb
{
Packit 4a16fb
	sm_selem_t *simple = snd_mixer_elem_get_private(elem);
Packit 4a16fb
Packit 4a16fb
	if (simple->id) {
Packit 4a16fb
		snd_mixer_selem_id_free(simple->id);
Packit 4a16fb
		simple->id = NULL;
Packit 4a16fb
	}
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *
Packit 4a16fb
pymelem_cap(struct pymelem *pymelem ATTRIBUTE_UNUSED, void *priv)
Packit 4a16fb
{
Packit 4a16fb
	return PyInt_FromLong((long)priv);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *
Packit 4a16fb
pymelem_get_caps(struct pymelem *pymelem, void *priv ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	return PyInt_FromLong(pymelem->selem.caps);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *
Packit 4a16fb
pymelem_get_name(struct pymelem *pymelem, void *priv ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	return PyUnicode_FromString(snd_mixer_selem_id_get_name(pymelem->selem.id));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *
Packit 4a16fb
pymelem_get_index(struct pymelem *pymelem, void *priv ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	return PyInt_FromLong(snd_mixer_selem_id_get_index(pymelem->selem.id));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int
Packit 4a16fb
pymelem_set_caps(struct pymelem *pymelem, PyObject *val, void *priv ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	if (PyLong_Check(val)) {
Packit 4a16fb
		pymelem->selem.caps = PyLong_AsLong(val);
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
#if PY_MAJOR_VERSION < 3
Packit 4a16fb
	if (PyInt_Check(val)) {
Packit 4a16fb
		pymelem->selem.caps = PyInt_AsLong(val);
Packit 4a16fb
		return 0;
Packit 4a16fb
	}
Packit 4a16fb
#endif
Packit 4a16fb
	PyErr_SetString(PyExc_TypeError, "The last attribute value must be an integer");
Packit 4a16fb
	return -1;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *
Packit 4a16fb
pymelem_ignore(struct pymelem *pymelem ATTRIBUTE_UNUSED, PyObject *args ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	Py_RETURN_NONE;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *
Packit 4a16fb
pymelem_ignore1(struct pymelem *pymelem ATTRIBUTE_UNUSED, PyObject *args ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	Py_RETURN_TRUE;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *
Packit 4a16fb
pymelem_error(struct pymelem *pymelem ATTRIBUTE_UNUSED, PyObject *args ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	return PyInt_FromLong(-EIO);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *
Packit 4a16fb
pymelem_attach(struct pymelem *pymelem, PyObject *args)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj;
Packit 4a16fb
	snd_hctl_elem_t *helem;
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	if (!PyArg_ParseTuple(args, "O", &obj))
Packit 4a16fb
		return NULL;
Packit 4a16fb
	helem = (snd_hctl_elem_t *)get_C_ptr(obj, "get_C_helem");
Packit 4a16fb
	if (helem == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	err = snd_mixer_elem_attach(pymelem->melem, helem);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		PyErr_Format(PyExc_RuntimeError, "Cannot attach hcontrol element to mixer element: %s", snd_strerror(err));
Packit 4a16fb
		return NULL;		
Packit 4a16fb
	}
Packit 4a16fb
	Py_RETURN_NONE;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *
Packit 4a16fb
pymelem_detach(struct pymelem *pymelem, PyObject *args)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj;
Packit 4a16fb
	snd_hctl_elem_t *helem;
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	if (!PyArg_ParseTuple(args, "O", &obj))
Packit 4a16fb
		return NULL;
Packit 4a16fb
	helem = (snd_hctl_elem_t *)get_C_ptr(obj, "get_C_helem");
Packit 4a16fb
	if (helem == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	err = snd_mixer_elem_detach(pymelem->melem, helem);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		PyErr_Format(PyExc_RuntimeError, "Cannot detach hcontrol element to mixer element: %s", snd_strerror(err));
Packit 4a16fb
		return NULL;		
Packit 4a16fb
	}
Packit 4a16fb
	Py_RETURN_NONE;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *
Packit 4a16fb
pymelem_event_info(struct pymelem *pymelem, PyObject *args)
Packit 4a16fb
{
Packit 4a16fb
	if (!PyArg_ParseTuple(args, ""))
Packit 4a16fb
		return NULL;
Packit 4a16fb
	return PyInt_FromLong(snd_mixer_elem_info(pymelem->melem));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *
Packit 4a16fb
pymelem_event_value(struct pymelem *pymelem, PyObject *args)
Packit 4a16fb
{
Packit 4a16fb
	if (!PyArg_ParseTuple(args, ""))
Packit 4a16fb
		return NULL;
Packit 4a16fb
	return PyInt_FromLong(snd_mixer_elem_value(pymelem->melem));
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int
Packit 4a16fb
pymelem_init(struct pymelem *pymelem, PyObject *args, PyObject *kwds ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	char *name;
Packit 4a16fb
	long index, weight;
Packit 4a16fb
	snd_mixer_selem_id_t *id;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	if (!PyArg_ParseTuple(args, "Osii", &pymelem->py_mixer, &name, &index, &weight))
Packit 4a16fb
		return -1;
Packit 4a16fb
	memset(&pymelem->selem, 0, sizeof(pymelem->selem));
Packit 4a16fb
	if (snd_mixer_selem_id_malloc(&id))
Packit 4a16fb
		return -1;
Packit 4a16fb
	snd_mixer_selem_id_set_name(id, name);
Packit 4a16fb
	snd_mixer_selem_id_set_index(id, index);
Packit 4a16fb
	pymelem->selem.id = id;
Packit 4a16fb
	pymelem->selem.ops = &simple_python_ops;
Packit 4a16fb
	err = snd_mixer_elem_new(&pymelem->melem, SND_MIXER_ELEM_SIMPLE,
Packit 4a16fb
				 weight, &pymelem->selem, selem_free);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		snd_mixer_selem_id_free(id);
Packit 4a16fb
		return -1;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void
Packit 4a16fb
pymelem_dealloc(struct pymelem *self)
Packit 4a16fb
{
Packit 4a16fb
	selem_free(self->melem);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyGetSetDef pymelem_getseters[] = {
Packit 4a16fb
	{"CAP_GVOLUME", (getter)pymelem_cap, NULL, NULL, (void *)SM_CAP_GVOLUME},
Packit 4a16fb
	{"CAP_GSWITCH", (getter)pymelem_cap, NULL, NULL, (void *)SM_CAP_GSWITCH},
Packit 4a16fb
	{"CAP_PVOLUME", (getter)pymelem_cap, NULL, NULL, (void *)SM_CAP_PVOLUME},
Packit 4a16fb
	{"CAP_PVOLUME_JOIN", (getter)pymelem_cap, NULL, NULL, (void *)SM_CAP_PVOLUME_JOIN},
Packit 4a16fb
	{"CAP_PSWITCH", (getter)pymelem_cap, NULL, NULL, (void *)SM_CAP_PSWITCH},
Packit 4a16fb
	{"CAP_PSWITCH_JOIN", (getter)pymelem_cap, NULL, NULL, (void *)SM_CAP_PSWITCH_JOIN},
Packit 4a16fb
	{"CAP_CVOLUME", (getter)pymelem_cap, NULL, NULL, (void *)SM_CAP_CVOLUME},
Packit 4a16fb
	{"CAP_CVOLUME_JOIN", (getter)pymelem_cap, NULL, NULL, (void *)SM_CAP_CVOLUME_JOIN},
Packit 4a16fb
	{"CAP_CSWITCH", (getter)pymelem_cap, NULL, NULL, (void *)SM_CAP_CSWITCH},
Packit 4a16fb
	{"CAP_CSWITCH_JOIN", (getter)pymelem_cap, NULL, NULL, (void *)SM_CAP_CSWITCH_JOIN},
Packit 4a16fb
	{"CAP_CSWITCH_EXCL", (getter)pymelem_cap, NULL, NULL, (void *)SM_CAP_CSWITCH_EXCL},
Packit 4a16fb
	{"CAP_PENUM", (getter)pymelem_cap, NULL, NULL, (void *)SM_CAP_PENUM},
Packit 4a16fb
	{"CAP_CENUM", (getter)pymelem_cap, NULL, NULL, (void *)SM_CAP_CENUM},
Packit 4a16fb
Packit 4a16fb
	{"caps", (getter)pymelem_get_caps, (setter)pymelem_set_caps, NULL, NULL},
Packit 4a16fb
Packit 4a16fb
	{"name", (getter)pymelem_get_name, NULL, NULL, NULL},
Packit 4a16fb
	{"index", (getter)pymelem_get_index, NULL, NULL, NULL},
Packit 4a16fb
	        
Packit 4a16fb
	{NULL,NULL,NULL,NULL,NULL}
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
static PyMethodDef pymelem_methods[] = {
Packit 4a16fb
	{"attach", (PyCFunction)pymelem_attach, METH_VARARGS, NULL},
Packit 4a16fb
	{"detach", (PyCFunction)pymelem_detach, METH_VARARGS, NULL},
Packit 4a16fb
	
Packit 4a16fb
	/* "default" functions - no functionality */
Packit 4a16fb
	{"opsIsActive", (PyCFunction)pymelem_ignore1, METH_VARARGS, NULL},
Packit 4a16fb
	{"opsIsMono", (PyCFunction)pymelem_ignore, METH_VARARGS, NULL},
Packit 4a16fb
	{"opsIsChannel", (PyCFunction)pymelem_ignore, METH_VARARGS, NULL},
Packit 4a16fb
	{"opsIsEnumerated", (PyCFunction)pymelem_ignore, METH_VARARGS, NULL},
Packit 4a16fb
	{"opsIsEnumCnt", (PyCFunction)pymelem_ignore, METH_VARARGS, NULL},
Packit 4a16fb
Packit 4a16fb
	{"opsGetDB", (PyCFunction)pymelem_error, METH_VARARGS, NULL},
Packit 4a16fb
	
Packit 4a16fb
	{"eventInfo", (PyCFunction)pymelem_event_info, METH_VARARGS, NULL},
Packit 4a16fb
	{"eventValue", (PyCFunction)pymelem_event_value, METH_VARARGS, NULL},
Packit 4a16fb
Packit 4a16fb
	{NULL,NULL,0,NULL}
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
static PyTypeObject pymelem_type = {
Packit 4a16fb
        PyVarObject_HEAD_INIT(NULL, 0)
Packit 4a16fb
        tp_name:        "smixer_python.InternalMElement",
Packit 4a16fb
        tp_basicsize:   sizeof(struct pymelem),
Packit 4a16fb
        tp_dealloc:     (destructor)pymelem_dealloc,
Packit 4a16fb
        tp_flags:       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
Packit 4a16fb
        tp_doc:         NULL /* mixerinit__doc__ */,
Packit 4a16fb
        tp_getset:      pymelem_getseters,
Packit 4a16fb
        tp_init:        (initproc)pymelem_init,
Packit 4a16fb
        tp_alloc:       PyType_GenericAlloc,
Packit 4a16fb
        tp_new:         PyType_GenericNew,
Packit 4a16fb
        tp_free:        PyObject_Del,
Packit 4a16fb
        tp_methods:     pymelem_methods,
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
static PyObject *
Packit 4a16fb
pymixer_attach_hctl(struct pymixer *pymixer, PyObject *args)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj;
Packit 4a16fb
	snd_hctl_t *hctl;
Packit 4a16fb
	void **hctls;
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	if (!PyArg_ParseTuple(args, "O", &obj))
Packit 4a16fb
		return NULL;
Packit 4a16fb
	hctl = (snd_hctl_t *)get_C_ptr(obj, "get_C_hctl");
Packit 4a16fb
	if (hctl == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	err = snd_mixer_attach_hctl(pymixer->mixer, hctl);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		PyErr_Format(PyExc_RuntimeError, "Cannot attach hctl: %s", snd_strerror(err));
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	hctls = realloc(pymixer->hctl, sizeof(void *) * (pymixer->hctl_count+1) * 2);
Packit 4a16fb
	if (hctls == NULL) {
Packit 4a16fb
		PyErr_SetString(PyExc_RuntimeError, "No enough memory");
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	pymixer->hctl = hctls;
Packit 4a16fb
	pymixer->hctl[pymixer->hctl_count*2] = (void *)hctl;
Packit 4a16fb
	pymixer->hctl[pymixer->hctl_count*2+1] = (void *)obj;
Packit 4a16fb
	pymixer->hctl_count++;
Packit 4a16fb
	Py_INCREF(obj);
Packit 4a16fb
	Py_RETURN_NONE;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *
Packit 4a16fb
pymixer_register(struct pymixer *pymixer, PyObject *args)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	if (!PyArg_ParseTuple(args, ""))
Packit 4a16fb
		return NULL;
Packit 4a16fb
	err = snd_mixer_class_register(pymixer->class, pymixer->mixer);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		PyErr_Format(PyExc_RuntimeError, "Cannot register mixer: %s", snd_strerror(err));
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	Py_RETURN_NONE;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *
Packit 4a16fb
pymixer_melement_new(struct pymixer *pymixer, PyObject *args)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj, *obj1, *obj2;
Packit 4a16fb
	char *class, *name;
Packit 4a16fb
	long index, weight;
Packit 4a16fb
	
Packit 4a16fb
	if (!PyArg_ParseTuple(args, "ssii", &class, &name, &index, &weight))
Packit 4a16fb
		return NULL;
Packit 4a16fb
	obj = PyDict_GetItemString(pymixer->mdict, class);
Packit 4a16fb
	if (obj) {
Packit 4a16fb
		obj1 = PyTuple_New(4);
Packit 4a16fb
		if (PyTuple_SET_ITEM(obj1, 0, (PyObject *)pymixer))
Packit 4a16fb
			Py_INCREF((PyObject *)pymixer);
Packit 4a16fb
		PyTuple_SET_ITEM(obj1, 1, PyUnicode_FromString(name));
Packit 4a16fb
		PyTuple_SET_ITEM(obj1, 2, PyInt_FromLong(index));
Packit 4a16fb
		PyTuple_SET_ITEM(obj1, 3, PyInt_FromLong(weight));
Packit 4a16fb
		obj2 = PyObject_CallObject(obj, obj1);
Packit 4a16fb
		Py_XDECREF(obj1);
Packit 4a16fb
		if (obj2) {
Packit 4a16fb
			struct pymelem *pymelem = (struct pymelem *)obj2;
Packit 4a16fb
			void **melems = realloc(pymixer->melem, sizeof(void *) * (pymixer->melem_count + 1) * 2);
Packit 4a16fb
			if (melems == NULL) {
Packit 4a16fb
				Py_DECREF(obj2);
Packit 4a16fb
				return NULL;
Packit 4a16fb
			}
Packit 4a16fb
			melems[pymixer->melem_count*2] = pymelem->melem;
Packit 4a16fb
			melems[pymixer->melem_count*2+1] = obj2;
Packit 4a16fb
			Py_INCREF(obj2);
Packit 4a16fb
			pymixer->melem = melems;
Packit 4a16fb
			pymixer->melem_count++;
Packit 4a16fb
		}
Packit 4a16fb
	} else {
Packit 4a16fb
		PyErr_Format(PyExc_RuntimeError, "Cannot find class '%s'", class);
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	return obj2;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *
Packit 4a16fb
pymixer_melement_add(struct pymixer *pymixer, PyObject *args)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj;
Packit 4a16fb
	struct pymelem *pymelem;
Packit 4a16fb
	int err;
Packit 4a16fb
	
Packit 4a16fb
	if (!PyArg_ParseTuple(args, "O", &obj))
Packit 4a16fb
		return NULL;
Packit 4a16fb
	pymelem = (struct pymelem *)obj;
Packit 4a16fb
	err = snd_mixer_elem_add(pymelem->melem, pymixer->class);
Packit 4a16fb
	if (err < 0) {
Packit 4a16fb
		PyErr_Format(PyExc_RuntimeError, "Cannot add mixer element: %s", snd_strerror(err));
Packit 4a16fb
		return NULL;		
Packit 4a16fb
	}
Packit 4a16fb
	Py_RETURN_NONE;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int
Packit 4a16fb
pymixer_init(struct pymixer *pymixer, PyObject *args, PyObject *kwds ATTRIBUTE_UNUSED)
Packit 4a16fb
{
Packit 4a16fb
	long class, mixer;
Packit 4a16fb
Packit 4a16fb
	if (!PyArg_ParseTuple(args, "iiO", &class, &mixer, &pymixer->mdict))
Packit 4a16fb
		return -1;
Packit 4a16fb
	pymixer->class = (snd_mixer_class_t *)class;
Packit 4a16fb
	pymixer->mixer = (snd_mixer_t *)mixer;
Packit 4a16fb
	pymixer->hctl_count = 0;
Packit 4a16fb
	pymixer->hctl = NULL;
Packit 4a16fb
	pymixer->helem_count = 0;
Packit 4a16fb
	pymixer->helem = NULL;
Packit 4a16fb
	pymixer->melem_count = 0;
Packit 4a16fb
	pymixer->melem = NULL;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void
Packit 4a16fb
pymixer_free(struct pymixer *self)
Packit 4a16fb
{
Packit 4a16fb
	int idx;
Packit 4a16fb
	
Packit 4a16fb
	for (idx = 0; idx < self->hctl_count; idx++) {
Packit 4a16fb
		snd_mixer_detach_hctl(self->mixer, self->hctl[idx*2]);
Packit 4a16fb
		Py_DECREF((PyObject *)self->hctl[idx*2+1]);
Packit 4a16fb
	}
Packit 4a16fb
	if (self->hctl)
Packit 4a16fb
		free(self->hctl);
Packit 4a16fb
	self->hctl_count = 0;
Packit 4a16fb
	self->hctl = NULL;
Packit 4a16fb
	for (idx = 0; idx < self->helem_count; idx++)
Packit 4a16fb
		Py_DECREF((PyObject *)self->helem[idx*2+1]);
Packit 4a16fb
	if (self->helem)
Packit 4a16fb
		free(self->helem);
Packit 4a16fb
	self->helem_count = 0;
Packit 4a16fb
	self->helem = NULL;
Packit 4a16fb
	for (idx = 0; idx < self->melem_count; idx++)
Packit 4a16fb
		Py_DECREF((PyObject *)self->melem[idx*2+1]);
Packit 4a16fb
	if (self->melem)
Packit 4a16fb
		free(self->melem);
Packit 4a16fb
	self->melem_count = 0;
Packit 4a16fb
	self->melem = NULL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void
Packit 4a16fb
pymixer_dealloc(struct pymixer *self)
Packit 4a16fb
{
Packit 4a16fb
	pymixer_free(self);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyGetSetDef pymixer_getseters[] = {
Packit 4a16fb
	{NULL,NULL,NULL,NULL,NULL}
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
static PyMethodDef pymixer_methods[] = {
Packit 4a16fb
	{"attachHCtl", (PyCFunction)pymixer_attach_hctl, METH_VARARGS, NULL},
Packit 4a16fb
	{"register", (PyCFunction)pymixer_register, METH_VARARGS, NULL},
Packit 4a16fb
	{"newMElement", (PyCFunction)pymixer_melement_new, METH_VARARGS, NULL},
Packit 4a16fb
	{"addMElement", (PyCFunction)pymixer_melement_add, METH_VARARGS, NULL},
Packit 4a16fb
	{NULL,NULL,0,NULL}
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
static PyTypeObject pymixer_type = {
Packit 4a16fb
        PyVarObject_HEAD_INIT(NULL, 0)
Packit 4a16fb
        tp_name:        "smixer_python.InternalMixer",
Packit 4a16fb
        tp_basicsize:   sizeof(struct pymixer),
Packit 4a16fb
        tp_dealloc:     (destructor)pymixer_dealloc,
Packit 4a16fb
        tp_flags:       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
Packit 4a16fb
        tp_doc:         NULL /* mixerinit__doc__ */,
Packit 4a16fb
        tp_getset:      pymixer_getseters,
Packit 4a16fb
        tp_init:        (initproc)pymixer_init,
Packit 4a16fb
        tp_alloc:       PyType_GenericAlloc,
Packit 4a16fb
        tp_new:         PyType_GenericNew,
Packit 4a16fb
        tp_free:        PyObject_Del,
Packit 4a16fb
        tp_methods:     pymixer_methods,
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
static PyMethodDef python_methods[] = {
Packit 4a16fb
	{NULL, NULL, 0, NULL}
Packit 4a16fb
};
Packit 4a16fb
Packit 4a16fb
static PyObject *new_helem(struct python_priv *priv, snd_hctl_elem_t *helem)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj, *py_hctl = NULL, *obj1, *obj2;
Packit 4a16fb
	snd_hctl_t *hctl = snd_hctl_elem_get_hctl(helem);
Packit 4a16fb
	struct pymixer *pymixer = (struct pymixer *)priv->py_mixer;
Packit 4a16fb
	int idx;
Packit 4a16fb
Packit 4a16fb
	for (idx = 0; idx < pymixer->hctl_count; idx++) {
Packit 4a16fb
		if (pymixer->hctl[idx] == hctl) {
Packit 4a16fb
			py_hctl = pymixer->hctl[idx*2+1];
Packit 4a16fb
			break;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	if (py_hctl == NULL)
Packit 4a16fb
		return NULL;
Packit 4a16fb
	obj = PyDict_GetItemString(priv->py_mdict, "HElement");
Packit 4a16fb
	if (obj) {
Packit 4a16fb
		obj1 = PyTuple_New(3);
Packit 4a16fb
		if (PyTuple_SET_ITEM(obj1, 0, py_hctl))
Packit 4a16fb
			Py_INCREF(py_hctl);
Packit 4a16fb
		PyTuple_SET_ITEM(obj1, 1, PyFloat_FromDouble(1));
Packit 4a16fb
		PyTuple_SET_ITEM(obj1, 2, PyInt_FromLong((long)helem));
Packit 4a16fb
		obj2 = PyObject_CallObject(obj, obj1);
Packit 4a16fb
		if (obj2 == NULL) {
Packit 4a16fb
			PyErr_Print();
Packit 4a16fb
			PyErr_Clear();
Packit 4a16fb
		}
Packit 4a16fb
		Py_XDECREF(obj1);
Packit 4a16fb
	} else {
Packit 4a16fb
		SNDERR("Unable to create InternalMixer object");
Packit 4a16fb
		return NULL;
Packit 4a16fb
	}
Packit 4a16fb
	if (obj2) {
Packit 4a16fb
		struct pymixer *pymixer = (struct pymixer *)priv->py_mixer;
Packit 4a16fb
		void **helems = realloc(pymixer->helem, sizeof(void *) * (pymixer->helem_count + 1) * 2);
Packit 4a16fb
		if (helems == NULL) {
Packit 4a16fb
			Py_DECREF(obj2);
Packit 4a16fb
			return NULL;
Packit 4a16fb
		}
Packit 4a16fb
		helems[pymixer->helem_count*2] = helem;
Packit 4a16fb
		helems[pymixer->helem_count*2+1] = obj2;
Packit 4a16fb
		Py_INCREF(obj2);
Packit 4a16fb
		pymixer->helem = helems;
Packit 4a16fb
		pymixer->helem_count++;
Packit 4a16fb
	}
Packit 4a16fb
	return obj2;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *find_helem(struct python_priv *priv, snd_hctl_elem_t *helem)
Packit 4a16fb
{
Packit 4a16fb
	struct pymixer *pymixer = (struct pymixer *)priv->py_mixer;
Packit 4a16fb
	int idx;
Packit 4a16fb
Packit 4a16fb
	for (idx = 0; idx < pymixer->helem_count; idx++) {
Packit 4a16fb
		if (pymixer->helem[idx*2] == helem)
Packit 4a16fb
			return (PyObject *)pymixer->helem[idx*2+1];
Packit 4a16fb
	}
Packit 4a16fb
	return NULL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static PyObject *find_melem(struct python_priv *priv, snd_mixer_elem_t *melem)
Packit 4a16fb
{
Packit 4a16fb
	struct pymixer *pymixer = (struct pymixer *)priv->py_mixer;
Packit 4a16fb
	int idx;
Packit 4a16fb
Packit 4a16fb
	for (idx = 0; idx < pymixer->melem_count; idx++) {
Packit 4a16fb
		if (pymixer->melem[idx*2] == melem)
Packit 4a16fb
			return (PyObject *)pymixer->melem[idx*2+1];
Packit 4a16fb
	}
Packit 4a16fb
	return NULL;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
int alsa_mixer_simple_event(snd_mixer_class_t *class, unsigned int mask,
Packit 4a16fb
			    snd_hctl_elem_t *helem, snd_mixer_elem_t *melem)
Packit 4a16fb
{
Packit 4a16fb
	struct python_priv *priv = snd_mixer_sbasic_get_private(class);
Packit 4a16fb
	PyThreadState *tstate;
Packit 4a16fb
	PyObject *t, *o, *r;
Packit 4a16fb
	int res = -ENOMEM;
Packit 4a16fb
Packit 4a16fb
	tstate = PyThreadState_New(main_interpreter);
Packit 4a16fb
	PyThreadState_Swap(tstate);
Packit 4a16fb
        
Packit 4a16fb
        t = PyTuple_New(3);
Packit 4a16fb
        if (t) {
Packit 4a16fb
        	PyTuple_SET_ITEM(t, 0, (PyObject *)PyInt_FromLong(mask));
Packit 4a16fb
        	o = find_helem(priv, helem);
Packit 4a16fb
	        if (mask & SND_CTL_EVENT_MASK_ADD) {
Packit 4a16fb
	        	if (o == NULL)
Packit 4a16fb
        			o = new_helem(priv, helem);
Packit 4a16fb
		}
Packit 4a16fb
        	if (o == NULL)
Packit 4a16fb
        		return 0;
Packit 4a16fb
        	if (PyTuple_SET_ITEM(t, 1, o))
Packit 4a16fb
        		Py_INCREF(o);
Packit 4a16fb
        	o = melem ? find_melem(priv, melem) : Py_None;
Packit 4a16fb
        	if (PyTuple_SET_ITEM(t, 2, o))
Packit 4a16fb
        		Py_INCREF(o);
Packit 4a16fb
		r = PyObject_CallObject(priv->py_event_func, t);
Packit 4a16fb
		Py_DECREF(t);
Packit 4a16fb
		if (r) {
Packit 4a16fb
			if (PyLong_Check(r)) {
Packit 4a16fb
				res = PyLong_AsLong(r);
Packit 4a16fb
#if PY_MAJOR_VERSION < 3
Packit 4a16fb
			} else if (PyInt_Check(r)) {
Packit 4a16fb
				res = PyInt_AsLong(r);
Packit 4a16fb
#endif
Packit 4a16fb
			} else if (r == Py_None) {
Packit 4a16fb
				res = 0;
Packit 4a16fb
			}
Packit 4a16fb
			Py_DECREF(r);
Packit 4a16fb
		} else {
Packit 4a16fb
			PyErr_Print();
Packit 4a16fb
			PyErr_Clear();
Packit 4a16fb
			res = -EIO;
Packit 4a16fb
		}
Packit 4a16fb
	}
Packit 4a16fb
	
Packit 4a16fb
	return res;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static void alsa_mixer_simple_free(snd_mixer_class_t *class)
Packit 4a16fb
{
Packit 4a16fb
	struct python_priv *priv = snd_mixer_sbasic_get_private(class);
Packit 4a16fb
Packit 4a16fb
	if (priv->py_mixer) {
Packit 4a16fb
		pymixer_free((struct pymixer *)priv->py_mixer);
Packit 4a16fb
		Py_DECREF(priv->py_mixer);
Packit 4a16fb
	}
Packit 4a16fb
	if (priv->py_initialized) {
Packit 4a16fb
		Py_XDECREF(priv->py_event_func);
Packit 4a16fb
		Py_Finalize();
Packit 4a16fb
	}
Packit 4a16fb
	free(priv);
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
static int alsa_mixer_simple_pyinit(struct python_priv *priv,
Packit 4a16fb
                                    PyObject *py_mod,
Packit 4a16fb
                                    FILE *fp,
Packit 4a16fb
				    const char *file,
Packit 4a16fb
				    snd_mixer_class_t *class,
Packit 4a16fb
				    snd_mixer_t *mixer,
Packit 4a16fb
				    const char *device)
Packit 4a16fb
{
Packit 4a16fb
	PyObject *obj, *obj1, *obj2, *mdict;
Packit 4a16fb
Packit 4a16fb
	mdict = priv->py_mdict = PyModule_GetDict(py_mod);
Packit 4a16fb
	obj = PyUnicode_FromString(file);
Packit 4a16fb
	if (obj)
Packit 4a16fb
		PyDict_SetItemString(mdict, "__file__", obj);
Packit 4a16fb
	Py_XDECREF(obj);
Packit 4a16fb
	obj = PyUnicode_FromString(device);
Packit 4a16fb
	if (obj)
Packit 4a16fb
		PyDict_SetItemString(mdict, "device", obj);
Packit 4a16fb
	Py_XDECREF(obj);
Packit 4a16fb
	Py_INCREF(&pymelem_type);
Packit 4a16fb
	Py_INCREF(&pymixer_type);
Packit 4a16fb
	PyModule_AddObject(py_mod, "InternalMElement", (PyObject *)&pymelem_type);
Packit 4a16fb
	PyModule_AddObject(py_mod, "InternalMixer", (PyObject *)&pymixer_type);
Packit 4a16fb
	obj = PyDict_GetItemString(mdict, "InternalMixer");
Packit 4a16fb
	if (obj) {
Packit 4a16fb
		obj1 = PyTuple_New(3);
Packit 4a16fb
		PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong((long)class));
Packit 4a16fb
		PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong((long)mixer));
Packit 4a16fb
		if (PyTuple_SET_ITEM(obj1, 2, mdict))
Packit 4a16fb
			Py_INCREF(mdict);
Packit 4a16fb
		obj2 = PyObject_CallObject(obj, obj1);
Packit 4a16fb
		Py_XDECREF(obj1);
Packit 4a16fb
		PyDict_SetItemString(mdict, "mixer", obj2);
Packit 4a16fb
		priv->py_mixer = obj2;
Packit 4a16fb
	} else {
Packit 4a16fb
		SNDERR("Unable to create InternalMixer object");
Packit 4a16fb
		return -EIO;
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
	obj = PyRun_FileEx(fp, file, Py_file_input, mdict, mdict, 1);
Packit 4a16fb
	if (obj == NULL)
Packit 4a16fb
		PyErr_Print();
Packit 4a16fb
	Py_XDECREF(obj);
Packit 4a16fb
	priv->py_event_func = PyDict_GetItemString(mdict, "event");
Packit 4a16fb
	if (priv->py_event_func == NULL) {
Packit 4a16fb
		SNDERR("Unable to find python function 'event'");
Packit 4a16fb
		return -EIO;
Packit 4a16fb
	}
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#if PY_MAJOR_VERSION >= 3
Packit 4a16fb
static struct PyModuleDef smixer_python_module = {
Packit 4a16fb
        PyModuleDef_HEAD_INIT,
Packit 4a16fb
        "smixer_python",
Packit 4a16fb
        NULL,
Packit 4a16fb
        0,
Packit 4a16fb
        python_methods,
Packit 4a16fb
        NULL,
Packit 4a16fb
        NULL,
Packit 4a16fb
        NULL,
Packit 4a16fb
        NULL
Packit 4a16fb
};
Packit 4a16fb
#endif
Packit 4a16fb
Packit 4a16fb
int alsa_mixer_simple_finit(snd_mixer_class_t *class,
Packit 4a16fb
			    snd_mixer_t *mixer,
Packit 4a16fb
			    const char *device)
Packit 4a16fb
{
Packit 4a16fb
	struct python_priv *priv;
Packit 4a16fb
	FILE *fp;
Packit 4a16fb
	const char *file;
Packit 4a16fb
	PyObject *obj, *py_mod;
Packit 4a16fb
Packit 4a16fb
	priv = calloc(1, sizeof(*priv));
Packit 4a16fb
	if (priv == NULL)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
Packit 4a16fb
	snd_mixer_sbasic_set_private(class, priv);
Packit 4a16fb
	snd_mixer_sbasic_set_private_free(class, alsa_mixer_simple_free);
Packit 4a16fb
Packit 4a16fb
	file = getenv("ALSA_MIXER_SIMPLE_MPYTHON");
Packit 4a16fb
	if (file == NULL)
Packit 4a16fb
		file = SCRIPT;
Packit 4a16fb
Packit 4a16fb
	fp = fopen(file, "r");
Packit 4a16fb
	if (fp == NULL) {
Packit 4a16fb
		SNDERR("Unable to find python module '%s'", file);
Packit 4a16fb
		return -ENODEV;
Packit 4a16fb
	}
Packit 4a16fb
	
Packit 4a16fb
	Py_Initialize();
Packit 4a16fb
	if (PyType_Ready(&pymelem_type) < 0 ||
Packit 4a16fb
	    PyType_Ready(&pymixer_type) < 0) {
Packit 4a16fb
		fclose(fp);
Packit 4a16fb
		return -EIO;
Packit 4a16fb
	}
Packit 4a16fb
#if PY_MAJOR_VERSION < 3
Packit 4a16fb
	Py_InitModule("smixer_python", python_methods);
Packit 4a16fb
#else
Packit 4a16fb
	PyModule_Create(&smixer_python_module);
Packit 4a16fb
#endif
Packit 4a16fb
	priv->py_initialized = 1;
Packit 4a16fb
	main_interpreter = PyThreadState_Get()->interp;
Packit 4a16fb
	obj = PyImport_GetModuleDict();
Packit 4a16fb
	py_mod = PyDict_GetItemString(obj, "__main__");
Packit 4a16fb
	if (py_mod)
Packit 4a16fb
		alsa_mixer_simple_pyinit(priv, py_mod, fp, file, class, mixer, device);
Packit 4a16fb
	return 0;
Packit 4a16fb
}