Codebase list alsa-lib / 345843f
modules: smixer_python - add support for python3 Signed-off-by: Jaroslav Kysela <perex@perex.cz> Jaroslav Kysela 6 years ago
3 changed file(s) with 245 addition(s) and 126 deletion(s). Raw diff Collapse all Expand all
412412 AC_ARG_ENABLE(python,
413413 AS_HELP_STRING([--disable-python], [disable the python components]),
414414 [build_python="$enableval"], [build_python="yes"])
415
416 AC_ARG_ENABLE(python2,
417 AS_HELP_STRING([--enable-python2], [prefer python2]),
418 [build_python2="$enableval"], [build_python2="no"])
415419 PYTHON_LIBS=""
416420 PYTHON_INCLUDES=""
417421 if test "$build_python" = "yes" -a "$build_mixer_pymodules" = "yes"; then
418 pythonlibs0=$(python3-config --libs)
419 pythoninc0=$(python3-config --includes)
422 pythonlibs0=
423 pythoninc0=
424 if test "$build_python2" != "yes"; then
425 pythonlibs0=$(python3-config --libs)
426 pythoninc0=$(python3-config --includes)
427 fi
420428 if test -z "$pythonlibs0"; then
421429 pythonlibs0=$(python-config --libs)
422430 pythoninc0=$(python-config --includes)
1919 alisp)
2020 alisp=yes
2121 echo "Forced alisp code build..."
22 shift ;;
23 python2)
24 python2=yes
25 echo "Forced python2 interpreter build..."
2226 shift ;;
2327 *)
2428 endloop=yes
5256
5357 if [ "$modules" = "yes" ]; then
5458 args="$args --enable-mixer-modules"
55 args="$args --enable-mixer-pymodules"
59 args="$args --enable-mixer-pymods"
5660 fi
5761
5862 if [ "$alisp" = "yes" ]; then
5963 args="$args --enable-alisp"
64 fi
65
66 if [ "$python2" = "yes" ]; then
67 args="$args --enable-python2"
6068 fi
6169
6270 touch ltconfig
2424 #include "asoundlib.h"
2525 #include "mixer_abst.h"
2626
27 #if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2))
28 #pragma GCC diagnostic ignored "-Wstrict-aliasing"
29 #endif
30
2731 struct python_priv {
2832 int py_initialized;
2933 PyObject *py_event_func;
5559
5660 static PyInterpreterState *main_interpreter;
5761
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
5890 static void *get_C_ptr(PyObject *obj, const char *attr)
5991 {
6092 PyObject *o;
61
62 o = PyObject_GetAttr(obj, PyString_InternFromString(attr));
93 long val;
94
95 o = PyObject_GetAttr(obj, InternFromString(attr));
6396 if (!o) {
6497 PyErr_Format(PyExc_TypeError, "missing '%s' attribute", attr);
6598 return NULL;
6699 }
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;
72105 }
73106
74107 static struct pymelem *melem_to_pymelem(snd_mixer_elem_t *elem)
79112 static int pcall(struct pymelem *pymelem, const char *attr, PyObject *args, PyObject **_res)
80113 {
81114 PyObject *obj = (PyObject *)pymelem, *res;
82 int xres = 0;
115 long xres = 0;
83116
84117 if (_res)
85118 *_res = NULL;
86 obj = PyObject_GetAttr(obj, PyString_InternFromString(attr));
119 obj = PyObject_GetAttr(obj, InternFromString(attr));
87120 if (!obj) {
88121 PyErr_Format(PyExc_TypeError, "missing '%s' attribute", attr);
89122 PyErr_Print();
102135 *_res = res;
103136 res = PyTuple_GetItem(res, 0);
104137 }
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)) {
106142 xres = PyInt_AsLong(res);
143 #endif
107144 } else if (res == Py_None) {
108145 xres = 0;
109146 } else if (PyBool_Check(res)) {
154191 static int get_x_range_ops(snd_mixer_elem_t *elem, int dir,
155192 long *min, long *max, const char *attr)
156193 {
157 PyObject *obj1, *res;
194 PyObject *obj1, *t1, *t2, *res;
158195 struct pymelem *pymelem = melem_to_pymelem(elem);
159196 int err;
160197
162199 PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(dir));
163200 err = pcall(pymelem, attr, obj1, &res);
164201 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)) {
178210 *min = PyInt_AsLong(PyTuple_GetItem(res, 1));
179211 *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;
180219 }
181220 }
182221 Py_XDECREF(res);
206245 long channel, long *value,
207246 const char *attr)
208247 {
209 PyObject *obj1, *res;
248 PyObject *obj1, *t1, *res;
210249 struct pymelem *pymelem = melem_to_pymelem(elem);
211250 int err;
212251
215254 PyTuple_SET_ITEM(obj1, 1, PyInt_FromLong(channel));
216255 err = pcall(pymelem, attr, obj1, &res);
217256 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
229266 } 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;
231271 }
232272 }
233273 Py_XDECREF(res);
264304 long *dbValue,
265305 int xdir)
266306 {
267 PyObject *obj1, *res;
307 PyObject *obj1, *t1, *res;
268308 struct pymelem *pymelem = melem_to_pymelem(elem);
269309 int err;
270310
274314 PyTuple_SET_ITEM(obj1, 2, PyInt_FromLong(xdir));
275315 err = pcall(pymelem, "opsGetDBVol", obj1, &res);
276316 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
288326 } 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;
290331 }
291332 }
292333 Py_XDECREF(res);
352393 unsigned int item,
353394 size_t maxlen, char *buf)
354395 {
355 PyObject *obj1, *res;
396 PyObject *obj1, *obj2, *t1, *res;
356397 struct pymelem *pymelem = melem_to_pymelem(elem);
357398 int err;
358399 unsigned int len;
362403 PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(item));
363404 err = pcall(pymelem, "opsGetEnumItemName", obj1, &res);
364405 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);
373423 len = strlen(s);
374424 if (maxlen - 1 > len)
375425 len = maxlen - 1;
376426 memcpy(buf, s, len);
377427 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;
378435 }
379436 }
380437 Py_XDECREF(res);
385442 snd_mixer_selem_channel_id_t channel,
386443 unsigned int *itemp)
387444 {
388 PyObject *obj1, *res;
445 PyObject *obj1, *t1, *res;
389446 struct pymelem *pymelem = melem_to_pymelem(elem);
390447 int err;
391448
393450 PyTuple_SET_ITEM(obj1, 0, PyInt_FromLong(channel));
394451 err = pcall(pymelem, "opsGetEnumItem", obj1, &res);
395452 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 {
398463 PyErr_Format(PyExc_TypeError, "wrong result (invalid tuple)");
399464 PyErr_Print();
400465 PyErr_Clear();
401466 err = -EIO;
402 } else {
403 *itemp = PyInt_AsLong(PyTuple_GetItem(res, 1));
404467 }
405468 }
406469 Py_XDECREF(res);
463526 static PyObject *
464527 pymelem_get_name(struct pymelem *pymelem, void *priv ATTRIBUTE_UNUSED)
465528 {
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));
467530 }
468531
469532 static PyObject *
475538 static int
476539 pymelem_set_caps(struct pymelem *pymelem, PyObject *val, void *priv ATTRIBUTE_UNUSED)
477540 {
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;
484553 }
485554
486555 static PyObject *
587656 pymelem_dealloc(struct pymelem *self)
588657 {
589658 selem_free(self->melem);
590 self->ob_type->tp_free(self);
591659 }
592660
593661 static PyGetSetDef pymelem_getseters[] = {
633701 };
634702
635703 static PyTypeObject pymelem_type = {
636 PyObject_HEAD_INIT(0)
704 PyVarObject_HEAD_INIT(NULL, 0)
637705 tp_name: "smixer_python.InternalMElement",
638706 tp_basicsize: sizeof(struct pymelem),
639707 tp_dealloc: (destructor)pymelem_dealloc,
707775 obj1 = PyTuple_New(4);
708776 if (PyTuple_SET_ITEM(obj1, 0, (PyObject *)pymixer))
709777 Py_INCREF((PyObject *)pymixer);
710 PyTuple_SET_ITEM(obj1, 1, PyString_FromString(name));
778 PyTuple_SET_ITEM(obj1, 1, PyUnicode_FromString(name));
711779 PyTuple_SET_ITEM(obj1, 2, PyInt_FromLong(index));
712780 PyTuple_SET_ITEM(obj1, 3, PyInt_FromLong(weight));
713781 obj2 = PyObject_CallObject(obj, obj1);
799867 pymixer_dealloc(struct pymixer *self)
800868 {
801869 pymixer_free(self);
802 self->ob_type->tp_free(self);
803870 }
804871
805872 static PyGetSetDef pymixer_getseters[] = {
815882 };
816883
817884 static PyTypeObject pymixer_type = {
818 PyObject_HEAD_INIT(0)
885 PyVarObject_HEAD_INIT(NULL, 0)
819886 tp_name: "smixer_python.InternalMixer",
820887 tp_basicsize: sizeof(struct pymixer),
821888 tp_dealloc: (destructor)pymixer_dealloc,
909976 snd_hctl_elem_t *helem, snd_mixer_elem_t *melem)
910977 {
911978 struct python_priv *priv = snd_mixer_sbasic_get_private(class);
912 PyThreadState *tstate, *origstate;
979 PyThreadState *tstate;
913980 PyObject *t, *o, *r;
914981 int res = -ENOMEM;
915982
916983 tstate = PyThreadState_New(main_interpreter);
917 origstate = PyThreadState_Swap(tstate);
984 PyThreadState_Swap(tstate);
918985
919986 t = PyTuple_New(3);
920987 if (t) {
9341001 r = PyObject_CallObject(priv->py_event_func, t);
9351002 Py_DECREF(t);
9361003 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)) {
9381008 res = PyInt_AsLong(r);
1009 #endif
9391010 } else if (r == Py_None) {
9401011 res = 0;
9411012 }
9651036 free(priv);
9661037 }
9671038
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
9681104 int alsa_mixer_simple_finit(snd_mixer_class_t *class,
9691105 snd_mixer_t *mixer,
9701106 const char *device)
9721108 struct python_priv *priv;
9731109 FILE *fp;
9741110 const char *file;
975 PyObject *obj, *obj1, *obj2, *py_mod, *mdict;
1111 PyObject *obj, *py_mod;
9761112
9771113 priv = calloc(1, sizeof(*priv));
9781114 if (priv == NULL)
9921128 }
9931129
9941130 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);
9961134 return -EIO;
997 if (PyType_Ready(&pymixer_type) < 0)
998 return -EIO;
1135 }
1136 #if PY_MAJOR_VERSION < 3
9991137 Py_InitModule("smixer_python", python_methods);
1138 #else
1139 PyModule_Create(&smixer_python_module);
1140 #endif
10001141 priv->py_initialized = 1;
10011142 main_interpreter = PyThreadState_Get()->interp;
10021143 obj = PyImport_GetModuleDict();
10031144 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);
10441147 return 0;
10451148 }