obs-scripting-lua.c 30 KB

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