1
0

obs-scripting-python.c 40 KB

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