Debian Codegraph @

src/gphoto2/camera.i

raw
1 // python-gphoto2 - Python interface to libgphoto2
2 // http://github.com/jim-easterbrook/python-gphoto2
3 // Copyright (C) 2014-25 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", threads="1") camera
21 %nothread;
22
23 %include "common/preamble.i"
24
25 %include "stdint.i"
26
27 %import "abilities_list.i"
28 %import "filesys.i"
29 %import "widget.i"
30
31 %rename(Camera) _Camera;
32
33 // Make docstring parameter types more Pythonic
34 %typemap(doc) Camera * "$1_name: gphoto2.$*1_type"
35 %typemap(doc) enum CameraCaptureType "$1_name: $1_type (gphoto2.GP_CAPTURE_IMAGE etc.)"
36
37 // Allow other Python threads to continue during some function calls
38 %thread gp_camera_wait_for_event;
39 %thread gp_camera_capture;
40 %thread gp_camera_trigger_capture;
41 %thread gp_camera_capture_preview;
42 %thread gp_camera_init;
43 %thread gp_camera_get_config;
44 %thread gp_camera_get_single_config;
45 %thread gp_camera_set_config;
46 %thread gp_camera_set_single_config;
47 %thread gp_camera_folder_list_files;
48 %thread gp_camera_folder_list_folders;
49 %thread gp_camera_folder_delete_all;
50 %thread gp_camera_folder_put_file;
51 %thread gp_camera_file_get;
52 %thread gp_camera_file_get_info;
53 %thread gp_camera_file_read;
54
55 // Turn on default exception handling
56 DEFAULT_EXCEPTION
57
58 // Many functions accept NULL context value
59 DEFAULT_CONTEXT
60
61 // gp_camera_get_abilities() returns a pointer in an output parameter
62 CALLOC_ARGOUT(CameraAbilities *abilities)
63
64 // gp_camera_new() returns a pointer in an output parameter
65 PLAIN_ARGOUT(Camera **)
66
67 // gp_camera_get_summary() etc. return a pointer in an output parameter
68 CALLOC_ARGOUT(CameraText *summary)
69 CALLOC_ARGOUT(CameraText *manual)
70 CALLOC_ARGOUT(CameraText *about)
71
72 // gp_camera_folder_list_files() etc. return a pointer in an output parameter
73 NEW_ARGOUT(CameraList *, gp_list_new, gp_list_unref)
74
75 // In python-gphoto2 v2.5.0+ you can optionally pass a CameraFile to
76 // gp_camera_capture_preview() & gp_camera_file_get(), otherwise one gets
77 // created and returned as a new object.
78 %typemap(default) CameraFile *camera_file (int new_file = 0) %{
79 $1 = NULL;
80 %}
81 %typemap(check) CameraFile *camera_file {
82 if (!$1) {
83 int error = gp_file_new(&$1);
84 if (error < GP_OK) {
85 GPHOTO2_ERROR(error)
86 SWIG_fail;
87 }
88 new_file$argnum = 1;
89 }
90 else
91 // Deprecated since 2025-06-28
92 PyErr_WarnEx(PyExc_DeprecationWarning,
93 "In function $symname a CameraFile object should not"
94 " be passed as an in/out parameter.", 1);
95 }
96 %typemap(argout) CameraFile *camera_file {
97 if (new_file$argnum) {
98 $result = SWIG_AppendOutput(
99 $result, SWIG_NewPointerObj($1, $1_descriptor, SWIG_POINTER_OWN));
100 new_file$argnum = 0;
101 }
102 }
103 %typemap(freearg) CameraFile *camera_file %{
104 if (new_file$argnum) gp_file_unref($1);
105 %}
106 %typemap(doc) CameraFile *camera_file
107 "$1_name: gphoto2.CameraFile (default=None)";
108
109 // Redefine signature as gp_camera_folder_put_file() also uses *file
110 %noexception gp_camera_capture_preview;
111 int gp_camera_capture_preview(Camera *camera, CameraFile *camera_file, GPContext *context);
112 %ignore gp_camera_capture_preview;
113
114 // gp_camera_capture() returns a pointer in an output parameter
115 CALLOC_ARGOUT(CameraFilePath *path)
116
117 // gp_camera_file_read() fills a user-supplied buffer
118 %typemap(doc) (char * buf, uint64_t * size) "$1_name: writable buffer (e.g. memoryview)"
119 %typemap(in, numinputs=1) (char * buf, uint64_t * size) (uint64_t temp) {
120 Py_buffer view;
121 if (PyObject_CheckBuffer($input) != 1) {
122 PyErr_SetString(
123 PyExc_TypeError,
124 "in method '$symname', argument $argnum does not support the buffer interface");
125 SWIG_fail;
126 }
127 if (PyObject_GetBuffer($input, &view, PyBUF_SIMPLE | PyBUF_WRITABLE) != 0) {
128 PyErr_SetString(
129 PyExc_TypeError,
130 "in method '$symname', argument $argnum does not export a writable buffer");
131 SWIG_fail;
132 }
133 $1 = view.buf;
134 temp = view.len;
135 $2 = &temp;
136 PyBuffer_Release(&view);
137 }
138 %typemap(argout) (char * buf, uint64_t * size) {
139 $result = SWIG_AppendOutput($result, PyLong_FromUnsignedLongLong(*$2));
140 }
141
142 // Add default constructor and destructor to _Camera
143 DEFAULT_CTOR(_Camera, gp_camera_new)
144 DEFAULT_DTOR(_Camera, gp_camera_unref)
145 %ignore gp_camera_ref;
146 %ignore gp_camera_unref;
147
148 // Add member methods to _Camera
149 MEMBER_FUNCTION(_Camera,
150 static void, autodetect, (CameraList *list, GPContext *context),
151 gp_camera_autodetect, (list, context), )
152 MEMBER_FUNCTION(_Camera,
153 void, set_abilities, (CameraAbilities abilities),
154 gp_camera_set_abilities, ($self, abilities), )
155 MEMBER_FUNCTION(_Camera,
156 void, get_abilities, (CameraAbilities *abilities),
157 gp_camera_get_abilities, ($self, abilities), )
158 MEMBER_FUNCTION(_Camera,
159 void, set_port_info, (GPPortInfo info),
160 gp_camera_set_port_info, ($self, info), )
161 MEMBER_FUNCTION(_Camera,
162 void, get_port_info, (GPPortInfo *info),
163 gp_camera_get_port_info, ($self, info), )
164 MEMBER_FUNCTION(_Camera,
165 void, set_port_speed, (int speed),
166 gp_camera_set_port_speed, ($self, speed), )
167 MEMBER_FUNCTION(_Camera,
168 int, get_port_speed, (),
169 gp_camera_get_port_speed, ($self), )
170 MEMBER_FUNCTION(_Camera,
171 void, init, (GPContext *context),
172 gp_camera_init, ($self, context), 1)
173 MEMBER_FUNCTION(_Camera,
174 void, exit, (GPContext *context),
175 gp_camera_exit, ($self, context), )
176 MEMBER_FUNCTION(_Camera,
177 void, get_config, (CameraWidget **window, GPContext *context),
178 gp_camera_get_config, ($self, window, context), 1)
179 MEMBER_FUNCTION(_Camera,
180 void, list_config, (CameraList *list, GPContext *context),
181 gp_camera_list_config, ($self, list, context), )
182 MEMBER_FUNCTION(_Camera,
183 void, get_single_config, (const char *name, CameraWidget **widget, GPContext *context),
184 gp_camera_get_single_config, ($self, name, widget, context), 1)
185 MEMBER_FUNCTION(_Camera,
186 void, set_config, (CameraWidget *window, GPContext *context),
187 gp_camera_set_config, ($self, window, context), 1)
188 MEMBER_FUNCTION(_Camera,
189 void, set_single_config, (const char *name, CameraWidget *widget, GPContext *context),
190 gp_camera_set_single_config, ($self, name, widget, context), 1)
191 MEMBER_FUNCTION(_Camera,
192 void, get_summary, (CameraText *summary, GPContext *context),
193 gp_camera_get_summary, ($self, summary, context), )
194 MEMBER_FUNCTION(_Camera,
195 void, get_manual, (CameraText *manual, GPContext *context),
196 gp_camera_get_manual, ($self, manual, context), )
197 MEMBER_FUNCTION(_Camera,
198 void, get_about, (CameraText *about, GPContext *context),
199 gp_camera_get_about, ($self, about, context), )
200 MEMBER_FUNCTION(_Camera,
201 void, capture, (CameraCaptureType type, CameraFilePath *path, GPContext *context),
202 gp_camera_capture, ($self, type, path, context), 1)
203 MEMBER_FUNCTION(_Camera,
204 void, trigger_capture, (GPContext *context),
205 gp_camera_trigger_capture, ($self, context), 1)
206 MEMBER_FUNCTION(_Camera,
207 void, capture_preview, (CameraFile *camera_file, GPContext *context),
208 gp_camera_capture_preview, ($self, camera_file, context), 1)
209 MEMBER_FUNCTION(_Camera,
210 void, wait_for_event,
211 (int timeout, CameraEventType *eventtype, void **eventdata, GPContext *context),
212 gp_camera_wait_for_event, ($self, timeout, eventtype, eventdata, context), 1)
213 MEMBER_FUNCTION(_Camera,
214 void, get_storageinfo, (CameraStorageInformation **sifs, int *nrofsifs, GPContext *context),
215 gp_camera_get_storageinfo, ($self, sifs, nrofsifs, context), )
216 MEMBER_FUNCTION(_Camera,
217 void, folder_list_files, (const char *folder, CameraList *list, GPContext *context),
218 gp_camera_folder_list_files, ($self, folder, list, context), 1)
219 MEMBER_FUNCTION(_Camera,
220 void, folder_list_folders, (const char *folder, CameraList *list, GPContext *context),
221 gp_camera_folder_list_folders, ($self, folder, list, context), 1)
222 MEMBER_FUNCTION(_Camera,
223 void, folder_delete_all, (const char *folder, GPContext *context),
224 gp_camera_folder_delete_all, ($self, folder, context), 1)
225 MEMBER_FUNCTION(_Camera,
226 void, folder_put_file, (const char *folder, const char *filename, CameraFileType type, CameraFile *file, GPContext *context),
227 gp_camera_folder_put_file, ($self, folder, filename, type, file, context), 1)
228 MEMBER_FUNCTION(_Camera,
229 void, folder_make_dir, (const char *folder, const char *name, GPContext *context),
230 gp_camera_folder_make_dir, ($self, folder, name, context), )
231 MEMBER_FUNCTION(_Camera,
232 void, folder_remove_dir, (const char *folder, const char *name, GPContext *context),
233 gp_camera_folder_remove_dir, ($self, folder, name, context), )
234 MEMBER_FUNCTION(_Camera,
235 void, file_get_info, (const char *folder, const char *file, CameraFileInfo *info, GPContext *context),
236 gp_camera_file_get_info, ($self, folder, file, info, context), 1)
237 MEMBER_FUNCTION(_Camera,
238 void, file_set_info, (const char *folder, const char *file, CameraFileInfo info, GPContext *context),
239 gp_camera_file_set_info, ($self, folder, file, info, context), )
240 MEMBER_FUNCTION(_Camera,
241 void, file_get, (const char *folder, const char *file, CameraFileType type, CameraFile *camera_file, GPContext *context),
242 gp_camera_file_get, ($self, folder, file, type, camera_file, context), 1)
243 MEMBER_FUNCTION(_Camera,
244 void, file_read, (const char *folder, const char *file, CameraFileType type, uint64_t offset, char *buf, uint64_t *size, GPContext *context),
245 gp_camera_file_read, ($self, folder, file, type, offset, buf, size, context), 1)
246 MEMBER_FUNCTION(_Camera,
247 void, file_delete, (const char *folder, const char *file, GPContext *context),
248 gp_camera_file_delete, ($self, folder, file, context), )
249
250 // gp_camera_get_storageinfo() returns an allocated array in an output parameter
251 %typemap(in, numinputs=0)
252 (CameraStorageInformation **, int *)
253 (CameraStorageInformation* temp_ptr, int temp_cnt),
254 (CameraStorageInformation **sifs, int *nrofsifs)
255 (CameraStorageInformation* temp_ptr, int temp_cnt){
256 temp_ptr = NULL;
257 temp_cnt = 0;
258 $1 = &temp_ptr;
259 $2 = &temp_cnt;
260 }
261 %typemap(argout) (CameraStorageInformation **, int *),
262 (CameraStorageInformation **sifs, int *nrofsifs) {
263 // copy single array of CameraStorageInformation to individual Python objects
264 PyObject* out_list = PyList_New(*$2);
265 int n;
266 for (n = 0; n < *$2; n++) {
267 CameraStorageInformation* new_sif = malloc(sizeof(CameraStorageInformation));
268 if (new_sif == NULL) {
269 PyErr_SetString(PyExc_MemoryError, "Cannot allocate " "$*1_type");
270 SWIG_fail;
271 }
272 memcpy(new_sif, &(*$1)[n], sizeof(CameraStorageInformation));
273 PyList_SetItem(out_list, n,
274 SWIG_NewPointerObj(new_sif, $descriptor(CameraStorageInformation*), SWIG_POINTER_OWN));
275 }
276 $result = SWIG_AppendOutput($result, out_list);
277 }
278 %typemap(freearg) (CameraStorageInformation **, int *),
279 (CameraStorageInformation **sifs, int *nrofsifs) {
280 // deallocate CameraStorageInformation array allocated by gp_camera_get_storageinfo
281 free(*$1);
282 }
283
284 // Substitute definitions of things added during libgphoto2 development
285 %{
286 #if GPHOTO2_VERSION < 0x02051100
287 unsigned int GP_EVENT_FILE_CHANGED = GP_EVENT_CAPTURE_COMPLETE + 1;
288 #endif
289 %}
290
291 // gp_camera_wait_for_event() returns two pointers in output parameters
292 %typemap(in, numinputs=0) (CameraEventType * eventtype, void ** eventdata)
293 (CameraEventType temp_type, void *temp_data) {
294 temp_type = -1;
295 temp_data = NULL;
296 $1 = &temp_type;
297 $2 = &temp_data;
298 }
299 %typemap(argout) (CameraEventType * eventtype, void ** eventdata) {
300 $result = SWIG_AppendOutput($result, PyInt_FromLong(*$1));
301 if (*$1 == GP_EVENT_FILE_ADDED || *$1 == GP_EVENT_FOLDER_ADDED
302 || *$1 == GP_EVENT_FILE_CHANGED) {
303 $result = SWIG_AppendOutput(
304 $result, SWIG_NewPointerObj(*$2, $descriptor(CameraFilePath*), SWIG_POINTER_OWN));
305 }
306 else if (*$1 == GP_EVENT_UNKNOWN && *$2 != NULL) {
307 $result = SWIG_AppendOutput($result, PyString_FromString(*$2));
308 free(*$2);
309 }
310 else {
311 SWIG_Py_INCREF(Py_None);
312 $result = SWIG_AppendOutput($result, Py_None);
313 if (*$2 != NULL)
314 free(*$2);
315 }
316 }
317
318 // Turn off default exception handling
319 %noexception;
320
321 // Add __str__ method to CameraText
322 %feature("python:slot", "tp_str", functype="reprfunc") CameraText::__str__;
323 %extend CameraText {
324 char *__str__() {
325 return $self->text;
326 }
327 };
328
329 // Don't wrap deprecated functions
330 %ignore gp_camera_free;
331
332 // Ignore functions called by camera drivers
333 %ignore gp_camera_set_timeout_funcs;
334 %ignore gp_camera_start_timeout;
335 %ignore gp_camera_stop_timeout;
336
337 // These structures are private
338 %ignore _CameraFunctions;
339
340 // Use library functions to access these
341 %ignore _Camera::pl;
342 %ignore _Camera::pc;
343 %ignore _Camera::port;
344 %ignore _Camera::fs;
345 %ignore _Camera::functions;
346
347 // Other structures are read only
348 %immutable;
349
350 %include "gphoto2/gphoto2-camera.h"