Debian Codegraph @

src/gphoto2/widget.i

raw
1 // python-gphoto2 - Python interface to libgphoto2
2 // http://github.com/jim-easterbrook/python-gphoto2
3 // Copyright (C) 2014-24 Jim Easterbrook [email protected]
4 //
5 // This file is part of python-gphoto2.
6 //
7 // python-gphoto2 is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Lesser General Public License as published
9 // by the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // python-gphoto2 is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Lesser General Public License for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with python-gphoto2. If not, see <https://www.gnu.org/licenses/>.
19
20 %module(package="gphoto2") widget
21
22 %include "common/preamble.i"
23
24 %rename(CameraWidget) _CameraWidget;
25
26 // Deprecate some functions intended for camera drivers (2023-07-20)
27 DEPRECATED(gp_widget_add_choice,)
28 DEPRECATED(_CameraWidget::add_choice, 1)
29 DEPRECATED(gp_widget_set_info,)
30 DEPRECATED(_CameraWidget::set_info, 1)
31 DEPRECATED(gp_widget_set_name,)
32 DEPRECATED(_CameraWidget::set_name, 1)
33 DEPRECATED(gp_widget_set_range,)
34 DEPRECATED(_CameraWidget::set_range, 1)
35 DEPRECATED(gp_widget_set_readonly,)
36 DEPRECATED(_CameraWidget::set_readonly, 1)
37
38 /* These are the only wrapped functions that return a CameraWidget:
39 gp_widget_get_child(..., CameraWidget **child)
40 gp_widget_get_child_by_label(..., CameraWidget **child)
41 gp_widget_get_child_by_id(..., CameraWidget **child)
42 gp_widget_get_child_by_name(..., CameraWidget **child)
43 gp_widget_get_root(..., CameraWidget **root)
44 gp_widget_get_parent(..., CameraWidget **parent)
45 gp_camera_get_config(..., CameraWidget **window, ...)
46 gp_camera_get_single_config(..., CameraWidget **widget, ...)
47
48 The gp_camera_get_xxx functions return a new widget, which may be the root of a
49 tree. The others all return a pointer to an existing widget. To ensure this
50 pointer remains valid the function must increment the ref count of the root
51 widget, then decrement it when the returned pointer is destroyed.
52
53 Fortunately the function signatures use different names for the CameraWidget
54 result, so it's easy to use different typemaps for the different functions.
55 Beware of changes in the libgphoto2 definitions though.
56 */
57
58 %typemap(in, numinputs=0) CameraWidget ** (CameraWidget *temp) {
59 temp = NULL;
60 $1 = &temp;
61 }
62 %typemap(argout) CameraWidget **window, CameraWidget **widget {
63 // Append result to output object
64 $result = SWIG_AppendOutput(
65 $result, SWIG_NewPointerObj(*$1, $*1_descriptor, SWIG_POINTER_OWN));
66 }
67 %typemap(argout) CameraWidget **child, CameraWidget **root, CameraWidget **parent {
68 if (*$1 != NULL) {
69 // Increment refcount on root widget
70 CameraWidget *root;
71 int error = gp_widget_get_root(*$1, &root);
72 if (error < GP_OK) {
73 GPHOTO2_ERROR(error);
74 SWIG_fail;
75 }
76 error = gp_widget_ref(root);
77 if (error < GP_OK) {
78 GPHOTO2_ERROR(error);
79 SWIG_fail;
80 }
81 }
82 // Append result to output object
83 $result = SWIG_AppendOutput(
84 $result, SWIG_NewPointerObj(*$1, $*1_descriptor, SWIG_POINTER_OWN));
85 }
86
87 // Make docstring parameter types more Pythonic
88 %typemap(doc) (CameraWidget *) "$1_name: gphoto2.$*1_type"
89
90 %apply int *OUTPUT { CameraWidgetType * };
91 %apply int *OUTPUT { int * };
92 %apply float *OUTPUT { float * };
93
94 // Use typemaps to convert result of gp_widget_get_value
95 %{
96 typedef union {
97 int int_val;
98 float flt_val;
99 char* str_val;
100 } VoidValue;
101 %}
102 %typemap(in, numinputs=0) (void *value_out) (VoidValue temp) {
103 temp.str_val = NULL;
104 $1 = &temp;
105 }
106 %typemap(argout) (CameraWidget *widget, void *value_out),
107 (struct _CameraWidget *self, void *value_out) {
108 CameraWidgetType type;
109 PyObject* py_value = NULL;
110 VoidValue* value = (VoidValue*) $2;
111 int error = gp_widget_get_type($1, &type);
112 if (error < GP_OK) {
113 GPHOTO2_ERROR(error);
114 SWIG_fail;
115 }
116 switch (type) {
117 case GP_WIDGET_DATE:
118 case GP_WIDGET_TOGGLE:
119 py_value = SWIG_From_int(value->int_val);
120 break;
121 case GP_WIDGET_RANGE:
122 py_value = SWIG_From_float(value->flt_val);
123 break;
124 case GP_WIDGET_MENU:
125 case GP_WIDGET_TEXT:
126 case GP_WIDGET_RADIO:
127 if (value->str_val) {
128 py_value = PyString_FromString(value->str_val);
129 } else {
130 SWIG_Py_INCREF(Py_None);
131 py_value = Py_None;
132 }
133 break;
134 default:
135 PyErr_SetString(PyExc_RuntimeError, "Unsupported widget type");
136 SWIG_fail;
137 }
138 $result = SWIG_AppendOutput($result, py_value);
139 }
140 %typemap(doc) (void *value) "$1_name: int/float/str"
141
142 // Redefine signature of gp_widget_get_value to select correct typemaps
143 int gp_widget_get_value(CameraWidget *widget, void *value_out);
144 %ignore gp_widget_get_value;
145
146 // Use typemaps to convert input to gp_widget_set_value
147 %typemap(in, noblock=1) const void *value
148 (VoidValue value, int alloc = 0, int res = 0, CameraWidgetType type) {
149 // Camera widget is stored in arg1 as it's definitely the first argument to gp_widget_set_value
150 res = gp_widget_get_type(arg1, &type);
151 if (res < GP_OK) {
152 GPHOTO2_ERROR(res);
153 SWIG_fail;
154 }
155 switch (type) {
156 case GP_WIDGET_DATE:
157 case GP_WIDGET_TOGGLE:
158 res = SWIG_AsVal_int($input, &value.int_val);
159 if (!SWIG_IsOK(res)) {
160 %argument_fail(res, int, $symname, $argnum);
161 }
162 $1 = &value.int_val;
163 break;
164 case GP_WIDGET_RANGE:
165 res = SWIG_AsVal_float($input, &value.flt_val);
166 if (!SWIG_IsOK(res)) {
167 %argument_fail(res, float, $symname, $argnum);
168 }
169 $1 = &value.flt_val;
170 break;
171 case GP_WIDGET_MENU:
172 case GP_WIDGET_TEXT:
173 case GP_WIDGET_RADIO:
174 res = SWIG_AsCharPtrAndSize($input, &value.str_val, NULL, &alloc);
175 if (!SWIG_IsOK(res)) {
176 %argument_fail(res, str, $symname, $argnum);
177 }
178 // Note this is a pointer set by SWIG_AsCharPtrAndSize, not the address of value
179 $1 = value.str_val;
180 break;
181 default:
182 PyErr_SetString(PyExc_RuntimeError, "Unsupported widget type");
183 SWIG_fail;
184 }
185 }
186 %typemap(freearg) const void *value {
187 if (alloc$argnum == SWIG_NEWOBJ) free($1);
188 }
189
190 // Turn on default exception handling
191 DEFAULT_EXCEPTION
192
193 #ifndef SWIGIMPORTED
194
195 // Typemaps for iterator return values
196 %typemap(in, numinputs=0) PyObject **iter (PyObject* temp=NULL) {
197 $1 = &temp;
198 }
199 %typemap(argout) PyObject **iter {
200 if (!*$1)
201 *$1 = SWIG_Py_Void();
202 $result = SWIG_AppendOutput($result, *$1);
203 }
204
205 // Add gp_widget_get_children() method that returns an iterator
206 %feature("docstring") gp_widget_get_children "Gets all the child widgets of a CameraWidget. The return value is a list
207 containing a gphoto2 error code and a Python iterator. The iterator can
208 be used to get each child in sequence.
209
210 Parameters
211 ----------
212 * `widget` :
213 a CameraWidget
214
215 Returns
216 -------
217 a gphoto2 error code and a Python iterator.
218
219 See also gphoto2.CameraWidget.get_children"
220
221 %feature("docstring") _CameraWidget::get_children "Gets all the child widgets of a CameraWidget. The return value is a
222 Python iterator which can be used to get each child in sequence.
223
224 Returns
225 -------
226 a Python iterator.
227
228 See also gphoto2.gp_widget_get_children"
229
230 %noexception gp_widget_get_children;
231 %inline %{
232 static int gp_widget_get_children(CameraWidget* widget, PyObject **iter) {
233 PyObject* py_self = SWIG_Python_NewPointerObj(
234 NULL, widget, SWIGTYPE_p__CameraWidget, 0);
235 *iter = PySeqIter_New(py_self);
236 SWIG_Py_DECREF(py_self);
237 return GP_OK;
238 };
239 %}
240
241 // Add gp_widget_get_choices() method that returns an iterator
242 %feature("docstring") gp_widget_get_choices "Gets all the choice values of a CameraWidget. The return value is a list
243 containing a gphoto2 error code and a Python iterator. The iterator can
244 be used to get each choice in sequence.
245
246 Parameters
247 ----------
248 * `widget` :
249 a CameraWidget
250
251 Returns
252 -------
253 a gphoto2 error code and a Python iterator.
254
255 See also gphoto2.CameraWidget.get_choices"
256
257 %feature("docstring") _CameraWidget::get_choices "Gets all the choice values of a CameraWidget. The return value is a
258 Python iterator which can be used to get each choice in sequence.
259
260 Returns
261 -------
262 a Python iterator.
263
264 See also gphoto2.gp_widget_get_choices"
265
266 %noexception gp_widget_get_choices;
267 %inline %{
268 int gp_widget_get_choices(CameraWidget* widget, PyObject **iter) {
269 int result = GP_OK;
270 int len = gp_widget_count_choices(widget);
271 if (len < GP_OK)
272 return len;
273 PyObject* list = PyTuple_New(len);
274 const char *choice = NULL;
275 for (int idx = 0; idx < len; idx++) {
276 result = gp_widget_get_choice(widget, idx, &choice);
277 if (result != GP_OK) {
278 SWIG_Py_DECREF(list);
279 return result;
280 }
281 PyTuple_SET_ITEM(list, idx, PyUnicode_FromString(choice));
282 }
283 *iter = PySeqIter_New(list);
284 SWIG_Py_DECREF(list);
285 return result;
286 };
287 %}
288
289 #endif //ifndef SWIGIMPORTED
290
291 // Add default destructor to _CameraWidget
292 // Destructor decrefs root widget
293 %{
294 static int widget_dtor(CameraWidget *widget) {
295 if (widget == NULL)
296 return GP_OK;
297 {
298 CameraWidget *root;
299 int error = gp_widget_get_root(widget, &root);
300 if (error < GP_OK)
301 return error;
302 return gp_widget_unref(root);
303 }
304 }
305 %}
306 struct _CameraWidget {};
307 DEFAULT_DTOR(_CameraWidget, widget_dtor)
308
309
310 // Make _CameraWidget more like a list
311 %feature("python:slot", "sq_item", functype="ssizeargfunc")
312 _CameraWidget::__getitem__;
313 %extend _CameraWidget {
314 void __getitem__(int child_number, CameraWidget **child) {
315 if ((child_number < 0) ||
316 (child_number >= gp_widget_count_children($self))) {
317 PyErr_SetNone(PyExc_IndexError);
318 return;
319 }
320 int result = gp_widget_get_child($self, child_number, child);
321 if (result < GP_OK) GPHOTO2_ERROR(result)
322 }
323 };
324
325 // Add member methods to _CameraWidget
326 LEN_MEMBER_FUNCTION(_CameraWidget, gp_widget_count_children)
327 MEMBER_FUNCTION(_CameraWidget,
328 int, count_children, (),
329 gp_widget_count_children, ($self), )
330 MEMBER_FUNCTION(_CameraWidget,
331 void, get_child, (int child_number, CameraWidget **child),
332 gp_widget_get_child, ($self, child_number, child), )
333 MEMBER_FUNCTION(_CameraWidget,
334 void, get_children, (PyObject** iter),
335 gp_widget_get_children, ($self, iter), )
336 MEMBER_FUNCTION(_CameraWidget,
337 void, get_child_by_label, (const char *label, CameraWidget **child),
338 gp_widget_get_child_by_label, ($self, label, child), )
339 MEMBER_FUNCTION(_CameraWidget,
340 void, get_child_by_id, (int id, CameraWidget **child),
341 gp_widget_get_child_by_id, ($self, id, child), )
342 MEMBER_FUNCTION(_CameraWidget,
343 void, get_child_by_name, (const char *name, CameraWidget **child),
344 gp_widget_get_child_by_name, ($self, name, child), )
345 MEMBER_FUNCTION(_CameraWidget,
346 void, get_root, (CameraWidget **root),
347 gp_widget_get_root, ($self, root), )
348 MEMBER_FUNCTION(_CameraWidget,
349 void, get_parent, (CameraWidget **parent),
350 gp_widget_get_parent, ($self, parent), )
351 MEMBER_FUNCTION(_CameraWidget,
352 void, set_value, (const void *value),
353 gp_widget_set_value, ($self, value), )
354 MEMBER_FUNCTION(_CameraWidget,
355 void, get_value, (void *value_out),
356 gp_widget_get_value, ($self, value_out), )
357 MEMBER_FUNCTION(_CameraWidget,
358 void, set_name, (const char *name),
359 gp_widget_set_name, ($self, name), )
360 MEMBER_FUNCTION(_CameraWidget,
361 void, get_name, (const char **name),
362 gp_widget_get_name, ($self, name), )
363 MEMBER_FUNCTION(_CameraWidget,
364 void, set_info, (const char *info),
365 gp_widget_set_info, ($self, info), )
366 MEMBER_FUNCTION(_CameraWidget,
367 void, get_info, (const char **info),
368 gp_widget_get_info, ($self, info), )
369 MEMBER_FUNCTION(_CameraWidget,
370 void, get_id, (int *id),
371 gp_widget_get_id, ($self, id), )
372 MEMBER_FUNCTION(_CameraWidget,
373 void, get_type, (CameraWidgetType *type),
374 gp_widget_get_type, ($self, type), )
375 MEMBER_FUNCTION(_CameraWidget,
376 void, get_label, (const char **label),
377 gp_widget_get_label, ($self, label), )
378 MEMBER_FUNCTION(_CameraWidget,
379 void, set_range, (float min, float max, float increment),
380 gp_widget_set_range, ($self, min, max, increment), )
381 MEMBER_FUNCTION(_CameraWidget,
382 void, get_range, (float *min, float *max, float *increment),
383 gp_widget_get_range, ($self, min, max, increment), )
384 MEMBER_FUNCTION(_CameraWidget,
385 void, add_choice, (const char *choice),
386 gp_widget_add_choice, ($self, choice), )
387 MEMBER_FUNCTION(_CameraWidget,
388 int, count_choices, (),
389 gp_widget_count_choices, ($self), )
390 MEMBER_FUNCTION(_CameraWidget,
391 void, get_choices, (PyObject **iter),
392 gp_widget_get_choices, ($self, iter), )
393 MEMBER_FUNCTION(_CameraWidget,
394 void, get_choice, (int choice_number, const char **choice),
395 gp_widget_get_choice, ($self, choice_number, choice), )
396 MEMBER_FUNCTION(_CameraWidget,
397 int, changed, (),
398 gp_widget_changed, ($self), )
399 MEMBER_FUNCTION(_CameraWidget,
400 void, set_changed, (int changed),
401 gp_widget_set_changed, ($self, changed), )
402 MEMBER_FUNCTION(_CameraWidget,
403 void, set_readonly, (int readonly),
404 gp_widget_set_readonly, ($self, readonly), )
405 MEMBER_FUNCTION(_CameraWidget,
406 void, get_readonly, (int *readonly),
407 gp_widget_get_readonly, ($self, readonly), )
408
409 // Ignore some functions
410 %ignore gp_widget_new;
411 %ignore gp_widget_free;
412 %ignore gp_widget_ref;
413 %ignore gp_widget_unref;
414
415 // Turn off default exception handling
416 %noexception;
417
418 %include "gphoto2/gphoto2-widget.h"