123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389 |
- /******************************************************************************
- Copyright (C) 2017 by Hugh Bailey <[email protected]>
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- ******************************************************************************/
- #include "obs-scripting-lua.h"
- #include <util/platform.h>
- #include <util/base.h>
- #include <util/dstr.h>
- #include <obs.h>
- /* ========================================================================= */
- #if ARCH_BITS == 64
- #define ARCH_DIR "64bit"
- #else
- #define ARCH_DIR "32bit"
- #endif
- #ifdef __APPLE__
- #define SO_EXT "so"
- #elif _WIN32
- #include <windows.h>
- #define SO_EXT "dll"
- #else
- #define SO_EXT "so"
- #endif
- static const char *startup_script_template =
- "\
- for val in pairs(package.preload) do\n\
- package.preload[val] = nil\n\
- end\n\
- package.cpath = package.cpath .. \";\" .. \"%s/?." SO_EXT
- "\" .. \";\" .. \"%s\" .. \"/?." SO_EXT "\"\n\
- require \"obslua\"\n";
- static const char *get_script_path_func = "\
- function script_path()\n\
- return \"%s\"\n\
- end\n\
- package.path = package.path .. \";\" .. script_path() .. \"/?.lua\"\n";
- static char *startup_script = NULL;
- static pthread_mutex_t tick_mutex;
- static struct obs_lua_script *first_tick_script = NULL;
- pthread_mutex_t lua_source_def_mutex;
- #define ls_get_libobs_obj(type, lua_index, obs_obj) \
- ls_get_libobs_obj_(script, #type " *", lua_index, obs_obj, NULL, \
- __FUNCTION__, __LINE__)
- #define ls_push_libobs_obj(type, obs_obj, ownership) \
- ls_push_libobs_obj_(script, #type " *", obs_obj, ownership, NULL, \
- __FUNCTION__, __LINE__)
- #define call_func(name, args, rets) \
- call_func_(script, cb->reg_idx, args, rets, #name, __FUNCTION__)
- /* ========================================================================= */
- static void add_hook_functions(lua_State *script);
- static int obs_lua_remove_tick_callback(lua_State *script);
- static int obs_lua_remove_main_render_callback(lua_State *script);
- #ifdef ENABLE_UI
- void add_lua_frontend_funcs(lua_State *script);
- #endif
- static bool load_lua_script(struct obs_lua_script *data)
- {
- struct dstr str = {0};
- bool success = false;
- int ret;
- char *file_data;
- lua_State *script = luaL_newstate();
- if (!script) {
- script_warn(&data->base, "Failed to create new lua state");
- goto fail;
- }
- pthread_mutex_lock(&data->mutex);
- luaL_openlibs(script);
- luaopen_ffi(script);
- if (luaL_dostring(script, startup_script) != 0) {
- script_warn(&data->base, "Error executing startup script 1: %s",
- lua_tostring(script, -1));
- goto fail;
- }
- dstr_printf(&str, get_script_path_func, data->dir.array);
- ret = luaL_dostring(script, str.array);
- dstr_free(&str);
- if (ret != 0) {
- script_warn(&data->base, "Error executing startup script 2: %s",
- lua_tostring(script, -1));
- goto fail;
- }
- current_lua_script = data;
- add_lua_source_functions(script);
- add_hook_functions(script);
- #ifdef ENABLE_UI
- add_lua_frontend_funcs(script);
- #endif
- file_data = os_quick_read_utf8_file(data->base.path.array);
- if (!file_data) {
- script_warn(&data->base, "Error opening file: %s",
- lua_tostring(script, -1));
- goto fail;
- }
- if (luaL_loadbuffer(script, file_data, strlen(file_data),
- data->base.path.array) != 0) {
- script_warn(&data->base, "Error loading file: %s",
- lua_tostring(script, -1));
- bfree(file_data);
- goto fail;
- }
- bfree(file_data);
- if (lua_pcall(script, 0, LUA_MULTRET, 0) != 0) {
- script_warn(&data->base, "Error running file: %s",
- lua_tostring(script, -1));
- goto fail;
- }
- ret = lua_gettop(script);
- if (ret == 1 && lua_isboolean(script, -1)) {
- bool success = lua_toboolean(script, -1);
- if (!success) {
- goto fail;
- }
- }
- lua_getglobal(script, "script_tick");
- if (lua_isfunction(script, -1)) {
- pthread_mutex_lock(&tick_mutex);
- struct obs_lua_script *next = first_tick_script;
- data->next_tick = next;
- data->p_prev_next_tick = &first_tick_script;
- if (next)
- next->p_prev_next_tick = &data->next_tick;
- first_tick_script = data;
- data->tick = luaL_ref(script, LUA_REGISTRYINDEX);
- pthread_mutex_unlock(&tick_mutex);
- }
- lua_getglobal(script, "script_properties");
- if (lua_isfunction(script, -1))
- data->get_properties = luaL_ref(script, LUA_REGISTRYINDEX);
- else
- data->get_properties = LUA_REFNIL;
- lua_getglobal(script, "script_update");
- if (lua_isfunction(script, -1))
- data->update = luaL_ref(script, LUA_REGISTRYINDEX);
- else
- data->update = LUA_REFNIL;
- lua_getglobal(script, "script_save");
- if (lua_isfunction(script, -1))
- data->save = luaL_ref(script, LUA_REGISTRYINDEX);
- else
- data->save = LUA_REFNIL;
- lua_getglobal(script, "script_defaults");
- if (lua_isfunction(script, -1)) {
- ls_push_libobs_obj(obs_data_t, data->base.settings, false);
- if (lua_pcall(script, 1, 0, 0) != 0) {
- script_warn(&data->base,
- "Error calling "
- "script_defaults: %s",
- lua_tostring(script, -1));
- }
- }
- lua_getglobal(script, "script_description");
- if (lua_isfunction(script, -1)) {
- if (lua_pcall(script, 0, 1, 0) != 0) {
- script_warn(&data->base,
- "Error calling "
- "script_defaults: %s",
- lua_tostring(script, -1));
- } else {
- const char *desc = lua_tostring(script, -1);
- dstr_copy(&data->base.desc, desc);
- }
- }
- lua_getglobal(script, "script_load");
- if (lua_isfunction(script, -1)) {
- ls_push_libobs_obj(obs_data_t, data->base.settings, false);
- if (lua_pcall(script, 1, 0, 0) != 0) {
- script_warn(&data->base,
- "Error calling "
- "script_load: %s",
- lua_tostring(script, -1));
- }
- }
- data->script = script;
- success = true;
- fail:
- if (script) {
- lua_settop(script, 0);
- pthread_mutex_unlock(&data->mutex);
- }
- if (!success && script) {
- lua_close(script);
- }
- current_lua_script = NULL;
- return success;
- }
- /* -------------------------------------------- */
- THREAD_LOCAL struct lua_obs_callback *current_lua_cb = NULL;
- THREAD_LOCAL struct obs_lua_script *current_lua_script = NULL;
- /* -------------------------------------------- */
- struct lua_obs_timer {
- struct lua_obs_timer *next;
- struct lua_obs_timer **p_prev_next;
- uint64_t last_ts;
- uint64_t interval;
- };
- static pthread_mutex_t timer_mutex;
- static struct lua_obs_timer *first_timer = NULL;
- static inline void lua_obs_timer_init(struct lua_obs_timer *timer)
- {
- pthread_mutex_lock(&timer_mutex);
- struct lua_obs_timer *next = first_timer;
- timer->next = next;
- timer->p_prev_next = &first_timer;
- if (next)
- next->p_prev_next = &timer->next;
- first_timer = timer;
- pthread_mutex_unlock(&timer_mutex);
- }
- static inline void lua_obs_timer_remove(struct lua_obs_timer *timer)
- {
- struct lua_obs_timer *next = timer->next;
- if (next)
- next->p_prev_next = timer->p_prev_next;
- *timer->p_prev_next = timer->next;
- }
- static inline struct lua_obs_callback *
- lua_obs_timer_cb(struct lua_obs_timer *timer)
- {
- return &((struct lua_obs_callback *)timer)[-1];
- }
- static int timer_remove(lua_State *script)
- {
- if (!is_function(script, 1))
- return 0;
- struct lua_obs_callback *cb = find_lua_obs_callback(script, 1);
- if (cb)
- remove_lua_obs_callback(cb);
- return 0;
- }
- static void timer_call(struct script_callback *p_cb)
- {
- struct lua_obs_callback *cb = (struct lua_obs_callback *)p_cb;
- if (script_callback_removed(p_cb))
- return;
- lock_callback();
- call_func_(cb->script, cb->reg_idx, 0, 0, "timer_cb", __FUNCTION__);
- unlock_callback();
- }
- static void defer_timer_init(void *p_cb)
- {
- struct lua_obs_callback *cb = p_cb;
- struct lua_obs_timer *timer = lua_obs_callback_extra_data(cb);
- lua_obs_timer_init(timer);
- }
- static int timer_add(lua_State *script)
- {
- if (!is_function(script, 1))
- return 0;
- int ms = (int)lua_tointeger(script, 2);
- if (!ms)
- return 0;
- struct lua_obs_callback *cb = add_lua_obs_callback_extra(
- script, 1, sizeof(struct lua_obs_timer));
- struct lua_obs_timer *timer = lua_obs_callback_extra_data(cb);
- timer->interval = (uint64_t)ms * 1000000ULL;
- timer->last_ts = obs_get_video_frame_time();
- defer_call_post(defer_timer_init, cb);
- return 0;
- }
- /* -------------------------------------------- */
- static void obs_lua_main_render_callback(void *priv, uint32_t cx, uint32_t cy)
- {
- struct lua_obs_callback *cb = priv;
- lua_State *script = cb->script;
- if (script_callback_removed(&cb->base)) {
- obs_remove_main_render_callback(obs_lua_main_render_callback,
- cb);
- return;
- }
- lock_callback();
- lua_pushinteger(script, (lua_Integer)cx);
- lua_pushinteger(script, (lua_Integer)cy);
- call_func(obs_lua_main_render_callback, 2, 0);
- unlock_callback();
- }
- static int obs_lua_remove_main_render_callback(lua_State *script)
- {
- if (!verify_args1(script, is_function))
- return 0;
- struct lua_obs_callback *cb = find_lua_obs_callback(script, 1);
- if (cb)
- remove_lua_obs_callback(cb);
- return 0;
- }
- static void defer_add_render(void *cb)
- {
- obs_add_main_render_callback(obs_lua_main_render_callback, cb);
- }
- static int obs_lua_add_main_render_callback(lua_State *script)
- {
- if (!verify_args1(script, is_function))
- return 0;
- struct lua_obs_callback *cb = add_lua_obs_callback(script, 1);
- defer_call_post(defer_add_render, cb);
- return 0;
- }
- /* -------------------------------------------- */
- static void obs_lua_tick_callback(void *priv, float seconds)
- {
- struct lua_obs_callback *cb = priv;
- lua_State *script = cb->script;
- if (script_callback_removed(&cb->base)) {
- obs_remove_tick_callback(obs_lua_tick_callback, cb);
- return;
- }
- lock_callback();
- lua_pushnumber(script, (lua_Number)seconds);
- call_func(obs_lua_tick_callback, 1, 0);
- unlock_callback();
- }
- static int obs_lua_remove_tick_callback(lua_State *script)
- {
- if (!verify_args1(script, is_function))
- return 0;
- struct lua_obs_callback *cb = find_lua_obs_callback(script, 1);
- if (cb)
- remove_lua_obs_callback(cb);
- return 0;
- }
- static void defer_add_tick(void *cb)
- {
- obs_add_tick_callback(obs_lua_tick_callback, cb);
- }
- static int obs_lua_add_tick_callback(lua_State *script)
- {
- if (!verify_args1(script, is_function))
- return 0;
- struct lua_obs_callback *cb = add_lua_obs_callback(script, 1);
- defer_call_post(defer_add_tick, cb);
- return 0;
- }
- /* -------------------------------------------- */
- static void calldata_signal_callback(void *priv, calldata_t *cd)
- {
- struct lua_obs_callback *cb = priv;
- lua_State *script = cb->script;
- if (script_callback_removed(&cb->base)) {
- signal_handler_remove_current();
- return;
- }
- lock_callback();
- ls_push_libobs_obj(calldata_t, cd, false);
- call_func(calldata_signal_callback, 1, 0);
- unlock_callback();
- }
- static int obs_lua_signal_handler_disconnect(lua_State *script)
- {
- signal_handler_t *handler;
- const char *signal;
- if (!ls_get_libobs_obj(signal_handler_t, 1, &handler))
- return 0;
- signal = lua_tostring(script, 2);
- if (!signal)
- return 0;
- if (!is_function(script, 3))
- return 0;
- struct lua_obs_callback *cb = find_lua_obs_callback(script, 3);
- while (cb) {
- signal_handler_t *cb_handler =
- calldata_ptr(&cb->base.extra, "handler");
- const char *cb_signal =
- calldata_string(&cb->base.extra, "signal");
- if (cb_signal && strcmp(signal, cb_signal) == 0 &&
- handler == cb_handler)
- break;
- cb = find_next_lua_obs_callback(script, cb, 3);
- }
- if (cb)
- remove_lua_obs_callback(cb);
- return 0;
- }
- static void defer_connect(void *p_cb)
- {
- struct script_callback *cb = p_cb;
- signal_handler_t *handler = calldata_ptr(&cb->extra, "handler");
- const char *signal = calldata_string(&cb->extra, "signal");
- signal_handler_connect(handler, signal, calldata_signal_callback, cb);
- }
- static int obs_lua_signal_handler_connect(lua_State *script)
- {
- signal_handler_t *handler;
- const char *signal;
- if (!ls_get_libobs_obj(signal_handler_t, 1, &handler))
- return 0;
- signal = lua_tostring(script, 2);
- if (!signal)
- return 0;
- if (!is_function(script, 3))
- return 0;
- struct lua_obs_callback *cb = add_lua_obs_callback(script, 3);
- calldata_set_ptr(&cb->base.extra, "handler", handler);
- calldata_set_string(&cb->base.extra, "signal", signal);
- defer_call_post(defer_connect, cb);
- return 0;
- }
- /* -------------------------------------------- */
- static void calldata_signal_callback_global(void *priv, const char *signal,
- calldata_t *cd)
- {
- struct lua_obs_callback *cb = priv;
- lua_State *script = cb->script;
- if (script_callback_removed(&cb->base)) {
- signal_handler_remove_current();
- return;
- }
- lock_callback();
- lua_pushstring(script, signal);
- ls_push_libobs_obj(calldata_t, cd, false);
- call_func(calldata_signal_callback_global, 2, 0);
- unlock_callback();
- }
- static int obs_lua_signal_handler_disconnect_global(lua_State *script)
- {
- signal_handler_t *handler;
- if (!ls_get_libobs_obj(signal_handler_t, 1, &handler))
- return 0;
- if (!is_function(script, 2))
- return 0;
- struct lua_obs_callback *cb = find_lua_obs_callback(script, 3);
- while (cb) {
- signal_handler_t *cb_handler =
- calldata_ptr(&cb->base.extra, "handler");
- if (handler == cb_handler)
- break;
- cb = find_next_lua_obs_callback(script, cb, 3);
- }
- if (cb)
- remove_lua_obs_callback(cb);
- return 0;
- }
- static void defer_connect_global(void *p_cb)
- {
- struct script_callback *cb = p_cb;
- signal_handler_t *handler = calldata_ptr(&cb->extra, "handler");
- signal_handler_connect_global(handler, calldata_signal_callback_global,
- cb);
- }
- static int obs_lua_signal_handler_connect_global(lua_State *script)
- {
- signal_handler_t *handler;
- if (!ls_get_libobs_obj(signal_handler_t, 1, &handler))
- return 0;
- if (!is_function(script, 2))
- return 0;
- struct lua_obs_callback *cb = add_lua_obs_callback(script, 2);
- calldata_set_ptr(&cb->base.extra, "handler", handler);
- defer_call_post(defer_connect_global, cb);
- return 0;
- }
- /* -------------------------------------------- */
- static bool enum_sources_proc(void *param, obs_source_t *source)
- {
- lua_State *script = param;
- obs_source_get_ref(source);
- ls_push_libobs_obj(obs_source_t, source, false);
- size_t idx = lua_rawlen(script, -2);
- lua_rawseti(script, -2, (int)idx + 1);
- return true;
- }
- static int enum_sources(lua_State *script)
- {
- lua_newtable(script);
- obs_enum_sources(enum_sources_proc, script);
- return 1;
- }
- /* -------------------------------------------- */
- static void source_enum_filters_proc(obs_source_t *source, obs_source_t *filter,
- void *param)
- {
- UNUSED_PARAMETER(source);
- lua_State *script = param;
- obs_source_get_ref(filter);
- ls_push_libobs_obj(obs_source_t, filter, false);
- size_t idx = lua_rawlen(script, -2);
- lua_rawseti(script, -2, (int)idx + 1);
- }
- static int source_enum_filters(lua_State *script)
- {
- obs_source_t *source;
- if (!ls_get_libobs_obj(obs_source_t, 1, &source))
- return 0;
- lua_newtable(script);
- obs_source_enum_filters(source, source_enum_filters_proc, script);
- return 1;
- }
- /* -------------------------------------------- */
- static bool enum_items_proc(obs_scene_t *scene, obs_sceneitem_t *item,
- void *param)
- {
- lua_State *script = param;
- UNUSED_PARAMETER(scene);
- obs_sceneitem_addref(item);
- ls_push_libobs_obj(obs_sceneitem_t, item, false);
- lua_rawseti(script, -2, (int)lua_rawlen(script, -2) + 1);
- return true;
- }
- static int scene_enum_items(lua_State *script)
- {
- obs_scene_t *scene;
- if (!ls_get_libobs_obj(obs_scene_t, 1, &scene))
- return 0;
- lua_newtable(script);
- obs_scene_enum_items(scene, enum_items_proc, script);
- return 1;
- }
- static int sceneitem_group_enum_items(lua_State *script)
- {
- obs_sceneitem_t *sceneitem;
- if (!ls_get_libobs_obj(obs_sceneitem_t, 1, &sceneitem))
- return 0;
- lua_newtable(script);
- obs_sceneitem_group_enum_items(sceneitem, enum_items_proc, script);
- return 1;
- }
- /* -------------------------------------------- */
- static void defer_hotkey_unregister(void *p_cb)
- {
- obs_hotkey_unregister((obs_hotkey_id)(uintptr_t)p_cb);
- }
- static void on_remove_hotkey(void *p_cb)
- {
- struct lua_obs_callback *cb = p_cb;
- obs_hotkey_id id = (obs_hotkey_id)calldata_int(&cb->base.extra, "id");
- if (id != OBS_INVALID_HOTKEY_ID)
- defer_call_post(defer_hotkey_unregister, (void *)(uintptr_t)id);
- }
- static void hotkey_pressed(void *p_cb, bool pressed)
- {
- struct lua_obs_callback *cb = p_cb;
- lua_State *script = cb->script;
- if (script_callback_removed(&cb->base))
- return;
- lock_callback();
- lua_pushboolean(script, pressed);
- call_func(hotkey_pressed, 1, 0);
- unlock_callback();
- }
- static void defer_hotkey_pressed(void *p_cb)
- {
- hotkey_pressed(p_cb, true);
- }
- static void defer_hotkey_unpressed(void *p_cb)
- {
- hotkey_pressed(p_cb, false);
- }
- static void hotkey_callback(void *p_cb, obs_hotkey_id id, obs_hotkey_t *hotkey,
- bool pressed)
- {
- struct lua_obs_callback *cb = p_cb;
- if (script_callback_removed(&cb->base))
- return;
- if (pressed)
- defer_call_post(defer_hotkey_pressed, cb);
- else
- defer_call_post(defer_hotkey_unpressed, cb);
- UNUSED_PARAMETER(hotkey);
- UNUSED_PARAMETER(id);
- }
- static int hotkey_unregister(lua_State *script)
- {
- if (!verify_args1(script, is_function))
- return 0;
- struct lua_obs_callback *cb = find_lua_obs_callback(script, 1);
- if (cb)
- remove_lua_obs_callback(cb);
- return 0;
- }
- static int hotkey_register_frontend(lua_State *script)
- {
- obs_hotkey_id id;
- const char *name = lua_tostring(script, 1);
- if (!name)
- return 0;
- const char *desc = lua_tostring(script, 2);
- if (!desc)
- return 0;
- if (!is_function(script, 3))
- return 0;
- struct lua_obs_callback *cb = add_lua_obs_callback(script, 3);
- cb->base.on_remove = on_remove_hotkey;
- id = obs_hotkey_register_frontend(name, desc, hotkey_callback, cb);
- calldata_set_int(&cb->base.extra, "id", id);
- lua_pushinteger(script, (lua_Integer)id);
- if (id == OBS_INVALID_HOTKEY_ID)
- remove_lua_obs_callback(cb);
- return 1;
- }
- /* -------------------------------------------- */
- static bool button_prop_clicked(obs_properties_t *props, obs_property_t *p,
- void *p_cb)
- {
- struct lua_obs_callback *cb = p_cb;
- lua_State *script = cb->script;
- bool ret = false;
- if (script_callback_removed(&cb->base))
- return false;
- lock_callback();
- if (!ls_push_libobs_obj(obs_properties_t, props, false))
- goto fail;
- if (!ls_push_libobs_obj(obs_property_t, p, false)) {
- lua_pop(script, 1);
- goto fail;
- }
- call_func(button_prop_clicked, 2, 1);
- if (lua_isboolean(script, -1))
- ret = lua_toboolean(script, -1);
- fail:
- unlock_callback();
- return ret;
- }
- static int properties_add_button(lua_State *script)
- {
- obs_properties_t *props;
- obs_property_t *p;
- if (!ls_get_libobs_obj(obs_properties_t, 1, &props))
- return 0;
- const char *name = lua_tostring(script, 2);
- if (!name)
- return 0;
- const char *text = lua_tostring(script, 3);
- if (!text)
- return 0;
- if (!is_function(script, 4))
- return 0;
- struct lua_obs_callback *cb = add_lua_obs_callback(script, 4);
- p = obs_properties_add_button2(props, name, text, button_prop_clicked,
- cb);
- if (!p || !ls_push_libobs_obj(obs_property_t, p, false))
- return 0;
- return 1;
- }
- /* -------------------------------------------- */
- static bool modified_callback(void *p_cb, obs_properties_t *props,
- obs_property_t *p, obs_data_t *settings)
- {
- struct lua_obs_callback *cb = p_cb;
- lua_State *script = cb->script;
- bool ret = false;
- if (script_callback_removed(&cb->base))
- return false;
- lock_callback();
- if (!ls_push_libobs_obj(obs_properties_t, props, false)) {
- goto fail;
- }
- if (!ls_push_libobs_obj(obs_property_t, p, false)) {
- lua_pop(script, 1);
- goto fail;
- }
- if (!ls_push_libobs_obj(obs_data_t, settings, false)) {
- lua_pop(script, 2);
- goto fail;
- }
- call_func(modified_callback, 3, 1);
- if (lua_isboolean(script, -1))
- ret = lua_toboolean(script, -1);
- fail:
- unlock_callback();
- return ret;
- }
- static int property_set_modified_callback(lua_State *script)
- {
- obs_property_t *p;
- if (!ls_get_libobs_obj(obs_property_t, 1, &p))
- return 0;
- if (!is_function(script, 2))
- return 0;
- struct lua_obs_callback *cb = add_lua_obs_callback(script, 2);
- obs_property_set_modified_callback2(p, modified_callback, cb);
- return 0;
- }
- /* -------------------------------------------- */
- static int remove_current_callback(lua_State *script)
- {
- UNUSED_PARAMETER(script);
- if (current_lua_cb)
- remove_lua_obs_callback(current_lua_cb);
- return 0;
- }
- /* -------------------------------------------- */
- static int calldata_source(lua_State *script)
- {
- calldata_t *cd;
- const char *str;
- int ret = 0;
- if (!ls_get_libobs_obj(calldata_t, 1, &cd))
- goto fail;
- str = lua_tostring(script, 2);
- if (!str)
- goto fail;
- obs_source_t *source = calldata_ptr(cd, str);
- if (ls_push_libobs_obj(obs_source_t, source, false))
- ++ret;
- fail:
- return ret;
- }
- static int calldata_sceneitem(lua_State *script)
- {
- calldata_t *cd;
- const char *str;
- int ret = 0;
- if (!ls_get_libobs_obj(calldata_t, 1, &cd))
- goto fail;
- str = lua_tostring(script, 2);
- if (!str)
- goto fail;
- obs_sceneitem_t *sceneitem = calldata_ptr(cd, str);
- if (ls_push_libobs_obj(obs_sceneitem_t, sceneitem, false))
- ++ret;
- fail:
- return ret;
- }
- /* -------------------------------------------- */
- static int source_list_release(lua_State *script)
- {
- size_t count = lua_rawlen(script, 1);
- for (size_t i = 0; i < count; i++) {
- obs_source_t *source;
- lua_rawgeti(script, 1, (int)i + 1);
- ls_get_libobs_obj(obs_source_t, -1, &source);
- lua_pop(script, 1);
- obs_source_release(source);
- }
- return 0;
- }
- static int sceneitem_list_release(lua_State *script)
- {
- size_t count = lua_rawlen(script, 1);
- for (size_t i = 0; i < count; i++) {
- obs_sceneitem_t *item;
- lua_rawgeti(script, 1, (int)i + 1);
- ls_get_libobs_obj(obs_sceneitem_t, -1, &item);
- lua_pop(script, 1);
- obs_sceneitem_release(item);
- }
- return 0;
- }
- /* -------------------------------------------- */
- static int hook_print(lua_State *script)
- {
- struct obs_lua_script *data = current_lua_script;
- const char *msg = lua_tostring(script, 1);
- if (!msg)
- return 0;
- script_info(&data->base, "%s", msg);
- return 0;
- }
- static int hook_error(lua_State *script)
- {
- struct obs_lua_script *data = current_lua_script;
- const char *msg = lua_tostring(script, 1);
- if (!msg)
- return 0;
- script_error(&data->base, "%s", msg);
- return 0;
- }
- /* -------------------------------------------- */
- static int lua_script_log(lua_State *script)
- {
- struct obs_lua_script *data = current_lua_script;
- int log_level = (int)lua_tointeger(script, 1);
- const char *msg = lua_tostring(script, 2);
- if (!msg)
- return 0;
- /* ------------------- */
- dstr_copy(&data->log_chunk, msg);
- const char *start = data->log_chunk.array;
- char *endl = strchr(start, '\n');
- while (endl) {
- *endl = 0;
- script_log(&data->base, log_level, "%s", start);
- *endl = '\n';
- start = endl + 1;
- endl = strchr(start, '\n');
- }
- if (start && *start)
- script_log(&data->base, log_level, "%s", start);
- dstr_resize(&data->log_chunk, 0);
- /* ------------------- */
- return 0;
- }
- /* -------------------------------------------- */
- static void add_hook_functions(lua_State *script)
- {
- #define add_func(name, func) \
- do { \
- lua_pushstring(script, name); \
- lua_pushcfunction(script, func); \
- lua_rawset(script, -3); \
- } while (false)
- lua_getglobal(script, "_G");
- add_func("print", hook_print);
- add_func("error", hook_error);
- lua_pop(script, 1);
- /* ------------- */
- lua_getglobal(script, "obslua");
- add_func("script_log", lua_script_log);
- add_func("timer_remove", timer_remove);
- add_func("timer_add", timer_add);
- add_func("obs_enum_sources", enum_sources);
- add_func("obs_source_enum_filters", source_enum_filters);
- add_func("obs_scene_enum_items", scene_enum_items);
- add_func("obs_sceneitem_group_enum_items", sceneitem_group_enum_items);
- add_func("source_list_release", source_list_release);
- add_func("sceneitem_list_release", sceneitem_list_release);
- add_func("calldata_source", calldata_source);
- add_func("calldata_sceneitem", calldata_sceneitem);
- add_func("obs_add_main_render_callback",
- obs_lua_add_main_render_callback);
- add_func("obs_remove_main_render_callback",
- obs_lua_remove_main_render_callback);
- add_func("obs_add_tick_callback", obs_lua_add_tick_callback);
- add_func("obs_remove_tick_callback", obs_lua_remove_tick_callback);
- add_func("signal_handler_connect", obs_lua_signal_handler_connect);
- add_func("signal_handler_disconnect",
- obs_lua_signal_handler_disconnect);
- add_func("signal_handler_connect_global",
- obs_lua_signal_handler_connect_global);
- add_func("signal_handler_disconnect_global",
- obs_lua_signal_handler_disconnect_global);
- add_func("obs_hotkey_unregister", hotkey_unregister);
- add_func("obs_hotkey_register_frontend", hotkey_register_frontend);
- add_func("obs_properties_add_button", properties_add_button);
- add_func("obs_property_set_modified_callback",
- property_set_modified_callback);
- add_func("remove_current_callback", remove_current_callback);
- lua_pop(script, 1);
- #undef add_func
- }
- /* -------------------------------------------- */
- static void lua_tick(void *param, float seconds)
- {
- struct obs_lua_script *data;
- struct lua_obs_timer *timer;
- uint64_t ts = obs_get_video_frame_time();
- /* --------------------------------- */
- /* process script_tick calls */
- pthread_mutex_lock(&tick_mutex);
- data = first_tick_script;
- while (data) {
- lua_State *script = data->script;
- current_lua_script = data;
- pthread_mutex_lock(&data->mutex);
- lua_pushnumber(script, (double)seconds);
- call_func_(script, data->tick, 1, 0, "tick", __FUNCTION__);
- pthread_mutex_unlock(&data->mutex);
- data = data->next_tick;
- }
- current_lua_script = NULL;
- pthread_mutex_unlock(&tick_mutex);
- /* --------------------------------- */
- /* process timers */
- pthread_mutex_lock(&timer_mutex);
- timer = first_timer;
- while (timer) {
- struct lua_obs_timer *next = timer->next;
- struct lua_obs_callback *cb = lua_obs_timer_cb(timer);
- if (script_callback_removed(&cb->base)) {
- lua_obs_timer_remove(timer);
- } else {
- uint64_t elapsed = ts - timer->last_ts;
- if (elapsed >= timer->interval) {
- timer_call(&cb->base);
- timer->last_ts += timer->interval;
- }
- }
- timer = next;
- }
- pthread_mutex_unlock(&timer_mutex);
- UNUSED_PARAMETER(param);
- }
- /* -------------------------------------------- */
- void obs_lua_script_update(obs_script_t *script, obs_data_t *settings);
- bool obs_lua_script_load(obs_script_t *s)
- {
- struct obs_lua_script *data = (struct obs_lua_script *)s;
- if (!data->base.loaded) {
- data->base.loaded = load_lua_script(data);
- if (data->base.loaded) {
- blog(LOG_INFO, "[obs-scripting]: Loaded lua script: %s",
- data->base.file.array);
- obs_lua_script_update(s, NULL);
- }
- }
- return data->base.loaded;
- }
- obs_script_t *obs_lua_script_create(const char *path, obs_data_t *settings)
- {
- struct obs_lua_script *data = bzalloc(sizeof(*data));
- data->base.type = OBS_SCRIPT_LANG_LUA;
- data->tick = LUA_REFNIL;
- pthread_mutex_init_value(&data->mutex);
- if (pthread_mutex_init_recursive(&data->mutex) != 0) {
- bfree(data);
- return NULL;
- }
- dstr_copy(&data->base.path, path);
- char *slash = path && *path ? strrchr(path, '/') : NULL;
- if (slash) {
- slash++;
- dstr_copy(&data->base.file, slash);
- dstr_left(&data->dir, &data->base.path, slash - path);
- } else {
- dstr_copy(&data->base.file, path);
- }
- data->base.settings = obs_data_create();
- if (settings)
- obs_data_apply(data->base.settings, settings);
- obs_lua_script_load((obs_script_t *)data);
- return (obs_script_t *)data;
- }
- extern void undef_lua_script_sources(struct obs_lua_script *data);
- void obs_lua_script_unload(obs_script_t *s)
- {
- struct obs_lua_script *data = (struct obs_lua_script *)s;
- if (!s->loaded)
- return;
- lua_State *script = data->script;
- /* ---------------------------- */
- /* mark callbacks as removed */
- pthread_mutex_lock(&data->mutex);
- /* XXX: scripts can potentially make callbacks when this happens, so
- * this probably still isn't ideal as we can't predict how the
- * processor or operating system is going to schedule things. a more
- * ideal method would be to reference count the script objects and
- * atomically share ownership with callbacks when they're called. */
- struct lua_obs_callback *cb =
- (struct lua_obs_callback *)data->first_callback;
- while (cb) {
- os_atomic_set_bool(&cb->base.removed, true);
- cb = (struct lua_obs_callback *)cb->base.next;
- }
- pthread_mutex_unlock(&data->mutex);
- /* ---------------------------- */
- /* undefine source types */
- undef_lua_script_sources(data);
- /* ---------------------------- */
- /* unhook tick function */
- if (data->p_prev_next_tick) {
- pthread_mutex_lock(&tick_mutex);
- struct obs_lua_script *next = data->next_tick;
- if (next)
- next->p_prev_next_tick = data->p_prev_next_tick;
- *data->p_prev_next_tick = next;
- pthread_mutex_unlock(&tick_mutex);
- data->p_prev_next_tick = NULL;
- data->next_tick = NULL;
- }
- /* ---------------------------- */
- /* call script_unload */
- pthread_mutex_lock(&data->mutex);
- lua_getglobal(script, "script_unload");
- lua_pcall(script, 0, 0, 0);
- /* ---------------------------- */
- /* remove all callbacks */
- cb = (struct lua_obs_callback *)data->first_callback;
- while (cb) {
- struct lua_obs_callback *next =
- (struct lua_obs_callback *)cb->base.next;
- remove_lua_obs_callback(cb);
- cb = next;
- }
- pthread_mutex_unlock(&data->mutex);
- /* ---------------------------- */
- /* close script */
- lua_close(script);
- s->loaded = false;
- blog(LOG_INFO, "[obs-scripting]: Unloaded lua script: %s",
- data->base.file.array);
- }
- void obs_lua_script_destroy(obs_script_t *s)
- {
- struct obs_lua_script *data = (struct obs_lua_script *)s;
- if (data) {
- pthread_mutex_destroy(&data->mutex);
- dstr_free(&data->base.path);
- dstr_free(&data->base.file);
- dstr_free(&data->base.desc);
- obs_data_release(data->base.settings);
- dstr_free(&data->log_chunk);
- dstr_free(&data->dir);
- bfree(data);
- }
- }
- void obs_lua_script_update(obs_script_t *s, obs_data_t *settings)
- {
- struct obs_lua_script *data = (struct obs_lua_script *)s;
- lua_State *script = data->script;
- if (!s->loaded)
- return;
- if (data->update == LUA_REFNIL)
- return;
- if (settings)
- obs_data_apply(s->settings, settings);
- current_lua_script = data;
- pthread_mutex_lock(&data->mutex);
- ls_push_libobs_obj(obs_data_t, s->settings, false);
- call_func_(script, data->update, 1, 0, "script_update", __FUNCTION__);
- pthread_mutex_unlock(&data->mutex);
- current_lua_script = NULL;
- }
- obs_properties_t *obs_lua_script_get_properties(obs_script_t *s)
- {
- struct obs_lua_script *data = (struct obs_lua_script *)s;
- lua_State *script = data->script;
- obs_properties_t *props = NULL;
- if (!s->loaded)
- return NULL;
- if (data->get_properties == LUA_REFNIL)
- return NULL;
- current_lua_script = data;
- pthread_mutex_lock(&data->mutex);
- call_func_(script, data->get_properties, 0, 1, "script_properties",
- __FUNCTION__);
- ls_get_libobs_obj(obs_properties_t, -1, &props);
- pthread_mutex_unlock(&data->mutex);
- current_lua_script = NULL;
- return props;
- }
- void obs_lua_script_save(obs_script_t *s)
- {
- struct obs_lua_script *data = (struct obs_lua_script *)s;
- lua_State *script = data->script;
- if (!s->loaded)
- return;
- if (data->save == LUA_REFNIL)
- return;
- current_lua_script = data;
- pthread_mutex_lock(&data->mutex);
- ls_push_libobs_obj(obs_data_t, s->settings, false);
- call_func_(script, data->save, 1, 0, "script_save", __FUNCTION__);
- pthread_mutex_unlock(&data->mutex);
- current_lua_script = NULL;
- }
- /* -------------------------------------------- */
- void obs_lua_load(void)
- {
- struct dstr tmp = {0};
- pthread_mutex_init(&tick_mutex, NULL);
- pthread_mutex_init_recursive(&timer_mutex);
- pthread_mutex_init(&lua_source_def_mutex, NULL);
- /* ---------------------------------------------- */
- /* Initialize Lua startup script */
- #if _WIN32
- #define PATH_MAX MAX_PATH
- #endif
- char import_path[PATH_MAX];
- #ifdef __APPLE__
- struct dstr bundle_path;
- dstr_init_move_array(&bundle_path, os_get_executable_path_ptr(""));
- dstr_cat(&bundle_path, "../PlugIns");
- char *absolute_plugin_path = os_get_abs_path_ptr(bundle_path.array);
- if (absolute_plugin_path != NULL) {
- strcpy(import_path, absolute_plugin_path);
- bfree(absolute_plugin_path);
- }
- dstr_free(&bundle_path);
- #else
- strcpy(import_path, "./");
- #endif
- dstr_printf(&tmp, startup_script_template, import_path, SCRIPT_DIR);
- startup_script = tmp.array;
- obs_add_tick_callback(lua_tick, NULL);
- }
- void obs_lua_unload(void)
- {
- obs_remove_tick_callback(lua_tick, NULL);
- bfree(startup_script);
- pthread_mutex_destroy(&tick_mutex);
- pthread_mutex_destroy(&timer_mutex);
- pthread_mutex_destroy(&lua_source_def_mutex);
- }
|