123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725 |
- /******************************************************************************
- Copyright (C) 2015 by Andrew Skinner <[email protected]>
- Copyright (C) 2017 by Hugh Bailey <[email protected]>
- 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/>.
- ******************************************************************************/
- #include "obs-scripting-python.h"
- #include "obs-scripting-config.h"
- #include <util/base.h>
- #include <util/platform.h>
- #include <util/darray.h>
- #include <util/dstr.h>
- #include <obs.h>
- /* ========================================================================= */
- // #define DEBUG_PYTHON_STARTUP
- static const char *startup_script = "\n\
- import sys\n\
- import os\n\
- import obspython\n\
- class stdout_logger(object):\n\
- def write(self, message):\n\
- obspython.script_log_no_endl(obspython.LOG_INFO, message)\n\
- def flush(self):\n\
- pass\n\
- class stderr_logger(object):\n\
- def write(self, message):\n\
- obspython.script_log_no_endl(obspython.LOG_ERROR, message)\n\
- def flush(self):\n\
- pass\n\
- os.environ['PYTHONUNBUFFERED'] = '1'\n\
- sys.stdout = stdout_logger()\n\
- sys.stderr = stderr_logger()\n";
- #if RUNTIME_LINK
- static wchar_t home_path[1024] = {0};
- #endif
- DARRAY(char*) python_paths;
- static bool python_loaded = false;
- static pthread_mutex_t tick_mutex = PTHREAD_MUTEX_INITIALIZER;
- static struct obs_python_script *first_tick_script = NULL;
- static PyObject *py_obspython = NULL;
- struct obs_python_script *cur_python_script = NULL;
- struct python_obs_callback *cur_python_cb = NULL;
- /* -------------------------------------------- */
- bool py_to_libobs_(const char *type,
- PyObject * py_in,
- void * libobs_out,
- const char *id,
- const char *func,
- int line)
- {
- swig_type_info *info = SWIG_TypeQuery(type);
- if (info == NULL) {
- warn("%s:%d: SWIG could not find type: %s%s%s",
- func,
- line,
- id ? id : "",
- id ? "::" : "",
- type);
- return false;
- }
- int ret = SWIG_ConvertPtr(py_in, libobs_out, info, 0);
- if (!SWIG_IsOK(ret)) {
- warn("%s:%d: SWIG failed to convert python object to obs "
- "object: %s%s%s",
- func,
- line,
- id ? id : "",
- id ? "::" : "",
- type);
- return false;
- }
- return true;
- }
- bool libobs_to_py_(const char *type,
- void * libobs_in,
- bool ownership,
- PyObject ** py_out,
- const char *id,
- const char *func,
- int line)
- {
- swig_type_info *info = SWIG_TypeQuery(type);
- if (info == NULL) {
- warn("%s:%d: SWIG could not find type: %s%s%s",
- func,
- line,
- id ? id : "",
- id ? "::" : "",
- type);
- return false;
- }
- *py_out = SWIG_NewPointerObj(libobs_in, info, (int)ownership);
- if (*py_out == Py_None) {
- warn("%s:%d: SWIG failed to convert obs object to python "
- "object: %s%s%s",
- func,
- line,
- id ? id : "",
- id ? "::" : "",
- type);
- return false;
- }
- return true;
- }
- #define libobs_to_py(type, obs_obj, ownership, py_obj) \
- libobs_to_py_(#type " *", obs_obj, ownership, py_obj, \
- NULL, __func__, __LINE__)
- #define py_to_libobs(type, py_obj, libobs_out) \
- py_to_libobs_(#type " *", py_obj, libobs_out, \
- NULL, __func__, __LINE__)
- #define lock_callback(cb) \
- lock_python(); \
- struct obs_python_script *__last_script = cur_python_script; \
- struct python_obs_callback *__last_cb = cur_python_cb; \
- cur_python_script = (struct obs_python_script *)cb->base.script; \
- cur_python_cb = cb
- #define unlock_callback() \
- cur_python_cb = __last_cb; \
- cur_python_script = __last_script; \
- unlock_python()
- /* ========================================================================= */
- void add_functions_to_py_module(PyObject *module, PyMethodDef *method_list)
- {
- PyObject *dict = PyModule_GetDict(module);
- PyObject *name = PyModule_GetNameObject(module);
- if (!dict || !name) {
- return;
- }
- for (PyMethodDef *ml = method_list; ml->ml_name != NULL; ml++) {
- PyObject *func = PyCFunction_NewEx(ml, module, name);
- if (!func) {
- continue;
- }
- PyDict_SetItemString(dict, ml->ml_name, func);
- Py_DECREF(func);
- }
- Py_DECREF(name);
- }
- /* -------------------------------------------- */
- static PyObject *py_get_current_script_path(PyObject *self, PyObject *args)
- {
- UNUSED_PARAMETER(args);
- return PyDict_GetItemString(PyModule_GetDict(self),
- "__script_dir__");
- }
- static void get_defaults(struct obs_python_script *data, PyObject *get_defs)
- {
- PyObject *py_settings;
- if (!libobs_to_py(obs_data_t, data->base.settings, false, &py_settings))
- return;
- PyObject *args = Py_BuildValue("(O)", py_settings);
- PyObject *py_ret = PyObject_CallObject(get_defs, args);
- py_error();
- Py_XDECREF(py_ret);
- Py_XDECREF(args);
- Py_XDECREF(py_settings);
- }
- static bool load_python_script(struct obs_python_script *data)
- {
- PyObject *py_file = NULL;
- PyObject *py_module = NULL;
- PyObject *py_success = NULL;
- PyObject *py_tick = NULL;
- PyObject *py_load = NULL;
- PyObject *py_defaults = NULL;
- bool success = false;
- int ret;
- cur_python_script = data;
- if (!data->module) {
- py_file = PyUnicode_FromString(data->name.array);
- py_module = PyImport_Import(py_file);
- } else {
- py_module = PyImport_ReloadModule(data->module);
- }
- if (!py_module) {
- py_error();
- goto fail;
- }
- Py_XINCREF(py_obspython);
- ret = PyModule_AddObject(py_module, "obspython", py_obspython);
- if (py_error() || ret != 0)
- goto fail;
- ret = PyModule_AddStringConstant(py_module, "__script_dir__",
- data->dir.array);
- if (py_error() || ret != 0)
- goto fail;
- PyObject *py_data = PyCapsule_New(data, NULL, NULL);
- ret = PyModule_AddObject(py_module, "__script_data__", py_data);
- if (py_error() || ret != 0)
- goto fail;
- static PyMethodDef global_funcs[] = {
- {"script_path",
- py_get_current_script_path,
- METH_NOARGS,
- "Gets the script path"},
- {0}
- };
- add_functions_to_py_module(py_module, global_funcs);
- data->update = PyObject_GetAttrString(py_module, "script_update");
- if (!data->update)
- PyErr_Clear();
- data->save = PyObject_GetAttrString(py_module, "script_save");
- if (!data->save)
- PyErr_Clear();
- data->get_properties = PyObject_GetAttrString(py_module,
- "script_properties");
- if (!data->get_properties)
- PyErr_Clear();
- PyObject *func = PyObject_GetAttrString(py_module, "script_defaults");
- if (func) {
- get_defaults(data, func);
- Py_DECREF(func);
- } else {
- PyErr_Clear();
- }
- func = PyObject_GetAttrString(py_module, "script_description");
- if (func) {
- PyObject *py_ret = PyObject_CallObject(func, NULL);
- py_error();
- PyObject *py_desc = PyUnicode_AsUTF8String(py_ret);
- if (py_desc) {
- const char *desc = PyBytes_AS_STRING(py_desc);
- if (desc) dstr_copy(&data->base.desc, desc);
- Py_DECREF(py_desc);
- }
- Py_XDECREF(py_ret);
- Py_DECREF(func);
- } else {
- PyErr_Clear();
- }
- py_tick = PyObject_GetAttrString(py_module, "script_tick");
- if (py_tick) {
- pthread_mutex_lock(&tick_mutex);
- struct obs_python_script *next = first_tick_script;
- data->next_tick = next;
- data->p_prev_next_tick = &first_tick_script;
- if (next) next->p_prev_next_tick = &data->next_tick;
- first_tick_script = data;
- data->tick = py_tick;
- py_tick = NULL;
- pthread_mutex_unlock(&tick_mutex);
- } else {
- PyErr_Clear();
- }
- py_load = PyObject_GetAttrString(py_module, "script_load");
- if (py_load) {
- PyObject *py_s;
- libobs_to_py(obs_data_t, data->base.settings, false, &py_s);
- PyObject *args = Py_BuildValue("(O)", py_s);
- PyObject *py_ret = PyObject_CallObject(py_load, args);
- py_error();
- Py_XDECREF(py_ret);
- Py_XDECREF(args);
- Py_XDECREF(py_s);
- } else {
- PyErr_Clear();
- }
- if (data->module)
- Py_XDECREF(data->module);
- data->module = py_module;
- py_module = NULL;
- success = true;
- fail:
- Py_XDECREF(py_load);
- Py_XDECREF(py_tick);
- Py_XDECREF(py_defaults);
- Py_XDECREF(py_success);
- Py_XDECREF(py_file);
- if (!success)
- Py_XDECREF(py_module);
- cur_python_script = NULL;
- return success;
- }
- static void unload_python_script(struct obs_python_script *data)
- {
- PyObject *py_module = data->module;
- PyObject *py_func = NULL;
- PyObject *py_ret = NULL;
- cur_python_script = data;
- py_func = PyObject_GetAttrString(py_module, "script_unload");
- if (PyErr_Occurred() || !py_func) {
- PyErr_Clear();
- goto fail;
- }
- py_ret = PyObject_CallObject(py_func, NULL);
- if (py_error())
- goto fail;
- fail:
- Py_XDECREF(py_ret);
- Py_XDECREF(py_func);
- cur_python_script = NULL;
- }
- static void add_to_python_path(const char *path)
- {
- PyObject *py_path_str = NULL;
- PyObject *py_path = NULL;
- int ret;
- if (!path || !*path)
- return;
- for (size_t i = 0; i < python_paths.num; i++) {
- const char *python_path = python_paths.array[i];
- if (strcmp(path, python_path) == 0)
- return;
- }
- ret = PyRun_SimpleString("import sys");
- if (py_error() || ret != 0)
- goto fail;
- /* borrowed reference here */
- py_path = PySys_GetObject("path");
- if (py_error() || !py_path)
- goto fail;
- py_path_str = PyUnicode_FromString(path);
- ret = PyList_Append(py_path, py_path_str);
- if (py_error() || ret != 0)
- goto fail;
- char *new_path = bstrdup(path);
- da_push_back(python_paths, &new_path);
- fail:
- Py_XDECREF(py_path_str);
- }
- /* -------------------------------------------- */
- struct python_obs_timer {
- struct python_obs_timer *next;
- struct python_obs_timer **p_prev_next;
- uint64_t last_ts;
- uint64_t interval;
- };
- static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
- static struct python_obs_timer *first_timer = NULL;
- static inline void python_obs_timer_init(struct python_obs_timer *timer)
- {
- pthread_mutex_lock(&timer_mutex);
- struct python_obs_timer *next = first_timer;
- timer->next = next;
- timer->p_prev_next = &first_timer;
- if (next) next->p_prev_next = &timer->next;
- first_timer = timer;
- pthread_mutex_unlock(&timer_mutex);
- }
- static inline void python_obs_timer_remove(struct python_obs_timer *timer)
- {
- struct python_obs_timer *next = timer->next;
- if (next) next->p_prev_next = timer->p_prev_next;
- *timer->p_prev_next = timer->next;
- }
- static inline struct python_obs_callback *python_obs_timer_cb(
- struct python_obs_timer *timer)
- {
- return &((struct python_obs_callback *)timer)[-1];
- }
- static PyObject *timer_remove(PyObject *self, PyObject *args)
- {
- struct obs_python_script *script = cur_python_script;
- PyObject *py_cb;
- UNUSED_PARAMETER(self);
- if (!parse_args(args, "O", &py_cb))
- return python_none();
- struct python_obs_callback *cb = find_python_obs_callback(script, py_cb);
- if (cb) remove_python_obs_callback(cb);
- return python_none();
- }
- static void timer_call(struct script_callback *p_cb)
- {
- struct python_obs_callback *cb = (struct python_obs_callback *)p_cb;
- if (p_cb->removed)
- return;
- lock_callback(cb);
- PyObject *py_ret = PyObject_CallObject(cb->func, NULL);
- py_error();
- Py_XDECREF(py_ret);
- unlock_callback();
- }
- static void defer_timer_init(void *p_cb)
- {
- struct python_obs_callback *cb = p_cb;
- struct python_obs_timer *timer = python_obs_callback_extra_data(cb);
- python_obs_timer_init(timer);
- }
- static PyObject *timer_add(PyObject *self, PyObject *args)
- {
- struct obs_python_script *script = cur_python_script;
- PyObject *py_cb;
- int ms;
- UNUSED_PARAMETER(self);
- if (!parse_args(args, "Oi", &py_cb, &ms))
- return python_none();
- struct python_obs_callback *cb = add_python_obs_callback_extra(
- script, py_cb, sizeof(struct python_obs_timer));
- struct python_obs_timer *timer = python_obs_callback_extra_data(cb);
- timer->interval = (uint64_t)ms * 1000000ULL;
- timer->last_ts = obs_get_video_frame_time();
- defer_call_post(defer_timer_init, cb);
- return python_none();
- }
- /* -------------------------------------------- */
- static void obs_python_tick_callback(void *priv, float seconds)
- {
- struct python_obs_callback *cb = priv;
- if (cb->base.removed) {
- obs_remove_tick_callback(obs_python_tick_callback, cb);
- return;
- }
- lock_callback(cb);
- PyObject *args = Py_BuildValue("(f)", seconds);
- PyObject *py_ret = PyObject_CallObject(cb->func, args);
- py_error();
- Py_XDECREF(py_ret);
- Py_XDECREF(args);
- unlock_callback();
- }
- static PyObject *obs_python_remove_tick_callback(PyObject *self, PyObject *args)
- {
- struct obs_python_script *script = cur_python_script;
- PyObject *py_cb = NULL;
- if (!script) {
- PyErr_SetString(PyExc_RuntimeError,
- "No active script, report this to Jim");
- return NULL;
- }
- UNUSED_PARAMETER(self);
- if (!parse_args(args, "O", &py_cb))
- return python_none();
- if (!py_cb || !PyFunction_Check(py_cb))
- return python_none();
- struct python_obs_callback *cb = find_python_obs_callback(script, py_cb);
- if (cb) remove_python_obs_callback(cb);
- return python_none();
- }
- static PyObject *obs_python_add_tick_callback(PyObject *self, PyObject *args)
- {
- struct obs_python_script *script = cur_python_script;
- PyObject *py_cb = NULL;
- if (!script) {
- PyErr_SetString(PyExc_RuntimeError,
- "No active script, report this to Jim");
- return NULL;
- }
- UNUSED_PARAMETER(self);
- if (!parse_args(args, "O", &py_cb))
- return python_none();
- if (!py_cb || !PyFunction_Check(py_cb))
- return python_none();
- struct python_obs_callback *cb = add_python_obs_callback(script, py_cb);
- obs_add_tick_callback(obs_python_tick_callback, cb);
- return python_none();
- }
- /* -------------------------------------------- */
- static void calldata_signal_callback(void *priv, calldata_t *cd)
- {
- struct python_obs_callback *cb = priv;
- if (cb->base.removed) {
- signal_handler_remove_current();
- return;
- }
- lock_callback(cb);
- PyObject *py_cd;
- if (libobs_to_py(calldata_t, cd, false, &py_cd)) {
- PyObject *args = Py_BuildValue("(O)", py_cd);
- PyObject *py_ret = PyObject_CallObject(cb->func, args);
- py_error();
- Py_XDECREF(py_ret);
- Py_XDECREF(args);
- Py_XDECREF(py_cd);
- }
- unlock_callback();
- }
- static PyObject *obs_python_signal_handler_disconnect(
- PyObject *self, PyObject *args)
- {
- struct obs_python_script *script = cur_python_script;
- PyObject *py_sh = NULL;
- PyObject *py_cb = NULL;
- const char *signal;
- if (!script) {
- PyErr_SetString(PyExc_RuntimeError,
- "No active script, report this to Jim");
- return NULL;
- }
- UNUSED_PARAMETER(self);
- signal_handler_t *handler;
- if (!parse_args(args, "OsO", &py_sh, &signal, &py_cb))
- return python_none();
- if (!py_to_libobs(signal_handler_t, py_sh, &handler))
- return python_none();
- if (!py_cb || !PyFunction_Check(py_cb))
- return python_none();
- struct python_obs_callback *cb = find_python_obs_callback(script, py_cb);
- while (cb) {
- signal_handler_t *cb_handler =
- calldata_ptr(&cb->base.extra, "handler");
- const char *cb_signal =
- calldata_string(&cb->base.extra, "signal");
- if (cb_signal &&
- strcmp(signal, cb_signal) != 0 &&
- handler == cb_handler)
- break;
- cb = find_next_python_obs_callback(script, cb, py_cb);
- }
- if (cb) remove_python_obs_callback(cb);
- return python_none();
- }
- static PyObject *obs_python_signal_handler_connect(
- PyObject *self, PyObject *args)
- {
- struct obs_python_script *script = cur_python_script;
- PyObject *py_sh = NULL;
- PyObject *py_cb = NULL;
- const char *signal;
- if (!script) {
- PyErr_SetString(PyExc_RuntimeError,
- "No active script, report this to Jim");
- return NULL;
- }
- UNUSED_PARAMETER(self);
- signal_handler_t *handler;
- if (!parse_args(args, "OsO", &py_sh, &signal, &py_cb))
- return python_none();
- if (!py_to_libobs(signal_handler_t, py_sh, &handler))
- return python_none();
- if (!py_cb || !PyFunction_Check(py_cb))
- return python_none();
- struct python_obs_callback *cb = add_python_obs_callback(script, py_cb);
- calldata_set_ptr(&cb->base.extra, "handler", handler);
- calldata_set_string(&cb->base.extra, "signal", signal);
- signal_handler_connect(handler, signal, calldata_signal_callback, cb);
- return python_none();
- }
- /* -------------------------------------------- */
- static void calldata_signal_callback_global(void *priv, const char *signal,
- calldata_t *cd)
- {
- struct python_obs_callback *cb = priv;
- if (cb->base.removed) {
- signal_handler_remove_current();
- return;
- }
- lock_callback(cb);
- PyObject *py_cd;
- if (libobs_to_py(calldata_t, cd, false, &py_cd)) {
- PyObject *args = Py_BuildValue("(sO)", signal, py_cd);
- PyObject *py_ret = PyObject_CallObject(cb->func, args);
- py_error();
- Py_XDECREF(py_ret);
- Py_XDECREF(args);
- Py_XDECREF(py_cd);
- }
- unlock_callback();
- }
- static PyObject *obs_python_signal_handler_disconnect_global(
- PyObject *self, PyObject *args)
- {
- struct obs_python_script *script = cur_python_script;
- PyObject *py_sh = NULL;
- PyObject *py_cb = NULL;
- if (!script) {
- PyErr_SetString(PyExc_RuntimeError,
- "No active script, report this to Jim");
- return NULL;
- }
- UNUSED_PARAMETER(self);
- signal_handler_t *handler;
- if (!parse_args(args, "OO", &py_sh, &py_cb))
- return python_none();
- if (!py_to_libobs(signal_handler_t, py_sh, &handler))
- return python_none();
- if (!py_cb || !PyFunction_Check(py_cb))
- return python_none();
- struct python_obs_callback *cb = find_python_obs_callback(script, py_cb);
- while (cb) {
- signal_handler_t *cb_handler =
- calldata_ptr(&cb->base.extra, "handler");
- if (handler == cb_handler)
- break;
- cb = find_next_python_obs_callback(script, cb, py_cb);
- }
- if (cb) remove_python_obs_callback(cb);
- return python_none();
- }
- static PyObject *obs_python_signal_handler_connect_global(
- PyObject *self, PyObject *args)
- {
- struct obs_python_script *script = cur_python_script;
- PyObject *py_sh = NULL;
- PyObject *py_cb = NULL;
- if (!script) {
- PyErr_SetString(PyExc_RuntimeError,
- "No active script, report this to Jim");
- return NULL;
- }
- UNUSED_PARAMETER(self);
- signal_handler_t *handler;
- if (!parse_args(args, "OO", &py_sh, &py_cb))
- return python_none();
- if (!py_to_libobs(signal_handler_t, py_sh, &handler))
- return python_none();
- if (!py_cb || !PyFunction_Check(py_cb))
- return python_none();
- struct python_obs_callback *cb = add_python_obs_callback(script, py_cb);
- calldata_set_ptr(&cb->base.extra, "handler", handler);
- signal_handler_connect_global(handler,
- calldata_signal_callback_global, cb);
- return python_none();
- }
- /* -------------------------------------------- */
- static void defer_hotkey_unregister(void *p_cb)
- {
- obs_hotkey_unregister((obs_hotkey_id)(uintptr_t)p_cb);
- }
- static void on_remove_hotkey(void *p_cb)
- {
- struct python_obs_callback *cb = p_cb;
- obs_hotkey_id id = (obs_hotkey_id)calldata_int(&cb->base.extra, "id");
- if (id != OBS_INVALID_HOTKEY_ID)
- defer_call_post(defer_hotkey_unregister, (void*)(uintptr_t)id);
- }
- static void hotkey_pressed(void *p_cb, bool pressed)
- {
- struct python_obs_callback *cb = p_cb;
- if (cb->base.removed)
- return;
- lock_callback(cb);
- PyObject *py_pressed = PyBool_FromLong(pressed);
- PyObject *args = Py_BuildValue("(O)", py_pressed);
- PyObject *py_ret = PyObject_CallObject(cb->func, args);
- py_error();
- Py_XDECREF(py_ret);
- Py_XDECREF(args);
- Py_XDECREF(py_pressed);
- unlock_callback();
- }
- static void defer_hotkey_pressed(void *p_cb)
- {
- hotkey_pressed(p_cb, true);
- }
- static void defer_hotkey_unpressed(void *p_cb)
- {
- hotkey_pressed(p_cb, false);
- }
- static inline PyObject *py_invalid_hotkey_id()
- {
- return PyLong_FromUnsignedLongLong(OBS_INVALID_HOTKEY_ID);
- }
- static void hotkey_callback(void *p_cb, obs_hotkey_id id,
- obs_hotkey_t *hotkey, bool pressed)
- {
- struct python_obs_callback *cb = p_cb;
- if (cb->base.removed)
- return;
- if (pressed)
- defer_call_post(defer_hotkey_pressed, cb);
- else
- defer_call_post(defer_hotkey_unpressed, cb);
- UNUSED_PARAMETER(hotkey);
- UNUSED_PARAMETER(id);
- }
- static PyObject *hotkey_unregister(PyObject *self, PyObject *args)
- {
- struct obs_python_script *script = cur_python_script;
- PyObject *py_cb = NULL;
- if (!script) {
- PyErr_SetString(PyExc_RuntimeError,
- "No active script, report this to Jim");
- return NULL;
- }
- if (!parse_args(args, "O", &py_cb))
- return python_none();
- if (!py_cb || !PyFunction_Check(py_cb))
- return python_none();
- struct python_obs_callback *cb = find_python_obs_callback(script, py_cb);
- if (cb) remove_python_obs_callback(cb);
- UNUSED_PARAMETER(self);
- return python_none();
- }
- static PyObject *hotkey_register_frontend(PyObject *self, PyObject *args)
- {
- struct obs_python_script *script = cur_python_script;
- const char *name;
- const char *desc;
- obs_hotkey_id id;
- PyObject *py_cb;
- if (!parse_args(args, "ssO", &name, &desc, &py_cb))
- return py_invalid_hotkey_id();
- if (!py_cb || !PyFunction_Check(py_cb))
- return py_invalid_hotkey_id();
- struct python_obs_callback *cb = add_python_obs_callback(script, py_cb);
- cb->base.on_remove = on_remove_hotkey;
- id = obs_hotkey_register_frontend(name, desc, hotkey_callback, cb);
- calldata_set_int(&cb->base.extra, "id", id);
- if (id == OBS_INVALID_HOTKEY_ID)
- remove_python_obs_callback(cb);
- UNUSED_PARAMETER(self);
- return PyLong_FromUnsignedLongLong(id);
- }
- /* -------------------------------------------- */
- static bool button_prop_clicked(obs_properties_t *props, obs_property_t *p,
- void *p_cb)
- {
- struct python_obs_callback *cb = p_cb;
- bool ret = false;
- if (cb->base.removed)
- return false;
- lock_callback(cb);
- PyObject *py_props = NULL;
- PyObject *py_p = NULL;
- if (libobs_to_py(obs_properties_t, props, false, &py_props) &&
- libobs_to_py(obs_property_t, p, false, &py_p)) {
- PyObject *args = Py_BuildValue("(OO)", py_props, py_p);
- PyObject *py_ret = PyObject_CallObject(cb->func, args);
- if (!py_error())
- ret = py_ret == Py_True;
- Py_XDECREF(py_ret);
- Py_XDECREF(args);
- }
- Py_XDECREF(py_p);
- Py_XDECREF(py_props);
- unlock_callback();
- return ret;
- }
- static PyObject *properties_add_button(PyObject *self, PyObject *args)
- {
- struct obs_python_script *script = cur_python_script;
- obs_properties_t *props;
- obs_property_t *p;
- PyObject *py_props;
- PyObject *py_ret;
- const char *name;
- const char *text;
- PyObject *py_cb;
- if (!parse_args(args, "OssO", &py_props, &name, &text, &py_cb))
- return python_none();
- if (!py_to_libobs(obs_properties_t, py_props, &props))
- return python_none();
- if (!py_cb || !PyFunction_Check(py_cb))
- return python_none();
- struct python_obs_callback *cb = add_python_obs_callback(script, py_cb);
- p = obs_properties_add_button2(props, name, text, button_prop_clicked,
- cb);
- if (!p || !libobs_to_py(obs_property_t, p, false, &py_ret))
- return python_none();
- UNUSED_PARAMETER(self);
- return py_ret;
- }
- /* -------------------------------------------- */
- static bool modified_callback(void *p_cb, obs_properties_t *props,
- obs_property_t *p, obs_data_t *settings)
- {
- struct python_obs_callback *cb = p_cb;
- bool ret = false;
- if (cb->base.removed)
- return false;
- lock_callback(cb);
- PyObject *py_props = NULL;
- PyObject *py_p = NULL;
- PyObject *py_settings = NULL;
- if (libobs_to_py(obs_properties_t, props, false, &py_props) &&
- libobs_to_py(obs_property_t, p, false, &py_p) &&
- libobs_to_py(obs_data_t, settings, false, &py_settings)) {
- PyObject *args = Py_BuildValue("(OOO)", py_props, py_p,
- py_settings);
- PyObject *py_ret = PyObject_CallObject(cb->func, args);
- if (!py_error())
- ret = py_ret == Py_True;
- Py_XDECREF(py_ret);
- Py_XDECREF(args);
- }
- Py_XDECREF(py_settings);
- Py_XDECREF(py_p);
- Py_XDECREF(py_props);
- unlock_callback();
- return ret;
- }
- static PyObject *property_set_modified_callback(PyObject *self, PyObject *args)
- {
- struct obs_python_script *script = cur_python_script;
- PyObject *py_p;
- PyObject *py_cb;
- obs_property_t *p;
- if (!parse_args(args, "OO", &py_p, &py_cb))
- return python_none();
- if (!py_to_libobs(obs_property_t, py_p, &p))
- return python_none();
- if (!py_cb || !PyFunction_Check(py_cb))
- return python_none();
- struct python_obs_callback *cb = add_python_obs_callback(script, py_cb);
- obs_property_set_modified_callback2(p, modified_callback, cb);
- UNUSED_PARAMETER(self);
- return python_none();
- }
- /* -------------------------------------------- */
- static PyObject *remove_current_callback(PyObject *self, PyObject *args)
- {
- UNUSED_PARAMETER(self);
- UNUSED_PARAMETER(args);
- if (cur_python_cb)
- remove_python_obs_callback(cur_python_cb);
- return python_none();
- }
- /* -------------------------------------------- */
- static PyObject *calldata_source(PyObject *self, PyObject *args)
- {
- PyObject *py_ret = NULL;
- PyObject *py_cd = NULL;
- calldata_t *cd;
- const char *name;
- UNUSED_PARAMETER(self);
- if (!parse_args(args, "Os", &py_cd, &name))
- goto fail;
- if (!py_to_libobs(calldata_t, py_cd, &cd))
- goto fail;
- obs_source_t *source = calldata_ptr(cd, name);
- libobs_to_py(obs_source_t, source, false, &py_ret);
- fail:
- return py_ret;
- }
- static PyObject *calldata_sceneitem(PyObject *self, PyObject *args)
- {
- PyObject *py_ret = NULL;
- PyObject *py_cd = NULL;
- calldata_t *cd;
- const char *name;
- UNUSED_PARAMETER(self);
- if (!parse_args(args, "Os", &py_cd, &name))
- goto fail;
- if (!py_to_libobs(calldata_t, py_cd, &cd))
- goto fail;
- obs_sceneitem_t *item = calldata_ptr(cd, name);
- libobs_to_py(obs_sceneitem_t, item, false, &py_ret);
- fail:
- return py_ret;
- }
- /* -------------------------------------------- */
- static bool enum_sources_proc(void *param, obs_source_t *source)
- {
- PyObject *list = param;
- PyObject *py_source;
- if (libobs_to_py(obs_source_t, source, false, &py_source)) {
- obs_source_get_ref(source);
- PyList_Append(list, py_source);
- Py_DECREF(py_source);
- }
- return true;
- }
- static PyObject *enum_sources(PyObject *self, PyObject *args)
- {
- UNUSED_PARAMETER(self);
- UNUSED_PARAMETER(args);
- PyObject *list = PyList_New(0);
- obs_enum_sources(enum_sources_proc, list);
- return list;
- }
- /* -------------------------------------------- */
- static bool enum_items_proc(obs_scene_t *scene, obs_sceneitem_t *item,
- void *param)
- {
- PyObject *list = param;
- PyObject *py_item;
- UNUSED_PARAMETER(scene);
- if (libobs_to_py(obs_sceneitem_t, item, false, &py_item)) {
- obs_sceneitem_addref(item);
- PyList_Append(list, py_item);
- Py_DECREF(py_item);
- }
- return true;
- }
- static PyObject *scene_enum_items(PyObject *self, PyObject *args)
- {
- PyObject *py_scene;
- obs_scene_t *scene;
- UNUSED_PARAMETER(self);
- if (!parse_args(args, "O", &py_scene))
- return python_none();
- if (!py_to_libobs(obs_scene_t, py_scene, &scene))
- return python_none();
- PyObject *list = PyList_New(0);
- obs_scene_enum_items(scene, enum_items_proc, list);
- return list;
- }
- /* -------------------------------------------- */
- static PyObject *source_list_release(PyObject *self, PyObject *args)
- {
- PyObject *list;
- if (!parse_args(args, "O", &list))
- return python_none();
- Py_ssize_t count = PyList_Size(list);
- for (Py_ssize_t i = 0; i < count; i++) {
- PyObject *py_source = PyList_GetItem(list, i);
- obs_source_t *source;
- if (py_to_libobs(obs_source_t, py_source, &source)) {
- obs_source_release(source);
- }
- }
- UNUSED_PARAMETER(self);
- return python_none();
- }
- static PyObject *sceneitem_list_release(PyObject *self, PyObject *args)
- {
- PyObject *list;
- if (!parse_args(args, "O", &list))
- return python_none();
- Py_ssize_t count = PyList_Size(list);
- for (Py_ssize_t i = 0; i < count; i++) {
- PyObject *py_item = PyList_GetItem(list, i);
- obs_sceneitem_t *item;
- if (py_to_libobs(obs_sceneitem_t, py_item, &item)) {
- obs_sceneitem_release(item);
- }
- }
- UNUSED_PARAMETER(self);
- return python_none();
- }
- /* -------------------------------------------- */
- struct dstr cur_py_log_chunk = {0};
- static PyObject *py_script_log_internal(PyObject *self, PyObject *args,
- bool add_endl)
- {
- static bool calling_self = false;
- int log_level;
- const char *msg;
- UNUSED_PARAMETER(self);
- if (calling_self)
- return python_none();
- calling_self = true;
- /* ------------------- */
- if (!parse_args(args, "is", &log_level, &msg))
- goto fail;
- if (!msg || !*msg)
- goto fail;
- dstr_cat(&cur_py_log_chunk, msg);
- if (add_endl)
- dstr_cat(&cur_py_log_chunk, "\n");
- const char *start = cur_py_log_chunk.array;
- char *endl = strchr(start, '\n');
- while (endl) {
- *endl = 0;
- if (cur_python_script)
- script_log(&cur_python_script->base, log_level, "%s",
- start);
- else
- script_log(NULL, log_level, "%s", start);
- *endl = '\n';
- start = endl + 1;
- endl = strchr(start, '\n');
- }
- if (start) {
- size_t len = strlen(start);
- if (len) memmove(cur_py_log_chunk.array, start, len);
- dstr_resize(&cur_py_log_chunk, len);
- }
- /* ------------------- */
- fail:
- calling_self = false;
- return python_none();
- }
- static PyObject *py_script_log_no_endl(PyObject *self, PyObject *args)
- {
- return py_script_log_internal(self, args, false);
- }
- static PyObject *py_script_log(PyObject *self, PyObject *args)
- {
- return py_script_log_internal(self, args, true);
- }
- /* -------------------------------------------- */
- static void add_hook_functions(PyObject *module)
- {
- static PyMethodDef funcs[] = {
- #define DEF_FUNC(n, c) {n, c, METH_VARARGS, NULL}
- DEF_FUNC("script_log_no_endl", py_script_log_no_endl),
- DEF_FUNC("script_log", py_script_log),
- DEF_FUNC("timer_remove", timer_remove),
- DEF_FUNC("timer_add", timer_add),
- DEF_FUNC("calldata_source", calldata_source),
- DEF_FUNC("calldata_sceneitem", calldata_sceneitem),
- DEF_FUNC("source_list_release", source_list_release),
- DEF_FUNC("sceneitem_list_release", sceneitem_list_release),
- DEF_FUNC("obs_enum_sources", enum_sources),
- DEF_FUNC("obs_scene_enum_items", scene_enum_items),
- DEF_FUNC("obs_remove_tick_callback",
- obs_python_remove_tick_callback),
- DEF_FUNC("obs_add_tick_callback",
- obs_python_add_tick_callback),
- DEF_FUNC("signal_handler_disconnect",
- obs_python_signal_handler_disconnect),
- DEF_FUNC("signal_handler_connect",
- obs_python_signal_handler_connect),
- DEF_FUNC("signal_handler_disconnect_global",
- obs_python_signal_handler_disconnect_global),
- DEF_FUNC("signal_handler_connect_global",
- obs_python_signal_handler_connect_global),
- DEF_FUNC("obs_hotkey_unregister",
- hotkey_unregister),
- DEF_FUNC("obs_hotkey_register_frontend",
- hotkey_register_frontend),
- DEF_FUNC("obs_properties_add_button",
- properties_add_button),
- DEF_FUNC("obs_property_set_modified_callback",
- property_set_modified_callback),
- DEF_FUNC("remove_current_callback",
- remove_current_callback),
- #undef DEF_FUNC
- {0}
- };
- add_functions_to_py_module(module, funcs);
- }
- /* -------------------------------------------- */
- void obs_python_script_update(obs_script_t *script, obs_data_t *settings);
- bool obs_python_script_load(obs_script_t *s)
- {
- struct obs_python_script *data = (struct obs_python_script *)s;
- if (python_loaded && !data->base.loaded) {
- lock_python();
- if (!data->module)
- add_to_python_path(data->dir.array);
- data->base.loaded = load_python_script(data);
- unlock_python();
- if (data->base.loaded)
- obs_python_script_update(s, NULL);
- }
- return data->base.loaded;
- }
- obs_script_t *obs_python_script_create(const char *path, obs_data_t *settings)
- {
- struct obs_python_script *data = bzalloc(sizeof(*data));
- data->base.type = OBS_SCRIPT_LANG_PYTHON;
- dstr_copy(&data->base.path, path);
- dstr_replace(&data->base.path, "\\", "/");
- path = data->base.path.array;
- const char *slash = path && *path ? strrchr(path, '/') : NULL;
- if (slash) {
- slash++;
- dstr_copy(&data->base.file, slash);
- dstr_left(&data->dir, &data->base.path, slash - path);
- } else {
- dstr_copy(&data->base.file, path);
- }
- path = data->base.file.array;
- dstr_copy_dstr(&data->name, &data->base.file);
- const char *ext = strstr(path, ".py");
- if (ext)
- dstr_resize(&data->name, ext - path);
- data->base.settings = obs_data_create();
- if (settings)
- obs_data_apply(data->base.settings, settings);
- if (!python_loaded)
- return (obs_script_t *)data;
- lock_python();
- add_to_python_path(data->dir.array);
- data->base.loaded = load_python_script(data);
- if (data->base.loaded) {
- cur_python_script = data;
- obs_python_script_update(&data->base, NULL);
- cur_python_script = NULL;
- }
- unlock_python();
- return (obs_script_t *)data;
- }
- void obs_python_script_unload(obs_script_t *s)
- {
- struct obs_python_script *data = (struct obs_python_script *)s;
- if (!s->loaded || !python_loaded)
- return;
- /* ---------------------------- */
- /* unhook tick function */
- if (data->p_prev_next_tick) {
- pthread_mutex_lock(&tick_mutex);
- struct obs_python_script *next = data->next_tick;
- if (next) next->p_prev_next_tick = data->p_prev_next_tick;
- *data->p_prev_next_tick = next;
- pthread_mutex_unlock(&tick_mutex);
- data->p_prev_next_tick = NULL;
- data->next_tick = NULL;
- }
- lock_python();
- Py_XDECREF(data->tick);
- Py_XDECREF(data->save);
- Py_XDECREF(data->update);
- Py_XDECREF(data->get_properties);
- data->tick = NULL;
- data->save = NULL;
- data->update = NULL;
- data->get_properties = NULL;
- /* ---------------------------- */
- /* remove all callbacks */
- struct script_callback *cb = data->first_callback;
- while (cb) {
- struct script_callback *next = cb->next;
- remove_script_callback(cb);
- cb = next;
- }
- /* ---------------------------- */
- /* unload */
- unload_python_script(data);
- unlock_python();
- s->loaded = false;
- }
- void obs_python_script_destroy(obs_script_t *s)
- {
- struct obs_python_script *data = (struct obs_python_script *)s;
- if (data) {
- if (python_loaded) {
- lock_python();
- Py_XDECREF(data->module);
- unlock_python();
- }
- dstr_free(&data->base.path);
- dstr_free(&data->base.file);
- dstr_free(&data->base.desc);
- obs_data_release(data->base.settings);
- dstr_free(&data->dir);
- dstr_free(&data->name);
- bfree(data);
- }
- }
- void obs_python_script_update(obs_script_t *s, obs_data_t *settings)
- {
- struct obs_python_script *data = (struct obs_python_script *)s;
- if (!s->loaded || !python_loaded)
- return;
- if (!data->update)
- return;
- if (settings)
- obs_data_apply(s->settings, settings);
- lock_python();
- cur_python_script = data;
- PyObject *py_settings;
- if (libobs_to_py(obs_data_t, s->settings, false, &py_settings)) {
- PyObject *args = Py_BuildValue("(O)", py_settings);
- PyObject *ret = PyObject_CallObject(data->update, args);
- py_error();
- Py_XDECREF(ret);
- Py_XDECREF(args);
- Py_XDECREF(py_settings);
- }
- cur_python_script = NULL;
- unlock_python();
- }
- obs_properties_t *obs_python_script_get_properties(obs_script_t *s)
- {
- struct obs_python_script *data = (struct obs_python_script *)s;
- obs_properties_t *props = NULL;
- if (!s->loaded || !python_loaded)
- return NULL;
- if (!data->get_properties)
- return NULL;
- lock_python();
- cur_python_script = data;
- PyObject *ret = PyObject_CallObject(data->get_properties, NULL);
- if (!py_error())
- py_to_libobs(obs_properties_t, ret, &props);
- Py_XDECREF(ret);
- cur_python_script = NULL;
- unlock_python();
- return props;
- }
- void obs_python_script_save(obs_script_t *s)
- {
- struct obs_python_script *data = (struct obs_python_script *)s;
- if (!s->loaded || !python_loaded)
- return;
- if (!data->save)
- return;
- lock_python();
- cur_python_script = data;
- PyObject *py_settings;
- if (libobs_to_py(obs_data_t, s->settings, false, &py_settings)) {
- PyObject *args = Py_BuildValue("(O)", py_settings);
- PyObject *ret = PyObject_CallObject(data->save, args);
- py_error();
- Py_XDECREF(ret);
- Py_XDECREF(args);
- Py_XDECREF(py_settings);
- }
- cur_python_script = NULL;
- unlock_python();
- }
- /* -------------------------------------------- */
- static void python_tick(void *param, float seconds)
- {
- struct obs_python_script *data;
- bool valid;
- uint64_t ts = obs_get_video_frame_time();
- pthread_mutex_lock(&tick_mutex);
- valid = !!first_tick_script;
- pthread_mutex_unlock(&tick_mutex);
- /* --------------------------------- */
- /* process script_tick calls */
- if (valid) {
- lock_python();
- PyObject *args = Py_BuildValue("(f)", seconds);
- pthread_mutex_lock(&tick_mutex);
- data = first_tick_script;
- while (data) {
- cur_python_script = data;
- PyObject *py_ret = PyObject_CallObject(data->tick, args);
- Py_XDECREF(py_ret);
- py_error();
- data = data->next_tick;
- }
- cur_python_script = NULL;
- pthread_mutex_unlock(&tick_mutex);
- Py_XDECREF(args);
- unlock_python();
- }
- /* --------------------------------- */
- /* process timers */
- pthread_mutex_lock(&timer_mutex);
- struct python_obs_timer *timer = first_timer;
- while (timer) {
- struct python_obs_timer *next = timer->next;
- struct python_obs_callback *cb = python_obs_timer_cb(timer);
- if (cb->base.removed) {
- python_obs_timer_remove(timer);
- } else {
- uint64_t elapsed = ts - timer->last_ts;
- if (elapsed >= timer->interval) {
- lock_python();
- timer_call(&cb->base);
- unlock_python();
- timer->last_ts += timer->interval;
- }
- }
- timer = next;
- }
- pthread_mutex_unlock(&timer_mutex);
- UNUSED_PARAMETER(param);
- }
- /* -------------------------------------------- */
- void obs_python_unload(void);
- bool obs_scripting_python_runtime_linked(void)
- {
- return (bool)RUNTIME_LINK;
- }
- bool obs_scripting_python_loaded(void)
- {
- return python_loaded;
- }
- void obs_python_load(void)
- {
- da_init(python_paths);
- pthread_mutexattr_t attr;
- pthread_mutexattr_init(&attr);
- pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
- pthread_mutex_init(&tick_mutex, NULL);
- pthread_mutex_init(&timer_mutex, &attr);
- }
- extern void add_python_frontend_funcs(PyObject *module);
- bool obs_scripting_load_python(const char *python_path)
- {
- if (python_loaded)
- return true;
- /* Use external python on windows and mac */
- #if RUNTIME_LINK
- # if 0
- struct dstr old_path = {0};
- struct dstr new_path = {0};
- # endif
- if (!import_python(python_path))
- return false;
- if (python_path && *python_path) {
- os_utf8_to_wcs(python_path, 0, home_path, 1024);
- Py_SetPythonHome(home_path);
- # if 0
- dstr_copy(&old_path, getenv("PATH"));
- _putenv("PYTHONPATH=");
- _putenv("PATH=");
- # endif
- }
- #else
- UNUSED_PARAMETER(python_path);
- #endif
- Py_Initialize();
- if (!Py_IsInitialized())
- return false;
- #if 0
- # ifdef _DEBUG
- if (pythondir && *pythondir) {
- dstr_printf(&new_path, "PATH=%s", old_path.array);
- _putenv(new_path.array);
- }
- # endif
- bfree(pythondir);
- dstr_free(&new_path);
- dstr_free(&old_path);
- #endif
- PyEval_InitThreads();
- if (!PyEval_ThreadsInitialized())
- return false;
- /* ---------------------------------------------- */
- /* Must set arguments for guis to work */
- wchar_t *argv[] = {L"", NULL};
- int argc = sizeof(argv) / sizeof(wchar_t*) - 1;
- PySys_SetArgv(argc, argv);
- #ifdef DEBUG_PYTHON_STARTUP
- /* ---------------------------------------------- */
- /* Debug logging to file if startup is failing */
- PyRun_SimpleString("import os");
- PyRun_SimpleString("import sys");
- PyRun_SimpleString("os.environ['PYTHONUNBUFFERED'] = '1'");
- PyRun_SimpleString("sys.stdout = open('./stdOut.txt','w',1)");
- PyRun_SimpleString("sys.stderr = open('./stdErr.txt','w',1)");
- PyRun_SimpleString("print(sys.version)");
- #endif
- /* ---------------------------------------------- */
- /* Load main interface module */
- add_to_python_path(SCRIPT_DIR);
- py_obspython = PyImport_ImportModule("obspython");
- bool success = !py_error();
- if (!success) {
- warn("Error importing obspython.py', unloading obs-python");
- goto out;
- }
- python_loaded = PyRun_SimpleString(startup_script) == 0;
- py_error();
- add_hook_functions(py_obspython);
- py_error();
- add_python_frontend_funcs(py_obspython);
- py_error();
- out:
- /* ---------------------------------------------- */
- /* Free data */
- PyEval_ReleaseThread(PyGILState_GetThisThreadState());
- if (!success) {
- warn("Failed to load python plugin");
- obs_python_unload();
- }
- if (python_loaded)
- obs_add_tick_callback(python_tick, NULL);
- return python_loaded;
- }
- void obs_python_unload(void)
- {
- if (python_loaded && Py_IsInitialized()) {
- PyGILState_Ensure();
- Py_XDECREF(py_obspython);
- Py_Finalize();
- }
- /* ---------------------- */
- obs_remove_tick_callback(python_tick, NULL);
- for (size_t i = 0; i < python_paths.num; i++)
- bfree(python_paths.array[i]);
- da_free(python_paths);
- pthread_mutex_destroy(&tick_mutex);
- pthread_mutex_destroy(&timer_mutex);
- dstr_free(&cur_py_log_chunk);
- }
|