X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=nageru%2Ftheme.cpp;h=35046c18ee17a0b77dc474c51d3cf5fae9c21b39;hb=c9181491b62cb8b928e7c92ae204ed234bf7f3ea;hp=d31a4b948f42384c3d554d6a8069ab54b0e5b5d6;hpb=5a8cccb37e6832197fac3afa0fa39cf83283d5af;p=nageru diff --git a/nageru/theme.cpp b/nageru/theme.cpp index d31a4b9..35046c1 100644 --- a/nageru/theme.cpp +++ b/nageru/theme.cpp @@ -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] <= 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; +} + + int EffectBlueprint_set_int(lua_State *L) { assert(lua_gettop(L) == 3); @@ -795,6 +858,7 @@ const luaL_Reg Block_funcs[] = { { "enable", Block_enable }, { "enable_if", Block_enable_if }, { "disable", Block_disable }, + { "always_disable_if_disabled", Block_always_disable_if_disabled }, { "set_int", Block_set_int }, { "set_float", Block_set_float }, { "set_vec3", Block_set_vec3 }, @@ -921,6 +985,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 } }; @@ -1148,6 +1213,7 @@ int call_num_channels(lua_State *L) if (lua_pcall(L, 0, 1, 0) != 0) { fprintf(stderr, "error running function `num_channels': %s\n", lua_tostring(L, -1)); + fprintf(stderr, "Try Nageru.set_num_channels(...) at the start of the script instead.\n"); abort(); } @@ -1159,6 +1225,57 @@ int call_num_channels(lua_State *L) } // namespace +int Nageru_set_channel_name(lua_State *L) +{ + // NOTE: m is already locked. + Theme *theme = get_theme_updata(L); + unsigned channel = luaL_checknumber(L, 1); + const string text = checkstdstring(L, 2); + theme->channel_names[channel] = text; + lua_pop(L, 2); + return 0; +} + +int Nageru_set_num_channels(lua_State *L) +{ + // NOTE: m is already locked. + Theme *theme = get_theme_updata(L); + if (theme->startup_finished) { + luaL_error(L, "set_num_channels() can only be called at startup."); + } + theme->num_channels = luaL_checknumber(L, 1); + lua_pop(L, 1); + 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) { @@ -1229,7 +1346,7 @@ Theme::Theme(const string &filename, const vector &search_dirs, Resource } // Set up the API we provide. - register_constants(); + register_globals(); register_class("Scene", Scene_funcs); register_class("Block", Block_funcs); register_class("EffectBlueprint", EffectBlueprint_funcs); @@ -1260,16 +1377,20 @@ Theme::Theme(const string &filename, const vector &search_dirs, Resource } assert(lua_gettop(L) == 0); - // Ask it for the number of channels. - num_channels = call_num_channels(L); + if (num_channels == -1) { + // Ask it for the number of channels. + num_channels = call_num_channels(L); + } + startup_finished = true; } Theme::~Theme() { + theme_menu.reset(); lua_close(L); } -void Theme::register_constants() +void Theme::register_globals() { // Set Nageru.VIDEO_FORMAT_BGRA = bmusb::PixelFormat_8BitBGRA, etc. const vector> num_constants = { @@ -1293,6 +1414,16 @@ void Theme::register_constants() lua_settable(L, 1); // t[key] = value } + 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); + luaL_setfuncs(L, Nageru_funcs, 1); // for (name,f in funcs) { mt[name] = f, with upvalue {theme} } + lua_setglobal(L, "Nageru"); // Nageru = t assert(lua_gettop(L) == 0); } @@ -1419,7 +1550,17 @@ Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned he string Theme::get_channel_name(unsigned channel) { lock_guard lock(m); + lua_getglobal(L, "channel_name"); + if (lua_isnil(L, -1)) { + lua_pop(L, 1); + if (channel_names.count(channel)) { + return channel_names[channel]; + } else { + return "(no title)"; + } + } + lua_pushnumber(L, channel); if (lua_pcall(L, 1, 1, 0) != 0) { fprintf(stderr, "error running function `channel_name': %s\n", lua_tostring(L, -1)); @@ -1428,6 +1569,7 @@ string Theme::get_channel_name(unsigned channel) const char *ret = lua_tostring(L, -1); if (ret == nullptr) { fprintf(stderr, "function `channel_name' returned nil for channel %d\n", channel); + fprintf(stderr, "Try Nageru.set_channel_name(channel, name) at the start of the script instead.\n"); abort(); } @@ -1441,9 +1583,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(); } @@ -1479,9 +1631,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(); } @@ -1597,25 +1759,71 @@ void Theme::channel_clicked(int preview_num) assert(lua_gettop(L) == 0); } -int Theme::set_theme_menu(lua_State *L) +template +void destroy(T &ref) { - for (const Theme::MenuEntry &entry : theme_menu) { - luaL_unref(L, LUA_REGISTRYINDEX, entry.lua_ref); + ref.~T(); +} + +Theme::MenuEntry::~MenuEntry() +{ + if (is_submenu) { + destroy(submenu); + } else { + luaL_unref(entry.L, LUA_REGISTRYINDEX, entry.lua_ref); } - theme_menu.clear(); +} - int num_elements = lua_gettop(L); - for (int i = 1; i <= num_elements; ++i) { - lua_rawgeti(L, i, 1); - const string text = checkstdstring(L, -1); - lua_pop(L, 1); +namespace { + +vector> create_recursive_theme_menu(lua_State *L); + +unique_ptr create_theme_menu_entry(lua_State *L, int index) +{ + unique_ptr entry; + + lua_rawgeti(L, index, 1); + const string text = checkstdstring(L, -1); + lua_pop(L, 1); - lua_rawgeti(L, i, 2); + lua_rawgeti(L, index, 2); + if (lua_istable(L, -1)) { + vector> submenu = create_recursive_theme_menu(L); + entry.reset(new Theme::MenuEntry{ text, move(submenu) }); + lua_pop(L, 1); + } else { luaL_checktype(L, -1, LUA_TFUNCTION); int ref = luaL_ref(L, LUA_REGISTRYINDEX); + entry.reset(new Theme::MenuEntry{ text, L, ref }); + } + return entry; +} - theme_menu.push_back(MenuEntry{ text, ref }); +vector> create_recursive_theme_menu(lua_State *L) +{ + vector> menu; + size_t num_elements = lua_objlen(L, -1); + for (size_t i = 1; i <= num_elements; ++i) { + lua_rawgeti(L, -1, i); + menu.emplace_back(create_theme_menu_entry(L, -1)); + lua_pop(L, 1); } + return menu; +} + +} // namespace + +int Theme::set_theme_menu(lua_State *L) +{ + theme_menu.reset(); + + vector> root_menu; + int num_elements = lua_gettop(L); + for (int i = 1; i <= num_elements; ++i) { + root_menu.emplace_back(create_theme_menu_entry(L, i)); + } + theme_menu.reset(new MenuEntry("", move(root_menu))); + lua_pop(L, num_elements); assert(lua_gettop(L) == 0);