1
0

obs-scripting-python.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721
  1. /******************************************************************************
  2. Copyright (C) 2015 by Andrew Skinner <[email protected]>
  3. Copyright (C) 2017 by Hugh Bailey <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #include "obs-scripting-python.h"
  16. #include "obs-scripting-config.h"
  17. #include <util/base.h>
  18. #include <util/platform.h>
  19. #include <util/darray.h>
  20. #include <util/dstr.h>
  21. #include <obs.h>
  22. /* ========================================================================= */
  23. // #define DEBUG_PYTHON_STARTUP
  24. static const char *startup_script = "\n\
  25. import sys\n\
  26. import os\n\
  27. import obspython\n\
  28. class stdout_logger(object):\n\
  29. def write(self, message):\n\
  30. obspython.script_log_no_endl(obspython.LOG_INFO, message)\n\
  31. def flush(self):\n\
  32. pass\n\
  33. class stderr_logger(object):\n\
  34. def write(self, message):\n\
  35. obspython.script_log_no_endl(obspython.LOG_ERROR, message)\n\
  36. def flush(self):\n\
  37. pass\n\
  38. os.environ['PYTHONUNBUFFERED'] = '1'\n\
  39. sys.stdout = stdout_logger()\n\
  40. sys.stderr = stderr_logger()\n";
  41. #if RUNTIME_LINK
  42. static wchar_t home_path[1024] = {0};
  43. #endif
  44. DARRAY(char *) python_paths;
  45. static bool python_loaded = false;
  46. static pthread_mutex_t tick_mutex = PTHREAD_MUTEX_INITIALIZER;
  47. static struct obs_python_script *first_tick_script = NULL;
  48. static PyObject *py_obspython = NULL;
  49. struct obs_python_script *cur_python_script = NULL;
  50. struct python_obs_callback *cur_python_cb = NULL;
  51. /* -------------------------------------------- */
  52. bool py_to_libobs_(const char *type, PyObject *py_in, void *libobs_out,
  53. const char *id, const char *func, int line)
  54. {
  55. swig_type_info *info = SWIG_TypeQuery(type);
  56. if (info == NULL) {
  57. warn("%s:%d: SWIG could not find type: %s%s%s", func, line,
  58. id ? id : "", id ? "::" : "", type);
  59. return false;
  60. }
  61. int ret = SWIG_ConvertPtr(py_in, libobs_out, info, 0);
  62. if (!SWIG_IsOK(ret)) {
  63. warn("%s:%d: SWIG failed to convert python object to obs "
  64. "object: %s%s%s",
  65. func, line, id ? id : "", id ? "::" : "", type);
  66. return false;
  67. }
  68. return true;
  69. }
  70. bool libobs_to_py_(const char *type, void *libobs_in, bool ownership,
  71. PyObject **py_out, const char *id, const char *func,
  72. int line)
  73. {
  74. swig_type_info *info = SWIG_TypeQuery(type);
  75. if (info == NULL) {
  76. warn("%s:%d: SWIG could not find type: %s%s%s", func, line,
  77. id ? id : "", id ? "::" : "", type);
  78. return false;
  79. }
  80. *py_out = SWIG_NewPointerObj(libobs_in, info, (int)ownership);
  81. if (*py_out == Py_None) {
  82. warn("%s:%d: SWIG failed to convert obs object to python "
  83. "object: %s%s%s",
  84. func, line, id ? id : "", id ? "::" : "", type);
  85. return false;
  86. }
  87. return true;
  88. }
  89. #define libobs_to_py(type, obs_obj, ownership, py_obj) \
  90. libobs_to_py_(#type " *", obs_obj, ownership, py_obj, NULL, __func__, \
  91. __LINE__)
  92. #define py_to_libobs(type, py_obj, libobs_out) \
  93. py_to_libobs_(#type " *", py_obj, libobs_out, NULL, __func__, __LINE__)
  94. #define lock_callback(cb) \
  95. lock_python(); \
  96. struct obs_python_script *__last_script = cur_python_script; \
  97. struct python_obs_callback *__last_cb = cur_python_cb; \
  98. cur_python_script = (struct obs_python_script *)cb->base.script; \
  99. cur_python_cb = cb
  100. #define unlock_callback() \
  101. cur_python_cb = __last_cb; \
  102. cur_python_script = __last_script; \
  103. unlock_python()
  104. /* ========================================================================= */
  105. void add_functions_to_py_module(PyObject *module, PyMethodDef *method_list)
  106. {
  107. PyObject *dict = PyModule_GetDict(module);
  108. PyObject *name = PyModule_GetNameObject(module);
  109. if (!dict || !name) {
  110. return;
  111. }
  112. for (PyMethodDef *ml = method_list; ml->ml_name != NULL; ml++) {
  113. PyObject *func = PyCFunction_NewEx(ml, module, name);
  114. if (!func) {
  115. continue;
  116. }
  117. PyDict_SetItemString(dict, ml->ml_name, func);
  118. Py_DECREF(func);
  119. }
  120. Py_DECREF(name);
  121. }
  122. /* -------------------------------------------- */
  123. static PyObject *py_get_current_script_path(PyObject *self, PyObject *args)
  124. {
  125. UNUSED_PARAMETER(args);
  126. return PyDict_GetItemString(PyModule_GetDict(self), "__script_dir__");
  127. }
  128. static void get_defaults(struct obs_python_script *data, PyObject *get_defs)
  129. {
  130. PyObject *py_settings;
  131. if (!libobs_to_py(obs_data_t, data->base.settings, false, &py_settings))
  132. return;
  133. PyObject *args = Py_BuildValue("(O)", py_settings);
  134. PyObject *py_ret = PyObject_CallObject(get_defs, args);
  135. py_error();
  136. Py_XDECREF(py_ret);
  137. Py_XDECREF(args);
  138. Py_XDECREF(py_settings);
  139. }
  140. static bool load_python_script(struct obs_python_script *data)
  141. {
  142. PyObject *py_file = NULL;
  143. PyObject *py_module = NULL;
  144. PyObject *py_success = NULL;
  145. PyObject *py_tick = NULL;
  146. PyObject *py_load = NULL;
  147. PyObject *py_defaults = NULL;
  148. bool success = false;
  149. int ret;
  150. cur_python_script = data;
  151. if (!data->module) {
  152. py_file = PyUnicode_FromString(data->name.array);
  153. py_module = PyImport_Import(py_file);
  154. } else {
  155. py_module = PyImport_ReloadModule(data->module);
  156. }
  157. if (!py_module) {
  158. py_error();
  159. goto fail;
  160. }
  161. Py_XINCREF(py_obspython);
  162. ret = PyModule_AddObject(py_module, "obspython", py_obspython);
  163. if (py_error() || ret != 0)
  164. goto fail;
  165. ret = PyModule_AddStringConstant(py_module, "__script_dir__",
  166. data->dir.array);
  167. if (py_error() || ret != 0)
  168. goto fail;
  169. PyObject *py_data = PyCapsule_New(data, NULL, NULL);
  170. ret = PyModule_AddObject(py_module, "__script_data__", py_data);
  171. if (py_error() || ret != 0)
  172. goto fail;
  173. static PyMethodDef global_funcs[] = {
  174. {"script_path", py_get_current_script_path, METH_NOARGS,
  175. "Gets the script path"},
  176. {0}};
  177. add_functions_to_py_module(py_module, global_funcs);
  178. data->update = PyObject_GetAttrString(py_module, "script_update");
  179. if (!data->update)
  180. PyErr_Clear();
  181. data->save = PyObject_GetAttrString(py_module, "script_save");
  182. if (!data->save)
  183. PyErr_Clear();
  184. data->get_properties =
  185. PyObject_GetAttrString(py_module, "script_properties");
  186. if (!data->get_properties)
  187. PyErr_Clear();
  188. PyObject *func = PyObject_GetAttrString(py_module, "script_defaults");
  189. if (func) {
  190. get_defaults(data, func);
  191. Py_DECREF(func);
  192. } else {
  193. PyErr_Clear();
  194. }
  195. func = PyObject_GetAttrString(py_module, "script_description");
  196. if (func) {
  197. PyObject *py_ret = PyObject_CallObject(func, NULL);
  198. py_error();
  199. PyObject *py_desc = PyUnicode_AsUTF8String(py_ret);
  200. if (py_desc) {
  201. const char *desc = PyBytes_AS_STRING(py_desc);
  202. if (desc)
  203. dstr_copy(&data->base.desc, desc);
  204. Py_DECREF(py_desc);
  205. }
  206. Py_XDECREF(py_ret);
  207. Py_DECREF(func);
  208. } else {
  209. PyErr_Clear();
  210. }
  211. py_tick = PyObject_GetAttrString(py_module, "script_tick");
  212. if (py_tick) {
  213. pthread_mutex_lock(&tick_mutex);
  214. struct obs_python_script *next = first_tick_script;
  215. data->next_tick = next;
  216. data->p_prev_next_tick = &first_tick_script;
  217. if (next)
  218. next->p_prev_next_tick = &data->next_tick;
  219. first_tick_script = data;
  220. data->tick = py_tick;
  221. py_tick = NULL;
  222. pthread_mutex_unlock(&tick_mutex);
  223. } else {
  224. PyErr_Clear();
  225. }
  226. py_load = PyObject_GetAttrString(py_module, "script_load");
  227. if (py_load) {
  228. PyObject *py_s;
  229. libobs_to_py(obs_data_t, data->base.settings, false, &py_s);
  230. PyObject *args = Py_BuildValue("(O)", py_s);
  231. PyObject *py_ret = PyObject_CallObject(py_load, args);
  232. py_error();
  233. Py_XDECREF(py_ret);
  234. Py_XDECREF(args);
  235. Py_XDECREF(py_s);
  236. } else {
  237. PyErr_Clear();
  238. }
  239. if (data->module)
  240. Py_XDECREF(data->module);
  241. data->module = py_module;
  242. py_module = NULL;
  243. success = true;
  244. fail:
  245. Py_XDECREF(py_load);
  246. Py_XDECREF(py_tick);
  247. Py_XDECREF(py_defaults);
  248. Py_XDECREF(py_success);
  249. Py_XDECREF(py_file);
  250. if (!success)
  251. Py_XDECREF(py_module);
  252. cur_python_script = NULL;
  253. return success;
  254. }
  255. static void unload_python_script(struct obs_python_script *data)
  256. {
  257. PyObject *py_module = data->module;
  258. PyObject *py_func = NULL;
  259. PyObject *py_ret = NULL;
  260. cur_python_script = data;
  261. py_func = PyObject_GetAttrString(py_module, "script_unload");
  262. if (PyErr_Occurred() || !py_func) {
  263. PyErr_Clear();
  264. goto fail;
  265. }
  266. py_ret = PyObject_CallObject(py_func, NULL);
  267. if (py_error())
  268. goto fail;
  269. fail:
  270. Py_XDECREF(py_ret);
  271. Py_XDECREF(py_func);
  272. cur_python_script = NULL;
  273. }
  274. static void add_to_python_path(const char *path)
  275. {
  276. PyObject *py_path_str = NULL;
  277. PyObject *py_path = NULL;
  278. int ret;
  279. if (!path || !*path)
  280. return;
  281. for (size_t i = 0; i < python_paths.num; i++) {
  282. const char *python_path = python_paths.array[i];
  283. if (strcmp(path, python_path) == 0)
  284. return;
  285. }
  286. ret = PyRun_SimpleString("import sys");
  287. if (py_error() || ret != 0)
  288. goto fail;
  289. /* borrowed reference here */
  290. py_path = PySys_GetObject("path");
  291. if (py_error() || !py_path)
  292. goto fail;
  293. py_path_str = PyUnicode_FromString(path);
  294. ret = PyList_Append(py_path, py_path_str);
  295. if (py_error() || ret != 0)
  296. goto fail;
  297. char *new_path = bstrdup(path);
  298. da_push_back(python_paths, &new_path);
  299. fail:
  300. Py_XDECREF(py_path_str);
  301. }
  302. /* -------------------------------------------- */
  303. struct python_obs_timer {
  304. struct python_obs_timer *next;
  305. struct python_obs_timer **p_prev_next;
  306. uint64_t last_ts;
  307. uint64_t interval;
  308. };
  309. static pthread_mutex_t timer_mutex = PTHREAD_MUTEX_INITIALIZER;
  310. static struct python_obs_timer *first_timer = NULL;
  311. static inline void python_obs_timer_init(struct python_obs_timer *timer)
  312. {
  313. pthread_mutex_lock(&timer_mutex);
  314. struct python_obs_timer *next = first_timer;
  315. timer->next = next;
  316. timer->p_prev_next = &first_timer;
  317. if (next)
  318. next->p_prev_next = &timer->next;
  319. first_timer = timer;
  320. pthread_mutex_unlock(&timer_mutex);
  321. }
  322. static inline void python_obs_timer_remove(struct python_obs_timer *timer)
  323. {
  324. struct python_obs_timer *next = timer->next;
  325. if (next)
  326. next->p_prev_next = timer->p_prev_next;
  327. *timer->p_prev_next = timer->next;
  328. }
  329. static inline struct python_obs_callback *
  330. python_obs_timer_cb(struct python_obs_timer *timer)
  331. {
  332. return &((struct python_obs_callback *)timer)[-1];
  333. }
  334. static PyObject *timer_remove(PyObject *self, PyObject *args)
  335. {
  336. struct obs_python_script *script = cur_python_script;
  337. PyObject *py_cb;
  338. UNUSED_PARAMETER(self);
  339. if (!parse_args(args, "O", &py_cb))
  340. return python_none();
  341. struct python_obs_callback *cb =
  342. find_python_obs_callback(script, py_cb);
  343. if (cb)
  344. remove_python_obs_callback(cb);
  345. return python_none();
  346. }
  347. static void timer_call(struct script_callback *p_cb)
  348. {
  349. struct python_obs_callback *cb = (struct python_obs_callback *)p_cb;
  350. if (p_cb->removed)
  351. return;
  352. lock_callback(cb);
  353. PyObject *py_ret = PyObject_CallObject(cb->func, NULL);
  354. py_error();
  355. Py_XDECREF(py_ret);
  356. unlock_callback();
  357. }
  358. static void defer_timer_init(void *p_cb)
  359. {
  360. struct python_obs_callback *cb = p_cb;
  361. struct python_obs_timer *timer = python_obs_callback_extra_data(cb);
  362. python_obs_timer_init(timer);
  363. }
  364. static PyObject *timer_add(PyObject *self, PyObject *args)
  365. {
  366. struct obs_python_script *script = cur_python_script;
  367. PyObject *py_cb;
  368. int ms;
  369. UNUSED_PARAMETER(self);
  370. if (!parse_args(args, "Oi", &py_cb, &ms))
  371. return python_none();
  372. struct python_obs_callback *cb = add_python_obs_callback_extra(
  373. script, py_cb, sizeof(struct python_obs_timer));
  374. struct python_obs_timer *timer = python_obs_callback_extra_data(cb);
  375. timer->interval = (uint64_t)ms * 1000000ULL;
  376. timer->last_ts = obs_get_video_frame_time();
  377. defer_call_post(defer_timer_init, cb);
  378. return python_none();
  379. }
  380. /* -------------------------------------------- */
  381. static void obs_python_tick_callback(void *priv, float seconds)
  382. {
  383. struct python_obs_callback *cb = priv;
  384. if (cb->base.removed) {
  385. obs_remove_tick_callback(obs_python_tick_callback, cb);
  386. return;
  387. }
  388. lock_callback(cb);
  389. PyObject *args = Py_BuildValue("(f)", seconds);
  390. PyObject *py_ret = PyObject_CallObject(cb->func, args);
  391. py_error();
  392. Py_XDECREF(py_ret);
  393. Py_XDECREF(args);
  394. unlock_callback();
  395. }
  396. static PyObject *obs_python_remove_tick_callback(PyObject *self, PyObject *args)
  397. {
  398. struct obs_python_script *script = cur_python_script;
  399. PyObject *py_cb = NULL;
  400. if (!script) {
  401. PyErr_SetString(PyExc_RuntimeError,
  402. "No active script, report this to Jim");
  403. return NULL;
  404. }
  405. UNUSED_PARAMETER(self);
  406. if (!parse_args(args, "O", &py_cb))
  407. return python_none();
  408. if (!py_cb || !PyFunction_Check(py_cb))
  409. return python_none();
  410. struct python_obs_callback *cb =
  411. find_python_obs_callback(script, py_cb);
  412. if (cb)
  413. remove_python_obs_callback(cb);
  414. return python_none();
  415. }
  416. static PyObject *obs_python_add_tick_callback(PyObject *self, PyObject *args)
  417. {
  418. struct obs_python_script *script = cur_python_script;
  419. PyObject *py_cb = NULL;
  420. if (!script) {
  421. PyErr_SetString(PyExc_RuntimeError,
  422. "No active script, report this to Jim");
  423. return NULL;
  424. }
  425. UNUSED_PARAMETER(self);
  426. if (!parse_args(args, "O", &py_cb))
  427. return python_none();
  428. if (!py_cb || !PyFunction_Check(py_cb))
  429. return python_none();
  430. struct python_obs_callback *cb = add_python_obs_callback(script, py_cb);
  431. obs_add_tick_callback(obs_python_tick_callback, cb);
  432. return python_none();
  433. }
  434. /* -------------------------------------------- */
  435. static void calldata_signal_callback(void *priv, calldata_t *cd)
  436. {
  437. struct python_obs_callback *cb = priv;
  438. if (cb->base.removed) {
  439. signal_handler_remove_current();
  440. return;
  441. }
  442. lock_callback(cb);
  443. PyObject *py_cd;
  444. if (libobs_to_py(calldata_t, cd, false, &py_cd)) {
  445. PyObject *args = Py_BuildValue("(O)", py_cd);
  446. PyObject *py_ret = PyObject_CallObject(cb->func, args);
  447. py_error();
  448. Py_XDECREF(py_ret);
  449. Py_XDECREF(args);
  450. Py_XDECREF(py_cd);
  451. }
  452. unlock_callback();
  453. }
  454. static PyObject *obs_python_signal_handler_disconnect(PyObject *self,
  455. PyObject *args)
  456. {
  457. struct obs_python_script *script = cur_python_script;
  458. PyObject *py_sh = NULL;
  459. PyObject *py_cb = NULL;
  460. const char *signal;
  461. if (!script) {
  462. PyErr_SetString(PyExc_RuntimeError,
  463. "No active script, report this to Jim");
  464. return NULL;
  465. }
  466. UNUSED_PARAMETER(self);
  467. signal_handler_t *handler;
  468. if (!parse_args(args, "OsO", &py_sh, &signal, &py_cb))
  469. return python_none();
  470. if (!py_to_libobs(signal_handler_t, py_sh, &handler))
  471. return python_none();
  472. if (!py_cb || !PyFunction_Check(py_cb))
  473. return python_none();
  474. struct python_obs_callback *cb =
  475. find_python_obs_callback(script, py_cb);
  476. while (cb) {
  477. signal_handler_t *cb_handler =
  478. calldata_ptr(&cb->base.extra, "handler");
  479. const char *cb_signal =
  480. calldata_string(&cb->base.extra, "signal");
  481. if (cb_signal && strcmp(signal, cb_signal) == 0 &&
  482. handler == cb_handler)
  483. break;
  484. cb = find_next_python_obs_callback(script, cb, py_cb);
  485. }
  486. if (cb)
  487. remove_python_obs_callback(cb);
  488. return python_none();
  489. }
  490. static PyObject *obs_python_signal_handler_connect(PyObject *self,
  491. PyObject *args)
  492. {
  493. struct obs_python_script *script = cur_python_script;
  494. PyObject *py_sh = NULL;
  495. PyObject *py_cb = NULL;
  496. const char *signal;
  497. if (!script) {
  498. PyErr_SetString(PyExc_RuntimeError,
  499. "No active script, report this to Jim");
  500. return NULL;
  501. }
  502. UNUSED_PARAMETER(self);
  503. signal_handler_t *handler;
  504. if (!parse_args(args, "OsO", &py_sh, &signal, &py_cb))
  505. return python_none();
  506. if (!py_to_libobs(signal_handler_t, py_sh, &handler))
  507. return python_none();
  508. if (!py_cb || !PyFunction_Check(py_cb))
  509. return python_none();
  510. struct python_obs_callback *cb = add_python_obs_callback(script, py_cb);
  511. calldata_set_ptr(&cb->base.extra, "handler", handler);
  512. calldata_set_string(&cb->base.extra, "signal", signal);
  513. signal_handler_connect(handler, signal, calldata_signal_callback, cb);
  514. return python_none();
  515. }
  516. /* -------------------------------------------- */
  517. static void calldata_signal_callback_global(void *priv, const char *signal,
  518. calldata_t *cd)
  519. {
  520. struct python_obs_callback *cb = priv;
  521. if (cb->base.removed) {
  522. signal_handler_remove_current();
  523. return;
  524. }
  525. lock_callback(cb);
  526. PyObject *py_cd;
  527. if (libobs_to_py(calldata_t, cd, false, &py_cd)) {
  528. PyObject *args = Py_BuildValue("(sO)", signal, py_cd);
  529. PyObject *py_ret = PyObject_CallObject(cb->func, args);
  530. py_error();
  531. Py_XDECREF(py_ret);
  532. Py_XDECREF(args);
  533. Py_XDECREF(py_cd);
  534. }
  535. unlock_callback();
  536. }
  537. static PyObject *obs_python_signal_handler_disconnect_global(PyObject *self,
  538. PyObject *args)
  539. {
  540. struct obs_python_script *script = cur_python_script;
  541. PyObject *py_sh = NULL;
  542. PyObject *py_cb = NULL;
  543. if (!script) {
  544. PyErr_SetString(PyExc_RuntimeError,
  545. "No active script, report this to Jim");
  546. return NULL;
  547. }
  548. UNUSED_PARAMETER(self);
  549. signal_handler_t *handler;
  550. if (!parse_args(args, "OO", &py_sh, &py_cb))
  551. return python_none();
  552. if (!py_to_libobs(signal_handler_t, py_sh, &handler))
  553. return python_none();
  554. if (!py_cb || !PyFunction_Check(py_cb))
  555. return python_none();
  556. struct python_obs_callback *cb =
  557. find_python_obs_callback(script, py_cb);
  558. while (cb) {
  559. signal_handler_t *cb_handler =
  560. calldata_ptr(&cb->base.extra, "handler");
  561. if (handler == cb_handler)
  562. break;
  563. cb = find_next_python_obs_callback(script, cb, py_cb);
  564. }
  565. if (cb)
  566. remove_python_obs_callback(cb);
  567. return python_none();
  568. }
  569. static PyObject *obs_python_signal_handler_connect_global(PyObject *self,
  570. PyObject *args)
  571. {
  572. struct obs_python_script *script = cur_python_script;
  573. PyObject *py_sh = NULL;
  574. PyObject *py_cb = NULL;
  575. if (!script) {
  576. PyErr_SetString(PyExc_RuntimeError,
  577. "No active script, report this to Jim");
  578. return NULL;
  579. }
  580. UNUSED_PARAMETER(self);
  581. signal_handler_t *handler;
  582. if (!parse_args(args, "OO", &py_sh, &py_cb))
  583. return python_none();
  584. if (!py_to_libobs(signal_handler_t, py_sh, &handler))
  585. return python_none();
  586. if (!py_cb || !PyFunction_Check(py_cb))
  587. return python_none();
  588. struct python_obs_callback *cb = add_python_obs_callback(script, py_cb);
  589. calldata_set_ptr(&cb->base.extra, "handler", handler);
  590. signal_handler_connect_global(handler, calldata_signal_callback_global,
  591. cb);
  592. return python_none();
  593. }
  594. /* -------------------------------------------- */
  595. static void defer_hotkey_unregister(void *p_cb)
  596. {
  597. obs_hotkey_unregister((obs_hotkey_id)(uintptr_t)p_cb);
  598. }
  599. static void on_remove_hotkey(void *p_cb)
  600. {
  601. struct python_obs_callback *cb = p_cb;
  602. obs_hotkey_id id = (obs_hotkey_id)calldata_int(&cb->base.extra, "id");
  603. if (id != OBS_INVALID_HOTKEY_ID)
  604. defer_call_post(defer_hotkey_unregister, (void *)(uintptr_t)id);
  605. }
  606. static void hotkey_pressed(void *p_cb, bool pressed)
  607. {
  608. struct python_obs_callback *cb = p_cb;
  609. if (cb->base.removed)
  610. return;
  611. lock_callback(cb);
  612. PyObject *py_pressed = PyBool_FromLong(pressed);
  613. PyObject *args = Py_BuildValue("(O)", py_pressed);
  614. PyObject *py_ret = PyObject_CallObject(cb->func, args);
  615. py_error();
  616. Py_XDECREF(py_ret);
  617. Py_XDECREF(args);
  618. Py_XDECREF(py_pressed);
  619. unlock_callback();
  620. }
  621. static void defer_hotkey_pressed(void *p_cb)
  622. {
  623. hotkey_pressed(p_cb, true);
  624. }
  625. static void defer_hotkey_unpressed(void *p_cb)
  626. {
  627. hotkey_pressed(p_cb, false);
  628. }
  629. static inline PyObject *py_invalid_hotkey_id()
  630. {
  631. return PyLong_FromUnsignedLongLong(OBS_INVALID_HOTKEY_ID);
  632. }
  633. static void hotkey_callback(void *p_cb, obs_hotkey_id id, obs_hotkey_t *hotkey,
  634. bool pressed)
  635. {
  636. struct python_obs_callback *cb = p_cb;
  637. if (cb->base.removed)
  638. return;
  639. if (pressed)
  640. defer_call_post(defer_hotkey_pressed, cb);
  641. else
  642. defer_call_post(defer_hotkey_unpressed, cb);
  643. UNUSED_PARAMETER(hotkey);
  644. UNUSED_PARAMETER(id);
  645. }
  646. static PyObject *hotkey_unregister(PyObject *self, PyObject *args)
  647. {
  648. struct obs_python_script *script = cur_python_script;
  649. PyObject *py_cb = NULL;
  650. if (!script) {
  651. PyErr_SetString(PyExc_RuntimeError,
  652. "No active script, report this to Jim");
  653. return NULL;
  654. }
  655. if (!parse_args(args, "O", &py_cb))
  656. return python_none();
  657. if (!py_cb || !PyFunction_Check(py_cb))
  658. return python_none();
  659. struct python_obs_callback *cb =
  660. find_python_obs_callback(script, py_cb);
  661. if (cb)
  662. remove_python_obs_callback(cb);
  663. UNUSED_PARAMETER(self);
  664. return python_none();
  665. }
  666. static PyObject *hotkey_register_frontend(PyObject *self, PyObject *args)
  667. {
  668. struct obs_python_script *script = cur_python_script;
  669. const char *name;
  670. const char *desc;
  671. obs_hotkey_id id;
  672. PyObject *py_cb;
  673. if (!parse_args(args, "ssO", &name, &desc, &py_cb))
  674. return py_invalid_hotkey_id();
  675. if (!py_cb || !PyFunction_Check(py_cb))
  676. return py_invalid_hotkey_id();
  677. struct python_obs_callback *cb = add_python_obs_callback(script, py_cb);
  678. cb->base.on_remove = on_remove_hotkey;
  679. id = obs_hotkey_register_frontend(name, desc, hotkey_callback, cb);
  680. calldata_set_int(&cb->base.extra, "id", id);
  681. if (id == OBS_INVALID_HOTKEY_ID)
  682. remove_python_obs_callback(cb);
  683. UNUSED_PARAMETER(self);
  684. return PyLong_FromUnsignedLongLong(id);
  685. }
  686. /* -------------------------------------------- */
  687. static bool button_prop_clicked(obs_properties_t *props, obs_property_t *p,
  688. void *p_cb)
  689. {
  690. struct python_obs_callback *cb = p_cb;
  691. bool ret = false;
  692. if (cb->base.removed)
  693. return false;
  694. lock_callback(cb);
  695. PyObject *py_props = NULL;
  696. PyObject *py_p = NULL;
  697. if (libobs_to_py(obs_properties_t, props, false, &py_props) &&
  698. libobs_to_py(obs_property_t, p, false, &py_p)) {
  699. PyObject *args = Py_BuildValue("(OO)", py_props, py_p);
  700. PyObject *py_ret = PyObject_CallObject(cb->func, args);
  701. if (!py_error())
  702. ret = py_ret == Py_True;
  703. Py_XDECREF(py_ret);
  704. Py_XDECREF(args);
  705. }
  706. Py_XDECREF(py_p);
  707. Py_XDECREF(py_props);
  708. unlock_callback();
  709. return ret;
  710. }
  711. static PyObject *properties_add_button(PyObject *self, PyObject *args)
  712. {
  713. struct obs_python_script *script = cur_python_script;
  714. obs_properties_t *props;
  715. obs_property_t *p;
  716. PyObject *py_props;
  717. PyObject *py_ret;
  718. const char *name;
  719. const char *text;
  720. PyObject *py_cb;
  721. if (!parse_args(args, "OssO", &py_props, &name, &text, &py_cb))
  722. return python_none();
  723. if (!py_to_libobs(obs_properties_t, py_props, &props))
  724. return python_none();
  725. if (!py_cb || !PyFunction_Check(py_cb))
  726. return python_none();
  727. struct python_obs_callback *cb = add_python_obs_callback(script, py_cb);
  728. p = obs_properties_add_button2(props, name, text, button_prop_clicked,
  729. cb);
  730. if (!p || !libobs_to_py(obs_property_t, p, false, &py_ret))
  731. return python_none();
  732. UNUSED_PARAMETER(self);
  733. return py_ret;
  734. }
  735. /* -------------------------------------------- */
  736. static bool modified_callback(void *p_cb, obs_properties_t *props,
  737. obs_property_t *p, obs_data_t *settings)
  738. {
  739. struct python_obs_callback *cb = p_cb;
  740. bool ret = false;
  741. if (cb->base.removed)
  742. return false;
  743. lock_callback(cb);
  744. PyObject *py_props = NULL;
  745. PyObject *py_p = NULL;
  746. PyObject *py_settings = NULL;
  747. if (libobs_to_py(obs_properties_t, props, false, &py_props) &&
  748. libobs_to_py(obs_property_t, p, false, &py_p) &&
  749. libobs_to_py(obs_data_t, settings, false, &py_settings)) {
  750. PyObject *args =
  751. Py_BuildValue("(OOO)", py_props, py_p, py_settings);
  752. PyObject *py_ret = PyObject_CallObject(cb->func, args);
  753. if (!py_error())
  754. ret = py_ret == Py_True;
  755. Py_XDECREF(py_ret);
  756. Py_XDECREF(args);
  757. }
  758. Py_XDECREF(py_settings);
  759. Py_XDECREF(py_p);
  760. Py_XDECREF(py_props);
  761. unlock_callback();
  762. return ret;
  763. }
  764. static PyObject *property_set_modified_callback(PyObject *self, PyObject *args)
  765. {
  766. struct obs_python_script *script = cur_python_script;
  767. PyObject *py_p;
  768. PyObject *py_cb;
  769. obs_property_t *p;
  770. if (!parse_args(args, "OO", &py_p, &py_cb))
  771. return python_none();
  772. if (!py_to_libobs(obs_property_t, py_p, &p))
  773. return python_none();
  774. if (!py_cb || !PyFunction_Check(py_cb))
  775. return python_none();
  776. struct python_obs_callback *cb = add_python_obs_callback(script, py_cb);
  777. obs_property_set_modified_callback2(p, modified_callback, cb);
  778. UNUSED_PARAMETER(self);
  779. return python_none();
  780. }
  781. /* -------------------------------------------- */
  782. static PyObject *remove_current_callback(PyObject *self, PyObject *args)
  783. {
  784. UNUSED_PARAMETER(self);
  785. UNUSED_PARAMETER(args);
  786. if (cur_python_cb)
  787. remove_python_obs_callback(cur_python_cb);
  788. return python_none();
  789. }
  790. /* -------------------------------------------- */
  791. static PyObject *calldata_source(PyObject *self, PyObject *args)
  792. {
  793. PyObject *py_ret = NULL;
  794. PyObject *py_cd = NULL;
  795. calldata_t *cd;
  796. const char *name;
  797. UNUSED_PARAMETER(self);
  798. if (!parse_args(args, "Os", &py_cd, &name))
  799. goto fail;
  800. if (!py_to_libobs(calldata_t, py_cd, &cd))
  801. goto fail;
  802. obs_source_t *source = calldata_ptr(cd, name);
  803. libobs_to_py(obs_source_t, source, false, &py_ret);
  804. fail:
  805. return py_ret;
  806. }
  807. static PyObject *calldata_sceneitem(PyObject *self, PyObject *args)
  808. {
  809. PyObject *py_ret = NULL;
  810. PyObject *py_cd = NULL;
  811. calldata_t *cd;
  812. const char *name;
  813. UNUSED_PARAMETER(self);
  814. if (!parse_args(args, "Os", &py_cd, &name))
  815. goto fail;
  816. if (!py_to_libobs(calldata_t, py_cd, &cd))
  817. goto fail;
  818. obs_sceneitem_t *item = calldata_ptr(cd, name);
  819. libobs_to_py(obs_sceneitem_t, item, false, &py_ret);
  820. fail:
  821. return py_ret;
  822. }
  823. /* -------------------------------------------- */
  824. static bool enum_sources_proc(void *param, obs_source_t *source)
  825. {
  826. PyObject *list = param;
  827. PyObject *py_source;
  828. if (libobs_to_py(obs_source_t, source, false, &py_source)) {
  829. obs_source_get_ref(source);
  830. PyList_Append(list, py_source);
  831. Py_DECREF(py_source);
  832. }
  833. return true;
  834. }
  835. static PyObject *enum_sources(PyObject *self, PyObject *args)
  836. {
  837. UNUSED_PARAMETER(self);
  838. UNUSED_PARAMETER(args);
  839. PyObject *list = PyList_New(0);
  840. obs_enum_sources(enum_sources_proc, list);
  841. return list;
  842. }
  843. /* -------------------------------------------- */
  844. static bool enum_items_proc(obs_scene_t *scene, obs_sceneitem_t *item,
  845. void *param)
  846. {
  847. PyObject *list = param;
  848. PyObject *py_item;
  849. UNUSED_PARAMETER(scene);
  850. if (libobs_to_py(obs_sceneitem_t, item, false, &py_item)) {
  851. obs_sceneitem_addref(item);
  852. PyList_Append(list, py_item);
  853. Py_DECREF(py_item);
  854. }
  855. return true;
  856. }
  857. static PyObject *scene_enum_items(PyObject *self, PyObject *args)
  858. {
  859. PyObject *py_scene;
  860. obs_scene_t *scene;
  861. UNUSED_PARAMETER(self);
  862. if (!parse_args(args, "O", &py_scene))
  863. return python_none();
  864. if (!py_to_libobs(obs_scene_t, py_scene, &scene))
  865. return python_none();
  866. PyObject *list = PyList_New(0);
  867. obs_scene_enum_items(scene, enum_items_proc, list);
  868. return list;
  869. }
  870. /* -------------------------------------------- */
  871. static PyObject *source_list_release(PyObject *self, PyObject *args)
  872. {
  873. PyObject *list;
  874. if (!parse_args(args, "O", &list))
  875. return python_none();
  876. Py_ssize_t count = PyList_Size(list);
  877. for (Py_ssize_t i = 0; i < count; i++) {
  878. PyObject *py_source = PyList_GetItem(list, i);
  879. obs_source_t *source;
  880. if (py_to_libobs(obs_source_t, py_source, &source)) {
  881. obs_source_release(source);
  882. }
  883. }
  884. UNUSED_PARAMETER(self);
  885. return python_none();
  886. }
  887. static PyObject *sceneitem_list_release(PyObject *self, PyObject *args)
  888. {
  889. PyObject *list;
  890. if (!parse_args(args, "O", &list))
  891. return python_none();
  892. Py_ssize_t count = PyList_Size(list);
  893. for (Py_ssize_t i = 0; i < count; i++) {
  894. PyObject *py_item = PyList_GetItem(list, i);
  895. obs_sceneitem_t *item;
  896. if (py_to_libobs(obs_sceneitem_t, py_item, &item)) {
  897. obs_sceneitem_release(item);
  898. }
  899. }
  900. UNUSED_PARAMETER(self);
  901. return python_none();
  902. }
  903. /* -------------------------------------------- */
  904. struct dstr cur_py_log_chunk = {0};
  905. static PyObject *py_script_log_internal(PyObject *self, PyObject *args,
  906. bool add_endl)
  907. {
  908. static bool calling_self = false;
  909. int log_level;
  910. const char *msg;
  911. UNUSED_PARAMETER(self);
  912. if (calling_self)
  913. return python_none();
  914. calling_self = true;
  915. /* ------------------- */
  916. if (!parse_args(args, "is", &log_level, &msg))
  917. goto fail;
  918. if (!msg || !*msg)
  919. goto fail;
  920. dstr_cat(&cur_py_log_chunk, msg);
  921. if (add_endl)
  922. dstr_cat(&cur_py_log_chunk, "\n");
  923. const char *start = cur_py_log_chunk.array;
  924. char *endl = strchr(start, '\n');
  925. while (endl) {
  926. *endl = 0;
  927. if (cur_python_script)
  928. script_log(&cur_python_script->base, log_level, "%s",
  929. start);
  930. else
  931. script_log(NULL, log_level, "%s", start);
  932. *endl = '\n';
  933. start = endl + 1;
  934. endl = strchr(start, '\n');
  935. }
  936. if (start) {
  937. size_t len = strlen(start);
  938. if (len)
  939. memmove(cur_py_log_chunk.array, start, len);
  940. dstr_resize(&cur_py_log_chunk, len);
  941. }
  942. /* ------------------- */
  943. fail:
  944. calling_self = false;
  945. return python_none();
  946. }
  947. static PyObject *py_script_log_no_endl(PyObject *self, PyObject *args)
  948. {
  949. return py_script_log_internal(self, args, false);
  950. }
  951. static PyObject *py_script_log(PyObject *self, PyObject *args)
  952. {
  953. return py_script_log_internal(self, args, true);
  954. }
  955. /* -------------------------------------------- */
  956. static void add_hook_functions(PyObject *module)
  957. {
  958. static PyMethodDef funcs[] = {
  959. #define DEF_FUNC(n, c) {n, c, METH_VARARGS, NULL}
  960. DEF_FUNC("script_log_no_endl", py_script_log_no_endl),
  961. DEF_FUNC("script_log", py_script_log),
  962. DEF_FUNC("timer_remove", timer_remove),
  963. DEF_FUNC("timer_add", timer_add),
  964. DEF_FUNC("calldata_source", calldata_source),
  965. DEF_FUNC("calldata_sceneitem", calldata_sceneitem),
  966. DEF_FUNC("source_list_release", source_list_release),
  967. DEF_FUNC("sceneitem_list_release", sceneitem_list_release),
  968. DEF_FUNC("obs_enum_sources", enum_sources),
  969. DEF_FUNC("obs_scene_enum_items", scene_enum_items),
  970. DEF_FUNC("obs_remove_tick_callback",
  971. obs_python_remove_tick_callback),
  972. DEF_FUNC("obs_add_tick_callback", obs_python_add_tick_callback),
  973. DEF_FUNC("signal_handler_disconnect",
  974. obs_python_signal_handler_disconnect),
  975. DEF_FUNC("signal_handler_connect",
  976. obs_python_signal_handler_connect),
  977. DEF_FUNC("signal_handler_disconnect_global",
  978. obs_python_signal_handler_disconnect_global),
  979. DEF_FUNC("signal_handler_connect_global",
  980. obs_python_signal_handler_connect_global),
  981. DEF_FUNC("obs_hotkey_unregister", hotkey_unregister),
  982. DEF_FUNC("obs_hotkey_register_frontend",
  983. hotkey_register_frontend),
  984. DEF_FUNC("obs_properties_add_button", properties_add_button),
  985. DEF_FUNC("obs_property_set_modified_callback",
  986. property_set_modified_callback),
  987. DEF_FUNC("remove_current_callback", remove_current_callback),
  988. #undef DEF_FUNC
  989. {0}};
  990. add_functions_to_py_module(module, funcs);
  991. }
  992. /* -------------------------------------------- */
  993. void obs_python_script_update(obs_script_t *script, obs_data_t *settings);
  994. bool obs_python_script_load(obs_script_t *s)
  995. {
  996. struct obs_python_script *data = (struct obs_python_script *)s;
  997. if (python_loaded && !data->base.loaded) {
  998. lock_python();
  999. if (!data->module)
  1000. add_to_python_path(data->dir.array);
  1001. data->base.loaded = load_python_script(data);
  1002. unlock_python();
  1003. if (data->base.loaded)
  1004. obs_python_script_update(s, NULL);
  1005. }
  1006. return data->base.loaded;
  1007. }
  1008. obs_script_t *obs_python_script_create(const char *path, obs_data_t *settings)
  1009. {
  1010. struct obs_python_script *data = bzalloc(sizeof(*data));
  1011. data->base.type = OBS_SCRIPT_LANG_PYTHON;
  1012. dstr_copy(&data->base.path, path);
  1013. dstr_replace(&data->base.path, "\\", "/");
  1014. path = data->base.path.array;
  1015. const char *slash = path && *path ? strrchr(path, '/') : NULL;
  1016. if (slash) {
  1017. slash++;
  1018. dstr_copy(&data->base.file, slash);
  1019. dstr_left(&data->dir, &data->base.path, slash - path);
  1020. } else {
  1021. dstr_copy(&data->base.file, path);
  1022. }
  1023. path = data->base.file.array;
  1024. dstr_copy_dstr(&data->name, &data->base.file);
  1025. const char *ext = strstr(path, ".py");
  1026. if (ext)
  1027. dstr_resize(&data->name, ext - path);
  1028. data->base.settings = obs_data_create();
  1029. if (settings)
  1030. obs_data_apply(data->base.settings, settings);
  1031. if (!python_loaded)
  1032. return (obs_script_t *)data;
  1033. lock_python();
  1034. add_to_python_path(data->dir.array);
  1035. data->base.loaded = load_python_script(data);
  1036. if (data->base.loaded) {
  1037. cur_python_script = data;
  1038. obs_python_script_update(&data->base, NULL);
  1039. cur_python_script = NULL;
  1040. }
  1041. unlock_python();
  1042. return (obs_script_t *)data;
  1043. }
  1044. void obs_python_script_unload(obs_script_t *s)
  1045. {
  1046. struct obs_python_script *data = (struct obs_python_script *)s;
  1047. if (!s->loaded || !python_loaded)
  1048. return;
  1049. /* ---------------------------- */
  1050. /* unhook tick function */
  1051. if (data->p_prev_next_tick) {
  1052. pthread_mutex_lock(&tick_mutex);
  1053. struct obs_python_script *next = data->next_tick;
  1054. if (next)
  1055. next->p_prev_next_tick = data->p_prev_next_tick;
  1056. *data->p_prev_next_tick = next;
  1057. pthread_mutex_unlock(&tick_mutex);
  1058. data->p_prev_next_tick = NULL;
  1059. data->next_tick = NULL;
  1060. }
  1061. lock_python();
  1062. Py_XDECREF(data->tick);
  1063. Py_XDECREF(data->save);
  1064. Py_XDECREF(data->update);
  1065. Py_XDECREF(data->get_properties);
  1066. data->tick = NULL;
  1067. data->save = NULL;
  1068. data->update = NULL;
  1069. data->get_properties = NULL;
  1070. /* ---------------------------- */
  1071. /* remove all callbacks */
  1072. struct script_callback *cb = data->first_callback;
  1073. while (cb) {
  1074. struct script_callback *next = cb->next;
  1075. remove_script_callback(cb);
  1076. cb = next;
  1077. }
  1078. /* ---------------------------- */
  1079. /* unload */
  1080. unload_python_script(data);
  1081. unlock_python();
  1082. s->loaded = false;
  1083. }
  1084. void obs_python_script_destroy(obs_script_t *s)
  1085. {
  1086. struct obs_python_script *data = (struct obs_python_script *)s;
  1087. if (data) {
  1088. if (python_loaded) {
  1089. lock_python();
  1090. Py_XDECREF(data->module);
  1091. unlock_python();
  1092. }
  1093. dstr_free(&data->base.path);
  1094. dstr_free(&data->base.file);
  1095. dstr_free(&data->base.desc);
  1096. obs_data_release(data->base.settings);
  1097. dstr_free(&data->dir);
  1098. dstr_free(&data->name);
  1099. bfree(data);
  1100. }
  1101. }
  1102. void obs_python_script_update(obs_script_t *s, obs_data_t *settings)
  1103. {
  1104. struct obs_python_script *data = (struct obs_python_script *)s;
  1105. if (!s->loaded || !python_loaded)
  1106. return;
  1107. if (!data->update)
  1108. return;
  1109. if (settings)
  1110. obs_data_apply(s->settings, settings);
  1111. lock_python();
  1112. cur_python_script = data;
  1113. PyObject *py_settings;
  1114. if (libobs_to_py(obs_data_t, s->settings, false, &py_settings)) {
  1115. PyObject *args = Py_BuildValue("(O)", py_settings);
  1116. PyObject *ret = PyObject_CallObject(data->update, args);
  1117. py_error();
  1118. Py_XDECREF(ret);
  1119. Py_XDECREF(args);
  1120. Py_XDECREF(py_settings);
  1121. }
  1122. cur_python_script = NULL;
  1123. unlock_python();
  1124. }
  1125. obs_properties_t *obs_python_script_get_properties(obs_script_t *s)
  1126. {
  1127. struct obs_python_script *data = (struct obs_python_script *)s;
  1128. obs_properties_t *props = NULL;
  1129. if (!s->loaded || !python_loaded)
  1130. return NULL;
  1131. if (!data->get_properties)
  1132. return NULL;
  1133. lock_python();
  1134. cur_python_script = data;
  1135. PyObject *ret = PyObject_CallObject(data->get_properties, NULL);
  1136. if (!py_error())
  1137. py_to_libobs(obs_properties_t, ret, &props);
  1138. Py_XDECREF(ret);
  1139. cur_python_script = NULL;
  1140. unlock_python();
  1141. return props;
  1142. }
  1143. void obs_python_script_save(obs_script_t *s)
  1144. {
  1145. struct obs_python_script *data = (struct obs_python_script *)s;
  1146. if (!s->loaded || !python_loaded)
  1147. return;
  1148. if (!data->save)
  1149. return;
  1150. lock_python();
  1151. cur_python_script = data;
  1152. PyObject *py_settings;
  1153. if (libobs_to_py(obs_data_t, s->settings, false, &py_settings)) {
  1154. PyObject *args = Py_BuildValue("(O)", py_settings);
  1155. PyObject *ret = PyObject_CallObject(data->save, args);
  1156. py_error();
  1157. Py_XDECREF(ret);
  1158. Py_XDECREF(args);
  1159. Py_XDECREF(py_settings);
  1160. }
  1161. cur_python_script = NULL;
  1162. unlock_python();
  1163. }
  1164. /* -------------------------------------------- */
  1165. static void python_tick(void *param, float seconds)
  1166. {
  1167. struct obs_python_script *data;
  1168. bool valid;
  1169. uint64_t ts = obs_get_video_frame_time();
  1170. pthread_mutex_lock(&tick_mutex);
  1171. valid = !!first_tick_script;
  1172. pthread_mutex_unlock(&tick_mutex);
  1173. /* --------------------------------- */
  1174. /* process script_tick calls */
  1175. if (valid) {
  1176. lock_python();
  1177. PyObject *args = Py_BuildValue("(f)", seconds);
  1178. pthread_mutex_lock(&tick_mutex);
  1179. data = first_tick_script;
  1180. while (data) {
  1181. cur_python_script = data;
  1182. PyObject *py_ret =
  1183. PyObject_CallObject(data->tick, args);
  1184. Py_XDECREF(py_ret);
  1185. py_error();
  1186. data = data->next_tick;
  1187. }
  1188. cur_python_script = NULL;
  1189. pthread_mutex_unlock(&tick_mutex);
  1190. Py_XDECREF(args);
  1191. unlock_python();
  1192. }
  1193. /* --------------------------------- */
  1194. /* process timers */
  1195. pthread_mutex_lock(&timer_mutex);
  1196. struct python_obs_timer *timer = first_timer;
  1197. while (timer) {
  1198. struct python_obs_timer *next = timer->next;
  1199. struct python_obs_callback *cb = python_obs_timer_cb(timer);
  1200. if (cb->base.removed) {
  1201. python_obs_timer_remove(timer);
  1202. } else {
  1203. uint64_t elapsed = ts - timer->last_ts;
  1204. if (elapsed >= timer->interval) {
  1205. lock_python();
  1206. timer_call(&cb->base);
  1207. unlock_python();
  1208. timer->last_ts += timer->interval;
  1209. }
  1210. }
  1211. timer = next;
  1212. }
  1213. pthread_mutex_unlock(&timer_mutex);
  1214. UNUSED_PARAMETER(param);
  1215. }
  1216. /* -------------------------------------------- */
  1217. void obs_python_unload(void);
  1218. bool obs_scripting_python_runtime_linked(void)
  1219. {
  1220. return (bool)RUNTIME_LINK;
  1221. }
  1222. bool obs_scripting_python_loaded(void)
  1223. {
  1224. return python_loaded;
  1225. }
  1226. void obs_python_load(void)
  1227. {
  1228. da_init(python_paths);
  1229. pthread_mutexattr_t attr;
  1230. pthread_mutexattr_init(&attr);
  1231. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  1232. pthread_mutex_init(&tick_mutex, NULL);
  1233. pthread_mutex_init(&timer_mutex, &attr);
  1234. }
  1235. extern void add_python_frontend_funcs(PyObject *module);
  1236. static bool python_loaded_at_all = false;
  1237. bool obs_scripting_load_python(const char *python_path)
  1238. {
  1239. if (python_loaded)
  1240. return true;
  1241. /* Use external python on windows and mac */
  1242. #if RUNTIME_LINK
  1243. #if 0
  1244. struct dstr old_path = {0};
  1245. struct dstr new_path = {0};
  1246. #endif
  1247. if (!import_python(python_path))
  1248. return false;
  1249. if (python_path && *python_path) {
  1250. os_utf8_to_wcs(python_path, 0, home_path, 1024);
  1251. Py_SetPythonHome(home_path);
  1252. #if 0
  1253. dstr_copy(&old_path, getenv("PATH"));
  1254. _putenv("PYTHONPATH=");
  1255. _putenv("PATH=");
  1256. #endif
  1257. }
  1258. #else
  1259. UNUSED_PARAMETER(python_path);
  1260. #endif
  1261. Py_Initialize();
  1262. if (!Py_IsInitialized())
  1263. return false;
  1264. #if 0
  1265. #ifdef _DEBUG
  1266. if (pythondir && *pythondir) {
  1267. dstr_printf(&new_path, "PATH=%s", old_path.array);
  1268. _putenv(new_path.array);
  1269. }
  1270. #endif
  1271. bfree(pythondir);
  1272. dstr_free(&new_path);
  1273. dstr_free(&old_path);
  1274. #endif
  1275. PyEval_InitThreads();
  1276. if (!PyEval_ThreadsInitialized())
  1277. return false;
  1278. /* ---------------------------------------------- */
  1279. /* Must set arguments for guis to work */
  1280. wchar_t *argv[] = {L"", NULL};
  1281. int argc = sizeof(argv) / sizeof(wchar_t *) - 1;
  1282. PySys_SetArgv(argc, argv);
  1283. #ifdef DEBUG_PYTHON_STARTUP
  1284. /* ---------------------------------------------- */
  1285. /* Debug logging to file if startup is failing */
  1286. PyRun_SimpleString("import os");
  1287. PyRun_SimpleString("import sys");
  1288. PyRun_SimpleString("os.environ['PYTHONUNBUFFERED'] = '1'");
  1289. PyRun_SimpleString("sys.stdout = open('./stdOut.txt','w',1)");
  1290. PyRun_SimpleString("sys.stderr = open('./stdErr.txt','w',1)");
  1291. PyRun_SimpleString("print(sys.version)");
  1292. #endif
  1293. /* ---------------------------------------------- */
  1294. /* Load main interface module */
  1295. add_to_python_path(SCRIPT_DIR);
  1296. #if __APPLE__
  1297. char *exec_path = os_get_executable_path_ptr("");
  1298. if (exec_path)
  1299. add_to_python_path(exec_path);
  1300. bfree(exec_path);
  1301. #endif
  1302. py_obspython = PyImport_ImportModule("obspython");
  1303. bool success = !py_error();
  1304. if (!success) {
  1305. warn("Error importing obspython.py', unloading obs-python");
  1306. goto out;
  1307. }
  1308. python_loaded = PyRun_SimpleString(startup_script) == 0;
  1309. py_error();
  1310. add_hook_functions(py_obspython);
  1311. py_error();
  1312. add_python_frontend_funcs(py_obspython);
  1313. py_error();
  1314. out:
  1315. /* ---------------------------------------------- */
  1316. /* Free data */
  1317. PyEval_ReleaseThread(PyGILState_GetThisThreadState());
  1318. if (!success) {
  1319. warn("Failed to load python plugin");
  1320. obs_python_unload();
  1321. }
  1322. python_loaded_at_all = success;
  1323. if (python_loaded)
  1324. obs_add_tick_callback(python_tick, NULL);
  1325. return python_loaded;
  1326. }
  1327. void obs_python_unload(void)
  1328. {
  1329. if (!python_loaded_at_all)
  1330. return;
  1331. if (python_loaded && Py_IsInitialized()) {
  1332. PyGILState_Ensure();
  1333. Py_XDECREF(py_obspython);
  1334. Py_Finalize();
  1335. }
  1336. /* ---------------------- */
  1337. obs_remove_tick_callback(python_tick, NULL);
  1338. for (size_t i = 0; i < python_paths.num; i++)
  1339. bfree(python_paths.array[i]);
  1340. da_free(python_paths);
  1341. pthread_mutex_destroy(&tick_mutex);
  1342. pthread_mutex_destroy(&timer_mutex);
  1343. dstr_free(&cur_py_log_chunk);
  1344. python_loaded_at_all = false;
  1345. }