+-- 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)
+ res = last_resolution[signal_num]
+ if (not res) or 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
+-- channels in case they change resolution.
+function channel_name(channel)
+ if channel == 2 then
+ return "Input 1 (" .. get_channel_resolution(0) .. ")"
+ elseif channel == 3 then
+ return "Input 2 (" .. get_channel_resolution(1) .. ")"
+ elseif channel == 4 then
+ return "Side-by-side"
+ elseif channel == 5 then
+ return "Static picture"
+ end
+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.
+-- (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);
+-- "transparent" is allowed.
+-- Will never be called for live (0) or preview (1).
+function channel_color(channel)
+ if channel_involved_in(channel, live_signal_num) then
+ return "#f00"
+ end
+ if channel_involved_in(channel, preview_signal_num) then
+ return "#0f0"
+ end
+ return "transparent"
+end
+
+function channel_involved_in(channel, signal_num)
+ if signal_num == INPUT0_SIGNAL_NUM then
+ return channel == 2
+ end
+ if signal_num == INPUT1_SIGNAL_NUM then
+ return channel == 3
+ end
+ if signal_num == SBS_SIGNAL_NUM then
+ return (channel == 2 or channel == 3)