24 | 24 |
#include "asoundlib.h"
|
25 | 25 |
#include "mixer_abst.h"
|
26 | 26 |
|
|
27 |
#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
|
|
28 |
#pragma GCC diagnostic ignored "-Wstrict-aliasing"
|
|
29 |
#endif
|
|
30 |
|
27 | 31 |
struct python_priv {
|
28 | 32 |
int py_initialized;
|
29 | 33 |
PyObject *py_event_func;
|
|
55 | 59 |
|
56 | 60 |
static PyInterpreterState *main_interpreter;
|
57 | 61 |
|
|
62 |
#if PY_MAJOR_VERSION >= 3
|
|
63 |
#define PyInt_FromLong PyLong_FromLong
|
|
64 |
#endif
|
|
65 |
|
|
66 |
static inline int get_long(PyObject *o, long *val)
|
|
67 |
{
|
|
68 |
#if PY_MAJOR_VERSION < 3
|
|
69 |
if (PyInt_Check(o)) {
|
|
70 |
*val = PyInt_AsLong(o);
|
|
71 |
return 0;
|
|
72 |
}
|
|
73 |
#endif
|
|
74 |
if (PyLong_Check(o)) {
|
|
75 |
*val = PyLong_AsLong(o);
|
|
76 |
return 0;
|
|
77 |
}
|
|
78 |
return 1;
|
|
79 |
}
|
|
80 |
|
|
81 |
static inline PyObject *InternFromString(const char *name)
|
|
82 |
{
|
|
83 |
#if PY_MAJOR_VERSION < 3
|
|
84 |
return PyString_InternFromString(name);
|
|
85 |
#else
|
|
86 |
return PyUnicode_InternFromString(name);
|
|
87 |
#endif
|
|
88 |
}
|
|
89 |
|
58 | 90 |
static void *get_C_ptr(PyObject *obj, const char *attr)
|
59 | 91 |
{
|
60 | 92 |
PyObject *o;
|
61 | |
|
62 | |
o = PyObject_GetAttr(obj, PyString_InternFromString(attr));
|
|
93 |
long val;
|
|
94 |
|
|
95 |
o = PyObject_GetAttr(obj, InternFromString(attr));
|
63 | 96 |
if (!o) {
|
64 | 97 |
PyErr_Format(PyExc_TypeError, "missing '%s' attribute", attr);
|
65 | 98 |
return NULL;
|
66 | 99 |
}
|
67 | |
if (!PyInt_Check(o)) {
|
68 | |
PyErr_Format(PyExc_TypeError, "'%s' attribute is not integer", attr);
|
69 | |
return NULL;
|
70 | |
}
|
71 | |
return (void *)PyInt_AsLong(o);
|
|
100 |
if (get_long(o, &val)) {
|
|
101 |
PyErr_Format(PyExc_TypeError, "'%s' attribute is not Int or Long", attr);
|
|
102 |
return NULL;
|
|
103 |
}
|
|
104 |
return (void *)val;
|
72 | 105 |
}
|
73 | 106 |
|
74 | 107 |
static struct pymelem *melem_to_pymelem(snd_mixer_elem_t *elem)
|
|
79 | 112 |
static int pcall(struct pymelem *pymelem, const char *attr, PyObject *args, PyObject **_res)
|
80 | 113 |
{
|
81 | 114 |
PyObject *obj = (PyObject *)pymelem, *res;
|
82 | |
int xres = 0;
|
|
115 |
long xres = 0;
|
83 | 116 |
|
84 | 117 |
if (_res)
|
85 | 118 |
*_res = NULL;
|
86 | |
obj = PyObject_GetAttr(obj, PyString_InternFromString(attr));
|
|
119 |
obj = PyObject_GetAttr(obj, InternFromString(attr));
|
87 | 120 |
if (!obj) {
|
88 | 121 |
PyErr_Format(PyExc_TypeError, "missing '%s' attribute", attr);
|
89 | 122 |
PyErr_Print();
|
|
102 | 135 |
*_res = res;
|
103 | 136 |
res = PyTuple_GetItem(res, 0);
|
104 | 137 |
}
|
105 | |
if (PyInt_Check(res)) {
|
|
138 |
if (PyLong_Check(res)) {
|
|
139 |
xres = PyLong_AsLong(res);
|
|
140 |
#if PY_MAJOR_VERSION < 3
|
|
141 |
} else if (PyInt_Check(res)) {
|
106 | 142 |
xres = PyInt_AsLong(res);
|
|
143 |
#endif
|
107 | 144 |
} else if (res == Py_None) {
|
108 | 145 |
xres = 0;
|
109 | 146 |
} else if (PyBool_Check(res)) {
|
|
154 | 191 |
static int get_x_range_ops(snd_mixer_elem_t *elem, int dir,
|
155 | 192 |
long *min, long *max, const char *attr)
|
156 | 193 |
{
|
157 | |
PyObject *obj1, *res;
|
|
194 |
PyObject *obj1, *t1, *t2, *res;
|
158 | 195 |
struct pymelem *pymelem = melem_to_pymelem(elem);
|
159 | 196 |
int err;
|
160 | 197 |
|
|
162 | 199 |
PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(dir));
|
163 | 200 |
err = pcall(pymelem, attr, obj1, &res);
|
164 | 201 |
if (err >= 0) {
|
165 | |
err = !PyInt_Check(PyTuple_GetItem(res, 1)) || !PyInt_Check(PyTuple_GetItem(res, 2));
|
166 | |
if (err) {
|
167 | |
err = !PyLong_Check(PyTuple_GetItem(res, 1)) || !PyLong_Check(PyTuple_GetItem(res, 2));
|
168 | |
if (err) {
|
169 | |
PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)");
|
170 | |
PyErr_Print();
|
171 | |
PyErr_Clear();
|
172 | |
err = -EIO;
|
173 | |
} else {
|
174 | |
*min = PyLong_AsLong(PyTuple_GetItem(res, 1));
|
175 | |
*max = PyLong_AsLong(PyTuple_GetItem(res, 2));
|
176 | |
}
|
177 | |
} else {
|
|
202 |
t1 = PyTuple_GetItem(res, 1);
|
|
203 |
t2 = PyTuple_GetItem(res, 2);
|
|
204 |
if (PyLong_Check(t1) && PyLong_Check(t2)) {
|
|
205 |
*min = PyLong_AsLong(PyTuple_GetItem(res, 1));
|
|
206 |
*max = PyLong_AsLong(PyTuple_GetItem(res, 2));
|
|
207 |
err = 0;
|
|
208 |
#if PY_MAJOR_VERSION < 3
|
|
209 |
} else if (PyInt_Check(t1) && PyInt_Check(t2)) {
|
178 | 210 |
*min = PyInt_AsLong(PyTuple_GetItem(res, 1));
|
179 | 211 |
*max = PyInt_AsLong(PyTuple_GetItem(res, 2));
|
|
212 |
err = 0;
|
|
213 |
#endif
|
|
214 |
} else {
|
|
215 |
PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)");
|
|
216 |
PyErr_Print();
|
|
217 |
PyErr_Clear();
|
|
218 |
err = -EIO;
|
180 | 219 |
}
|
181 | 220 |
}
|
182 | 221 |
Py_XDECREF(res);
|
|
206 | 245 |
long channel, long *value,
|
207 | 246 |
const char *attr)
|
208 | 247 |
{
|
209 | |
PyObject *obj1, *res;
|
|
248 |
PyObject *obj1, *t1, *res;
|
210 | 249 |
struct pymelem *pymelem = melem_to_pymelem(elem);
|
211 | 250 |
int err;
|
212 | 251 |
|
|
215 | 254 |
PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong(channel));
|
216 | 255 |
err = pcall(pymelem, attr, obj1, &res);
|
217 | 256 |
if (err >= 0) {
|
218 | |
err = !PyInt_Check(PyTuple_GetItem(res, 1));
|
219 | |
if (err) {
|
220 | |
err = !PyLong_Check(PyTuple_GetItem(res, 1));
|
221 | |
if (err) {
|
222 | |
PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)");
|
223 | |
PyErr_Print();
|
224 | |
PyErr_Clear();
|
225 | |
err = -EIO;
|
226 | |
} else {
|
227 | |
*value = PyLong_AsLong(PyTuple_GetItem(res, 1));
|
228 | |
}
|
|
257 |
t1 = PyTuple_GetItem(res, 1);
|
|
258 |
if (PyLong_Check(t1)) {
|
|
259 |
*value = PyLong_AsLong(t1);
|
|
260 |
err = 0;
|
|
261 |
#if PY_MAJOR_VERSION < 3
|
|
262 |
} else if (PyInt_Check(t1)) {
|
|
263 |
*value = PyInt_AsLong(t1);
|
|
264 |
err = 0;
|
|
265 |
#endif
|
229 | 266 |
} else {
|
230 | |
*value = PyInt_AsLong(PyTuple_GetItem(res, 1));
|
|
267 |
PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)");
|
|
268 |
PyErr_Print();
|
|
269 |
PyErr_Clear();
|
|
270 |
err = -EIO;
|
231 | 271 |
}
|
232 | 272 |
}
|
233 | 273 |
Py_XDECREF(res);
|
|
264 | 304 |
long *dbValue,
|
265 | 305 |
int xdir)
|
266 | 306 |
{
|
267 | |
PyObject *obj1, *res;
|
|
307 |
PyObject *obj1, *t1, *res;
|
268 | 308 |
struct pymelem *pymelem = melem_to_pymelem(elem);
|
269 | 309 |
int err;
|
270 | 310 |
|
|
274 | 314 |
PyTuple_SET_ITEM(obj1, 2, PyInt_FromLong(xdir));
|
275 | 315 |
err = pcall(pymelem, "opsGetDBVol", obj1, &res);
|
276 | 316 |
if (err >= 0) {
|
277 | |
err = !PyInt_Check(PyTuple_GetItem(res, 1));
|
278 | |
if (err) {
|
279 | |
err = !PyLong_Check(PyTuple_GetItem(res, 1));
|
280 | |
if (err) {
|
281 | |
PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)");
|
282 | |
PyErr_Print();
|
283 | |
PyErr_Clear();
|
284 | |
err = -EIO;
|
285 | |
} else {
|
286 | |
*dbValue = PyLong_AsLong(PyTuple_GetItem(res, 1));
|
287 | |
}
|
|
317 |
t1 = PyTuple_GetItem(res, 1);
|
|
318 |
if (PyLong_Check(t1)) {
|
|
319 |
*dbValue = PyLong_AsLong(t1);
|
|
320 |
err = 0;
|
|
321 |
#if PY_MAJOR_VERSION < 3
|
|
322 |
} else if (PyInt_Check(t1)) {
|
|
323 |
*dbValue = PyInt_AsLong(t1);
|
|
324 |
err = 0;
|
|
325 |
#endif
|
288 | 326 |
} else {
|
289 | |
*dbValue = PyInt_AsLong(PyTuple_GetItem(res, 1));
|
|
327 |
PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)");
|
|
328 |
PyErr_Print();
|
|
329 |
PyErr_Clear();
|
|
330 |
err = -EIO;
|
290 | 331 |
}
|
291 | 332 |
}
|
292 | 333 |
Py_XDECREF(res);
|
|
352 | 393 |
unsigned int item,
|
353 | 394 |
size_t maxlen, char *buf)
|
354 | 395 |
{
|
355 | |
PyObject *obj1, *res;
|
|
396 |
PyObject *obj1, *obj2, *t1, *res;
|
356 | 397 |
struct pymelem *pymelem = melem_to_pymelem(elem);
|
357 | 398 |
int err;
|
358 | 399 |
unsigned int len;
|
|
362 | 403 |
PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(item));
|
363 | 404 |
err = pcall(pymelem, "opsGetEnumItemName", obj1, &res);
|
364 | 405 |
if (err >= 0) {
|
365 | |
err = !PyString_Check(PyTuple_GetItem(res, 1));
|
366 | |
if (err) {
|
367 | |
PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)");
|
368 | |
PyErr_Print();
|
369 | |
PyErr_Clear();
|
370 | |
err = -EIO;
|
371 | |
} else {
|
372 | |
s = PyString_AsString(PyTuple_GetItem(res, 1));
|
|
406 |
t1 = PyTuple_GetItem(res, 1);
|
|
407 |
if (PyUnicode_Check(t1)) {
|
|
408 |
obj2 = PyUnicode_AsEncodedString(t1, "utf-8", "strict");
|
|
409 |
if (obj2) {
|
|
410 |
s = PyBytes_AsString(obj2);
|
|
411 |
len = strlen(s);
|
|
412 |
if (maxlen - 1 > len)
|
|
413 |
len = maxlen - 1;
|
|
414 |
memcpy(buf, s, len);
|
|
415 |
buf[len] = '\0';
|
|
416 |
Py_DECREF(obj2);
|
|
417 |
} else {
|
|
418 |
goto errlbl;
|
|
419 |
}
|
|
420 |
#if PY_MAJOR_VERSION < 3
|
|
421 |
} else if (PyString_Check(t1)) {
|
|
422 |
s = PyString_AsString(t1);
|
373 | 423 |
len = strlen(s);
|
374 | 424 |
if (maxlen - 1 > len)
|
375 | 425 |
len = maxlen - 1;
|
376 | 426 |
memcpy(buf, s, len);
|
377 | 427 |
buf[len] = '\0';
|
|
428 |
#endif
|
|
429 |
} else {
|
|
430 |
errlbl:
|
|
431 |
PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)");
|
|
432 |
PyErr_Print();
|
|
433 |
PyErr_Clear();
|
|
434 |
err = -EIO;
|
378 | 435 |
}
|
379 | 436 |
}
|
380 | 437 |
Py_XDECREF(res);
|
|
385 | 442 |
snd_mixer_selem_channel_id_t channel,
|
386 | 443 |
unsigned int *itemp)
|
387 | 444 |
{
|
388 | |
PyObject *obj1, *res;
|
|
445 |
PyObject *obj1, *t1, *res;
|
389 | 446 |
struct pymelem *pymelem = melem_to_pymelem(elem);
|
390 | 447 |
int err;
|
391 | 448 |
|
|
393 | 450 |
PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(channel));
|
394 | 451 |
err = pcall(pymelem, "opsGetEnumItem", obj1, &res);
|
395 | 452 |
if (err >= 0) {
|
396 | |
err = !PyInt_Check(PyTuple_GetItem(res, 1));
|
397 | |
if (err) {
|
|
453 |
t1 = PyTuple_GetItem(res, 1);
|
|
454 |
if (PyLong_Check(t1)) {
|
|
455 |
*itemp = PyLong_AsLong(t1);
|
|
456 |
err = 0;
|
|
457 |
#if PY_MAJOR_VERSION < 3
|
|
458 |
} else if (PyInt_Check(t1)) {
|
|
459 |
*itemp = PyInt_AsLong(t1);
|
|
460 |
err = 0;
|
|
461 |
#endif
|
|
462 |
} else {
|
398 | 463 |
PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)");
|
399 | 464 |
PyErr_Print();
|
400 | 465 |
PyErr_Clear();
|
401 | 466 |
err = -EIO;
|
402 | |
} else {
|
403 | |
*itemp = PyInt_AsLong(PyTuple_GetItem(res, 1));
|
404 | 467 |
}
|
405 | 468 |
}
|
406 | 469 |
Py_XDECREF(res);
|
|
463 | 526 |
static PyObject *
|
464 | 527 |
pymelem_get_name(struct pymelem *pymelem, void *priv ATTRIBUTE_UNUSED)
|
465 | 528 |
{
|
466 | |
return PyString_FromString(snd_mixer_selem_id_get_name(pymelem->selem.id));
|
|
529 |
return PyUnicode_FromString(snd_mixer_selem_id_get_name(pymelem->selem.id));
|
467 | 530 |
}
|
468 | 531 |
|
469 | 532 |
static PyObject *
|
|
475 | 538 |
static int
|
476 | 539 |
pymelem_set_caps(struct pymelem *pymelem, PyObject *val, void *priv ATTRIBUTE_UNUSED)
|
477 | 540 |
{
|
478 | |
if (!PyInt_Check(val)) {
|
479 | |
PyErr_SetString(PyExc_TypeError, "The last attribute value must be an integer");
|
480 | |
return -1;
|
481 | |
}
|
482 | |
pymelem->selem.caps = PyInt_AsLong(val);
|
483 | |
return 0;
|
|
541 |
if (PyLong_Check(val)) {
|
|
542 |
pymelem->selem.caps = PyLong_AsLong(val);
|
|
543 |
return 0;
|
|
544 |
}
|
|
545 |
#if PY_MAJOR_VERSION < 3
|
|
546 |
if (PyInt_Check(val)) {
|
|
547 |
pymelem->selem.caps = PyInt_AsLong(val);
|
|
548 |
return 0;
|
|
549 |
}
|
|
550 |
#endif
|
|
551 |
PyErr_SetString(PyExc_TypeError, "The last attribute value must be an integer");
|
|
552 |
return -1;
|
484 | 553 |
}
|
485 | 554 |
|
486 | 555 |
static PyObject *
|
|
587 | 656 |
pymelem_dealloc(struct pymelem *self)
|
588 | 657 |
{
|
589 | 658 |
selem_free(self->melem);
|
590 | |
self->ob_type->tp_free(self);
|
591 | 659 |
}
|
592 | 660 |
|
593 | 661 |
static PyGetSetDef pymelem_getseters[] = {
|
|
633 | 701 |
};
|
634 | 702 |
|
635 | 703 |
static PyTypeObject pymelem_type = {
|
636 | |
PyObject_HEAD_INIT(0)
|
|
704 |
PyVarObject_HEAD_INIT(NULL, 0)
|
637 | 705 |
tp_name: "smixer_python.InternalMElement",
|
638 | 706 |
tp_basicsize: sizeof(struct pymelem),
|
639 | 707 |
tp_dealloc: (destructor)pymelem_dealloc,
|
|
707 | 775 |
obj1 = PyTuple_New(4);
|
708 | 776 |
if (PyTuple_SET_ITEM(obj1, 0, (PyObject *)pymixer))
|
709 | 777 |
Py_INCREF((PyObject *)pymixer);
|
710 | |
PyTuple_SET_ITEM(obj1, 1, PyString_FromString(name));
|
|
778 |
PyTuple_SET_ITEM(obj1, 1, PyUnicode_FromString(name));
|
711 | 779 |
PyTuple_SET_ITEM(obj1, 2, PyInt_FromLong(index));
|
712 | 780 |
PyTuple_SET_ITEM(obj1, 3, PyInt_FromLong(weight));
|
713 | 781 |
obj2 = PyObject_CallObject(obj, obj1);
|
|
799 | 867 |
pymixer_dealloc(struct pymixer *self)
|
800 | 868 |
{
|
801 | 869 |
pymixer_free(self);
|
802 | |
self->ob_type->tp_free(self);
|
803 | 870 |
}
|
804 | 871 |
|
805 | 872 |
static PyGetSetDef pymixer_getseters[] = {
|
|
815 | 882 |
};
|
816 | 883 |
|
817 | 884 |
static PyTypeObject pymixer_type = {
|
818 | |
PyObject_HEAD_INIT(0)
|
|
885 |
PyVarObject_HEAD_INIT(NULL, 0)
|
819 | 886 |
tp_name: "smixer_python.InternalMixer",
|
820 | 887 |
tp_basicsize: sizeof(struct pymixer),
|
821 | 888 |
tp_dealloc: (destructor)pymixer_dealloc,
|
|
909 | 976 |
snd_hctl_elem_t *helem, snd_mixer_elem_t *melem)
|
910 | 977 |
{
|
911 | 978 |
struct python_priv *priv = snd_mixer_sbasic_get_private(class);
|
912 | |
PyThreadState *tstate, *origstate;
|
|
979 |
PyThreadState *tstate;
|
913 | 980 |
PyObject *t, *o, *r;
|
914 | 981 |
int res = -ENOMEM;
|
915 | 982 |
|
916 | 983 |
tstate = PyThreadState_New(main_interpreter);
|
917 | |
origstate = PyThreadState_Swap(tstate);
|
|
984 |
PyThreadState_Swap(tstate);
|
918 | 985 |
|
919 | 986 |
t = PyTuple_New(3);
|
920 | 987 |
if (t) {
|
|
934 | 1001 |
r = PyObject_CallObject(priv->py_event_func, t);
|
935 | 1002 |
Py_DECREF(t);
|
936 | 1003 |
if (r) {
|
937 | |
if (PyInt_Check(r)) {
|
|
1004 |
if (PyLong_Check(r)) {
|
|
1005 |
res = PyLong_AsLong(r);
|
|
1006 |
#if PY_MAJOR_VERSION < 3
|
|
1007 |
} else if (PyInt_Check(r)) {
|
938 | 1008 |
res = PyInt_AsLong(r);
|
|
1009 |
#endif
|
939 | 1010 |
} else if (r == Py_None) {
|
940 | 1011 |
res = 0;
|
941 | 1012 |
}
|
|
965 | 1036 |
free(priv);
|
966 | 1037 |
}
|
967 | 1038 |
|
|
1039 |
static int alsa_mixer_simple_pyinit(struct python_priv *priv,
|
|
1040 |
PyObject *py_mod,
|
|
1041 |
FILE *fp,
|
|
1042 |
const char *file,
|
|
1043 |
snd_mixer_class_t *class,
|
|
1044 |
snd_mixer_t *mixer,
|
|
1045 |
const char *device)
|
|
1046 |
{
|
|
1047 |
PyObject *obj, *obj1, *obj2, *mdict;
|
|
1048 |
|
|
1049 |
mdict = priv->py_mdict = PyModule_GetDict(py_mod);
|
|
1050 |
obj = PyUnicode_FromString(file);
|
|
1051 |
if (obj)
|
|
1052 |
PyDict_SetItemString(mdict, "__file__", obj);
|
|
1053 |
Py_XDECREF(obj);
|
|
1054 |
obj = PyUnicode_FromString(device);
|
|
1055 |
if (obj)
|
|
1056 |
PyDict_SetItemString(mdict, "device", obj);
|
|
1057 |
Py_XDECREF(obj);
|
|
1058 |
Py_INCREF(&pymelem_type);
|
|
1059 |
Py_INCREF(&pymixer_type);
|
|
1060 |
PyModule_AddObject(py_mod, "InternalMElement", (PyObject *)&pymelem_type);
|
|
1061 |
PyModule_AddObject(py_mod, "InternalMixer", (PyObject *)&pymixer_type);
|
|
1062 |
obj = PyDict_GetItemString(mdict, "InternalMixer");
|
|
1063 |
if (obj) {
|
|
1064 |
obj1 = PyTuple_New(3);
|
|
1065 |
PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong((long)class));
|
|
1066 |
PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong((long)mixer));
|
|
1067 |
if (PyTuple_SET_ITEM(obj1, 2, mdict))
|
|
1068 |
Py_INCREF(mdict);
|
|
1069 |
obj2 = PyObject_CallObject(obj, obj1);
|
|
1070 |
Py_XDECREF(obj1);
|
|
1071 |
PyDict_SetItemString(mdict, "mixer", obj2);
|
|
1072 |
priv->py_mixer = obj2;
|
|
1073 |
} else {
|
|
1074 |
SNDERR("Unable to create InternalMixer object");
|
|
1075 |
return -EIO;
|
|
1076 |
}
|
|
1077 |
|
|
1078 |
obj = PyRun_FileEx(fp, file, Py_file_input, mdict, mdict, 1);
|
|
1079 |
if (obj == NULL)
|
|
1080 |
PyErr_Print();
|
|
1081 |
Py_XDECREF(obj);
|
|
1082 |
priv->py_event_func = PyDict_GetItemString(mdict, "event");
|
|
1083 |
if (priv->py_event_func == NULL) {
|
|
1084 |
SNDERR("Unable to find python function 'event'");
|
|
1085 |
return -EIO;
|
|
1086 |
}
|
|
1087 |
return 0;
|
|
1088 |
}
|
|
1089 |
|
|
1090 |
#if PY_MAJOR_VERSION >= 3
|
|
1091 |
static struct PyModuleDef smixer_python_module = {
|
|
1092 |
PyModuleDef_HEAD_INIT,
|
|
1093 |
"smixer_python",
|
|
1094 |
NULL,
|
|
1095 |
0,
|
|
1096 |
python_methods,
|
|
1097 |
NULL,
|
|
1098 |
NULL,
|
|
1099 |
NULL,
|
|
1100 |
NULL
|
|
1101 |
};
|
|
1102 |
#endif
|
|
1103 |
|
968 | 1104 |
int alsa_mixer_simple_finit(snd_mixer_class_t *class,
|
969 | 1105 |
snd_mixer_t *mixer,
|
970 | 1106 |
const char *device)
|
|
972 | 1108 |
struct python_priv *priv;
|
973 | 1109 |
FILE *fp;
|
974 | 1110 |
const char *file;
|
975 | |
PyObject *obj, *obj1, *obj2, *py_mod, *mdict;
|
|
1111 |
PyObject *obj, *py_mod;
|
976 | 1112 |
|
977 | 1113 |
priv = calloc(1, sizeof(*priv));
|
978 | 1114 |
if (priv == NULL)
|
|
992 | 1128 |
}
|
993 | 1129 |
|
994 | 1130 |
Py_Initialize();
|
995 | |
if (PyType_Ready(&pymelem_type) < 0)
|
|
1131 |
if (PyType_Ready(&pymelem_type) < 0 ||
|
|
1132 |
PyType_Ready(&pymixer_type) < 0) {
|
|
1133 |
fclose(fp);
|
996 | 1134 |
return -EIO;
|
997 | |
if (PyType_Ready(&pymixer_type) < 0)
|
998 | |
return -EIO;
|
|
1135 |
}
|
|
1136 |
#if PY_MAJOR_VERSION < 3
|
999 | 1137 |
Py_InitModule("smixer_python", python_methods);
|
|
1138 |
#else
|
|
1139 |
PyModule_Create(&smixer_python_module);
|
|
1140 |
#endif
|
1000 | 1141 |
priv->py_initialized = 1;
|
1001 | 1142 |
main_interpreter = PyThreadState_Get()->interp;
|
1002 | 1143 |
obj = PyImport_GetModuleDict();
|
1003 | 1144 |
py_mod = PyDict_GetItemString(obj, "__main__");
|
1004 | |
if (py_mod) {
|
1005 | |
mdict = priv->py_mdict = PyModule_GetDict(py_mod);
|
1006 | |
obj = PyString_FromString(file);
|
1007 | |
if (obj)
|
1008 | |
PyDict_SetItemString(mdict, "__file__", obj);
|
1009 | |
Py_XDECREF(obj);
|
1010 | |
obj = PyString_FromString(device);
|
1011 | |
if (obj)
|
1012 | |
PyDict_SetItemString(mdict, "device", obj);
|
1013 | |
Py_XDECREF(obj);
|
1014 | |
Py_INCREF(&pymixer_type);
|
1015 | |
PyModule_AddObject(py_mod, "InternalMElement", (PyObject *)&pymelem_type);
|
1016 | |
PyModule_AddObject(py_mod, "InternalMixer", (PyObject *)&pymixer_type);
|
1017 | |
obj = PyDict_GetItemString(mdict, "InternalMixer");
|
1018 | |
if (obj) {
|
1019 | |
obj1 = PyTuple_New(3);
|
1020 | |
PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong((long)class));
|
1021 | |
PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong((long)mixer));
|
1022 | |
if (PyTuple_SET_ITEM(obj1, 2, mdict))
|
1023 | |
Py_INCREF(mdict);
|
1024 | |
obj2 = PyObject_CallObject(obj, obj1);
|
1025 | |
Py_XDECREF(obj1);
|
1026 | |
PyDict_SetItemString(mdict, "mixer", obj2);
|
1027 | |
priv->py_mixer = obj2;
|
1028 | |
} else {
|
1029 | |
SNDERR("Unable to create InternalMixer object");
|
1030 | |
return -EIO;
|
1031 | |
}
|
1032 | |
|
1033 | |
|
1034 | |
obj = PyRun_FileEx(fp, file, Py_file_input, mdict, mdict, 1);
|
1035 | |
if (obj == NULL)
|
1036 | |
PyErr_Print();
|
1037 | |
Py_XDECREF(obj);
|
1038 | |
priv->py_event_func = PyDict_GetItemString(mdict, "event");
|
1039 | |
if (priv->py_event_func == NULL) {
|
1040 | |
SNDERR("Unable to find python function 'event'");
|
1041 | |
return -EIO;
|
1042 | |
}
|
1043 | |
}
|
|
1145 |
if (py_mod)
|
|
1146 |
alsa_mixer_simple_pyinit(priv, py_mod, fp, file, class, mixer, device);
|
1044 | 1147 |
return 0;
|
1045 | 1148 |
}
|