obs-scripting-lua.c 32 KB

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