obs-scripting-lua.c 30 KB

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