+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] <= 0) {
+ str = "no signal";
+ } else if (!input_state_info->last_has_signal[signal_num]) {
+ if (input_state_info->last_height[signal_num] == 525) {
+ // 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;
+}
+
+