File: obs-scripting-python.h

package info (click to toggle)
obs-studio 22.0.3%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 23,052 kB
  • sloc: ansic: 134,708; cpp: 49,169; objc: 1,036; makefile: 829; sh: 410; python: 360
file content (244 lines) | stat: -rw-r--r-- 6,502 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/******************************************************************************
    Copyright (C) 2015 by Andrew Skinner <obs@theandyroid.com>
    Copyright (C) 2017 by Hugh Bailey <jim@obsproject.com>

    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, see <http://www.gnu.org/licenses/>.
******************************************************************************/

#pragma once

/* ---------------------------- */

#define SWIG_TYPE_TABLE obspython
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4100)
#pragma warning(disable : 4115)
#pragma warning(disable : 4204)
#endif

#include "obs-scripting-python-import.h"

#include <structmember.h>
#include "swig/swigpyrun.h"

#ifdef _MSC_VER
#pragma warning(pop)
#endif

/* ---------------------------- */

#include "obs-scripting-internal.h"
#include "obs-scripting-callback.h"

#ifdef _WIN32
#define __func__ __FUNCTION__
#else
#include <dlfcn.h>
#endif

#include <callback/calldata.h>
#include <util/threading.h>
#include <util/base.h>

#define do_log(level, format, ...) \
	blog(level, "[Python] " format, ##__VA_ARGS__)

#define warn(format, ...)  do_log(LOG_WARNING, format, ##__VA_ARGS__)
#define info(format, ...)  do_log(LOG_INFO,    format, ##__VA_ARGS__)
#define debug(format, ...) do_log(LOG_DEBUG,   format, ##__VA_ARGS__)

/* ------------------------------------------------------------ */

struct python_obs_callback;

struct obs_python_script {
	obs_script_t base;

	struct dstr dir;
	struct dstr name;

	PyObject *module;

	PyObject *save;
	PyObject *update;
	PyObject *get_properties;

	struct script_callback *first_callback;

	PyObject *tick;
	struct obs_python_script *next_tick;
	struct obs_python_script **p_prev_next_tick;
};

/* ------------------------------------------------------------ */

struct python_obs_callback {
	struct script_callback base;

	PyObject *func;
};

static inline struct python_obs_callback *add_python_obs_callback_extra(
		struct obs_python_script *script,
		PyObject *func,
		size_t extra_size)
{
	struct python_obs_callback *cb = add_script_callback(
			&script->first_callback,
			(obs_script_t *)script,
			sizeof(*cb) + extra_size);

	Py_XINCREF(func);
	cb->func = func;
	return cb;
}

static inline struct python_obs_callback *add_python_obs_callback(
		struct obs_python_script *script,
		PyObject *func)
{
	return add_python_obs_callback_extra(script, func, 0);
}

static inline void *python_obs_callback_extra_data(
		struct python_obs_callback *cb)
{
	return (void*)&cb[1];
}

static inline struct obs_python_script *python_obs_callback_script(
		struct python_obs_callback *cb)
{
	return (struct obs_python_script *)cb->base.script;
}

static inline struct python_obs_callback *find_next_python_obs_callback(
		struct obs_python_script *script,
		struct python_obs_callback *cb, PyObject *func)
{
	cb = cb ? (struct python_obs_callback *)cb->base.next
		: (struct python_obs_callback *)script->first_callback;

	while (cb) {
		if (cb->func == func)
			break;
		cb = (struct python_obs_callback *)cb->base.next;
	}

	return cb;
}

static inline struct python_obs_callback *find_python_obs_callback(
		struct obs_python_script *script,
		PyObject *func)
{
	return find_next_python_obs_callback(script, NULL, func);
}

static inline void remove_python_obs_callback(struct python_obs_callback *cb)
{
	remove_script_callback(&cb->base);

	Py_XDECREF(cb->func);
	cb->func = NULL;
}

static inline void just_free_python_obs_callback(struct python_obs_callback *cb)
{
	just_free_script_callback(&cb->base);
}

static inline void free_python_obs_callback(struct python_obs_callback *cb)
{
	free_script_callback(&cb->base);
}

/* ------------------------------------------------------------ */

static int parse_args_(PyObject *args, const char *func, const char *format, ...)
{
	char new_format[128];
	va_list va_args;
	int ret;

	snprintf(new_format, sizeof(new_format), "%s:%s", format, func);

	va_start(va_args, format);
	ret = PyArg_VaParse(args, new_format, va_args);
	va_end(va_args);

	return ret;
}

#define parse_args(args, format, ...) \
	parse_args_(args, __FUNCTION__, format, ##__VA_ARGS__)

static inline bool py_error_(const char *func, int line)
{
	if (PyErr_Occurred()) {
		warn("Python failure in %s:%d:", func, line);
		PyErr_Print();
		return true;
	}
	return false;
}

#define py_error() py_error_(__FUNCTION__, __LINE__)

#define lock_python() \
	PyGILState_STATE gstate = PyGILState_Ensure()
#define unlock_python() \
	PyGILState_Release(gstate)

struct py_source;
typedef struct py_source py_source_t;

extern PyObject* py_libobs;
extern struct python_obs_callback *cur_python_cb;
extern struct obs_python_script *cur_python_script;

extern void py_to_obs_source_info(py_source_t *py_info);
extern PyObject *py_obs_register_source(PyObject *self, PyObject *args);
extern PyObject *py_obs_get_script_config_path(PyObject *self, PyObject *args);
extern void add_functions_to_py_module(PyObject *module,
		PyMethodDef *method_list);

/* ------------------------------------------------------------ */
/* Warning: the following functions expect python to be locked! */

extern bool py_to_libobs_(const char *type,
                          PyObject *  py_in,
                          void *      libobs_out,
                          const char *id,
                          const char *func,
                          int         line);

extern bool libobs_to_py_(const char *type,
                          void *      libobs_in,
                          bool        ownership,
                          PyObject ** py_out,
                          const char *id,
                          const char *func,
                          int         line);

extern bool py_call(PyObject *call, PyObject **ret, const char *arg_def, ...);
extern bool py_import_script(const char *name);

static inline PyObject *python_none(void)
{
	PyObject *none = Py_None;
	Py_INCREF(none);
	return none;
}