obs-scripting-lua.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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-scripting-lua.h"
  15. #include <util/platform.h>
  16. #include <util/base.h>
  17. #include <util/dstr.h>
  18. #include <obs.h>
  19. /* ========================================================================= */
  20. #if ARCH_BITS == 64
  21. #define ARCH_DIR "64bit"
  22. #else
  23. #define ARCH_DIR "32bit"
  24. #endif
  25. #ifdef __APPLE__
  26. #define SO_EXT "so"
  27. #elif _WIN32
  28. #include <windows.h>
  29. #define SO_EXT "dll"
  30. #else
  31. #define SO_EXT "so"
  32. #endif
  33. static const char *startup_script_template = "\
  34. for val in pairs(package.preload) do\n\
  35. package.preload[val] = nil\n\
  36. end\n\
  37. %s\
  38. require \"obslua\"\n";
  39. static const char *get_script_path_func = "\
  40. function script_path()\n\
  41. return \"%s\"\n\
  42. end\n\
  43. package.cpath = package.cpath .. \";\" .. script_path() .. \"/?." SO_EXT "\"\n\
  44. package.path = package.path .. \";\" .. script_path() .. \"/?.lua\"\n";
  45. static char *startup_script = NULL;
  46. static pthread_mutex_t tick_mutex;
  47. static struct obs_lua_script *first_tick_script = NULL;
  48. pthread_mutex_t lua_source_def_mutex;
  49. #define ls_get_libobs_obj(type, lua_index, obs_obj) \
  50. ls_get_libobs_obj_(script, #type " *", lua_index, obs_obj, NULL, \
  51. __FUNCTION__, __LINE__)
  52. #define ls_push_libobs_obj(type, obs_obj, ownership) \
  53. ls_push_libobs_obj_(script, #type " *", obs_obj, ownership, NULL, \
  54. __FUNCTION__, __LINE__)
  55. #define call_func(name, args, rets) \
  56. call_func_(script, cb->reg_idx, args, rets, #name, __FUNCTION__)
  57. /* ========================================================================= */
  58. static void add_hook_functions(lua_State *script);
  59. static int obs_lua_remove_tick_callback(lua_State *script);
  60. static int obs_lua_remove_main_render_callback(lua_State *script);
  61. #ifdef ENABLE_UI
  62. void add_lua_frontend_funcs(lua_State *script);
  63. #endif
  64. static bool load_lua_script(struct obs_lua_script *data)
  65. {
  66. struct dstr str = {0};
  67. bool success = false;
  68. int ret;
  69. char *file_data;
  70. lua_State *script = luaL_newstate();
  71. if (!script) {
  72. script_warn(&data->base, "Failed to create new lua state");
  73. goto fail;
  74. }
  75. pthread_mutex_lock(&data->mutex);
  76. luaL_openlibs(script);
  77. luaopen_ffi(script);
  78. if (luaL_dostring(script, startup_script) != 0) {
  79. script_warn(&data->base, "Error executing startup script 1: %s",
  80. lua_tostring(script, -1));
  81. goto fail;
  82. }
  83. dstr_printf(&str, get_script_path_func, data->dir.array);
  84. ret = luaL_dostring(script, str.array);
  85. dstr_free(&str);
  86. if (ret != 0) {
  87. script_warn(&data->base, "Error executing startup script 2: %s",
  88. lua_tostring(script, -1));
  89. goto fail;
  90. }
  91. current_lua_script = data;
  92. add_lua_source_functions(script);
  93. add_hook_functions(script);
  94. #ifdef ENABLE_UI
  95. add_lua_frontend_funcs(script);
  96. #endif
  97. file_data = os_quick_read_utf8_file(data->base.path.array);
  98. if (!file_data) {
  99. script_warn(&data->base, "Error opening file: %s",
  100. lua_tostring(script, -1));
  101. goto fail;
  102. }
  103. if (luaL_loadbuffer(script, file_data, strlen(file_data),
  104. data->base.path.array) != 0) {
  105. script_warn(&data->base, "Error loading file: %s",
  106. lua_tostring(script, -1));
  107. bfree(file_data);
  108. goto fail;
  109. }
  110. bfree(file_data);
  111. if (lua_pcall(script, 0, LUA_MULTRET, 0) != 0) {
  112. script_warn(&data->base, "Error running file: %s",
  113. lua_tostring(script, -1));
  114. goto fail;
  115. }
  116. ret = lua_gettop(script);
  117. if (ret == 1 && lua_isboolean(script, -1)) {
  118. bool success = lua_toboolean(script, -1);
  119. if (!success) {
  120. goto fail;
  121. }
  122. }
  123. lua_getglobal(script, "script_properties");
  124. if (lua_isfunction(script, -1))
  125. data->get_properties = luaL_ref(script, LUA_REGISTRYINDEX);
  126. else
  127. data->get_properties = LUA_REFNIL;
  128. lua_getglobal(script, "script_update");
  129. if (lua_isfunction(script, -1))
  130. data->update = luaL_ref(script, LUA_REGISTRYINDEX);
  131. else
  132. data->update = LUA_REFNIL;
  133. lua_getglobal(script, "script_save");
  134. if (lua_isfunction(script, -1))
  135. data->save = luaL_ref(script, LUA_REGISTRYINDEX);
  136. else
  137. data->save = LUA_REFNIL;
  138. lua_getglobal(script, "script_defaults");
  139. if (lua_isfunction(script, -1)) {
  140. ls_push_libobs_obj(obs_data_t, data->base.settings, false);
  141. if (lua_pcall(script, 1, 0, 0) != 0) {
  142. script_warn(&data->base,
  143. "Error calling "
  144. "script_defaults: %s",
  145. lua_tostring(script, -1));
  146. }
  147. }
  148. lua_getglobal(script, "script_description");
  149. if (lua_isfunction(script, -1)) {
  150. if (lua_pcall(script, 0, 1, 0) != 0) {
  151. script_warn(&data->base,
  152. "Error calling "
  153. "script_defaults: %s",
  154. lua_tostring(script, -1));
  155. } else {
  156. const char *desc = lua_tostring(script, -1);
  157. dstr_copy(&data->base.desc, desc);
  158. }
  159. }
  160. lua_getglobal(script, "script_load");
  161. if (lua_isfunction(script, -1)) {
  162. ls_push_libobs_obj(obs_data_t, data->base.settings, false);
  163. if (lua_pcall(script, 1, 0, 0) != 0) {
  164. script_warn(&data->base,
  165. "Error calling "
  166. "script_load: %s",
  167. lua_tostring(script, -1));
  168. }
  169. }
  170. data->script = script;
  171. lua_getglobal(script, "script_tick");
  172. if (lua_isfunction(script, -1)) {
  173. pthread_mutex_lock(&tick_mutex);
  174. struct obs_lua_script *next = first_tick_script;
  175. data->next_tick = next;
  176. data->p_prev_next_tick = &first_tick_script;
  177. if (next)
  178. next->p_prev_next_tick = &data->next_tick;
  179. first_tick_script = data;
  180. data->tick = luaL_ref(script, LUA_REGISTRYINDEX);
  181. pthread_mutex_unlock(&tick_mutex);
  182. }
  183. success = true;
  184. fail:
  185. if (script) {
  186. lua_settop(script, 0);
  187. pthread_mutex_unlock(&data->mutex);
  188. }
  189. if (!success && script) {
  190. lua_close(script);
  191. }
  192. current_lua_script = NULL;
  193. return success;
  194. }
  195. /* -------------------------------------------- */
  196. THREAD_LOCAL struct lua_obs_callback *current_lua_cb = NULL;
  197. THREAD_LOCAL struct obs_lua_script *current_lua_script = NULL;
  198. /* -------------------------------------------- */
  199. struct lua_obs_timer {
  200. struct lua_obs_timer *next;
  201. struct lua_obs_timer **p_prev_next;
  202. uint64_t last_ts;
  203. uint64_t interval;
  204. };
  205. static pthread_mutex_t timer_mutex;
  206. static struct lua_obs_timer *first_timer = NULL;
  207. static inline void lua_obs_timer_init(struct lua_obs_timer *timer)
  208. {
  209. pthread_mutex_lock(&timer_mutex);
  210. struct lua_obs_timer *next = first_timer;
  211. timer->next = next;
  212. timer->p_prev_next = &first_timer;
  213. if (next)
  214. next->p_prev_next = &timer->next;
  215. first_timer = timer;
  216. pthread_mutex_unlock(&timer_mutex);
  217. }
  218. static inline void lua_obs_timer_remove(struct lua_obs_timer *timer)
  219. {
  220. struct lua_obs_timer *next = timer->next;
  221. if (next)
  222. next->p_prev_next = timer->p_prev_next;
  223. *timer->p_prev_next = timer->next;
  224. }
  225. static inline struct lua_obs_callback *
  226. lua_obs_timer_cb(struct lua_obs_timer *timer)
  227. {
  228. return &((struct lua_obs_callback *)timer)[-1];
  229. }
  230. static int timer_remove(lua_State *script)
  231. {
  232. if (!is_function(script, 1))
  233. return 0;
  234. struct lua_obs_callback *cb = find_lua_obs_callback(script, 1);
  235. if (cb)
  236. remove_lua_obs_callback(cb);
  237. return 0;
  238. }
  239. static void timer_call(struct script_callback *p_cb)
  240. {
  241. struct lua_obs_callback *cb = (struct lua_obs_callback *)p_cb;
  242. if (script_callback_removed(p_cb))
  243. return;
  244. lock_callback();
  245. call_func_(cb->script, cb->reg_idx, 0, 0, "timer_cb", __FUNCTION__);
  246. unlock_callback();
  247. }
  248. static void defer_timer_init(void *p_cb)
  249. {
  250. struct lua_obs_callback *cb = p_cb;
  251. struct lua_obs_timer *timer = lua_obs_callback_extra_data(cb);
  252. lua_obs_timer_init(timer);
  253. }
  254. static int timer_add(lua_State *script)
  255. {
  256. if (!is_function(script, 1))
  257. return 0;
  258. int ms = (int)lua_tointeger(script, 2);
  259. if (!ms)
  260. return 0;
  261. struct lua_obs_callback *cb = add_lua_obs_callback_extra(
  262. script, 1, sizeof(struct lua_obs_timer));
  263. struct lua_obs_timer *timer = lua_obs_callback_extra_data(cb);
  264. timer->interval = (uint64_t)ms * 1000000ULL;
  265. timer->last_ts = obs_get_video_frame_time();
  266. defer_call_post(defer_timer_init, cb);
  267. return 0;
  268. }
  269. /* -------------------------------------------- */
  270. static void obs_lua_main_render_callback(void *priv, uint32_t cx, uint32_t cy)
  271. {
  272. struct lua_obs_callback *cb = priv;
  273. lua_State *script = cb->script;
  274. if (script_callback_removed(&cb->base)) {
  275. obs_remove_main_render_callback(obs_lua_main_render_callback,
  276. cb);
  277. return;
  278. }
  279. lock_callback();
  280. lua_pushinteger(script, (lua_Integer)cx);
  281. lua_pushinteger(script, (lua_Integer)cy);
  282. call_func(obs_lua_main_render_callback, 2, 0);
  283. unlock_callback();
  284. }
  285. static int obs_lua_remove_main_render_callback(lua_State *script)
  286. {
  287. if (!verify_args1(script, is_function))
  288. return 0;
  289. struct lua_obs_callback *cb = find_lua_obs_callback(script, 1);
  290. if (cb)
  291. remove_lua_obs_callback(cb);
  292. return 0;
  293. }
  294. static void defer_add_render(void *cb)
  295. {
  296. obs_add_main_render_callback(obs_lua_main_render_callback, cb);
  297. }
  298. static int obs_lua_add_main_render_callback(lua_State *script)
  299. {
  300. if (!verify_args1(script, is_function))
  301. return 0;
  302. struct lua_obs_callback *cb = add_lua_obs_callback(script, 1);
  303. defer_call_post(defer_add_render, cb);
  304. return 0;
  305. }
  306. /* -------------------------------------------- */
  307. static void obs_lua_tick_callback(void *priv, float seconds)
  308. {
  309. struct lua_obs_callback *cb = priv;
  310. lua_State *script = cb->script;
  311. if (script_callback_removed(&cb->base)) {
  312. obs_remove_tick_callback(obs_lua_tick_callback, cb);
  313. return;
  314. }
  315. lock_callback();
  316. lua_pushnumber(script, (lua_Number)seconds);
  317. call_func(obs_lua_tick_callback, 1, 0);
  318. unlock_callback();
  319. }
  320. static int obs_lua_remove_tick_callback(lua_State *script)
  321. {
  322. if (!verify_args1(script, is_function))
  323. return 0;
  324. struct lua_obs_callback *cb = find_lua_obs_callback(script, 1);
  325. if (cb)
  326. remove_lua_obs_callback(cb);
  327. return 0;
  328. }
  329. static void defer_add_tick(void *cb)
  330. {
  331. obs_add_tick_callback(obs_lua_tick_callback, cb);
  332. }
  333. static int obs_lua_add_tick_callback(lua_State *script)
  334. {
  335. if (!verify_args1(script, is_function))
  336. return 0;
  337. struct lua_obs_callback *cb = add_lua_obs_callback(script, 1);
  338. defer_call_post(defer_add_tick, cb);
  339. return 0;
  340. }
  341. /* -------------------------------------------- */
  342. static void calldata_signal_callback(void *priv, calldata_t *cd)
  343. {
  344. struct lua_obs_callback *cb = priv;
  345. lua_State *script = cb->script;
  346. if (script_callback_removed(&cb->base)) {
  347. signal_handler_remove_current();
  348. return;
  349. }
  350. lock_callback();
  351. ls_push_libobs_obj(calldata_t, cd, false);
  352. call_func(calldata_signal_callback, 1, 0);
  353. unlock_callback();
  354. }
  355. static int obs_lua_signal_handler_disconnect(lua_State *script)
  356. {
  357. signal_handler_t *handler;
  358. const char *signal;
  359. if (!ls_get_libobs_obj(signal_handler_t, 1, &handler))
  360. return 0;
  361. signal = lua_tostring(script, 2);
  362. if (!signal)
  363. return 0;
  364. if (!is_function(script, 3))
  365. return 0;
  366. struct lua_obs_callback *cb = find_lua_obs_callback(script, 3);
  367. while (cb) {
  368. signal_handler_t *cb_handler =
  369. calldata_ptr(&cb->base.extra, "handler");
  370. const char *cb_signal =
  371. calldata_string(&cb->base.extra, "signal");
  372. if (cb_signal && strcmp(signal, cb_signal) == 0 &&
  373. handler == cb_handler)
  374. break;
  375. cb = find_next_lua_obs_callback(script, cb, 3);
  376. }
  377. if (cb)
  378. remove_lua_obs_callback(cb);
  379. return 0;
  380. }
  381. static void defer_connect(void *p_cb)
  382. {
  383. struct script_callback *cb = p_cb;
  384. signal_handler_t *handler = calldata_ptr(&cb->extra, "handler");
  385. const char *signal = calldata_string(&cb->extra, "signal");
  386. signal_handler_connect(handler, signal, calldata_signal_callback, cb);
  387. }
  388. static int obs_lua_signal_handler_connect(lua_State *script)
  389. {
  390. signal_handler_t *handler;
  391. const char *signal;
  392. if (!ls_get_libobs_obj(signal_handler_t, 1, &handler))
  393. return 0;
  394. signal = lua_tostring(script, 2);
  395. if (!signal)
  396. return 0;
  397. if (!is_function(script, 3))
  398. return 0;
  399. struct lua_obs_callback *cb = add_lua_obs_callback(script, 3);
  400. calldata_set_ptr(&cb->base.extra, "handler", handler);
  401. calldata_set_string(&cb->base.extra, "signal", signal);
  402. defer_call_post(defer_connect, cb);
  403. return 0;
  404. }
  405. /* -------------------------------------------- */
  406. static void calldata_signal_callback_global(void *priv, const char *signal,
  407. calldata_t *cd)
  408. {
  409. struct lua_obs_callback *cb = priv;
  410. lua_State *script = cb->script;
  411. if (script_callback_removed(&cb->base)) {
  412. signal_handler_remove_current();
  413. return;
  414. }
  415. lock_callback();
  416. lua_pushstring(script, signal);
  417. ls_push_libobs_obj(calldata_t, cd, false);
  418. call_func(calldata_signal_callback_global, 2, 0);
  419. unlock_callback();
  420. }
  421. static int obs_lua_signal_handler_disconnect_global(lua_State *script)
  422. {
  423. signal_handler_t *handler;
  424. if (!ls_get_libobs_obj(signal_handler_t, 1, &handler))
  425. return 0;
  426. if (!is_function(script, 2))
  427. return 0;
  428. struct lua_obs_callback *cb = find_lua_obs_callback(script, 3);
  429. while (cb) {
  430. signal_handler_t *cb_handler =
  431. calldata_ptr(&cb->base.extra, "handler");
  432. if (handler == cb_handler)
  433. break;
  434. cb = find_next_lua_obs_callback(script, cb, 3);
  435. }
  436. if (cb)
  437. remove_lua_obs_callback(cb);
  438. return 0;
  439. }
  440. static void defer_connect_global(void *p_cb)
  441. {
  442. struct script_callback *cb = p_cb;
  443. signal_handler_t *handler = calldata_ptr(&cb->extra, "handler");
  444. signal_handler_connect_global(handler, calldata_signal_callback_global,
  445. cb);
  446. }
  447. static int obs_lua_signal_handler_connect_global(lua_State *script)
  448. {
  449. signal_handler_t *handler;
  450. if (!ls_get_libobs_obj(signal_handler_t, 1, &handler))
  451. return 0;
  452. if (!is_function(script, 2))
  453. return 0;
  454. struct lua_obs_callback *cb = add_lua_obs_callback(script, 2);
  455. calldata_set_ptr(&cb->base.extra, "handler", handler);
  456. defer_call_post(defer_connect_global, cb);
  457. return 0;
  458. }
  459. /* -------------------------------------------- */
  460. static bool enum_sources_proc(void *param, obs_source_t *source)
  461. {
  462. lua_State *script = param;
  463. obs_source_get_ref(source);
  464. ls_push_libobs_obj(obs_source_t, source, false);
  465. size_t idx = lua_rawlen(script, -2);
  466. lua_rawseti(script, -2, (int)idx + 1);
  467. return true;
  468. }
  469. static int enum_sources(lua_State *script)
  470. {
  471. lua_newtable(script);
  472. obs_enum_sources(enum_sources_proc, script);
  473. return 1;
  474. }
  475. /* -------------------------------------------- */
  476. static void source_enum_filters_proc(obs_source_t *source, obs_source_t *filter,
  477. void *param)
  478. {
  479. UNUSED_PARAMETER(source);
  480. lua_State *script = param;
  481. obs_source_get_ref(filter);
  482. ls_push_libobs_obj(obs_source_t, filter, false);
  483. size_t idx = lua_rawlen(script, -2);
  484. lua_rawseti(script, -2, (int)idx + 1);
  485. }
  486. static int source_enum_filters(lua_State *script)
  487. {
  488. obs_source_t *source;
  489. if (!ls_get_libobs_obj(obs_source_t, 1, &source))
  490. return 0;
  491. lua_newtable(script);
  492. obs_source_enum_filters(source, source_enum_filters_proc, script);
  493. return 1;
  494. }
  495. /* -------------------------------------------- */
  496. static bool enum_items_proc(obs_scene_t *scene, obs_sceneitem_t *item,
  497. void *param)
  498. {
  499. lua_State *script = param;
  500. UNUSED_PARAMETER(scene);
  501. obs_sceneitem_addref(item);
  502. ls_push_libobs_obj(obs_sceneitem_t, item, false);
  503. lua_rawseti(script, -2, (int)lua_rawlen(script, -2) + 1);
  504. return true;
  505. }
  506. static int scene_enum_items(lua_State *script)
  507. {
  508. obs_scene_t *scene;
  509. if (!ls_get_libobs_obj(obs_scene_t, 1, &scene))
  510. return 0;
  511. lua_newtable(script);
  512. obs_scene_enum_items(scene, enum_items_proc, script);
  513. return 1;
  514. }
  515. static int sceneitem_group_enum_items(lua_State *script)
  516. {
  517. obs_sceneitem_t *sceneitem;
  518. if (!ls_get_libobs_obj(obs_sceneitem_t, 1, &sceneitem))
  519. return 0;
  520. lua_newtable(script);
  521. obs_sceneitem_group_enum_items(sceneitem, enum_items_proc, script);
  522. return 1;
  523. }
  524. /* -------------------------------------------- */
  525. static void defer_hotkey_unregister(void *p_cb)
  526. {
  527. obs_hotkey_unregister((obs_hotkey_id)(uintptr_t)p_cb);
  528. }
  529. static void on_remove_hotkey(void *p_cb)
  530. {
  531. struct lua_obs_callback *cb = p_cb;
  532. obs_hotkey_id id = (obs_hotkey_id)calldata_int(&cb->base.extra, "id");
  533. if (id != OBS_INVALID_HOTKEY_ID)
  534. defer_call_post(defer_hotkey_unregister, (void *)(uintptr_t)id);
  535. }
  536. static void hotkey_pressed(void *p_cb, bool pressed)
  537. {
  538. struct lua_obs_callback *cb = p_cb;
  539. lua_State *script = cb->script;
  540. if (script_callback_removed(&cb->base))
  541. return;
  542. lock_callback();
  543. lua_pushboolean(script, pressed);
  544. call_func(hotkey_pressed, 1, 0);
  545. unlock_callback();
  546. }
  547. static void defer_hotkey_pressed(void *p_cb)
  548. {
  549. hotkey_pressed(p_cb, true);
  550. }
  551. static void defer_hotkey_unpressed(void *p_cb)
  552. {
  553. hotkey_pressed(p_cb, false);
  554. }
  555. static void hotkey_callback(void *p_cb, obs_hotkey_id id, obs_hotkey_t *hotkey,
  556. bool pressed)
  557. {
  558. struct lua_obs_callback *cb = p_cb;
  559. if (script_callback_removed(&cb->base))
  560. return;
  561. if (pressed)
  562. defer_call_post(defer_hotkey_pressed, cb);
  563. else
  564. defer_call_post(defer_hotkey_unpressed, cb);
  565. UNUSED_PARAMETER(hotkey);
  566. UNUSED_PARAMETER(id);
  567. }
  568. static int hotkey_unregister(lua_State *script)
  569. {
  570. if (!verify_args1(script, is_function))
  571. return 0;
  572. struct lua_obs_callback *cb = find_lua_obs_callback(script, 1);
  573. if (cb)
  574. remove_lua_obs_callback(cb);
  575. return 0;
  576. }
  577. static int hotkey_register_frontend(lua_State *script)
  578. {
  579. obs_hotkey_id id;
  580. const char *name = lua_tostring(script, 1);
  581. if (!name)
  582. return 0;
  583. const char *desc = lua_tostring(script, 2);
  584. if (!desc)
  585. return 0;
  586. if (!is_function(script, 3))
  587. return 0;
  588. struct lua_obs_callback *cb = add_lua_obs_callback(script, 3);
  589. cb->base.on_remove = on_remove_hotkey;
  590. id = obs_hotkey_register_frontend(name, desc, hotkey_callback, cb);
  591. calldata_set_int(&cb->base.extra, "id", id);
  592. lua_pushinteger(script, (lua_Integer)id);
  593. if (id == OBS_INVALID_HOTKEY_ID)
  594. remove_lua_obs_callback(cb);
  595. return 1;
  596. }
  597. /* -------------------------------------------- */
  598. static bool button_prop_clicked(obs_properties_t *props, obs_property_t *p,
  599. void *p_cb)
  600. {
  601. struct lua_obs_callback *cb = p_cb;
  602. lua_State *script = cb->script;
  603. bool ret = false;
  604. if (script_callback_removed(&cb->base))
  605. return false;
  606. lock_callback();
  607. if (!ls_push_libobs_obj(obs_properties_t, props, false))
  608. goto fail;
  609. if (!ls_push_libobs_obj(obs_property_t, p, false)) {
  610. lua_pop(script, 1);
  611. goto fail;
  612. }
  613. call_func(button_prop_clicked, 2, 1);
  614. if (lua_isboolean(script, -1))
  615. ret = lua_toboolean(script, -1);
  616. fail:
  617. unlock_callback();
  618. return ret;
  619. }
  620. static int properties_add_button(lua_State *script)
  621. {
  622. obs_properties_t *props;
  623. obs_property_t *p;
  624. if (!ls_get_libobs_obj(obs_properties_t, 1, &props))
  625. return 0;
  626. const char *name = lua_tostring(script, 2);
  627. if (!name)
  628. return 0;
  629. const char *text = lua_tostring(script, 3);
  630. if (!text)
  631. return 0;
  632. if (!is_function(script, 4))
  633. return 0;
  634. struct lua_obs_callback *cb = add_lua_obs_callback(script, 4);
  635. p = obs_properties_add_button2(props, name, text, button_prop_clicked,
  636. cb);
  637. if (!p || !ls_push_libobs_obj(obs_property_t, p, false))
  638. return 0;
  639. return 1;
  640. }
  641. /* -------------------------------------------- */
  642. static bool modified_callback(void *p_cb, obs_properties_t *props,
  643. obs_property_t *p, obs_data_t *settings)
  644. {
  645. struct lua_obs_callback *cb = p_cb;
  646. lua_State *script = cb->script;
  647. bool ret = false;
  648. if (script_callback_removed(&cb->base))
  649. return false;
  650. lock_callback();
  651. if (!ls_push_libobs_obj(obs_properties_t, props, false)) {
  652. goto fail;
  653. }
  654. if (!ls_push_libobs_obj(obs_property_t, p, false)) {
  655. lua_pop(script, 1);
  656. goto fail;
  657. }
  658. if (!ls_push_libobs_obj(obs_data_t, settings, false)) {
  659. lua_pop(script, 2);
  660. goto fail;
  661. }
  662. call_func(modified_callback, 3, 1);
  663. if (lua_isboolean(script, -1))
  664. ret = lua_toboolean(script, -1);
  665. fail:
  666. unlock_callback();
  667. return ret;
  668. }
  669. static int property_set_modified_callback(lua_State *script)
  670. {
  671. obs_property_t *p;
  672. if (!ls_get_libobs_obj(obs_property_t, 1, &p))
  673. return 0;
  674. if (!is_function(script, 2))
  675. return 0;
  676. struct lua_obs_callback *cb = add_lua_obs_callback(script, 2);
  677. obs_property_set_modified_callback2(p, modified_callback, cb);
  678. return 0;
  679. }
  680. /* -------------------------------------------- */
  681. static int remove_current_callback(lua_State *script)
  682. {
  683. UNUSED_PARAMETER(script);
  684. if (current_lua_cb)
  685. remove_lua_obs_callback(current_lua_cb);
  686. return 0;
  687. }
  688. /* -------------------------------------------- */
  689. static int calldata_source(lua_State *script)
  690. {
  691. calldata_t *cd;
  692. const char *str;
  693. int ret = 0;
  694. if (!ls_get_libobs_obj(calldata_t, 1, &cd))
  695. goto fail;
  696. str = lua_tostring(script, 2);
  697. if (!str)
  698. goto fail;
  699. obs_source_t *source = calldata_ptr(cd, str);
  700. if (ls_push_libobs_obj(obs_source_t, source, false))
  701. ++ret;
  702. fail:
  703. return ret;
  704. }
  705. static int calldata_sceneitem(lua_State *script)
  706. {
  707. calldata_t *cd;
  708. const char *str;
  709. int ret = 0;
  710. if (!ls_get_libobs_obj(calldata_t, 1, &cd))
  711. goto fail;
  712. str = lua_tostring(script, 2);
  713. if (!str)
  714. goto fail;
  715. obs_sceneitem_t *sceneitem = calldata_ptr(cd, str);
  716. if (ls_push_libobs_obj(obs_sceneitem_t, sceneitem, false))
  717. ++ret;
  718. fail:
  719. return ret;
  720. }
  721. /* -------------------------------------------- */
  722. static int source_list_release(lua_State *script)
  723. {
  724. size_t count = lua_rawlen(script, 1);
  725. for (size_t i = 0; i < count; i++) {
  726. obs_source_t *source;
  727. lua_rawgeti(script, 1, (int)i + 1);
  728. ls_get_libobs_obj(obs_source_t, -1, &source);
  729. lua_pop(script, 1);
  730. obs_source_release(source);
  731. }
  732. return 0;
  733. }
  734. static int sceneitem_list_release(lua_State *script)
  735. {
  736. size_t count = lua_rawlen(script, 1);
  737. for (size_t i = 0; i < count; i++) {
  738. obs_sceneitem_t *item;
  739. lua_rawgeti(script, 1, (int)i + 1);
  740. ls_get_libobs_obj(obs_sceneitem_t, -1, &item);
  741. lua_pop(script, 1);
  742. obs_sceneitem_release(item);
  743. }
  744. return 0;
  745. }
  746. /* -------------------------------------------- */
  747. static int hook_print(lua_State *script)
  748. {
  749. struct obs_lua_script *data = current_lua_script;
  750. const char *msg = lua_tostring(script, 1);
  751. if (!msg)
  752. return 0;
  753. script_info(&data->base, "%s", msg);
  754. return 0;
  755. }
  756. static int hook_error(lua_State *script)
  757. {
  758. struct obs_lua_script *data = current_lua_script;
  759. const char *msg = lua_tostring(script, 1);
  760. if (!msg)
  761. return 0;
  762. script_error(&data->base, "%s", msg);
  763. return 0;
  764. }
  765. /* -------------------------------------------- */
  766. static int lua_script_log(lua_State *script)
  767. {
  768. struct obs_lua_script *data = current_lua_script;
  769. int log_level = (int)lua_tointeger(script, 1);
  770. const char *msg = lua_tostring(script, 2);
  771. if (!msg)
  772. return 0;
  773. /* ------------------- */
  774. dstr_copy(&data->log_chunk, msg);
  775. const char *start = data->log_chunk.array;
  776. char *endl = strchr(start, '\n');
  777. while (endl) {
  778. *endl = 0;
  779. script_log(&data->base, log_level, "%s", start);
  780. *endl = '\n';
  781. start = endl + 1;
  782. endl = strchr(start, '\n');
  783. }
  784. if (start && *start)
  785. script_log(&data->base, log_level, "%s", start);
  786. dstr_resize(&data->log_chunk, 0);
  787. /* ------------------- */
  788. return 0;
  789. }
  790. /* -------------------------------------------- */
  791. static void add_hook_functions(lua_State *script)
  792. {
  793. #define add_func(name, func) \
  794. do { \
  795. lua_pushstring(script, name); \
  796. lua_pushcfunction(script, func); \
  797. lua_rawset(script, -3); \
  798. } while (false)
  799. lua_getglobal(script, "_G");
  800. add_func("print", hook_print);
  801. add_func("error", hook_error);
  802. lua_pop(script, 1);
  803. /* ------------- */
  804. lua_getglobal(script, "obslua");
  805. add_func("script_log", lua_script_log);
  806. add_func("timer_remove", timer_remove);
  807. add_func("timer_add", timer_add);
  808. add_func("obs_enum_sources", enum_sources);
  809. add_func("obs_source_enum_filters", source_enum_filters);
  810. add_func("obs_scene_enum_items", scene_enum_items);
  811. add_func("obs_sceneitem_group_enum_items", sceneitem_group_enum_items);
  812. add_func("source_list_release", source_list_release);
  813. add_func("sceneitem_list_release", sceneitem_list_release);
  814. add_func("calldata_source", calldata_source);
  815. add_func("calldata_sceneitem", calldata_sceneitem);
  816. add_func("obs_add_main_render_callback",
  817. obs_lua_add_main_render_callback);
  818. add_func("obs_remove_main_render_callback",
  819. obs_lua_remove_main_render_callback);
  820. add_func("obs_add_tick_callback", obs_lua_add_tick_callback);
  821. add_func("obs_remove_tick_callback", obs_lua_remove_tick_callback);
  822. add_func("signal_handler_connect", obs_lua_signal_handler_connect);
  823. add_func("signal_handler_disconnect",
  824. obs_lua_signal_handler_disconnect);
  825. add_func("signal_handler_connect_global",
  826. obs_lua_signal_handler_connect_global);
  827. add_func("signal_handler_disconnect_global",
  828. obs_lua_signal_handler_disconnect_global);
  829. add_func("obs_hotkey_unregister", hotkey_unregister);
  830. add_func("obs_hotkey_register_frontend", hotkey_register_frontend);
  831. add_func("obs_properties_add_button", properties_add_button);
  832. add_func("obs_property_set_modified_callback",
  833. property_set_modified_callback);
  834. add_func("remove_current_callback", remove_current_callback);
  835. lua_pop(script, 1);
  836. #undef add_func
  837. }
  838. /* -------------------------------------------- */
  839. static void lua_tick(void *param, float seconds)
  840. {
  841. struct obs_lua_script *data;
  842. struct lua_obs_timer *timer;
  843. uint64_t ts = obs_get_video_frame_time();
  844. /* --------------------------------- */
  845. /* process script_tick calls */
  846. pthread_mutex_lock(&tick_mutex);
  847. data = first_tick_script;
  848. while (data) {
  849. lua_State *script = data->script;
  850. current_lua_script = data;
  851. pthread_mutex_lock(&data->mutex);
  852. lua_pushnumber(script, (double)seconds);
  853. call_func_(script, data->tick, 1, 0, "tick", __FUNCTION__);
  854. pthread_mutex_unlock(&data->mutex);
  855. data = data->next_tick;
  856. }
  857. current_lua_script = NULL;
  858. pthread_mutex_unlock(&tick_mutex);
  859. /* --------------------------------- */
  860. /* process timers */
  861. pthread_mutex_lock(&timer_mutex);
  862. timer = first_timer;
  863. while (timer) {
  864. struct lua_obs_timer *next = timer->next;
  865. struct lua_obs_callback *cb = lua_obs_timer_cb(timer);
  866. if (script_callback_removed(&cb->base)) {
  867. lua_obs_timer_remove(timer);
  868. } else {
  869. uint64_t elapsed = ts - timer->last_ts;
  870. if (elapsed >= timer->interval) {
  871. timer_call(&cb->base);
  872. timer->last_ts += timer->interval;
  873. }
  874. }
  875. timer = next;
  876. }
  877. pthread_mutex_unlock(&timer_mutex);
  878. UNUSED_PARAMETER(param);
  879. }
  880. /* -------------------------------------------- */
  881. void obs_lua_script_update(obs_script_t *script, obs_data_t *settings);
  882. bool obs_lua_script_load(obs_script_t *s)
  883. {
  884. struct obs_lua_script *data = (struct obs_lua_script *)s;
  885. if (!data->base.loaded) {
  886. data->base.loaded = load_lua_script(data);
  887. if (data->base.loaded) {
  888. blog(LOG_INFO, "[obs-scripting]: Loaded lua script: %s",
  889. data->base.file.array);
  890. obs_lua_script_update(s, NULL);
  891. }
  892. }
  893. return data->base.loaded;
  894. }
  895. obs_script_t *obs_lua_script_create(const char *path, obs_data_t *settings)
  896. {
  897. struct obs_lua_script *data = bzalloc(sizeof(*data));
  898. data->base.type = OBS_SCRIPT_LANG_LUA;
  899. data->tick = LUA_REFNIL;
  900. pthread_mutex_init_value(&data->mutex);
  901. if (pthread_mutex_init_recursive(&data->mutex) != 0) {
  902. bfree(data);
  903. return NULL;
  904. }
  905. dstr_copy(&data->base.path, path);
  906. char *slash = path && *path ? strrchr(path, '/') : NULL;
  907. if (slash) {
  908. slash++;
  909. dstr_copy(&data->base.file, slash);
  910. dstr_left(&data->dir, &data->base.path, slash - path);
  911. } else {
  912. dstr_copy(&data->base.file, path);
  913. }
  914. data->base.settings = obs_data_create();
  915. if (settings)
  916. obs_data_apply(data->base.settings, settings);
  917. obs_lua_script_load((obs_script_t *)data);
  918. return (obs_script_t *)data;
  919. }
  920. extern void undef_lua_script_sources(struct obs_lua_script *data);
  921. void obs_lua_script_unload(obs_script_t *s)
  922. {
  923. struct obs_lua_script *data = (struct obs_lua_script *)s;
  924. if (!s->loaded)
  925. return;
  926. lua_State *script = data->script;
  927. /* ---------------------------- */
  928. /* mark callbacks as removed */
  929. pthread_mutex_lock(&data->mutex);
  930. /* XXX: scripts can potentially make callbacks when this happens, so
  931. * this probably still isn't ideal as we can't predict how the
  932. * processor or operating system is going to schedule things. a more
  933. * ideal method would be to reference count the script objects and
  934. * atomically share ownership with callbacks when they're called. */
  935. struct lua_obs_callback *cb =
  936. (struct lua_obs_callback *)data->first_callback;
  937. while (cb) {
  938. os_atomic_set_bool(&cb->base.removed, true);
  939. cb = (struct lua_obs_callback *)cb->base.next;
  940. }
  941. pthread_mutex_unlock(&data->mutex);
  942. /* ---------------------------- */
  943. /* undefine source types */
  944. undef_lua_script_sources(data);
  945. /* ---------------------------- */
  946. /* unhook tick function */
  947. if (data->p_prev_next_tick) {
  948. pthread_mutex_lock(&tick_mutex);
  949. struct obs_lua_script *next = data->next_tick;
  950. if (next)
  951. next->p_prev_next_tick = data->p_prev_next_tick;
  952. *data->p_prev_next_tick = next;
  953. pthread_mutex_unlock(&tick_mutex);
  954. data->p_prev_next_tick = NULL;
  955. data->next_tick = NULL;
  956. }
  957. /* ---------------------------- */
  958. /* call script_unload */
  959. pthread_mutex_lock(&data->mutex);
  960. current_lua_script = data;
  961. lua_getglobal(script, "script_unload");
  962. lua_pcall(script, 0, 0, 0);
  963. current_lua_script = NULL;
  964. /* ---------------------------- */
  965. /* remove all callbacks */
  966. cb = (struct lua_obs_callback *)data->first_callback;
  967. while (cb) {
  968. struct lua_obs_callback *next =
  969. (struct lua_obs_callback *)cb->base.next;
  970. remove_lua_obs_callback(cb);
  971. cb = next;
  972. }
  973. pthread_mutex_unlock(&data->mutex);
  974. /* ---------------------------- */
  975. /* close script */
  976. lua_close(script);
  977. s->loaded = false;
  978. blog(LOG_INFO, "[obs-scripting]: Unloaded lua script: %s",
  979. data->base.file.array);
  980. }
  981. void obs_lua_script_destroy(obs_script_t *s)
  982. {
  983. struct obs_lua_script *data = (struct obs_lua_script *)s;
  984. if (data) {
  985. pthread_mutex_destroy(&data->mutex);
  986. dstr_free(&data->base.path);
  987. dstr_free(&data->base.file);
  988. dstr_free(&data->base.desc);
  989. obs_data_release(data->base.settings);
  990. dstr_free(&data->log_chunk);
  991. dstr_free(&data->dir);
  992. bfree(data);
  993. }
  994. }
  995. void obs_lua_script_update(obs_script_t *s, obs_data_t *settings)
  996. {
  997. struct obs_lua_script *data = (struct obs_lua_script *)s;
  998. lua_State *script = data->script;
  999. if (!s->loaded)
  1000. return;
  1001. if (data->update == LUA_REFNIL)
  1002. return;
  1003. if (settings)
  1004. obs_data_apply(s->settings, settings);
  1005. current_lua_script = data;
  1006. pthread_mutex_lock(&data->mutex);
  1007. ls_push_libobs_obj(obs_data_t, s->settings, false);
  1008. call_func_(script, data->update, 1, 0, "script_update", __FUNCTION__);
  1009. pthread_mutex_unlock(&data->mutex);
  1010. current_lua_script = NULL;
  1011. }
  1012. obs_properties_t *obs_lua_script_get_properties(obs_script_t *s)
  1013. {
  1014. struct obs_lua_script *data = (struct obs_lua_script *)s;
  1015. lua_State *script = data->script;
  1016. obs_properties_t *props = NULL;
  1017. if (!s->loaded)
  1018. return NULL;
  1019. if (data->get_properties == LUA_REFNIL)
  1020. return NULL;
  1021. current_lua_script = data;
  1022. pthread_mutex_lock(&data->mutex);
  1023. call_func_(script, data->get_properties, 0, 1, "script_properties",
  1024. __FUNCTION__);
  1025. ls_get_libobs_obj(obs_properties_t, -1, &props);
  1026. pthread_mutex_unlock(&data->mutex);
  1027. current_lua_script = NULL;
  1028. return props;
  1029. }
  1030. void obs_lua_script_save(obs_script_t *s)
  1031. {
  1032. struct obs_lua_script *data = (struct obs_lua_script *)s;
  1033. lua_State *script = data->script;
  1034. if (!s->loaded)
  1035. return;
  1036. if (data->save == LUA_REFNIL)
  1037. return;
  1038. current_lua_script = data;
  1039. pthread_mutex_lock(&data->mutex);
  1040. ls_push_libobs_obj(obs_data_t, s->settings, false);
  1041. call_func_(script, data->save, 1, 0, "script_save", __FUNCTION__);
  1042. pthread_mutex_unlock(&data->mutex);
  1043. current_lua_script = NULL;
  1044. }
  1045. /* -------------------------------------------- */
  1046. static inline void add_package_cpath(struct dstr *cpath, const char *path)
  1047. {
  1048. dstr_catf(cpath, " .. \";\" .. \"%s\" .. \"/?." SO_EXT "\"", path);
  1049. }
  1050. void obs_lua_load(void)
  1051. {
  1052. struct dstr tmp = {0};
  1053. pthread_mutex_init(&tick_mutex, NULL);
  1054. pthread_mutex_init_recursive(&timer_mutex);
  1055. pthread_mutex_init(&lua_source_def_mutex, NULL);
  1056. /* ---------------------------------------------- */
  1057. /* Initialize Lua startup script */
  1058. #if _WIN32
  1059. #define PATH_MAX MAX_PATH
  1060. #endif
  1061. struct dstr package_cpath = {0};
  1062. char import_path[PATH_MAX];
  1063. #ifdef __APPLE__
  1064. struct dstr bundle_path;
  1065. dstr_init_move_array(&bundle_path, os_get_executable_path_ptr(""));
  1066. dstr_cat(&bundle_path, "../PlugIns");
  1067. char *absolute_plugin_path = os_get_abs_path_ptr(bundle_path.array);
  1068. if (absolute_plugin_path != NULL) {
  1069. strcpy(import_path, absolute_plugin_path);
  1070. bfree(absolute_plugin_path);
  1071. }
  1072. dstr_free(&bundle_path);
  1073. #else
  1074. strcpy(import_path, "./");
  1075. #endif
  1076. dstr_cat(&package_cpath, "package.cpath = package.cpath");
  1077. add_package_cpath(&package_cpath, import_path);
  1078. #if !defined(_WIN32) && !defined(__APPLE__)
  1079. char *relative_script_path =
  1080. os_get_executable_path_ptr("../" SCRIPT_DIR);
  1081. if (relative_script_path)
  1082. add_package_cpath(&package_cpath, relative_script_path);
  1083. bfree(relative_script_path);
  1084. #endif
  1085. add_package_cpath(&package_cpath, SCRIPT_DIR);
  1086. dstr_cat(&package_cpath, "\n");
  1087. dstr_printf(&tmp, startup_script_template, package_cpath.array);
  1088. dstr_free(&package_cpath);
  1089. startup_script = tmp.array;
  1090. obs_add_tick_callback(lua_tick, NULL);
  1091. }
  1092. void obs_lua_unload(void)
  1093. {
  1094. obs_remove_tick_callback(lua_tick, NULL);
  1095. bfree(startup_script);
  1096. pthread_mutex_destroy(&tick_mutex);
  1097. pthread_mutex_destroy(&timer_mutex);
  1098. pthread_mutex_destroy(&lua_source_def_mutex);
  1099. }