| 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") context |
| 21 |
|
| 22 |
%include "common/preamble.i" |
| 23 |
|
| 24 |
%rename(GPContext) _GPContext; |
| 25 |
%pythoncode %{ |
| 26 |
Context = GPContext |
| 27 |
%} |
| 28 |
|
| 29 |
// Make docstring parameter types more Pythonic |
| 30 |
%typemap(doc) GPContext * "$1_name: gphoto2.$*1_type"; |
| 31 |
|
| 32 |
// Ignore "backend" functions |
| 33 |
%ignore gp_context_cancel; |
| 34 |
%ignore gp_context_error; |
| 35 |
%ignore gp_context_idle; |
| 36 |
%ignore gp_context_message; |
| 37 |
%ignore gp_context_progress_start; |
| 38 |
%ignore gp_context_progress_stop; |
| 39 |
%ignore gp_context_progress_update; |
| 40 |
%ignore gp_context_question; |
| 41 |
%ignore gp_context_ref; |
| 42 |
%ignore gp_context_status; |
| 43 |
%ignore gp_context_unref; |
| 44 |
|
| 45 |
#ifndef SWIGIMPORTED |
| 46 |
|
| 47 |
// Structs to store callback details |
| 48 |
%ignore CallbackDetails::context; |
| 49 |
%ignore CallbackDetails::func_1; |
| 50 |
%ignore CallbackDetails::func_2; |
| 51 |
%ignore CallbackDetails::func_3; |
| 52 |
%ignore CallbackDetails::data; |
| 53 |
%ignore CallbackDetails::remove; |
| 54 |
%ignore del_CallbackDetails; |
| 55 |
|
| 56 |
%inline %{ |
| 57 |
typedef void (*RemoveFunc) (GPContext *context, void *func, void *data); |
| 58 |
|
| 59 |
typedef struct CallbackDetails { |
| 60 |
GPContext *context; |
| 61 |
PyObject *func_1; |
| 62 |
PyObject *func_2; |
| 63 |
PyObject *func_3; |
| 64 |
PyObject *data; |
| 65 |
RemoveFunc remove; |
| 66 |
} CallbackDetails; |
| 67 |
|
| 68 |
// Function to remove progress callbacks, compatible with RemoveFunc |
| 69 |
static void unset_progress_funcs(GPContext *context, |
| 70 |
GPContextProgressStartFunc start_func, |
| 71 |
void *data) { |
| 72 |
gp_context_set_progress_funcs(context, NULL, NULL, NULL, NULL); |
| 73 |
}; |
| 74 |
|
| 75 |
// Destructor |
| 76 |
static int del_CallbackDetails(struct CallbackDetails *this) { |
| 77 |
if (this->context && this->remove) { |
| 78 |
this->remove(this->context, NULL, NULL); |
| 79 |
gp_context_unref(this->context); |
| 80 |
} |
| 81 |
SWIG_Py_XDECREF(this->func_1); |
| 82 |
SWIG_Py_XDECREF(this->func_2); |
| 83 |
SWIG_Py_XDECREF(this->func_3); |
| 84 |
SWIG_Py_XDECREF(this->data); |
| 85 |
free(this); |
| 86 |
return GP_OK; |
| 87 |
}; |
| 88 |
%} |
| 89 |
DEFAULT_DTOR(CallbackDetails, del_CallbackDetails); |
| 90 |
|
| 91 |
// Define wrapper functions to call Python callbacks from C callbacks |
| 92 |
%define CB_WRAPPER(rtn_type, cb_name, cb_args, func_arglist, function) |
| 93 |
%{ |
| 94 |
static rtn_type cb_name cb_args { |
| 95 |
PyGILState_STATE gstate = PyGILState_Ensure(); |
| 96 |
CallbackDetails *this = data; |
| 97 |
PyObject *result = NULL; |
| 98 |
PyObject *arglist = NULL; |
| 99 |
PyObject *self = NULL; |
| 100 |
%} |
| 101 |
#if #rtn_type == "int" |
| 102 |
%{ |
| 103 |
rtn_type c_result = 0; |
| 104 |
%} |
| 105 |
#elif #rtn_type == "GPContextFeedback" |
| 106 |
%{ |
| 107 |
rtn_type c_result = GP_CONTEXT_FEEDBACK_OK; |
| 108 |
%} |
| 109 |
#endif |
| 110 |
%{ |
| 111 |
PyObject *py_context = SWIG_NewPointerObj( |
| 112 |
SWIG_as_voidptr(context), SWIGTYPE_p__GPContext, 0); |
| 113 |
arglist = Py_BuildValue func_arglist; |
| 114 |
if (arglist == NULL) { |
| 115 |
PyErr_Print(); |
| 116 |
goto fail; |
| 117 |
} |
| 118 |
result = PyObject_CallObject(function, arglist); |
| 119 |
SWIG_Py_DECREF(arglist); |
| 120 |
if (result == NULL) { |
| 121 |
PyErr_Print(); |
| 122 |
goto fail; |
| 123 |
} |
| 124 |
%} |
| 125 |
#if #rtn_type != "void" |
| 126 |
%{ |
| 127 |
c_result = PyInt_AsLong(result); |
| 128 |
%} |
| 129 |
#endif |
| 130 |
%{ |
| 131 |
SWIG_Py_DECREF(result); |
| 132 |
fail: |
| 133 |
PyGILState_Release(gstate); |
| 134 |
%} |
| 135 |
#if #rtn_type != "void" |
| 136 |
%{ |
| 137 |
return c_result; |
| 138 |
%} |
| 139 |
#endif |
| 140 |
%{ |
| 141 |
}; |
| 142 |
%} |
| 143 |
%enddef // CB_WRAPPER |
| 144 |
|
| 145 |
CB_WRAPPER(void, wrap_idle_func, (GPContext *context, void *data), |
| 146 |
("(OO)", py_context, this->data), this->func_1) |
| 147 |
|
| 148 |
CB_WRAPPER(void, wrap_error_func, |
| 149 |
(GPContext *context, const char *text, void *data), |
| 150 |
("(OyO)", py_context, text, this->data), this->func_1) |
| 151 |
|
| 152 |
CB_WRAPPER(void, wrap_status_func, |
| 153 |
(GPContext *context, const char *text, void *data), |
| 154 |
("(OyO)", py_context, text, this->data), this->func_1) |
| 155 |
|
| 156 |
CB_WRAPPER(void, wrap_message_func, |
| 157 |
(GPContext *context, const char *text, void *data), |
| 158 |
("(OyO)", py_context, text, this->data), this->func_1) |
| 159 |
|
| 160 |
CB_WRAPPER(GPContextFeedback, wrap_question_func, |
| 161 |
(GPContext *context, const char *text, void *data), |
| 162 |
("(OyO)", py_context, text, this->data), this->func_1) |
| 163 |
|
| 164 |
CB_WRAPPER(GPContextFeedback, wrap_cancel_func, |
| 165 |
(GPContext *context, void *data), |
| 166 |
("(OO)", py_context, this->data), this->func_1) |
| 167 |
|
| 168 |
CB_WRAPPER(int, py_progress_start, |
| 169 |
(GPContext *context, float target, const char *text, void *data), |
| 170 |
("(OfyO)", py_context, target, text, this->data), this->func_1) |
| 171 |
|
| 172 |
CB_WRAPPER(void, py_progress_update, |
| 173 |
(GPContext *context, unsigned int id, float current, void *data), |
| 174 |
("(OifO)", py_context, id, current, this->data), this->func_2) |
| 175 |
|
| 176 |
CB_WRAPPER(void, py_progress_stop, |
| 177 |
(GPContext *context, unsigned int id, void *data), |
| 178 |
("(OiO)", py_context, id, this->data), this->func_3) |
| 179 |
|
| 180 |
// Typemaps for all callback setting functions |
| 181 |
%typemap(arginit) void *data (CallbackDetails *_global_callbacks) { |
| 182 |
_global_callbacks = malloc(sizeof(CallbackDetails)); |
| 183 |
if (!_global_callbacks) { |
| 184 |
PyErr_SetNone(PyExc_MemoryError); |
| 185 |
SWIG_fail; |
| 186 |
} |
| 187 |
_global_callbacks->context = NULL; |
| 188 |
_global_callbacks->func_1 = NULL; |
| 189 |
_global_callbacks->func_2 = NULL; |
| 190 |
_global_callbacks->func_3 = NULL; |
| 191 |
_global_callbacks->data = NULL; |
| 192 |
_global_callbacks->remove = NULL; |
| 193 |
} |
| 194 |
%typemap(freearg) void *data { |
| 195 |
if (_global_callbacks) |
| 196 |
del_CallbackDetails(_global_callbacks); |
| 197 |
} |
| 198 |
|
| 199 |
// Assumes the GPContext value is always in arg1 |
| 200 |
%typemap(in) void *data { |
| 201 |
gp_context_ref(arg1); |
| 202 |
_global_callbacks->context = arg1; |
| 203 |
SWIG_Py_INCREF($input); |
| 204 |
_global_callbacks->data = $input; |
| 205 |
$1 = _global_callbacks; |
| 206 |
} |
| 207 |
%typemap(doc) void *data "$1_name: object" |
| 208 |
|
| 209 |
%typemap(argout) void *data { |
| 210 |
$result = SWIG_AppendOutput($result, |
| 211 |
SWIG_NewPointerObj(_global_callbacks, $descriptor(CallbackDetails*), SWIG_POINTER_OWN)); |
| 212 |
_global_callbacks = NULL; |
| 213 |
} |
| 214 |
|
| 215 |
// Macro to define typemaps for the six single callback function variants |
| 216 |
%define SINGLE_CALLBACK_FUNCTION(cb_func_type, remove_func, cb_wrapper) |
| 217 |
|
| 218 |
%typemap(in) cb_func_type { |
| 219 |
if (!PyCallable_Check($input)) { |
| 220 |
%argument_fail(SWIG_TypeError, callable, $symname, $argnum); |
| 221 |
} |
| 222 |
SWIG_Py_INCREF($input); |
| 223 |
_global_callbacks->func_1 = $input; |
| 224 |
_global_callbacks->remove = (RemoveFunc) remove_func; |
| 225 |
$1 = (cb_func_type) cb_wrapper; |
| 226 |
} |
| 227 |
|
| 228 |
%typemap(doc) cb_func_type "$1_name: callable function" |
| 229 |
|
| 230 |
%enddef // SINGLE_CALLBACK_FUNCTION |
| 231 |
|
| 232 |
SINGLE_CALLBACK_FUNCTION(GPContextIdleFunc, |
| 233 |
gp_context_set_idle_func, wrap_idle_func) |
| 234 |
SINGLE_CALLBACK_FUNCTION(GPContextErrorFunc, |
| 235 |
gp_context_set_error_func, wrap_error_func) |
| 236 |
SINGLE_CALLBACK_FUNCTION(GPContextStatusFunc, |
| 237 |
gp_context_set_status_func, wrap_status_func) |
| 238 |
SINGLE_CALLBACK_FUNCTION(GPContextMessageFunc, |
| 239 |
gp_context_set_message_func, wrap_message_func) |
| 240 |
SINGLE_CALLBACK_FUNCTION(GPContextQuestionFunc, |
| 241 |
gp_context_set_question_func, wrap_question_func) |
| 242 |
SINGLE_CALLBACK_FUNCTION(GPContextCancelFunc, |
| 243 |
gp_context_set_cancel_func, wrap_cancel_func) |
| 244 |
|
| 245 |
// Progress callbacks are more complicated |
| 246 |
// Use macro for first function |
| 247 |
SINGLE_CALLBACK_FUNCTION(GPContextProgressStartFunc, |
| 248 |
unset_progress_funcs, py_progress_start) |
| 249 |
|
| 250 |
// Use typemaps for other two functions |
| 251 |
%typemap(in) GPContextProgressUpdateFunc { |
| 252 |
if (!PyCallable_Check($input)) { |
| 253 |
%argument_fail(SWIG_TypeError, callable, $symname, $argnum); |
| 254 |
} |
| 255 |
_global_callbacks->func_2 = $input; |
| 256 |
SWIG_Py_INCREF(_global_callbacks->func_2); |
| 257 |
$1 = (GPContextProgressUpdateFunc) py_progress_update; |
| 258 |
} |
| 259 |
%typemap(in) GPContextProgressStopFunc { |
| 260 |
if (!PyCallable_Check($input)) { |
| 261 |
%argument_fail(SWIG_TypeError, callable, $symname, $argnum); |
| 262 |
} |
| 263 |
_global_callbacks->func_3 = $input; |
| 264 |
SWIG_Py_INCREF(_global_callbacks->func_3); |
| 265 |
$1 = (GPContextProgressStopFunc) py_progress_stop; |
| 266 |
} |
| 267 |
%typemap(doc) GPContextProgressUpdateFunc, GPContextProgressStopFunc |
| 268 |
"$1_name: callable function" |
| 269 |
|
| 270 |
#endif //ifndef SWIGIMPORTED |
| 271 |
|
| 272 |
// Add default constructor and destructor to _GPContext |
| 273 |
struct _GPContext {}; |
| 274 |
%extend _GPContext { |
| 275 |
_GPContext() { |
| 276 |
return gp_context_new(); |
| 277 |
} |
| 278 |
~_GPContext() { |
| 279 |
gp_context_unref($self); |
| 280 |
} |
| 281 |
}; |
| 282 |
%newobject gp_context_new; |
| 283 |
%delobject gp_context_unref; |
| 284 |
|
| 285 |
// Add member methods to _GPContext |
| 286 |
VOID_MEMBER_FUNCTION(_GPContext, |
| 287 |
set_idle_func, (GPContextIdleFunc func, void *data), |
| 288 |
gp_context_set_idle_func, ($self, func, data)) |
| 289 |
VOID_MEMBER_FUNCTION(_GPContext, |
| 290 |
set_error_func, (GPContextErrorFunc func, void *data), |
| 291 |
gp_context_set_error_func, ($self, func, data)) |
| 292 |
VOID_MEMBER_FUNCTION(_GPContext, |
| 293 |
set_message_func, (GPContextMessageFunc func, void *data), |
| 294 |
gp_context_set_message_func, ($self, func, data)) |
| 295 |
VOID_MEMBER_FUNCTION(_GPContext, |
| 296 |
set_question_func, (GPContextQuestionFunc func, void *data), |
| 297 |
gp_context_set_question_func, ($self, func, data)) |
| 298 |
VOID_MEMBER_FUNCTION(_GPContext, |
| 299 |
set_cancel_func, (GPContextCancelFunc func, void *data), |
| 300 |
gp_context_set_cancel_func, ($self, func, data)) |
| 301 |
VOID_MEMBER_FUNCTION(_GPContext, |
| 302 |
set_progress_funcs, (GPContextProgressStartFunc start_func, |
| 303 |
GPContextProgressUpdateFunc update_func, |
| 304 |
GPContextProgressStopFunc stop_func, |
| 305 |
void *data), |
| 306 |
gp_context_set_progress_funcs, ($self, start_func, update_func, stop_func, data)) |
| 307 |
VOID_MEMBER_FUNCTION(_GPContext, |
| 308 |
set_status_func, (GPContextStatusFunc func, void *data), |
| 309 |
gp_context_set_status_func, ($self, func, data)) |
| 310 |
|
| 311 |
%include "gphoto2/gphoto2-context.h" |