linphone_module.mustache 12.5 KB
Newer Older
Ghislain MARY's avatar
Ghislain MARY committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
Copyright (C) 2014 Belledonne Communications SARL

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
16
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
Ghislain MARY's avatar
Ghislain MARY committed
17 18
*/

19
#include <Python.h>
20
#include <structmember.h>
21
#include <datetime.h>
Ghislain MARY's avatar
Ghislain MARY committed
22 23 24 25 26
#include <linphone/core.h>
#include <linphone/tunnel.h>
#include <linphone/core_utils.h>
#include <linphone/wrapper_utils.h>
#include <belle-sip/belle-sip.h>
27 28
#include <stdarg.h>

29 30
#include "gitversion.h"

31
#ifdef _WIN32
32 33
#include <windows.h>
#endif
34 35 36 37 38 39

#ifdef _MSC_VER
#define PYLINPHONE_INLINE __inline
#else
#define PYLINPHONE_INLINE inline
#endif
40

41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
/**
 * Definitions for Python 2 and 3 support.
 */

#ifndef PyVarObject_HEAD_INIT
#define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size,
#endif

#ifndef PyInt_Check
#define PyInt_Check(p) PyLong_Check(p)
#endif

#ifndef PyInt_AsLong
#define PyInt_AsLong(io) PyLong_AsLong(io)
#endif

#ifndef PyInt_AS_LONG
static int pylinphone_aslongoverflow;
#define PyInt_AS_LONG(io) PyLong_AsLongAndOverflow(io, &pylinphone_aslongoverflow)
#endif

#ifndef PyInt_AsSsize_t
#define PyInt_AsSsize_t(io) PyLong_AsSsize_t(io)
#endif

#ifndef PyInt_AsUnsignedLongMask
#define PyInt_AsUnsignedLongMask(io) PyLong_AsUnsignedLongMask(io)
#endif

#ifndef PyString_Check
#define PyString_Check(io) PyUnicode_Check(io)
#endif

#ifndef PyString_FromString
#define PyString_FromString(v) PyUnicode_FromString(v)
#endif

#if PY_MAJOR_VERSION >= 3
#define PyString_AsString(s) PyUnicode_AsUTF8(s)
#endif

#if PY_MAJOR_VERSION >= 3
#define MOD_DEF(ob, name, methods, doc) \
	static struct PyModuleDef moduledef_##ob = { \
		PyModuleDef_HEAD_INIT, name, doc, -1, methods, }; \
	ob = PyModule_Create(&moduledef_##ob);
#else
#define MOD_DEF(ob, name, methods, doc) \
	ob = Py_InitModule3(name, methods, doc);
#endif

92

93
static void pylinphone_dispatch_messages(void);
94
static PYLINPHONE_INLINE void pylinphone_trace(int indent, const char *fmt, ...);
95 96


97 98 99 100
{{> handwritten_declarations}}



101 102 103 104
{{#classes}}
static PyTypeObject pylinphone_{{class_name}}Type;
{{/classes}}

105 106 107 108
{{#classes}}

typedef struct {
	PyObject_HEAD
109
	PyObject *user_data;
110
	{{class_cname}} *native_ptr;
111
{{{class_object_members_code}}}
112 113
} pylinphone_{{class_name}}Object;

114 115
{{/classes}}

116 117
{{#classes}}
static {{class_cname}} * pylinphone_{{class_name}}_get_native_ptr(PyObject *self);
Ghislain MARY's avatar
Ghislain MARY committed
118
static PyObject * pylinphone_{{class_name}}_from_native_ptr(PyTypeObject *type, const {{class_cname}} *native_ptr, bool_t take_native_ref);
119 120 121
{{#class_type_hand_written_methods}}
static PyObject * pylinphone_{{class_name}}_class_method_{{method_name}}(PyObject *cls, PyObject *args);
{{/class_type_hand_written_methods}}
122 123 124
{{#class_instance_hand_written_methods}}
static PyObject * pylinphone_{{class_name}}_instance_method_{{method_name}}(PyObject *self, PyObject *args);
{{/class_instance_hand_written_methods}}
125 126
{{/classes}}

127 128
{{#bctbxlist_types}}
PyObject * PyList_FromBctbxListOf{{c_contained_type}}(const bctbx_list_t *msl) {
129 130 131
	PyObject *pyl = PyList_New(0);
	while (msl != NULL) {
		{{c_contained_type}} *native_ptr = ({{c_contained_type}} *)msl->data;
Ghislain MARY's avatar
Ghislain MARY committed
132
		PyObject *item = pylinphone_{{python_contained_type}}_from_native_ptr(&pylinphone_{{python_contained_type}}Type, native_ptr, TRUE);
133
		PyList_Append(pyl, item);
134
		msl = bctbx_list_next(msl);
135 136 137 138
	}
	return pyl;
}

139
bctbx_list_t * PyList_AsBctbxListOf{{c_contained_type}}(PyObject *pyl) {
140
	bctbx_list_t *msl = NULL;
141 142 143 144 145
	Py_ssize_t idx;
	Py_ssize_t size = PyList_Size(pyl);
	for (idx = 0; idx < size; idx++) {
		PyObject *item = PyList_GetItem(pyl, idx);
		{{c_contained_type}} *native_ptr = pylinphone_{{python_contained_type}}_get_native_ptr(item);
146
		msl = bctbx_list_append(msl, native_ptr);
147 148 149 150
	}
	return msl;
}

151
{{/bctbxlist_types}}
152

153
{{#core_events}}
154
{{{event_callback_definition}}}
155 156 157 158 159 160 161
{{/core_events}}

{{#classes}}
{{#class_events}}
{{{event_callback_definition}}}
{{/class_events}}
{{/classes}}
162

163 164
{{#classes}}

165 166
static {{class_cname}} * pylinphone_{{class_name}}_get_native_ptr(PyObject *self) {
	return ((pylinphone_{{class_name}}Object *)self)->native_ptr;
Ghislain MARY's avatar
Ghislain MARY committed
167 168
}

Ghislain MARY's avatar
Ghislain MARY committed
169
static PyObject * pylinphone_{{class_name}}_from_native_ptr(PyTypeObject *type, const {{class_cname}} *native_ptr, bool_t take_native_ref) {
170
{{{from_native_pointer_body}}}
171 172
}

173
static PyObject * pylinphone_{{class_name}}_new(PyTypeObject *type, PyObject *args, PyObject *kw) {
174
{{{new_body}}}
175 176
}

177 178 179 180
static int pylinphone_{{class_name}}_init(PyObject *self, PyObject *args, PyObject *kw) {
{{{init_body}}}
}

181
{{{dealloc_definition}}}
182 183 184

{{#class_type_methods}}

185
static PyObject * pylinphone_{{class_name}}_class_method_{{method_name}}(PyObject *cls, PyObject *args) {
186
{{{method_body}}}
187 188 189 190 191 192 193
}

{{/class_type_methods}}

{{#class_instance_methods}}

static PyObject * pylinphone_{{class_name}}_instance_method_{{method_name}}(PyObject *self, PyObject *args) {
194
{{{method_body}}}
195 196 197 198
}

{{/class_instance_methods}}

199
static PyMethodDef pylinphone_{{class_name}}_methods[] = {
200
	/* Class methods */
201
{{#class_type_hand_written_methods}}
202
	{ "{{method_name}}", pylinphone_{{class_name}}_class_method_{{method_name}}, METH_VARARGS | METH_CLASS, "{{{method_doc}}}" },
203
{{/class_type_hand_written_methods}}
204
{{#class_type_methods}}
205
	{ "{{method_name}}", pylinphone_{{class_name}}_class_method_{{method_name}}, METH_VARARGS | METH_CLASS, "{{{method_doc}}}" },
206 207
{{/class_type_methods}}
	/* Instance methods */
208
{{#class_instance_hand_written_methods}}
209
	{ "{{method_name}}", pylinphone_{{class_name}}_instance_method_{{method_name}}, METH_VARARGS, "{{{method_doc}}}" },
210
{{/class_instance_hand_written_methods}}
211
{{#class_instance_methods}}
212
	{ "{{method_name}}", pylinphone_{{class_name}}_instance_method_{{method_name}}, METH_VARARGS, "{{{method_doc}}}" },
213
{{/class_instance_methods}}
214 215
	/* Sentinel */
	{ NULL, NULL, 0, NULL }
216 217
};

218 219 220 221 222
static PyMemberDef pylinphone_{{class_name}}_members[] = {
	{ "user_data", T_OBJECT, offsetof(pylinphone_{{class_name}}Object, user_data), 0, "A place to store some user data." },
	{ NULL, 0, 0, 0, NULL }	/* Sentinel */
};

223 224
{{#class_properties}}

225
{{{getter_definition_begin}}}
Ghislain MARY's avatar
Ghislain MARY committed
226
{{{getter_body}}}
227
{{{getter_definition_end}}}
228

229
{{{setter_definition_begin}}}
Ghislain MARY's avatar
Ghislain MARY committed
230
{{{setter_body}}}
231
{{{setter_definition_end}}}
232 233 234 235

{{/class_properties}}

static PyGetSetDef pylinphone_{{class_name}}_getseters[] = {
236
{{#class_hand_written_properties}}
237
	{ "{{property_name}}", {{getter_reference}}, {{setter_reference}}, "{{{property_doc}}}" },
238
{{/class_hand_written_properties}}
239
{{#class_properties}}
240
	{ "{{property_name}}", {{getter_reference}}, {{setter_reference}}, "{{{property_doc}}}" },
241
{{/class_properties}}
242 243
	/* Sentinel */
	{ NULL, NULL, NULL, NULL, NULL }
244 245 246
};

static PyTypeObject pylinphone_{{class_name}}Type = {
247
	PyVarObject_HEAD_INIT(NULL, 0)
248
	"linphone.{{class_name}}",	/* tp_name */
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
	sizeof(pylinphone_{{class_name}}Object),	/* tp_basicsize */
	0,	/* tp_itemsize */
	pylinphone_{{class_name}}_dealloc,	/* tp_dealloc */
	0,	/* tp_print */
	0,	/* tp_getattr */
	0,	/* tp_setattr */
	0,	/* tp_compare */
	0,	/* tp_repr */
	0,	/* tp_as_number */
	0,	/* tp_as_sequence */
	0,	/* tp_as_mapping */
	0,	/* tp_hash */
	0,	/* tp_call */
	0,	/* tp_str */
	0,	/* tp_getattro */
	0,	/* tp_setattro */
	0,	/* tp_as_buffer */
	Py_TPFLAGS_DEFAULT,	/* tp_flags */
267
	"{{{class_doc}}}",	/* tp_doc */
268 269 270 271 272 273
	0,	/* tp_traverse */
	0,	/* tp_clear */
	0,	/* tp_richcompare */
	0,	/* tp_weaklistoffset */
	0,	/* tp_iter */
	0,	/* tp_iternext */
274
	pylinphone_{{class_name}}_methods,	/* tp_methods */
275
	pylinphone_{{class_name}}_members,	/* tp_members */
276 277 278 279 280 281
	pylinphone_{{class_name}}_getseters,	/* tp_getset */
	0,	/* tp_base */
	0,	/* tp_dict */
	0,	/* tp_descr_get */
	0,	/* tp_descr_set */
	0,	/* tp_dictoffset */
282
	pylinphone_{{class_name}}_init,	/* tp_init */
283 284 285 286 287 288 289
	0,	/* tp_alloc */
	pylinphone_{{class_name}}_new,	/* tp_new */
	0,	/* tp_free */
};

{{/classes}}

290

291
{{> handwritten_definitions}}
292 293


294
static PyMethodDef pylinphone_ModuleMethods[] = {
295
	{ "set_log_handler", pylinphone_module_method_set_log_handler, METH_VARARGS, "" },
296
	/* Sentinel */
297
	{ NULL, NULL, 0, NULL }
298 299
};

300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
{{#enums}}
static PyObject * pylinphone_{{enum_name}}_module_method_string(PyObject *self, PyObject *args) {
	const char *value_str = "[invalid]";
	int value;
	PyObject *pyret;
	if (!PyArg_ParseTuple(args, "i", &value)) {
		return NULL;
	}
	pylinphone_trace(1, "[PYLINPHONE] >>> %s(%d)", __FUNCTION__, value);
	switch (value) {
{{#enum_values}}
		case {{enum_value_cname}}:
			value_str = "{{enum_value_name}}";
			break;
{{/enum_values}}
		default:
			break;
	}
	pyret = Py_BuildValue("z", value_str);
	pylinphone_trace(-1, "[PYLINPHONE] <<< %s -> %p", __FUNCTION__, pyret);
	return pyret;
}

static PyMethodDef pylinphone_{{enum_name}}_ModuleMethods[] = {
324
	{ "string", pylinphone_{{enum_name}}_module_method_string, METH_VARARGS, "Get a string representation of a linphone.{{enum_name}} value." },
325 326
	/* Sentinel */
	{ NULL, NULL, 0, NULL }
327 328
};

329 330
{{/enums}}

Ghislain MARY's avatar
Ghislain MARY committed
331 332
{{> linphone_testing_module}}

333
static PyObject * pylinphone_moduleinit(void) {
334
	PyObject *m;
335
	PyObject *menum_PayloadType;
336

337
	PyDateTime_IMPORT;
338
	PyEval_InitThreads();
339
	pylinphone_init_logging();
340

341
{{#classes}}
342
	if (PyType_Ready(&pylinphone_{{class_name}}Type) < 0) return NULL;
343 344
{{/classes}}

345
	/* Hand-written classes. */
346 347
	if (PyType_Ready(&pylinphone_VideoSizeType) < 0) return NULL;
	if (PyType_Ready(&pylinphone_SipTransportsType) < 0) return NULL;
348

349 350 351
	MOD_DEF(m, "linphone", pylinphone_ModuleMethods, "Python module giving access to the Linphone library.");
	if (m == NULL) return NULL;
	if (PyModule_AddStringConstant(m, "__version__", LINPHONE_GIT_REVISION) < 0) return NULL;
352 353

{{#enums}}
354 355 356 357 358 359
	{
		PyObject *menum_{{enum_name}};
		MOD_DEF(menum_{{enum_name}}, "{{enum_name}}", pylinphone_{{enum_name}}_ModuleMethods, "{{{enum_doc}}}");
		if (menum_{{enum_name}} == NULL) return NULL;
		Py_INCREF(menum_{{enum_name}});
		if (PyModule_AddObject(m, "{{enum_name}}", menum_{{enum_name}}) < 0) return NULL;
360
{{#enum_values}}
361
		if (PyModule_AddIntConstant(menum_{{enum_name}}, "{{enum_value_name}}", {{enum_value_cname}}) < 0) return NULL;
362
{{/enum_values}}
363
{{#enum_deprecated_values}}
364
		if (PyModule_AddIntConstant(menum_{{enum_name}}, "{{enum_value_name}}", {{enum_value_cname}}) < 0) return NULL;
365
{{/enum_deprecated_values}}
366
	}
367 368
{{/enums}}

369 370 371 372 373 374 375 376 377
	MOD_DEF(menum_PayloadType, "PayloadTypeType", pylinphone_PayloadTypeType_ModuleMethods, "Type of :py:class:`linphone.PayloadType`.\n\n.. csv-table::\n   :delim: |\n   :header: Value,Description\n\n   AudioContinuous|\n   AudioPacketized|\n   Video|\n   Text|\n   Other|\n");
	if (menum_PayloadType == NULL) return NULL;
	Py_INCREF(menum_PayloadType);
	if (PyModule_AddObject(m, "PayloadTypeType", menum_PayloadType) < 0) return NULL;
	if (PyModule_AddIntConstant(menum_PayloadType, "AudioContinuous", PAYLOAD_AUDIO_CONTINUOUS) < 0) return NULL;
	if (PyModule_AddIntConstant(menum_PayloadType, "AudioPacketized", PAYLOAD_AUDIO_PACKETIZED) < 0) return NULL;
	if (PyModule_AddIntConstant(menum_PayloadType, "Video", PAYLOAD_VIDEO) < 0) return NULL;
	if (PyModule_AddIntConstant(menum_PayloadType, "Text", PAYLOAD_TEXT) < 0) return NULL;
	if (PyModule_AddIntConstant(menum_PayloadType, "Other", PAYLOAD_OTHER) < 0) return NULL;
378
	// TODO: To remove. Deprecated enum values.
379 380 381 382 383
	if (PyModule_AddIntConstant(menum_PayloadType, "PAYLOAD_AUDIO_CONTINUOUS", PAYLOAD_AUDIO_CONTINUOUS) < 0) return NULL;
	if (PyModule_AddIntConstant(menum_PayloadType, "PAYLOAD_AUDIO_PACKETIZED", PAYLOAD_AUDIO_PACKETIZED) < 0) return NULL;
	if (PyModule_AddIntConstant(menum_PayloadType, "PAYLOAD_VIDEO", PAYLOAD_VIDEO) < 0) return NULL;
	if (PyModule_AddIntConstant(menum_PayloadType, "PAYLOAD_TEXT", PAYLOAD_TEXT) < 0) return NULL;
	if (PyModule_AddIntConstant(menum_PayloadType, "PAYLOAD_OTHER", PAYLOAD_OTHER) < 0) return NULL;
384

385 386 387 388
{{#classes}}
	Py_INCREF(&pylinphone_{{class_name}}Type);
	PyModule_AddObject(m, "{{class_name}}", (PyObject *)&pylinphone_{{class_name}}Type);
{{/classes}}
389 390 391 392

	/* Hand-written classes. */
	Py_INCREF(&pylinphone_VideoSizeType);
	PyModule_AddObject(m, "VideoSize", (PyObject *)&pylinphone_VideoSizeType);
393 394
	Py_INCREF(&pylinphone_SipTransportsType);
	PyModule_AddObject(m, "SipTransports", (PyObject *)&pylinphone_SipTransportsType);
Ghislain MARY's avatar
Ghislain MARY committed
395 396

	pylinphone_init_testing_module(m);
397 398 399 400 401 402 403

	return m;
}

#if PY_MAJOR_VERSION < 3
PyMODINIT_FUNC initlinphone(void) {
	pylinphone_moduleinit();
404
}
405 406 407 408 409
#else
PyMODINIT_FUNC PyInit_linphone(void) {
	return pylinphone_moduleinit();
}
#endif