]>
Commit | Line | Data |
---|---|---|
758b01ad | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
758b01ad ML |
3 | * |
4 | * Copyright (C) 2013 Martin Ling <martin-sigrok@earth.li> | |
5 | * | |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
02206269 ML |
20 | %module lowlevel |
21 | ||
5a7e6221 | 22 | %include "../../../swig/lowlevel.i" |
0849c7d3 ML |
23 | |
24 | %{ | |
25 | ||
26 | void sr_datafeed_python_callback(const struct sr_dev_inst *sdi, | |
27 | const struct sr_datafeed_packet *packet, void *cb_data) | |
28 | { | |
29 | PyObject *sdi_obj; | |
30 | PyObject *packet_obj; | |
31 | PyObject *arglist; | |
32 | PyObject *result; | |
33 | PyGILState_STATE gstate; | |
34 | PyObject *python_callback; | |
35 | ||
36 | python_callback = (PyObject *) cb_data; | |
37 | gstate = PyGILState_Ensure(); | |
38 | ||
39 | sdi_obj = SWIG_NewPointerObj(SWIG_as_voidptr(sdi), | |
40 | SWIGTYPE_p_sr_dev_inst, 0); | |
41 | ||
42 | packet_obj = SWIG_NewPointerObj(SWIG_as_voidptr(packet), | |
43 | SWIGTYPE_p_sr_datafeed_packet, 0); | |
44 | ||
45 | arglist = Py_BuildValue("(OO)", sdi_obj, packet_obj); | |
46 | ||
47 | result = PyEval_CallObject(python_callback, arglist); | |
48 | ||
49 | Py_XDECREF(arglist); | |
50 | Py_XDECREF(sdi_obj); | |
51 | Py_XDECREF(packet_obj); | |
52 | Py_XDECREF(result); | |
53 | ||
54 | PyGILState_Release(gstate); | |
55 | } | |
56 | ||
a25932e0 | 57 | int sr_session_datafeed_python_callback_add(PyObject *cb) |
0849c7d3 | 58 | { |
a25932e0 ML |
59 | int ret; |
60 | ||
0849c7d3 | 61 | if (!PyCallable_Check(cb)) |
a25932e0 ML |
62 | return SR_ERR_ARG; |
63 | else { | |
64 | ret = sr_session_datafeed_callback_add( | |
0849c7d3 | 65 | sr_datafeed_python_callback, cb); |
a25932e0 ML |
66 | if (ret == SR_OK) |
67 | Py_XINCREF(cb); | |
68 | return ret; | |
69 | } | |
0849c7d3 ML |
70 | } |
71 | ||
08d59537 ML |
72 | PyObject *cdata(const void *data, unsigned long size) |
73 | { | |
74 | #if PY_MAJOR_VERSION < 3 | |
75 | return PyString_FromStringAndSize(data, size); | |
76 | #else | |
77 | return PyBytes_FromStringAndSize(data, size); | |
78 | #endif | |
79 | } | |
80 | ||
05cfe114 ML |
81 | GSList *python_to_gslist(PyObject *pylist) |
82 | { | |
83 | if (PyList_Check(pylist)) { | |
84 | GSList *gslist = NULL; | |
85 | int size = PyList_Size(pylist); | |
86 | int i; | |
87 | for (i = size - 1; i >= 0; i--) { | |
88 | SwigPyObject *o = (SwigPyObject *)PyList_GetItem(pylist, i); | |
89 | void *data = o->ptr; | |
90 | gslist = g_slist_prepend(gslist, data); | |
91 | } | |
92 | return gslist; | |
93 | } | |
94 | return NULL; | |
95 | } | |
96 | ||
97 | PyObject *gslist_to_python(GSList *gslist) | |
98 | { | |
99 | PyObject *pylist = PyList_New(0); | |
100 | GSList *l; | |
101 | for (l = gslist; l; l = l->next) | |
102 | PyList_Append(pylist, SWIG_NewPointerObj(l->data, SWIGTYPE_p_void, 0)); | |
103 | return pylist; | |
104 | } | |
105 | ||
0849c7d3 ML |
106 | %} |
107 | ||
a25932e0 | 108 | int sr_session_datafeed_python_callback_add(PyObject *cb); |
08d59537 ML |
109 | |
110 | PyObject *cdata(const void *data, unsigned long size); | |
05cfe114 ML |
111 | |
112 | GSList *python_to_gslist(PyObject *pylist); | |
113 | PyObject *gslist_to_python(GSList *gslist); |