1
0

obs-scripting.c 10 KB

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