obs-scripting-lua.c 31 KB

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