2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Martin Ling <martin-sigrok@earth.li>
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.
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.
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/>.
23 #include <pygobject.h>
26 PyTypeObject *IOChannel;
32 pygobject_init(-1, -1, -1);
33 GLib = PyImport_ImportModule("gi.repository.GLib");
34 IOChannel = (PyTypeObject *) PyObject_GetAttrString(GLib, "IOChannel");
35 PollFD = (PyTypeObject *) PyObject_GetAttrString(GLib, "PollFD");
38 /* Map file objects to file descriptors. */
39 %typecheck(SWIG_TYPECHECK_POINTER) int fd {
40 $1 = (PyObject_AsFileDescriptor($input) != -1);
44 int fd = PyObject_AsFileDescriptor($input);
46 SWIG_exception(SWIG_TypeError,
47 "Expected file object or integer file descriptor");
52 /* Map from Glib::Variant to native Python types. */
53 %typemap(out) Glib::VariantBase {
54 GValue *value = g_new0(GValue, 1);
55 g_value_init(value, G_TYPE_VARIANT);
56 g_value_set_variant(value, $1.gobj());
57 PyObject *variant = pyg_value_as_pyobject(value, true);
58 $result = PyObject_CallMethod(variant,
59 const_cast<char *>("unpack"),
60 const_cast<char *>(""), NULL);
65 /* Map from Glib::IOCondition to GLib.IOCondition. */
66 %typecheck(SWIG_TYPECHECK_POINTER) Glib::IOCondition {
68 $1 = pygobject_check($input, &PyGFlags_Type) &&
69 (pyg_flags_get_value(G_TYPE_IO_CONDITION, $input, &flags) != -1);
72 %typemap(in) Glib::IOCondition {
73 if (!pygobject_check($input, &PyGFlags_Type))
74 SWIG_exception(SWIG_TypeError, "Expected GLib.IOCondition value");
76 if (pyg_flags_get_value(G_TYPE_IO_CONDITION, $input, &flags) == -1)
77 SWIG_exception(SWIG_TypeError, "Not a valid Glib.IOCondition value");
78 $1 = (Glib::IOCondition) flags;
82 %typemap(out) Glib::IOCondition {
83 GValue *value = g_new0(GValue, 1);
84 g_value_init(value, G_TYPE_IO_CONDITION);
85 g_value_set_flags(value, &$1);
86 $result = pyg_value_as_pyobject(value, true);
90 /* Map from GLib.PollFD to Glib::PollFD *. */
91 %typecheck(SWIG_TYPECHECK_POINTER) Glib::PollFD {
92 $1 = pygobject_check($input, PollFD);
95 %typemap(in) Glib::PollFD {
96 if (!pygobject_check($input, PollFD))
97 SWIG_exception(SWIG_TypeError, "Expected GLib.PollFD");
98 PyObject *fd_obj = PyObject_GetAttrString($input, "fd");
99 PyObject *events_obj = PyObject_GetAttrString($input, "events");
101 pyg_flags_get_value(G_TYPE_IO_CONDITION, events_obj, &flags);
102 int fd = PyInt_AsLong(fd_obj);
103 Glib::IOCondition events = (Glib::IOCondition) flags;
104 $1 = Glib::PollFD(fd, events);
107 /* Map from GLib.IOChannel to Glib::IOChannel *. */
108 %typecheck(SWIG_TYPECHECK_POINTER) Glib::RefPtr<Glib::IOChannel> {
109 $1 = pygobject_check($input, IOChannel);
112 %typemap(in) Glib::RefPtr<Glib::IOChannel> {
113 if (!pygobject_check($input, IOChannel))
114 SWIG_exception(SWIG_TypeError, "Expected GLib.IOChannel");
115 $1 = Glib::wrap((GIOChannel *) PyObject_Hash($input), true);
118 /* Map from callable PyObject to SourceCallbackFunction. */
119 %typecheck(SWIG_TYPECHECK_POINTER) sigrok::SourceCallbackFunction {
120 $1 = PyCallable_Check($input);
123 %typemap(in) sigrok::SourceCallbackFunction {
124 if (!PyCallable_Check($input))
125 SWIG_exception(SWIG_TypeError, "Expected a callable Python object");
127 $1 = [=] (Glib::IOCondition revents) {
128 auto gstate = PyGILState_Ensure();
130 GValue *value = g_new0(GValue, 1);
131 g_value_init(value, G_TYPE_IO_CONDITION);
132 g_value_set_flags(value, revents);
133 auto revents_obj = pyg_value_as_pyobject(value, true);
136 auto arglist = Py_BuildValue("(O)", revents_obj);
138 auto result = PyEval_CallObject($input, arglist);
141 Py_XDECREF(revents_obj);
143 if (PyErr_Occurred() || !PyBool_Check(result))
144 throw sigrok::Error(SR_ERR);
146 bool retval = (result == Py_True);
150 PyGILState_Release(gstate);
158 /* Map from callable PyObject to LogCallbackFunction */
159 %typecheck(SWIG_TYPECHECK_POINTER) sigrok::LogCallbackFunction {
160 $1 = PyCallable_Check($input);
163 %typemap(in) sigrok::LogCallbackFunction {
164 if (!PyCallable_Check($input))
165 SWIG_exception(SWIG_TypeError, "Expected a callable Python object");
167 $1 = [=] (const sigrok::LogLevel *loglevel, string message) {
168 auto gstate = PyGILState_Ensure();
170 auto log_obj = SWIG_NewPointerObj(
171 SWIG_as_voidptr(loglevel), SWIGTYPE_p_sigrok__LogLevel, 0);
173 auto string_obj = PyString_FromString(message.c_str());
175 auto arglist = Py_BuildValue("(OO)", log_obj, string_obj);
177 auto result = PyEval_CallObject($input, arglist);
181 Py_XDECREF(string_obj);
184 PyGILState_Release(gstate);
190 /* Map from callable PyObject to DatafeedCallbackFunction */
191 %typecheck(SWIG_TYPECHECK_POINTER) sigrok::DatafeedCallbackFunction {
192 $1 = PyCallable_Check($input);
195 %typemap(in) sigrok::DatafeedCallbackFunction {
196 if (!PyCallable_Check($input))
197 SWIG_exception(SWIG_TypeError, "Expected a callable Python object");
199 $1 = [=] (std::shared_ptr<sigrok::Device> device,
200 std::shared_ptr<sigrok::Packet> packet) {
201 auto gstate = PyGILState_Ensure();
203 auto device_obj = SWIG_NewPointerObj(
204 SWIG_as_voidptr(new std::shared_ptr<sigrok::Device>(device)),
205 SWIGTYPE_p_std__shared_ptrT_sigrok__Device_t, SWIG_POINTER_OWN);
207 auto packet_obj = SWIG_NewPointerObj(
208 SWIG_as_voidptr(new std::shared_ptr<sigrok::Packet>(packet)),
209 SWIGTYPE_p_std__shared_ptrT_sigrok__Packet_t, SWIG_POINTER_OWN);
211 auto arglist = Py_BuildValue("(OO)", device_obj, packet_obj);
213 auto result = PyEval_CallObject($input, arglist);
216 Py_XDECREF(device_obj);
217 Py_XDECREF(packet_obj);
220 PyGILState_Release(gstate);
228 #include "libsigrok/libsigrok.hpp"
230 /* Convert from a Python dict to a std::map<std::string, std::string> */
231 std::map<std::string, std::string> dict_to_map(PyObject *dict)
233 if (!PyDict_Check(dict))
234 throw sigrok::Error(SR_ERR_ARG);
236 std::map<std::string, std::string> output;
238 PyObject *py_key, *py_value;
241 while (PyDict_Next(dict, &pos, &py_key, &py_value)) {
242 if (!PyString_Check(py_key))
243 throw sigrok::Error(SR_ERR_ARG);
244 if (!PyString_Check(py_value))
245 throw sigrok::Error(SR_ERR_ARG);
246 auto key = PyString_AsString(py_key);
247 auto value = PyString_AsString(py_value);
254 /* Convert from a Python type to Glib::Variant, according to config key data type. */
255 Glib::VariantBase python_to_variant_by_key(PyObject *input, const sigrok::ConfigKey *key)
257 enum sr_datatype type = key->get_data_type()->get_id();
259 if (type == SR_T_UINT64 && PyInt_Check(input))
260 return Glib::Variant<guint64>::create(PyInt_AsLong(input));
261 if (type == SR_T_UINT64 && PyLong_Check(input))
262 return Glib::Variant<guint64>::create(PyLong_AsLong(input));
263 else if (type == SR_T_STRING && PyString_Check(input))
264 return Glib::Variant<std::string>::create(PyString_AsString(input));
265 else if (type == SR_T_BOOL && PyBool_Check(input))
266 return Glib::Variant<bool>::create(input == Py_True);
267 else if (type == SR_T_FLOAT && PyFloat_Check(input))
268 return Glib::Variant<double>::create(PyFloat_AsDouble(input));
269 else if (type == SR_T_INT32 && PyInt_Check(input))
270 return Glib::Variant<gint32>::create(PyInt_AsLong(input));
272 throw sigrok::Error(SR_ERR_ARG);
277 /* Ignore these methods, we will override them below. */
278 %ignore sigrok::Driver::scan;
279 %ignore sigrok::InputFormat::open_file;
280 %ignore sigrok::OutputFormat::create_output;
282 %include "../../../swig/classes.i"
284 /* Support Driver.scan() with keyword arguments. */
285 %extend sigrok::Driver
287 std::vector<std::shared_ptr<sigrok::HardwareDevice> > _scan_kwargs(PyObject *dict)
289 if (!PyDict_Check(dict))
290 throw sigrok::Error(SR_ERR_ARG);
292 PyObject *py_key, *py_value;
294 std::map<const sigrok::ConfigKey *, Glib::VariantBase> options;
296 while (PyDict_Next(dict, &pos, &py_key, &py_value))
298 if (!PyString_Check(py_key))
299 throw sigrok::Error(SR_ERR_ARG);
300 auto key = sigrok::ConfigKey::get(PyString_AsString(py_key));
301 auto value = python_to_variant_by_key(py_value, key);
302 options[key] = value;
305 return $self->scan(options);
311 def _Driver_scan(self, **kwargs):
312 return self._scan_kwargs(kwargs)
314 Driver.scan = _Driver_scan
317 /* Support InputFormat.open_file() with keyword arguments. */
318 %extend sigrok::InputFormat
320 std::shared_ptr<sigrok::InputFileDevice> _open_file_kwargs(std::string filename, PyObject *dict)
322 return $self->open_file(filename, dict_to_map(dict));
328 def _InputFormat_open_file(self, filename, **kwargs):
329 return self._open_file_kwargs(filename, kwargs)
331 InputFormat.open_file = _InputFormat_open_file
334 /* Support OutputFormat.create_output() with keyword arguments. */
335 %extend sigrok::OutputFormat
337 std::shared_ptr<sigrok::Output> _create_output_kwargs(
338 std::shared_ptr<sigrok::Device> device, PyObject *dict)
340 return $self->create_output(device, dict_to_map(dict));
346 def _OutputFormat_create_output(self, device, **kwargs):
347 return self._create_output_kwargs(device, kwargs)
349 OutputFormat.create_output = _OutputFormat_create_output
352 /* Support config_set() with Python input types. */
353 %extend sigrok::Configurable
355 void config_set(const ConfigKey *key, PyObject *input)
357 $self->config_set(key, python_to_variant_by_key(input, key));