From 9f5622b1cb36a07b5b717ab5c4cbd938a550c7c3 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Fri, 21 Jun 2019 19:43:29 +0200 Subject: [PATCH] Make it possible to set number of channels imperatively instead of using a callback. num_channels() will still be used if it exists, for compatibility with older themes. --- nageru/simple.lua | 8 +------- nageru/theme.cpp | 21 +++++++++++++++++++-- nageru/theme.h | 4 +++- nageru/theme.lua | 8 +------- 4 files changed, 24 insertions(+), 17 deletions(-) diff --git a/nageru/simple.lua b/nageru/simple.lua index 8d5b3e6..5444a20 100644 --- a/nageru/simple.lua +++ b/nageru/simple.lua @@ -25,14 +25,8 @@ local input = scene:add_input() local wb_effect = scene:add_effect(WhiteBalanceEffect.new()) scene:finalize() --- API ENTRY POINT --- Returns the number of outputs in addition to the live (0) and preview (1). --- Called only once, at the start of the program. -function num_channels() - return 2 -end - -- Set some global state. +Nageru.set_num_channels(2) -- Can only be called at the start of the program. Nageru.set_channel_name(2, "First input") Nageru.set_channel_name(3, "Second input") diff --git a/nageru/theme.cpp b/nageru/theme.cpp index 6a4de4b..f7f7522 100644 --- a/nageru/theme.cpp +++ b/nageru/theme.cpp @@ -1212,6 +1212,7 @@ int call_num_channels(lua_State *L) if (lua_pcall(L, 0, 1, 0) != 0) { fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1)); + fprintf(stderr, "Try Nageru.set_num_channels(...) at the start of the script instead.\n"); abort(); } @@ -1234,6 +1235,18 @@ int Nageru_set_channel_name(lua_State *L) return 0; } +int Nageru_set_num_channels(lua_State *L) +{ + // NOTE: m is already locked. + Theme *theme = get_theme_updata(L); + if (theme->startup_finished) { + luaL_error(L, "set_num_channels() can only be called at startup."); + } + theme->num_channels = luaL_checknumber(L, 1); + lua_pop(L, 1); + return 0; +} + Theme::Theme(const string &filename, const vector &search_dirs, ResourcePool *resource_pool, unsigned num_cards) : resource_pool(resource_pool), num_cards(num_cards), signal_to_card_mapping(global_flags.default_stream_mapping) { @@ -1335,8 +1348,11 @@ Theme::Theme(const string &filename, const vector &search_dirs, Resource } assert(lua_gettop(L) == 0); - // Ask it for the number of channels. - num_channels = call_num_channels(L); + if (num_channels == -1) { + // Ask it for the number of channels. + num_channels = call_num_channels(L); + } + startup_finished = true; } Theme::~Theme() @@ -1371,6 +1387,7 @@ void Theme::register_globals() const luaL_Reg Nageru_funcs[] = { { "set_channel_name", Nageru_set_channel_name }, + { "set_num_channels", Nageru_set_num_channels }, { NULL, NULL } }; lua_pushlightuserdata(L, this); diff --git a/nageru/theme.h b/nageru/theme.h index 7945425..34a8d29 100644 --- a/nageru/theme.h +++ b/nageru/theme.h @@ -191,8 +191,9 @@ private: lua_State *L; // Protected by . const InputState *input_state = nullptr; // Protected by . Only set temporarily, during chain setup. movit::ResourcePool *resource_pool; - int num_channels; + int num_channels = -1; unsigned num_cards; + bool startup_finished = false; std::mutex map_m; std::map signal_to_card_mapping; // Protected by . @@ -223,6 +224,7 @@ private: friend class Scene; friend int ThemeMenu_set(lua_State *L); friend int Nageru_set_channel_name(lua_State *L); + friend int Nageru_set_num_channels(lua_State *L); }; // LiveInputWrapper is a facade on top of an YCbCrInput, exposed to diff --git a/nageru/theme.lua b/nageru/theme.lua index c762a3f..c524d56 100644 --- a/nageru/theme.lua +++ b/nageru/theme.lua @@ -111,16 +111,10 @@ static_scene:add_input(static_image) -- Note: Locks this input to images only. static_scene:finalize() -- Set some global state. +Nageru.set_num_channels(4) -- Can only be called at the start of the program. Nageru.set_channel_name(SBS_SIGNAL_NUM + 2, "Side-by-side") Nageru.set_channel_name(STATIC_SIGNAL_NUM + 2, "Static picture") --- API ENTRY POINT --- Returns the number of outputs in addition to the live (0) and preview (1). --- Called only once, at the start of the program. -function num_channels() - return 4 -end - function is_plain_signal(num) return num == INPUT0_SIGNAL_NUM or num == INPUT1_SIGNAL_NUM end -- 2.39.2