]> git.sesse.net Git - nageru/commitdiff
Move the handling of human-readable input resolution printing into C++; every theme...
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 21 Jun 2019 16:22:28 +0000 (18:22 +0200)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Fri, 21 Jun 2019 16:22:28 +0000 (18:22 +0200)
nageru/theme.cpp
nageru/theme.lua

index c881dc6891be2b1a32e90fb1b3fb8ec3b043f515..8050003b2025459651d20a99dea5f4d120347da3 100644 (file)
@@ -700,6 +700,69 @@ int InputStateInfo_get_last_subtitle(lua_State* L)
        return 1;
 }
 
+namespace {
+
+// Helper function to write e.g. “60” or “59.94”.
+string format_frame_rate(int nom, int den)
+{
+       char buf[256];
+       if (nom % den == 0) {
+               snprintf(buf, sizeof(buf), "%d", nom / den);
+       } else {
+               snprintf(buf, sizeof(buf), "%.2f", double(nom) / den);
+       }
+       return buf;
+}
+
+// Helper function to write e.g. “720p60”.
+string get_human_readable_resolution(const InputStateInfo *input_state_info, int signal_num)
+{
+       char buf[256];
+       if (input_state_info->last_interlaced[signal_num]) {
+               snprintf(buf, sizeof(buf), "%di", input_state_info->last_height[signal_num] * 2);
+
+               // Show field rate instead of frame rate; really for cosmetics only
+               // (and actually contrary to EBU recommendations, although in line
+               // with typical user expectations).
+               return buf + format_frame_rate(input_state_info->last_frame_rate_nom[signal_num] * 2,
+                       input_state_info->last_frame_rate_den[signal_num]);
+       } else {
+               snprintf(buf, sizeof(buf), "%dp", input_state_info->last_height[signal_num]);
+               return buf + format_frame_rate(input_state_info->last_frame_rate_nom[signal_num],
+                       input_state_info->last_frame_rate_den[signal_num]);
+       }
+}
+
+} // namespace
+
+int InputStateInfo_get_human_readable_resolution(lua_State* L)
+{
+       assert(lua_gettop(L) == 2);
+       InputStateInfo *input_state_info = get_input_state_info(L, 1);
+       Theme *theme = get_theme_updata(L);
+       int signal_num = theme->map_signal(luaL_checknumber(L, 2));
+
+       string str;
+       if (!input_state_info->last_is_connected[signal_num]) {
+               str = "disconnected";
+       } else if (input_state_info->last_height[signal_num]) {
+               str = "no signal";
+       } else if (!input_state_info->last_has_signal[signal_num]) {
+               if (input_state_info->last_height[signal_num]) {
+                       // Special mode for the USB3 cards.
+                       str = "no signal";
+               } else {
+                       str = get_human_readable_resolution(input_state_info, signal_num) + ", no signal";
+               }
+       } else {
+               str = get_human_readable_resolution(input_state_info, signal_num);
+       }
+
+       lua_pushstring(L, str.c_str());
+       return 1;
+}
+
+
 int EffectBlueprint_set_int(lua_State *L)
 {
        assert(lua_gettop(L) == 3);
@@ -921,6 +984,7 @@ const luaL_Reg InputStateInfo_funcs[] = {
        { "get_frame_rate_nom", InputStateInfo_get_frame_rate_nom },
        { "get_frame_rate_den", InputStateInfo_get_frame_rate_den },
        { "get_last_subtitle", InputStateInfo_get_last_subtitle },
+       { "get_human_readable_resolution", InputStateInfo_get_human_readable_resolution },
        { NULL, NULL }
 };
 
index 973e389d784f2c59e5345f458289e5dd23c0dd40..de8d25222e9492e1118d94897bc7c0fc9743750f 100644 (file)
@@ -126,48 +126,6 @@ function is_plain_signal(num)
        return num == INPUT0_SIGNAL_NUM or num == INPUT1_SIGNAL_NUM
 end
 
--- Helper function to write e.g. “720p60”. The difference between this
--- and get_channel_resolution_raw() is that this one also can say that
--- there's no signal.
-function get_channel_resolution(signal_num)
-       local res = last_resolution[signal_num]
-       if (not res) or not res.is_connected then
-               return "disconnected"
-       end
-       if res.height <= 0 then
-               return "no signal"
-       end
-       if not res.has_signal then
-               if res.height == 525 then
-                       -- Special mode for the USB3 cards.
-                       return "no signal"
-               end
-               return get_channel_resolution_raw(res) .. ", no signal"
-       else
-               return get_channel_resolution_raw(res)
-       end
-end
-
--- Helper function to write e.g. “60” or “59.94”.
-function get_frame_rate(res)
-       local nom = res.frame_rate_nom
-       local den = res.frame_rate_den
-       if nom % den == 0 then
-               return nom / den
-       else
-               return string.format("%.2f", nom / den)
-       end
-end
-
--- Helper function to write e.g. “720p60”.
-function get_channel_resolution_raw(res)
-       if res.interlaced then
-               return res.height .. "i" .. get_frame_rate(res)
-       else
-               return res.height .. "p" .. get_frame_rate(res)
-       end
-end
-
 -- API ENTRY POINT
 -- Returns the name for each additional channel (starting from 2).
 -- Called at the start of the program, and then each frame for live
@@ -175,7 +133,11 @@ end
 function channel_name(channel)
        local signal_num = channel - 2
        if is_plain_signal(signal_num) then
-               return "Input " .. (signal_num + 1) .. " (" .. get_channel_resolution(signal_num) .. ")"
+               if last_resolution[signal_num] then
+                       return "Input " .. (signal_num + 1) .. " (" .. last_resolution[signal_num].human_readable_resolution .. ")"
+               else
+                       return "Input " .. (signal_num + 1)
+               end
        elseif signal_num == SBS_SIGNAL_NUM then
                return "Side-by-side"
        elseif signal_num == STATIC_SIGNAL_NUM then
@@ -435,7 +397,8 @@ function get_scene(num, t, width, height, signals)
                        is_connected = signals:get_is_connected(signal_num),
                        has_signal = signals:get_has_signal(signal_num),
                        frame_rate_nom = signals:get_frame_rate_nom(signal_num),
-                       frame_rate_den = signals:get_frame_rate_den(signal_num)
+                       frame_rate_den = signals:get_frame_rate_den(signal_num),
+                       human_readable_resolution = signals:get_human_readable_resolution(signal_num)
                }
 
                if res.interlaced then