]> git.sesse.net Git - nageru/commitdiff
Convert channel_signal() and supports_set_wb() to imperative versions.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 21 Jun 2019 18:12:39 +0000 (20:12 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 21 Jun 2019 18:12:39 +0000 (20:12 +0200)
This completes the theme conversion for all called-only-once functions.
As before, the old versions are supported for pre-1.9.0 compatibility.

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

index 5444a200be9787c0e8c1f62269dcf6f0d7ccfe45..f349f3f5fcdac067e837b1ce60880d1a656aae1d 100644 (file)
@@ -25,28 +25,25 @@ local input = scene:add_input()
 local wb_effect = scene:add_effect(WhiteBalanceEffect.new())
 scene:finalize()
 
--- Set some global state.
-Nageru.set_num_channels(2)  -- Can only be called at the start of the program.
+-- Set some global state. Unless marked otherwise, these can only be set once,
+-- at the start of the program.
+Nageru.set_num_channels(2)
+
+-- Sets, for each channel, which signal it corresponds to (starting from 0).
+-- The information is used for whether right-click on the channel should bring up
+-- an input selector or not. Only call this for channels that actually correspond
+-- directly to a signal (ie., live inputs, not live (0) or preview (1)).
+Nageru.set_channel_signal(2, 0)
+Nageru.set_channel_signal(3, 1)
+
+-- Set whether a given channel supports setting white balance. (Default is false.)
+Nageru.set_supports_wb(2, true)
+Nageru.set_supports_wb(3, true)
+
+-- These can be set at any time.
 Nageru.set_channel_name(2, "First input")
 Nageru.set_channel_name(3, "Second input")
 
--- API ENTRY POINT
--- Returns, given a channel number, which signal it corresponds to (starting from 0).
--- Should return -1 if the channel does not correspond to a simple signal.
--- (The information is used for whether right-click on the channel should bring up
--- an input selector or not.)
--- Called once for each channel, at the start of the program.
--- Will never be called for live (0) or preview (1).
-function channel_signal(channel)
-       if channel == 2 then
-               return 0
-       elseif channel == 3 then
-               return 1
-       else
-               return -1
-       end
-end
-
 -- API ENTRY POINT
 -- Called every frame. Returns the color (if any) to paint around the given
 -- channel. Returns a CSS color (typically to mark live and preview signals);
@@ -56,13 +53,6 @@ function channel_color(channel)
        return "transparent"
 end
 
--- API ENTRY POINT
--- Returns if a given channel supports setting white balance (starting from 2).
--- Called only once for each channel, at the start of the program.
-function supports_set_wb(channel)
-       return channel == 2 or channel == 3
-end
-
 -- API ENTRY POINT
 -- Gets called with a new gray point when the white balance is changing.
 -- The color is in linear light (not sRGB gamma).
index b53c65bc3434f61aae29904c242254893a672d75..7c3b366e49fafa46514281f2137fb940b4f28cd1 100644 (file)
@@ -1247,6 +1247,34 @@ int Nageru_set_num_channels(lua_State *L)
        return 0;
 }
 
+int Nageru_set_channel_signal(lua_State *L)
+{
+       // NOTE: m is already locked.
+       Theme *theme = get_theme_updata(L);
+       if (theme->startup_finished) {
+               luaL_error(L, "set_channel_signal() can only be called at startup.");
+       }
+       unsigned channel = luaL_checknumber(L, 1);
+       int signal = luaL_checknumber(L, 2);
+       theme->channel_signals[channel] = signal;
+       lua_pop(L, 2);
+       return 0;
+}
+
+int Nageru_set_supports_wb(lua_State *L)
+{
+       // NOTE: m is already locked.
+       Theme *theme = get_theme_updata(L);
+       if (theme->startup_finished) {
+               luaL_error(L, "set_supports_wb() can only be called at startup.");
+       }
+       unsigned channel = luaL_checknumber(L, 1);
+       bool supports_wb = checkbool(L, 2);
+       theme->channel_supports_wb[channel] = supports_wb;
+       lua_pop(L, 2);
+       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)
 {
@@ -1388,6 +1416,8 @@ void Theme::register_globals()
        const luaL_Reg Nageru_funcs[] = {
                { "set_channel_name", Nageru_set_channel_name },
                { "set_num_channels", Nageru_set_num_channels },
+               { "set_channel_signal", Nageru_set_channel_signal },
+               { "set_supports_wb", Nageru_set_supports_wb },
                { NULL, NULL }
        };
        lua_pushlightuserdata(L, this);
@@ -1552,9 +1582,19 @@ int Theme::get_channel_signal(unsigned channel)
 {
        lock_guard<mutex> lock(m);
        lua_getglobal(L, "channel_signal");
+       if (lua_isnil(L, -1)) {
+               lua_pop(L, 1);
+               if (channel_signals.count(channel)) {
+                       return channel_signals[channel];
+               } else {
+                       return -1;
+               }
+       }
+
        lua_pushnumber(L, channel);
        if (lua_pcall(L, 1, 1, 0) != 0) {
                fprintf(stderr, "error running function `channel_signal': %s\n", lua_tostring(L, -1));
+               fprintf(stderr, "Try Nageru.set_channel_signal(channel, signal) at the start of the script instead.\n");
                abort();
        }
 
@@ -1590,9 +1630,19 @@ bool Theme::get_supports_set_wb(unsigned channel)
 {
        lock_guard<mutex> lock(m);
        lua_getglobal(L, "supports_set_wb");
+       if (lua_isnil(L, -1)) {
+               lua_pop(L, 1);
+               if (channel_supports_wb.count(channel)) {
+                       return channel_supports_wb[channel];
+               } else {
+                       return false;
+               }
+       }
+
        lua_pushnumber(L, channel);
        if (lua_pcall(L, 1, 1, 0) != 0) {
                fprintf(stderr, "error running function `supports_set_wb': %s\n", lua_tostring(L, -1));
+               fprintf(stderr, "Try Nageru.set_supports_wb(channel, bool) at the start of the script instead.\n");
                abort();
        }
 
index d955e643458773eefa3527176d088072744264fa..22a9ff7d3423a4e203c9fc688be2f4f354306ca5 100644 (file)
@@ -219,12 +219,16 @@ private:
        std::function<void()> theme_menu_callback;
 
        std::map<unsigned, std::string> channel_names;  // Set using Nageru.set_channel_name(). Protected by <m>.
+       std::map<unsigned, int> channel_signals;  // Set using Nageru.set_channel_signal(). Protected by <m>.
+       std::map<unsigned, bool> channel_supports_wb;  // Set using Nageru.set_supports_wb(). Protected by <m>.
 
        friend class LiveInputWrapper;
        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);
+       friend int Nageru_set_channel_signal(lua_State *L);
+       friend int Nageru_set_supports_wb(lua_State *L);
 };
 
 // LiveInputWrapper is a facade on top of an YCbCrInput, exposed to
index c524d566deb1a2ddf057ad5a0b6f25265f97e6a5..6137cae6c0705fd39fb6c6631f0a9c7b38fb7838 100644 (file)
@@ -112,6 +112,23 @@ static_scene:finalize()
 
 -- Set some global state.
 Nageru.set_num_channels(4)  -- Can only be called at the start of the program.
+
+-- Set some global state. Unless marked otherwise, these can only be set once,
+-- at the start of the program.
+Nageru.set_num_channels(4)
+
+-- Sets, for each channel, which signal it corresponds to (starting from 0).
+-- The information is used for whether right-click on the channel should bring up
+-- an input selector or not. Only call this for channels that actually correspond
+-- directly to a signal (ie., live inputs, not live (0) or preview (1)).
+Nageru.set_channel_signal(2, 0)
+Nageru.set_channel_signal(3, 1)
+
+-- Set whether a given channel supports setting white balance. (Default is false.)
+Nageru.set_supports_wb(2, true)
+Nageru.set_supports_wb(3, true)
+
+-- These can be set at any time.
 Nageru.set_channel_name(SBS_SIGNAL_NUM + 2, "Side-by-side")
 Nageru.set_channel_name(STATIC_SIGNAL_NUM + 2, "Static picture")
 
@@ -119,25 +136,6 @@ function is_plain_signal(num)
        return num == INPUT0_SIGNAL_NUM or num == INPUT1_SIGNAL_NUM
 end
 
--- API ENTRY POINT
--- Returns, given a channel number, which signal it corresponds to (starting from 0).
--- Should return -1 if the channel does not correspond to a simple signal
--- (one connected to a capture card, or a video input). The information is used for
--- whether right-click on the channel should bring up a context menu or not,
--- typically containing an input selector, resolution menu etc.
---
--- Called once for each channel, at the start of the program.
--- Will never be called for live (0) or preview (1).
-function channel_signal(channel)
-       if channel == 2 then
-               return 0
-       elseif channel == 3 then
-               return 1
-       else
-               return -1
-       end
-end
-
 -- API ENTRY POINT
 -- Called every frame. Returns the color (if any) to paint around the given
 -- channel. Returns a CSS color (typically to mark live and preview signals);
@@ -173,13 +171,6 @@ function channel_involved_in(channel, signal_num)
        return false
 end
 
--- API ENTRY POINT
--- Returns if a given channel supports setting white balance (starting from 2).
--- Called only once for each channel, at the start of the program.
-function supports_set_wb(channel)
-       return is_plain_signal(channel - 2)
-end
-
 -- API ENTRY POINT
 -- Gets called with a new gray point when the white balance is changing.
 -- The color is in linear light (not sRGB gamma).