Make it possible to set number of channels imperatively instead of using a callback.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 21 Jun 2019 17:43:29 +0000 (19:43 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 21 Jun 2019 17:44:15 +0000 (19:44 +0200)
num_channels() will still be used if it exists, for compatibility with older
themes.

nageru/simple.lua
nageru/theme.cpp
nageru/theme.h
nageru/theme.lua

index 8d5b3e6..5444a20 100644 (file)
@@ -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")
 
index 6a4de4b..f7f7522 100644 (file)
@@ -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<string> &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<string> &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);
index 7945425..34a8d29 100644 (file)
@@ -191,8 +191,9 @@ private:
        lua_State *L;  // Protected by <m>.
        const InputState *input_state = nullptr;  // Protected by <m>. 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<int, int> signal_to_card_mapping;  // Protected by <map_m>.
@@ -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
index c762a3f..c524d56 100644 (file)
@@ -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