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;
29 #include "../../../../config.h"
31 #if PYGOBJECT_FLAGS_SIGNED
32 typedef gint pyg_flags_type;
34 typedef guint pyg_flags_type;
40 pygobject_init(-1, -1, -1);
41 GLib = PyImport_ImportModule("gi.repository.GLib");
42 IOChannel = (PyTypeObject *) PyObject_GetAttrString(GLib, "IOChannel");
43 PollFD = (PyTypeObject *) PyObject_GetAttrString(GLib, "PollFD");
46 /* Map file objects to file descriptors. */
47 %typecheck(SWIG_TYPECHECK_POINTER) int fd {
48 $1 = (PyObject_AsFileDescriptor($input) != -1);
52 int fd = PyObject_AsFileDescriptor($input);
54 SWIG_exception(SWIG_TypeError,
55 "Expected file object or integer file descriptor");
60 /* Map from Glib::Variant to native Python types. */
61 %typemap(out) Glib::VariantBase {
62 GValue *value = g_new0(GValue, 1);
63 g_value_init(value, G_TYPE_VARIANT);
64 g_value_set_variant(value, $1.gobj());
65 PyObject *variant = pyg_value_as_pyobject(value, true);
66 $result = PyObject_CallMethod(variant,
67 const_cast<char *>("unpack"),
68 const_cast<char *>(""), NULL);
73 /* Map from Glib::IOCondition to GLib.IOCondition. */
74 %typecheck(SWIG_TYPECHECK_POINTER) Glib::IOCondition {
76 $1 = pygobject_check($input, &PyGFlags_Type) &&
77 (pyg_flags_get_value(G_TYPE_IO_CONDITION, $input, &flags) != -1);
80 %typemap(in) Glib::IOCondition {
81 if (!pygobject_check($input, &PyGFlags_Type))
82 SWIG_exception(SWIG_TypeError, "Expected GLib.IOCondition value");
84 if (pyg_flags_get_value(G_TYPE_IO_CONDITION, $input, &flags) == -1)
85 SWIG_exception(SWIG_TypeError, "Not a valid Glib.IOCondition value");
86 $1 = (Glib::IOCondition) flags;
90 %typemap(out) Glib::IOCondition {
91 GValue *value = g_new0(GValue, 1);
92 g_value_init(value, G_TYPE_IO_CONDITION);
93 g_value_set_flags(value, &$1);
94 $result = pyg_value_as_pyobject(value, true);
98 /* Map from GLib.PollFD to Glib::PollFD *. */
99 %typecheck(SWIG_TYPECHECK_POINTER) Glib::PollFD {
100 $1 = pygobject_check($input, PollFD);
103 %typemap(in) Glib::PollFD {
104 if (!pygobject_check($input, PollFD))
105 SWIG_exception(SWIG_TypeError, "Expected GLib.PollFD");
106 PyObject *fd_obj = PyObject_GetAttrString($input, "fd");
107 PyObject *events_obj = PyObject_GetAttrString($input, "events");
108 pyg_flags_type flags;
109 pyg_flags_get_value(G_TYPE_IO_CONDITION, events_obj, &flags);
110 int fd = PyInt_AsLong(fd_obj);
111 Glib::IOCondition events = (Glib::IOCondition) flags;
112 $1 = Glib::PollFD(fd, events);
115 /* Map from GLib.IOChannel to Glib::IOChannel *. */
116 %typecheck(SWIG_TYPECHECK_POINTER) Glib::RefPtr<Glib::IOChannel> {
117 $1 = pygobject_check($input, IOChannel);
120 %typemap(in) Glib::RefPtr<Glib::IOChannel> {
121 if (!pygobject_check($input, IOChannel))
122 SWIG_exception(SWIG_TypeError, "Expected GLib.IOChannel");
123 $1 = Glib::wrap((GIOChannel *) PyObject_Hash($input), true);
126 /* Map from callable PyObject to SourceCallbackFunction. */
127 %typecheck(SWIG_TYPECHECK_POINTER) sigrok::SourceCallbackFunction {
128 $1 = PyCallable_Check($input);
131 %typemap(in) sigrok::SourceCallbackFunction {
132 if (!PyCallable_Check($input))
133 SWIG_exception(SWIG_TypeError, "Expected a callable Python object");
135 $1 = [=] (Glib::IOCondition revents) {
136 auto gstate = PyGILState_Ensure();
138 GValue *value = g_new0(GValue, 1);
139 g_value_init(value, G_TYPE_IO_CONDITION);
140 g_value_set_flags(value, revents);
141 auto revents_obj = pyg_value_as_pyobject(value, true);
144 auto arglist = Py_BuildValue("(O)", revents_obj);
146 auto result = PyEval_CallObject($input, arglist);
149 Py_XDECREF(revents_obj);
151 if (PyErr_Occurred() || !PyBool_Check(result))
152 throw sigrok::Error(SR_ERR);
154 bool retval = (result == Py_True);
158 PyGILState_Release(gstate);
166 /* Map from callable PyObject to LogCallbackFunction */
167 %typecheck(SWIG_TYPECHECK_POINTER) sigrok::LogCallbackFunction {
168 $1 = PyCallable_Check($input);
171 %typemap(in) sigrok::LogCallbackFunction {
172 if (!PyCallable_Check($input))
173 SWIG_exception(SWIG_TypeError, "Expected a callable Python object");
175 $1 = [=] (const sigrok::LogLevel *loglevel, string message) {
176 auto gstate = PyGILState_Ensure();
178 auto log_obj = SWIG_NewPointerObj(
179 SWIG_as_voidptr(loglevel), SWIGTYPE_p_sigrok__LogLevel, 0);
181 auto string_obj = PyString_FromString(message.c_str());
183 auto arglist = Py_BuildValue("(OO)", log_obj, string_obj);
185 auto result = PyEval_CallObject($input, arglist);
189 Py_XDECREF(string_obj);
192 PyGILState_Release(gstate);
198 /* Map from callable PyObject to DatafeedCallbackFunction */
199 %typecheck(SWIG_TYPECHECK_POINTER) sigrok::DatafeedCallbackFunction {
200 $1 = PyCallable_Check($input);
203 %typemap(in) sigrok::DatafeedCallbackFunction {
204 if (!PyCallable_Check($input))
205 SWIG_exception(SWIG_TypeError, "Expected a callable Python object");
207 $1 = [=] (std::shared_ptr<sigrok::Device> device,
208 std::shared_ptr<sigrok::Packet> packet) {
209 auto gstate = PyGILState_Ensure();
211 auto device_obj = SWIG_NewPointerObj(
212 SWIG_as_voidptr(new std::shared_ptr<sigrok::Device>(device)),
213 SWIGTYPE_p_std__shared_ptrT_sigrok__Device_t, SWIG_POINTER_OWN);
215 auto packet_obj = SWIG_NewPointerObj(
216 SWIG_as_voidptr(new std::shared_ptr<sigrok::Packet>(packet)),
217 SWIGTYPE_p_std__shared_ptrT_sigrok__Packet_t, SWIG_POINTER_OWN);
219 auto arglist = Py_BuildValue("(OO)", device_obj, packet_obj);
221 auto result = PyEval_CallObject($input, arglist);
224 Py_XDECREF(device_obj);
225 Py_XDECREF(packet_obj);
228 PyGILState_Release(gstate);
236 #include "libsigrok/libsigrok.hpp"
238 /* Convert from a Python dict to a std::map<std::string, std::string> */
239 std::map<std::string, std::string> dict_to_map_string(PyObject *dict)
241 if (!PyDict_Check(dict))
242 throw sigrok::Error(SR_ERR_ARG);
244 std::map<std::string, std::string> output;
246 PyObject *py_key, *py_value;
249 while (PyDict_Next(dict, &pos, &py_key, &py_value)) {
250 if (!PyString_Check(py_key))
251 throw sigrok::Error(SR_ERR_ARG);
252 if (!PyString_Check(py_value))
253 throw sigrok::Error(SR_ERR_ARG);
254 auto key = PyString_AsString(py_key);
255 auto value = PyString_AsString(py_value);
262 /* Convert from a Python type to Glib::Variant, according to config key data type. */
263 Glib::VariantBase python_to_variant_by_key(PyObject *input, const sigrok::ConfigKey *key)
265 enum sr_datatype type = key->get_data_type()->get_id();
267 if (type == SR_T_UINT64 && PyInt_Check(input))
268 return Glib::Variant<guint64>::create(PyInt_AsLong(input));
269 if (type == SR_T_UINT64 && PyLong_Check(input))
270 return Glib::Variant<guint64>::create(PyLong_AsLong(input));
271 else if (type == SR_T_STRING && PyString_Check(input))
272 return Glib::Variant<std::string>::create(PyString_AsString(input));
273 else if (type == SR_T_BOOL && PyBool_Check(input))
274 return Glib::Variant<bool>::create(input == Py_True);
275 else if (type == SR_T_FLOAT && PyFloat_Check(input))
276 return Glib::Variant<double>::create(PyFloat_AsDouble(input));
277 else if (type == SR_T_INT32 && PyInt_Check(input))
278 return Glib::Variant<gint32>::create(PyInt_AsLong(input));
280 throw sigrok::Error(SR_ERR_ARG);
283 /* Convert from a Python type to Glib::Variant, according to Option data type. */
284 Glib::VariantBase python_to_variant_by_option(PyObject *input,
285 std::shared_ptr<sigrok::Option> option)
287 GVariantType *type = option->get_default_value().get_type().gobj();
289 if (type == G_VARIANT_TYPE_UINT64 && PyInt_Check(input))
290 return Glib::Variant<guint64>::create(PyInt_AsLong(input));
291 if (type == G_VARIANT_TYPE_UINT64 && PyLong_Check(input))
292 return Glib::Variant<guint64>::create(PyLong_AsLong(input));
293 else if (type == G_VARIANT_TYPE_STRING && PyString_Check(input))
294 return Glib::Variant<std::string>::create(PyString_AsString(input));
295 else if (type == G_VARIANT_TYPE_BOOLEAN && PyBool_Check(input))
296 return Glib::Variant<bool>::create(input == Py_True);
297 else if (type == G_VARIANT_TYPE_DOUBLE && PyFloat_Check(input))
298 return Glib::Variant<double>::create(PyFloat_AsDouble(input));
299 else if (type == G_VARIANT_TYPE_INT32 && PyInt_Check(input))
300 return Glib::Variant<gint32>::create(PyInt_AsLong(input));
302 throw sigrok::Error(SR_ERR_ARG);
305 /* Convert from a Python dict to a std::map<std::string, std::string> */
306 std::map<std::string, Glib::VariantBase> dict_to_map_options(PyObject *dict,
307 std::map<std::string, std::shared_ptr<sigrok::Option> > options)
309 if (!PyDict_Check(dict))
310 throw sigrok::Error(SR_ERR_ARG);
312 std::map<std::string, Glib::VariantBase> output;
314 PyObject *py_key, *py_value;
317 while (PyDict_Next(dict, &pos, &py_key, &py_value)) {
318 if (!PyString_Check(py_key))
319 throw sigrok::Error(SR_ERR_ARG);
320 auto key = PyString_AsString(py_key);
321 auto value = python_to_variant_by_option(py_value, options[key]);
330 /* Ignore these methods, we will override them below. */
331 %ignore sigrok::Driver::scan;
332 %ignore sigrok::InputFormat::open_file;
333 %ignore sigrok::OutputFormat::create_output;
335 %include "../../../swig/classes.i"
337 /* Support Driver.scan() with keyword arguments. */
338 %extend sigrok::Driver
340 std::vector<std::shared_ptr<sigrok::HardwareDevice> > _scan_kwargs(PyObject *dict)
342 if (!PyDict_Check(dict))
343 throw sigrok::Error(SR_ERR_ARG);
345 PyObject *py_key, *py_value;
347 std::map<const sigrok::ConfigKey *, Glib::VariantBase> options;
349 while (PyDict_Next(dict, &pos, &py_key, &py_value))
351 if (!PyString_Check(py_key))
352 throw sigrok::Error(SR_ERR_ARG);
353 auto key = sigrok::ConfigKey::get(PyString_AsString(py_key));
354 auto value = python_to_variant_by_key(py_value, key);
355 options[key] = value;
358 return $self->scan(options);
364 def _Driver_scan(self, **kwargs):
365 return self._scan_kwargs(kwargs)
367 Driver.scan = _Driver_scan
370 /* Support InputFormat.open_file() with keyword arguments. */
371 %extend sigrok::InputFormat
373 std::shared_ptr<sigrok::InputFileDevice> _open_file_kwargs(std::string filename, PyObject *dict)
375 return $self->open_file(filename, dict_to_map_string(dict));
381 def _InputFormat_open_file(self, filename, **kwargs):
382 return self._open_file_kwargs(filename, kwargs)
384 InputFormat.open_file = _InputFormat_open_file
387 /* Support OutputFormat.create_output() with keyword arguments. */
388 %extend sigrok::OutputFormat
390 std::shared_ptr<sigrok::Output> _create_output_kwargs(
391 std::shared_ptr<sigrok::Device> device, PyObject *dict)
393 return $self->create_output(device,
394 dict_to_map_options(dict, $self->get_options()));
400 def _OutputFormat_create_output(self, device, **kwargs):
401 return self._create_output_kwargs(device, kwargs)
403 OutputFormat.create_output = _OutputFormat_create_output
406 /* Support config_set() with Python input types. */
407 %extend sigrok::Configurable
409 void config_set(const ConfigKey *key, PyObject *input)
411 $self->config_set(key, python_to_variant_by_key(input, key));