From: Steinar H. Gunderson Date: Fri, 21 Jun 2019 18:12:39 +0000 (+0200) Subject: Convert channel_signal() and supports_set_wb() to imperative versions. X-Git-Tag: 1.9.0~14 X-Git-Url: https://git.sesse.net/?p=nageru;a=commitdiff_plain;h=fe13490dcc54882e6f1fc9293cbdffabf6daa290 Convert channel_signal() and supports_set_wb() to imperative versions. This completes the theme conversion for all called-only-once functions. As before, the old versions are supported for pre-1.9.0 compatibility. --- diff --git a/nageru/simple.lua b/nageru/simple.lua index 5444a20..f349f3f 100644 --- a/nageru/simple.lua +++ b/nageru/simple.lua @@ -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). diff --git a/nageru/theme.cpp b/nageru/theme.cpp index b53c65b..7c3b366 100644 --- a/nageru/theme.cpp +++ b/nageru/theme.cpp @@ -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 &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 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 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(); } diff --git a/nageru/theme.h b/nageru/theme.h index d955e64..22a9ff7 100644 --- a/nageru/theme.h +++ b/nageru/theme.h @@ -219,12 +219,16 @@ private: std::function theme_menu_callback; std::map channel_names; // Set using Nageru.set_channel_name(). Protected by . + std::map channel_signals; // Set using Nageru.set_channel_signal(). Protected by . + std::map channel_supports_wb; // Set using Nageru.set_supports_wb(). Protected by . 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 diff --git a/nageru/theme.lua b/nageru/theme.lua index c524d56..6137cae 100644 --- a/nageru/theme.lua +++ b/nageru/theme.lua @@ -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).