obs-scripting-lua.c 31 KB

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