1
0

obs-scripting-lua.c 33 KB

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