obs-scripting-lua.c 33 KB

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