obs-scripting-lua.c 30 KB

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