obs-scripting-lua.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324
  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, 1, 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 source_enum_filters_proc(obs_source_t *source, obs_source_t *filter, void *param)
  457. {
  458. lua_State *script = param;
  459. obs_source_get_ref(filter);
  460. ls_push_libobs_obj(obs_source_t, filter, false);
  461. size_t idx = lua_rawlen(script, -2);
  462. lua_rawseti(script, -2, (int)idx + 1);
  463. return true;
  464. }
  465. static int source_enum_filters(lua_State *script)
  466. {
  467. obs_source_t *source;
  468. if (!ls_get_libobs_obj(obs_source_t, 1, &source))
  469. return 0;
  470. lua_newtable(script);
  471. obs_source_enum_filters(source, source_enum_filters_proc, script);
  472. return 1;
  473. }
  474. /* -------------------------------------------- */
  475. static bool enum_items_proc(obs_scene_t *scene, obs_sceneitem_t *item,
  476. void *param)
  477. {
  478. lua_State *script = param;
  479. UNUSED_PARAMETER(scene);
  480. obs_sceneitem_addref(item);
  481. ls_push_libobs_obj(obs_sceneitem_t, item, false);
  482. lua_rawseti(script, -2, (int)lua_rawlen(script, -2) + 1);
  483. return true;
  484. }
  485. static int scene_enum_items(lua_State *script)
  486. {
  487. obs_scene_t *scene;
  488. if (!ls_get_libobs_obj(obs_scene_t, 1, &scene))
  489. return 0;
  490. lua_newtable(script);
  491. obs_scene_enum_items(scene, enum_items_proc, script);
  492. return 1;
  493. }
  494. /* -------------------------------------------- */
  495. static void defer_hotkey_unregister(void *p_cb)
  496. {
  497. obs_hotkey_unregister((obs_hotkey_id)(uintptr_t)p_cb);
  498. }
  499. static void on_remove_hotkey(void *p_cb)
  500. {
  501. struct lua_obs_callback *cb = p_cb;
  502. obs_hotkey_id id = (obs_hotkey_id)calldata_int(&cb->base.extra, "id");
  503. if (id != OBS_INVALID_HOTKEY_ID)
  504. defer_call_post(defer_hotkey_unregister, (void*)(uintptr_t)id);
  505. }
  506. static void hotkey_pressed(void *p_cb, bool pressed)
  507. {
  508. struct lua_obs_callback *cb = p_cb;
  509. lua_State *script = cb->script;
  510. if (cb->base.removed)
  511. return;
  512. lock_callback();
  513. lua_pushboolean(script, pressed);
  514. call_func(hotkey_pressed, 1, 0);
  515. unlock_callback();
  516. }
  517. static void defer_hotkey_pressed(void *p_cb)
  518. {
  519. hotkey_pressed(p_cb, true);
  520. }
  521. static void defer_hotkey_unpressed(void *p_cb)
  522. {
  523. hotkey_pressed(p_cb, false);
  524. }
  525. static void hotkey_callback(void *p_cb, obs_hotkey_id id,
  526. obs_hotkey_t *hotkey, bool pressed)
  527. {
  528. struct lua_obs_callback *cb = p_cb;
  529. if (cb->base.removed)
  530. return;
  531. if (pressed)
  532. defer_call_post(defer_hotkey_pressed, cb);
  533. else
  534. defer_call_post(defer_hotkey_unpressed, cb);
  535. UNUSED_PARAMETER(hotkey);
  536. UNUSED_PARAMETER(id);
  537. }
  538. static int hotkey_unregister(lua_State *script)
  539. {
  540. if (!verify_args1(script, is_function))
  541. return 0;
  542. struct lua_obs_callback *cb = find_lua_obs_callback(script, 1);
  543. if (cb) remove_lua_obs_callback(cb);
  544. return 0;
  545. }
  546. static int hotkey_register_frontend(lua_State *script)
  547. {
  548. obs_hotkey_id id;
  549. const char *name = lua_tostring(script, 1);
  550. if (!name)
  551. return 0;
  552. const char *desc = lua_tostring(script, 2);
  553. if (!desc)
  554. return 0;
  555. if (!is_function(script, 3))
  556. return 0;
  557. struct lua_obs_callback *cb = add_lua_obs_callback(script, 3);
  558. cb->base.on_remove = on_remove_hotkey;
  559. id = obs_hotkey_register_frontend(name, desc, hotkey_callback, cb);
  560. calldata_set_int(&cb->base.extra, "id", id);
  561. lua_pushinteger(script, (lua_Integer)id);
  562. if (id == OBS_INVALID_HOTKEY_ID)
  563. remove_lua_obs_callback(cb);
  564. return 1;
  565. }
  566. /* -------------------------------------------- */
  567. static bool button_prop_clicked(obs_properties_t *props, obs_property_t *p,
  568. void *p_cb)
  569. {
  570. struct lua_obs_callback *cb = p_cb;
  571. lua_State *script = cb->script;
  572. bool ret = false;
  573. if (cb->base.removed)
  574. return false;
  575. lock_callback();
  576. if (!ls_push_libobs_obj(obs_properties_t, props, false))
  577. goto fail;
  578. if (!ls_push_libobs_obj(obs_property_t, p, false)) {
  579. lua_pop(script, 1);
  580. goto fail;
  581. }
  582. call_func(button_prop_clicked, 2, 1);
  583. if (lua_isboolean(script, -1))
  584. ret = lua_toboolean(script, -1);
  585. fail:
  586. unlock_callback();
  587. return ret;
  588. }
  589. static int properties_add_button(lua_State *script)
  590. {
  591. obs_properties_t *props;
  592. obs_property_t *p;
  593. if (!ls_get_libobs_obj(obs_properties_t, 1, &props))
  594. return 0;
  595. const char *name = lua_tostring(script, 2);
  596. if (!name)
  597. return 0;
  598. const char *text = lua_tostring(script, 3);
  599. if (!text)
  600. return 0;
  601. if (!is_function(script, 4))
  602. return 0;
  603. struct lua_obs_callback *cb = add_lua_obs_callback(script, 4);
  604. p = obs_properties_add_button2(props, name, text, button_prop_clicked,
  605. cb);
  606. if (!p || !ls_push_libobs_obj(obs_property_t, p, false))
  607. return 0;
  608. return 1;
  609. }
  610. /* -------------------------------------------- */
  611. static bool modified_callback(void *p_cb, obs_properties_t *props,
  612. obs_property_t *p, obs_data_t *settings)
  613. {
  614. struct lua_obs_callback *cb = p_cb;
  615. lua_State *script = cb->script;
  616. bool ret = false;
  617. if (cb->base.removed)
  618. return false;
  619. lock_callback();
  620. if (!ls_push_libobs_obj(obs_properties_t, props, false)) {
  621. goto fail;
  622. }
  623. if (!ls_push_libobs_obj(obs_property_t, p, false)) {
  624. lua_pop(script, 1);
  625. goto fail;
  626. }
  627. if (!ls_push_libobs_obj(obs_data_t, settings, false)) {
  628. lua_pop(script, 2);
  629. goto fail;
  630. }
  631. call_func(modified_callback, 3, 1);
  632. if (lua_isboolean(script, -1))
  633. ret = lua_toboolean(script, -1);
  634. fail:
  635. unlock_callback();
  636. return ret;
  637. }
  638. static int property_set_modified_callback(lua_State *script)
  639. {
  640. obs_property_t *p;
  641. if (!ls_get_libobs_obj(obs_property_t, 1, &p))
  642. return 0;
  643. if (!is_function(script, 2))
  644. return 0;
  645. struct lua_obs_callback *cb = add_lua_obs_callback(script, 2);
  646. obs_property_set_modified_callback2(p, modified_callback, cb);
  647. return 0;
  648. }
  649. /* -------------------------------------------- */
  650. static int remove_current_callback(lua_State *script)
  651. {
  652. UNUSED_PARAMETER(script);
  653. if (current_lua_cb)
  654. remove_lua_obs_callback(current_lua_cb);
  655. return 0;
  656. }
  657. /* -------------------------------------------- */
  658. static int calldata_source(lua_State *script)
  659. {
  660. calldata_t *cd;
  661. const char *str;
  662. int ret = 0;
  663. if (!ls_get_libobs_obj(calldata_t, 1, &cd))
  664. goto fail;
  665. str = lua_tostring(script, 2);
  666. if (!str)
  667. goto fail;
  668. obs_source_t *source = calldata_ptr(cd, str);
  669. if (ls_push_libobs_obj(obs_source_t, source, false))
  670. ++ret;
  671. fail:
  672. return ret;
  673. }
  674. static int calldata_sceneitem(lua_State *script)
  675. {
  676. calldata_t *cd;
  677. const char *str;
  678. int ret = 0;
  679. if (!ls_get_libobs_obj(calldata_t, 1, &cd))
  680. goto fail;
  681. str = lua_tostring(script, 2);
  682. if (!str)
  683. goto fail;
  684. obs_sceneitem_t *sceneitem = calldata_ptr(cd, str);
  685. if (ls_push_libobs_obj(obs_sceneitem_t, sceneitem, false))
  686. ++ret;
  687. fail:
  688. return ret;
  689. }
  690. /* -------------------------------------------- */
  691. static int source_list_release(lua_State *script)
  692. {
  693. size_t count = lua_rawlen(script, 1);
  694. for (size_t i = 0; i < count; i++) {
  695. obs_source_t *source;
  696. lua_rawgeti(script, 1, (int)i + 1);
  697. ls_get_libobs_obj(obs_source_t, -1, &source);
  698. lua_pop(script, 1);
  699. obs_source_release(source);
  700. }
  701. return 0;
  702. }
  703. static int sceneitem_list_release(lua_State *script)
  704. {
  705. size_t count = lua_rawlen(script, 1);
  706. for (size_t i = 0; i < count; i++) {
  707. obs_sceneitem_t *item;
  708. lua_rawgeti(script, 1, (int)i + 1);
  709. ls_get_libobs_obj(obs_sceneitem_t, -1, &item);
  710. lua_pop(script, 1);
  711. obs_sceneitem_release(item);
  712. }
  713. return 0;
  714. }
  715. /* -------------------------------------------- */
  716. static int hook_print(lua_State *script)
  717. {
  718. struct obs_lua_script *data = current_lua_script;
  719. const char *msg = lua_tostring(script, 1);
  720. if (!msg)
  721. return 0;
  722. script_info(&data->base, "%s", msg);
  723. return 0;
  724. }
  725. static int hook_error(lua_State *script)
  726. {
  727. struct obs_lua_script *data = current_lua_script;
  728. const char *msg = lua_tostring(script, 1);
  729. if (!msg)
  730. return 0;
  731. script_error(&data->base, "%s", msg);
  732. return 0;
  733. }
  734. /* -------------------------------------------- */
  735. static int lua_script_log(lua_State *script)
  736. {
  737. struct obs_lua_script *data = current_lua_script;
  738. int log_level = (int)lua_tointeger(script, 1);
  739. const char *msg = lua_tostring(script, 2);
  740. if (!msg)
  741. return 0;
  742. /* ------------------- */
  743. dstr_copy(&data->log_chunk, msg);
  744. const char *start = data->log_chunk.array;
  745. char *endl = strchr(start, '\n');
  746. while (endl) {
  747. *endl = 0;
  748. script_log(&data->base, log_level, "%s", start);
  749. *endl = '\n';
  750. start = endl + 1;
  751. endl = strchr(start, '\n');
  752. }
  753. if (start && *start)
  754. script_log(&data->base, log_level, "%s", start);
  755. dstr_resize(&data->log_chunk, 0);
  756. /* ------------------- */
  757. return 0;
  758. }
  759. /* -------------------------------------------- */
  760. static void add_hook_functions(lua_State *script)
  761. {
  762. #define add_func(name, func) \
  763. do { \
  764. lua_pushstring(script, name); \
  765. lua_pushcfunction(script, func); \
  766. lua_rawset(script, -3); \
  767. } while (false)
  768. lua_getglobal(script, "_G");
  769. add_func("print", hook_print);
  770. add_func("error", hook_error);
  771. lua_pop(script, 1);
  772. /* ------------- */
  773. lua_getglobal(script, "obslua");
  774. add_func("script_log", lua_script_log);
  775. add_func("timer_remove", timer_remove);
  776. add_func("timer_add", timer_add);
  777. add_func("obs_enum_sources", enum_sources);
  778. add_func("obs_source_enum_filters", source_enum_filters);
  779. add_func("obs_scene_enum_items", scene_enum_items);
  780. add_func("source_list_release", source_list_release);
  781. add_func("sceneitem_list_release", sceneitem_list_release);
  782. add_func("calldata_source", calldata_source);
  783. add_func("calldata_sceneitem", calldata_sceneitem);
  784. add_func("obs_add_main_render_callback",
  785. obs_lua_add_main_render_callback);
  786. add_func("obs_remove_main_render_callback",
  787. obs_lua_remove_main_render_callback);
  788. add_func("obs_add_tick_callback",
  789. obs_lua_add_tick_callback);
  790. add_func("obs_remove_tick_callback",
  791. obs_lua_remove_tick_callback);
  792. add_func("signal_handler_connect",
  793. obs_lua_signal_handler_connect);
  794. add_func("signal_handler_disconnect",
  795. obs_lua_signal_handler_disconnect);
  796. add_func("signal_handler_connect_global",
  797. obs_lua_signal_handler_connect_global);
  798. add_func("signal_handler_disconnect_global",
  799. obs_lua_signal_handler_disconnect_global);
  800. add_func("obs_hotkey_unregister",
  801. hotkey_unregister);
  802. add_func("obs_hotkey_register_frontend",
  803. hotkey_register_frontend);
  804. add_func("obs_properties_add_button",
  805. properties_add_button);
  806. add_func("obs_property_set_modified_callback",
  807. property_set_modified_callback);
  808. add_func("remove_current_callback",
  809. remove_current_callback);
  810. lua_pop(script, 1);
  811. #undef add_func
  812. }
  813. /* -------------------------------------------- */
  814. static void lua_tick(void *param, float seconds)
  815. {
  816. struct obs_lua_script *data;
  817. struct lua_obs_timer *timer;
  818. uint64_t ts = obs_get_video_frame_time();
  819. /* --------------------------------- */
  820. /* process script_tick calls */
  821. pthread_mutex_lock(&tick_mutex);
  822. data = first_tick_script;
  823. while (data) {
  824. lua_State *script = data->script;
  825. current_lua_script = data;
  826. pthread_mutex_lock(&data->mutex);
  827. lua_pushnumber(script, (double)seconds);
  828. call_func_(script, data->tick, 1, 0, "tick", __FUNCTION__);
  829. pthread_mutex_unlock(&data->mutex);
  830. data = data->next_tick;
  831. }
  832. current_lua_script = NULL;
  833. pthread_mutex_unlock(&tick_mutex);
  834. /* --------------------------------- */
  835. /* process timers */
  836. pthread_mutex_lock(&timer_mutex);
  837. timer = first_timer;
  838. while (timer) {
  839. struct lua_obs_timer *next = timer->next;
  840. struct lua_obs_callback *cb = lua_obs_timer_cb(timer);
  841. if (cb->base.removed) {
  842. lua_obs_timer_remove(timer);
  843. } else {
  844. uint64_t elapsed = ts - timer->last_ts;
  845. if (elapsed >= timer->interval) {
  846. timer_call(&cb->base);
  847. timer->last_ts += timer->interval;
  848. }
  849. }
  850. timer = next;
  851. }
  852. pthread_mutex_unlock(&timer_mutex);
  853. UNUSED_PARAMETER(param);
  854. }
  855. /* -------------------------------------------- */
  856. void obs_lua_script_update(obs_script_t *script, obs_data_t *settings);
  857. bool obs_lua_script_load(obs_script_t *s)
  858. {
  859. struct obs_lua_script *data = (struct obs_lua_script *)s;
  860. if (!data->base.loaded) {
  861. data->base.loaded = load_lua_script(data);
  862. if (data->base.loaded)
  863. obs_lua_script_update(s, NULL);
  864. }
  865. return data->base.loaded;
  866. }
  867. obs_script_t *obs_lua_script_create(const char *path, obs_data_t *settings)
  868. {
  869. struct obs_lua_script *data = bzalloc(sizeof(*data));
  870. data->base.type = OBS_SCRIPT_LANG_LUA;
  871. data->tick = LUA_REFNIL;
  872. pthread_mutexattr_t attr;
  873. pthread_mutexattr_init(&attr);
  874. pthread_mutex_init_value(&data->mutex);
  875. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  876. if (pthread_mutex_init(&data->mutex, &attr) != 0) {
  877. bfree(data);
  878. return NULL;
  879. }
  880. dstr_copy(&data->base.path, path);
  881. char *slash = path && *path ? strrchr(path, '/') : NULL;
  882. if (slash) {
  883. slash++;
  884. dstr_copy(&data->base.file, slash);
  885. dstr_left(&data->dir, &data->base.path, slash - path);
  886. } else {
  887. dstr_copy(&data->base.file, path);
  888. }
  889. data->base.settings = obs_data_create();
  890. if (settings)
  891. obs_data_apply(data->base.settings, settings);
  892. obs_lua_script_load((obs_script_t *)data);
  893. return (obs_script_t *)data;
  894. }
  895. extern void undef_lua_script_sources(struct obs_lua_script *data);
  896. void obs_lua_script_unload(obs_script_t *s)
  897. {
  898. struct obs_lua_script *data = (struct obs_lua_script *)s;
  899. if (!s->loaded)
  900. return;
  901. lua_State *script = data->script;
  902. /* ---------------------------- */
  903. /* undefine source types */
  904. undef_lua_script_sources(data);
  905. /* ---------------------------- */
  906. /* unhook tick function */
  907. if (data->p_prev_next_tick) {
  908. pthread_mutex_lock(&tick_mutex);
  909. struct obs_lua_script *next = data->next_tick;
  910. if (next) next->p_prev_next_tick = data->p_prev_next_tick;
  911. *data->p_prev_next_tick = next;
  912. pthread_mutex_unlock(&tick_mutex);
  913. data->p_prev_next_tick = NULL;
  914. data->next_tick = NULL;
  915. }
  916. /* ---------------------------- */
  917. /* call script_unload */
  918. pthread_mutex_lock(&data->mutex);
  919. lua_getglobal(script, "script_unload");
  920. lua_pcall(script, 0, 0, 0);
  921. /* ---------------------------- */
  922. /* remove all callbacks */
  923. struct lua_obs_callback *cb =
  924. (struct lua_obs_callback *)data->first_callback;
  925. while (cb) {
  926. struct lua_obs_callback *next =
  927. (struct lua_obs_callback *)cb->base.next;
  928. remove_lua_obs_callback(cb);
  929. cb = next;
  930. }
  931. pthread_mutex_unlock(&data->mutex);
  932. /* ---------------------------- */
  933. /* close script */
  934. lua_close(script);
  935. s->loaded = false;
  936. }
  937. void obs_lua_script_destroy(obs_script_t *s)
  938. {
  939. struct obs_lua_script *data = (struct obs_lua_script *)s;
  940. if (data) {
  941. pthread_mutex_destroy(&data->mutex);
  942. dstr_free(&data->base.path);
  943. dstr_free(&data->base.file);
  944. dstr_free(&data->base.desc);
  945. obs_data_release(data->base.settings);
  946. dstr_free(&data->log_chunk);
  947. dstr_free(&data->dir);
  948. bfree(data);
  949. }
  950. }
  951. void obs_lua_script_update(obs_script_t *s, obs_data_t *settings)
  952. {
  953. struct obs_lua_script *data = (struct obs_lua_script *)s;
  954. lua_State *script = data->script;
  955. if (!s->loaded)
  956. return;
  957. if (data->update == LUA_REFNIL)
  958. return;
  959. if (settings)
  960. obs_data_apply(s->settings, settings);
  961. current_lua_script = data;
  962. pthread_mutex_lock(&data->mutex);
  963. ls_push_libobs_obj(obs_data_t, s->settings, false);
  964. call_func_(script, data->update, 1, 0, "script_update", __FUNCTION__);
  965. pthread_mutex_unlock(&data->mutex);
  966. current_lua_script = NULL;
  967. }
  968. obs_properties_t *obs_lua_script_get_properties(obs_script_t *s)
  969. {
  970. struct obs_lua_script *data = (struct obs_lua_script *)s;
  971. lua_State *script = data->script;
  972. obs_properties_t *props = NULL;
  973. if (!s->loaded)
  974. return NULL;
  975. if (data->get_properties == LUA_REFNIL)
  976. return NULL;
  977. current_lua_script = data;
  978. pthread_mutex_lock(&data->mutex);
  979. call_func_(script, data->get_properties, 0, 1, "script_properties",
  980. __FUNCTION__);
  981. ls_get_libobs_obj(obs_properties_t, -1, &props);
  982. pthread_mutex_unlock(&data->mutex);
  983. current_lua_script = NULL;
  984. return props;
  985. }
  986. void obs_lua_script_save(obs_script_t *s)
  987. {
  988. struct obs_lua_script *data = (struct obs_lua_script *)s;
  989. lua_State *script = data->script;
  990. if (!s->loaded)
  991. return;
  992. if (data->save == LUA_REFNIL)
  993. return;
  994. current_lua_script = data;
  995. pthread_mutex_lock(&data->mutex);
  996. ls_push_libobs_obj(obs_data_t, s->settings, false);
  997. call_func_(script, data->save, 1, 0, "script_save", __FUNCTION__);
  998. pthread_mutex_unlock(&data->mutex);
  999. current_lua_script = NULL;
  1000. }
  1001. /* -------------------------------------------- */
  1002. void obs_lua_load(void)
  1003. {
  1004. struct dstr dep_paths = {0};
  1005. struct dstr tmp = {0};
  1006. pthread_mutexattr_t attr;
  1007. pthread_mutexattr_init(&attr);
  1008. pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  1009. pthread_mutex_init(&tick_mutex, NULL);
  1010. pthread_mutex_init(&timer_mutex, &attr);
  1011. pthread_mutex_init(&lua_source_def_mutex, NULL);
  1012. /* ---------------------------------------------- */
  1013. /* Initialize Lua startup script */
  1014. dstr_printf(&tmp, startup_script_template, SCRIPT_DIR);
  1015. startup_script = tmp.array;
  1016. dstr_free(&dep_paths);
  1017. obs_add_tick_callback(lua_tick, NULL);
  1018. }
  1019. void obs_lua_unload(void)
  1020. {
  1021. obs_remove_tick_callback(lua_tick, NULL);
  1022. bfree(startup_script);
  1023. pthread_mutex_destroy(&tick_mutex);
  1024. pthread_mutex_destroy(&timer_mutex);
  1025. pthread_mutex_destroy(&lua_source_def_mutex);
  1026. }