obs-scripting-lua.c 32 KB

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