1
0

obs-scripting.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <obs.h>
  15. #include <util/dstr.h>
  16. #include <util/platform.h>
  17. #include <util/threading.h>
  18. #include <util/deque.h>
  19. #include "obs-scripting-internal.h"
  20. #include "obs-scripting-callback.h"
  21. static obs_module_t *obs_module_pointer;
  22. void obs_scripting_set_module(obs_module_t *module)
  23. {
  24. obs_module_pointer = module;
  25. }
  26. obs_module_t *obs_current_module(void)
  27. {
  28. return obs_module_pointer;
  29. }
  30. #if defined(LUAJIT_FOUND)
  31. extern obs_script_t *obs_lua_script_create(const char *path, obs_data_t *settings);
  32. extern bool obs_lua_script_load(obs_script_t *s);
  33. extern void obs_lua_script_unload(obs_script_t *s);
  34. extern void obs_lua_script_destroy(obs_script_t *s);
  35. extern void obs_lua_load(void);
  36. extern void obs_lua_unload(void);
  37. extern obs_properties_t *obs_lua_script_get_properties(obs_script_t *script);
  38. extern void obs_lua_script_update(obs_script_t *script, obs_data_t *settings);
  39. extern void obs_lua_script_save(obs_script_t *script);
  40. #endif
  41. #if defined(Python_FOUND)
  42. extern obs_script_t *obs_python_script_create(const char *path, obs_data_t *settings);
  43. extern bool obs_python_script_load(obs_script_t *s);
  44. extern void obs_python_script_unload(obs_script_t *s);
  45. extern void obs_python_script_destroy(obs_script_t *s);
  46. extern void obs_python_load(void);
  47. extern void obs_python_unload(void);
  48. extern obs_properties_t *obs_python_script_get_properties(obs_script_t *script);
  49. extern void obs_python_script_update(obs_script_t *script, obs_data_t *settings);
  50. extern void obs_python_script_save(obs_script_t *script);
  51. #endif
  52. pthread_mutex_t detach_mutex;
  53. struct script_callback *detached_callbacks;
  54. static struct dstr file_filter = {0};
  55. static bool scripting_loaded = false;
  56. static const char *supported_formats[] = {
  57. #if defined(LUAJIT_FOUND)
  58. "lua",
  59. #endif
  60. #if defined(Python_FOUND)
  61. "py",
  62. #endif
  63. NULL};
  64. /* -------------------------------------------- */
  65. static pthread_mutex_t defer_call_mutex;
  66. static struct deque defer_call_queue;
  67. static bool defer_call_exit = false;
  68. static os_sem_t *defer_call_semaphore;
  69. static pthread_t defer_call_thread;
  70. struct defer_call {
  71. defer_call_cb call;
  72. void *cb;
  73. };
  74. static void *defer_thread(void *unused)
  75. {
  76. UNUSED_PARAMETER(unused);
  77. os_set_thread_name("scripting: defer");
  78. while (os_sem_wait(defer_call_semaphore) == 0) {
  79. struct defer_call info;
  80. pthread_mutex_lock(&defer_call_mutex);
  81. if (defer_call_exit) {
  82. pthread_mutex_unlock(&defer_call_mutex);
  83. return NULL;
  84. }
  85. deque_pop_front(&defer_call_queue, &info, sizeof(info));
  86. pthread_mutex_unlock(&defer_call_mutex);
  87. info.call(info.cb);
  88. }
  89. return NULL;
  90. }
  91. void defer_call_post(defer_call_cb call, void *cb)
  92. {
  93. struct defer_call info;
  94. info.call = call;
  95. info.cb = cb;
  96. pthread_mutex_lock(&defer_call_mutex);
  97. if (!defer_call_exit)
  98. deque_push_back(&defer_call_queue, &info, sizeof(info));
  99. pthread_mutex_unlock(&defer_call_mutex);
  100. os_sem_post(defer_call_semaphore);
  101. }
  102. /* -------------------------------------------- */
  103. bool obs_scripting_load(void)
  104. {
  105. deque_init(&defer_call_queue);
  106. if (pthread_mutex_init(&detach_mutex, NULL) != 0) {
  107. return false;
  108. }
  109. if (pthread_mutex_init(&defer_call_mutex, NULL) != 0) {
  110. pthread_mutex_destroy(&detach_mutex);
  111. return false;
  112. }
  113. if (os_sem_init(&defer_call_semaphore, 0) != 0) {
  114. pthread_mutex_destroy(&defer_call_mutex);
  115. pthread_mutex_destroy(&detach_mutex);
  116. return false;
  117. }
  118. if (pthread_create(&defer_call_thread, NULL, defer_thread, NULL) != 0) {
  119. os_sem_destroy(defer_call_semaphore);
  120. pthread_mutex_destroy(&defer_call_mutex);
  121. pthread_mutex_destroy(&detach_mutex);
  122. return false;
  123. }
  124. #if defined(LUAJIT_FOUND)
  125. obs_lua_load();
  126. #endif
  127. #if defined(Python_FOUND)
  128. obs_python_load();
  129. #if !defined(_WIN32) && !defined(__APPLE__) /* Win32 and macOS need user-provided Python library paths */
  130. obs_scripting_load_python(NULL);
  131. #endif
  132. #endif
  133. scripting_loaded = true;
  134. return true;
  135. }
  136. void obs_scripting_unload(void)
  137. {
  138. if (!scripting_loaded)
  139. return;
  140. /* ---------------------- */
  141. #if defined(LUAJIT_FOUND)
  142. obs_lua_unload();
  143. #endif
  144. #if defined(Python_FOUND)
  145. obs_python_unload();
  146. #endif
  147. dstr_free(&file_filter);
  148. /* ---------------------- */
  149. int total_detached = 0;
  150. pthread_mutex_lock(&detach_mutex);
  151. struct script_callback *cur = detached_callbacks;
  152. while (cur) {
  153. struct script_callback *next = cur->next;
  154. just_free_script_callback(cur);
  155. cur = next;
  156. ++total_detached;
  157. }
  158. pthread_mutex_unlock(&detach_mutex);
  159. pthread_mutex_destroy(&detach_mutex);
  160. blog(LOG_INFO, "[Scripting] Total detached callbacks: %d", total_detached);
  161. /* ---------------------- */
  162. pthread_mutex_lock(&defer_call_mutex);
  163. /* TODO */
  164. defer_call_exit = true;
  165. deque_free(&defer_call_queue);
  166. pthread_mutex_unlock(&defer_call_mutex);
  167. os_sem_post(defer_call_semaphore);
  168. pthread_join(defer_call_thread, NULL);
  169. pthread_mutex_destroy(&defer_call_mutex);
  170. os_sem_destroy(defer_call_semaphore);
  171. scripting_loaded = false;
  172. }
  173. const char **obs_scripting_supported_formats(void)
  174. {
  175. return supported_formats;
  176. }
  177. static inline bool pointer_valid(const void *x, const char *name, const char *func)
  178. {
  179. if (!x) {
  180. blog(LOG_WARNING, "obs-scripting: [%s] %s is null", func, name);
  181. return false;
  182. }
  183. return true;
  184. }
  185. #define ptr_valid(x) pointer_valid(x, #x, __FUNCTION__)
  186. obs_script_t *obs_script_create(const char *path, obs_data_t *settings)
  187. {
  188. obs_script_t *script = NULL;
  189. const char *ext;
  190. if (!scripting_loaded)
  191. return NULL;
  192. if (!ptr_valid(path))
  193. return NULL;
  194. ext = strrchr(path, '.');
  195. if (!ext)
  196. return NULL;
  197. #if defined(LUAJIT_FOUND)
  198. if (strcmp(ext, ".lua") == 0) {
  199. script = obs_lua_script_create(path, settings);
  200. } else
  201. #endif
  202. #if defined(Python_FOUND)
  203. if (strcmp(ext, ".py") == 0) {
  204. script = obs_python_script_create(path, settings);
  205. } else
  206. #endif
  207. {
  208. blog(LOG_WARNING, "Unsupported/unknown script type: %s", path);
  209. }
  210. return script;
  211. }
  212. const char *obs_script_get_description(const obs_script_t *script)
  213. {
  214. return ptr_valid(script) ? script->desc.array : NULL;
  215. }
  216. const char *obs_script_get_path(const obs_script_t *script)
  217. {
  218. const char *path = ptr_valid(script) ? script->path.array : "";
  219. return path ? path : "";
  220. }
  221. const char *obs_script_get_file(const obs_script_t *script)
  222. {
  223. const char *file = ptr_valid(script) ? script->file.array : "";
  224. return file ? file : "";
  225. }
  226. enum obs_script_lang obs_script_get_lang(const obs_script_t *script)
  227. {
  228. return ptr_valid(script) ? script->type : OBS_SCRIPT_LANG_UNKNOWN;
  229. }
  230. obs_data_t *obs_script_get_settings(obs_script_t *script)
  231. {
  232. obs_data_t *settings;
  233. if (!ptr_valid(script))
  234. return NULL;
  235. settings = script->settings;
  236. obs_data_addref(settings);
  237. return settings;
  238. }
  239. obs_properties_t *obs_script_get_properties(obs_script_t *script)
  240. {
  241. obs_properties_t *props = NULL;
  242. if (!ptr_valid(script))
  243. return NULL;
  244. #if defined(LUAJIT_FOUND)
  245. if (script->type == OBS_SCRIPT_LANG_LUA) {
  246. props = obs_lua_script_get_properties(script);
  247. goto out;
  248. }
  249. #endif
  250. #if defined(Python_FOUND)
  251. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  252. props = obs_python_script_get_properties(script);
  253. goto out;
  254. }
  255. #endif
  256. out:
  257. if (!props)
  258. props = obs_properties_create();
  259. return props;
  260. }
  261. obs_data_t *obs_script_save(obs_script_t *script)
  262. {
  263. obs_data_t *settings;
  264. if (!ptr_valid(script))
  265. return NULL;
  266. #if defined(LUAJIT_FOUND)
  267. if (script->type == OBS_SCRIPT_LANG_LUA) {
  268. obs_lua_script_save(script);
  269. goto out;
  270. }
  271. #endif
  272. #if defined(Python_FOUND)
  273. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  274. obs_python_script_save(script);
  275. goto out;
  276. }
  277. #endif
  278. out:
  279. settings = script->settings;
  280. obs_data_addref(settings);
  281. return settings;
  282. }
  283. static void clear_queue_signal(void *p_event)
  284. {
  285. os_event_t *event = p_event;
  286. os_event_signal(event);
  287. }
  288. static void clear_call_queue(void)
  289. {
  290. os_event_t *event;
  291. if (os_event_init(&event, OS_EVENT_TYPE_AUTO) != 0)
  292. return;
  293. defer_call_post(clear_queue_signal, event);
  294. os_event_wait(event);
  295. os_event_destroy(event);
  296. }
  297. void obs_script_update(obs_script_t *script, obs_data_t *settings)
  298. {
  299. if (!ptr_valid(script))
  300. return;
  301. #if defined(LUAJIT_FOUND)
  302. if (script->type == OBS_SCRIPT_LANG_LUA) {
  303. obs_lua_script_update(script, settings);
  304. }
  305. #endif
  306. #if defined(Python_FOUND)
  307. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  308. obs_python_script_update(script, settings);
  309. }
  310. #endif
  311. }
  312. bool obs_script_reload(obs_script_t *script)
  313. {
  314. if (!scripting_loaded)
  315. return false;
  316. if (!ptr_valid(script))
  317. return false;
  318. #if defined(LUAJIT_FOUND)
  319. if (script->type == OBS_SCRIPT_LANG_LUA) {
  320. obs_lua_script_unload(script);
  321. clear_call_queue();
  322. obs_lua_script_load(script);
  323. goto out;
  324. }
  325. #endif
  326. #if defined(Python_FOUND)
  327. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  328. obs_python_script_unload(script);
  329. clear_call_queue();
  330. obs_python_script_load(script);
  331. goto out;
  332. }
  333. #endif
  334. out:
  335. return script->loaded;
  336. }
  337. bool obs_script_loaded(const obs_script_t *script)
  338. {
  339. return ptr_valid(script) ? script->loaded : false;
  340. }
  341. void obs_script_destroy(obs_script_t *script)
  342. {
  343. if (!script)
  344. return;
  345. #if defined(LUAJIT_FOUND)
  346. if (script->type == OBS_SCRIPT_LANG_LUA) {
  347. obs_lua_script_unload(script);
  348. obs_lua_script_destroy(script);
  349. return;
  350. }
  351. #endif
  352. #if defined(Python_FOUND)
  353. if (script->type == OBS_SCRIPT_LANG_PYTHON) {
  354. obs_python_script_unload(script);
  355. obs_python_script_destroy(script);
  356. return;
  357. }
  358. #endif
  359. }
  360. #if !defined(Python_FOUND)
  361. bool obs_scripting_load_python(const char *python_path)
  362. {
  363. UNUSED_PARAMETER(python_path);
  364. return false;
  365. }
  366. bool obs_scripting_python_loaded(void)
  367. {
  368. return false;
  369. }
  370. bool obs_scripting_python_runtime_linked(void)
  371. {
  372. return (bool)true;
  373. }
  374. void obs_scripting_python_version(char *version, size_t version_length)
  375. {
  376. UNUSED_PARAMETER(version_length);
  377. version[0] = 0;
  378. }
  379. #endif