obs-scripting-lua.c 33 KB

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