X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=nageru%2Ftheme.cpp;h=ed99e5f346e9c8eb54b1c9670f4709f0c0dbe093;hb=95a7a58f73728eb027d2bbd3cb819eede4d2ba79;hp=ca4de4fdb22029037438bcedde53769ae03276c6;hpb=392f9d1ccb835c05a3874c4bea163788b2c37024;p=nageru diff --git a/nageru/theme.cpp b/nageru/theme.cpp index ca4de4f..ed99e5f 100644 --- a/nageru/theme.cpp +++ b/nageru/theme.cpp @@ -38,67 +38,6 @@ #include "input_state.h" #include "pbo_frame_allocator.h" -#if !defined LUA_VERSION_NUM || LUA_VERSION_NUM==501 - -// Compatibility shims for LuaJIT 2.0 (LuaJIT 2.1 implements the entire Lua 5.2 API). -// Adapted from https://github.com/keplerproject/lua-compat-5.2/blob/master/c-api/compat-5.2.c -// and licensed as follows: -// -// The MIT License (MIT) -// -// Copyright (c) 2013 Hisham Muhammad -// -// Permission is hereby granted, free of charge, to any person obtaining a copy of -// this software and associated documentation files (the "Software"), to deal in -// the Software without restriction, including without limitation the rights to -// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of -// the Software, and to permit persons to whom the Software is furnished to do so, -// subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in all -// copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS -// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR -// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER -// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN -// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - -/* -** Adapted from Lua 5.2.0 -*/ -void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup) { - luaL_checkstack(L, nup+1, "too many upvalues"); - for (; l->name != NULL; l++) { /* fill the table with given functions */ - int i; - lua_pushstring(L, l->name); - for (i = 0; i < nup; i++) /* copy upvalues to the top */ - lua_pushvalue(L, -(nup + 1)); - lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */ - lua_settable(L, -(nup + 3)); /* table must be below the upvalues, the name and the closure */ - } - lua_pop(L, nup); /* remove upvalues */ -} - -void *luaL_testudata(lua_State *L, int i, const char *tname) { - void *p = lua_touserdata(L, i); - luaL_checkstack(L, 2, "not enough stack slots"); - if (p == NULL || !lua_getmetatable(L, i)) - return NULL; - else { - int res = 0; - luaL_getmetatable(L, tname); - res = lua_rawequal(L, -1, -2); - lua_pop(L, 2); - if (!res) - p = NULL; - } - return p; -} - -#endif - class Mixer; namespace movit { @@ -133,6 +72,8 @@ struct InputStateInfo { unsigned last_width[MAX_VIDEO_CARDS], last_height[MAX_VIDEO_CARDS]; bool last_interlaced[MAX_VIDEO_CARDS], last_has_signal[MAX_VIDEO_CARDS], last_is_connected[MAX_VIDEO_CARDS]; unsigned last_frame_rate_nom[MAX_VIDEO_CARDS], last_frame_rate_den[MAX_VIDEO_CARDS]; + bool has_last_subtitle[MAX_VIDEO_CARDS]; + std::string last_subtitle[MAX_VIDEO_CARDS]; }; InputStateInfo::InputStateInfo(const InputState &input_state) @@ -154,6 +95,8 @@ InputStateInfo::InputStateInfo(const InputState &input_state) last_is_connected[signal_num] = userdata->last_is_connected; last_frame_rate_nom[signal_num] = userdata->last_frame_rate_nom; last_frame_rate_den[signal_num] = userdata->last_frame_rate_den; + has_last_subtitle[signal_num] = userdata->has_last_subtitle; + last_subtitle[signal_num] = userdata->last_subtitle; } } @@ -161,7 +104,7 @@ class LuaRefWithDeleter { public: LuaRefWithDeleter(mutex *m, lua_State *L, int ref) : m(m), L(L), ref(ref) {} ~LuaRefWithDeleter() { - unique_lock lock(*m); + lock_guard lock(*m); luaL_unref(L, LUA_REGISTRYINDEX, ref); } int get() const { return ref; } @@ -512,7 +455,7 @@ int HTMLInput_new(lua_State* L) #else fprintf(stderr, "This version of Nageru has been compiled without CEF support.\n"); fprintf(stderr, "HTMLInput is not available.\n"); - exit(1); + abort(); #endif } @@ -695,6 +638,20 @@ int InputStateInfo_get_frame_rate_den(lua_State* L) return 1; } +int InputStateInfo_get_last_subtitle(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)); + if (!input_state_info->has_last_subtitle[signal_num]) { + lua_pushnil(L); + } else { + lua_pushstring(L, input_state_info->last_subtitle[signal_num].c_str()); + } + return 1; +} + int Effect_set_float(lua_State *L) { assert(lua_gettop(L) == 3); @@ -890,6 +847,7 @@ const luaL_Reg InputStateInfo_funcs[] = { { "get_is_connected", InputStateInfo_get_is_connected }, { "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 }, { NULL, NULL } }; @@ -1121,7 +1079,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)); - exit(1); + abort(); } int num_channels = luaL_checknumber(L, 1); @@ -1184,7 +1142,7 @@ Theme::Theme(const string &filename, const vector &search_dirs, Resource for (const string &error : errors) { fprintf(stderr, "%s\n", error.c_str()); } - exit(1); + abort(); } assert(lua_gettop(L) == 0); @@ -1225,7 +1183,7 @@ Theme::Theme(const string &filename, const vector &search_dirs, Resource luaL_unref(L, LUA_REGISTRYINDEX, theme_code_ref); if (lua_pcall(L, 0, 0, 0)) { fprintf(stderr, "Error when running %s: %s\n", path.c_str(), lua_tostring(L, -1)); - exit(1); + abort(); } assert(lua_gettop(L) == 0); @@ -1278,11 +1236,11 @@ void Theme::register_class(const char *class_name, const luaL_Reg *funcs) assert(lua_gettop(L) == 0); } -Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height, InputState input_state) +Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned height, const InputState &input_state) { Chain chain; - unique_lock lock(m); + lock_guard lock(m); assert(lua_gettop(L) == 0); lua_getglobal(L, "get_chain"); /* function to be called */ lua_pushnumber(L, num); @@ -1293,19 +1251,19 @@ Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned he if (lua_pcall(L, 5, 2, 0) != 0) { fprintf(stderr, "error running function `get_chain': %s\n", lua_tostring(L, -1)); - exit(1); + abort(); } EffectChain *effect_chain = (EffectChain *)luaL_testudata(L, -2, "EffectChain"); if (effect_chain == nullptr) { fprintf(stderr, "get_chain() for chain number %d did not return an EffectChain\n", num); - exit(1); + abort(); } chain.chain = effect_chain; if (!lua_isfunction(L, -1)) { fprintf(stderr, "Argument #-1 should be a function\n"); - exit(1); + abort(); } lua_pushvalue(L, -1); shared_ptr funcref(new LuaRefWithDeleter(&m, L, luaL_ref(L, LUA_REGISTRYINDEX))); @@ -1313,7 +1271,7 @@ Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned he assert(lua_gettop(L) == 0); chain.setup_chain = [this, funcref, input_state, effect_chain]{ - unique_lock lock(m); + lock_guard lock(m); assert(this->input_state == nullptr); this->input_state = &input_state; @@ -1322,7 +1280,7 @@ Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned he lua_rawgeti(L, LUA_REGISTRYINDEX, funcref->get()); if (lua_pcall(L, 0, 0, 0) != 0) { fprintf(stderr, "error running chain setup callback: %s\n", lua_tostring(L, -1)); - exit(1); + abort(); } assert(lua_gettop(L) == 0); @@ -1358,17 +1316,17 @@ Theme::Chain Theme::get_chain(unsigned num, float t, unsigned width, unsigned he string Theme::get_channel_name(unsigned channel) { - unique_lock lock(m); + lock_guard lock(m); lua_getglobal(L, "channel_name"); 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)); - exit(1); + abort(); } const char *ret = lua_tostring(L, -1); if (ret == nullptr) { fprintf(stderr, "function `channel_name' returned nil for channel %d\n", channel); - exit(1); + abort(); } string retstr = ret; @@ -1379,12 +1337,12 @@ string Theme::get_channel_name(unsigned channel) int Theme::get_channel_signal(unsigned channel) { - unique_lock lock(m); + lock_guard lock(m); lua_getglobal(L, "channel_signal"); 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)); - exit(1); + abort(); } int ret = luaL_checknumber(L, 1); @@ -1395,18 +1353,18 @@ int Theme::get_channel_signal(unsigned channel) std::string Theme::get_channel_color(unsigned channel) { - unique_lock lock(m); + lock_guard lock(m); lua_getglobal(L, "channel_color"); lua_pushnumber(L, channel); if (lua_pcall(L, 1, 1, 0) != 0) { fprintf(stderr, "error running function `channel_color': %s\n", lua_tostring(L, -1)); - exit(1); + abort(); } const char *ret = lua_tostring(L, -1); if (ret == nullptr) { fprintf(stderr, "function `channel_color' returned nil for channel %d\n", channel); - exit(1); + abort(); } string retstr = ret; @@ -1417,12 +1375,12 @@ std::string Theme::get_channel_color(unsigned channel) bool Theme::get_supports_set_wb(unsigned channel) { - unique_lock lock(m); + lock_guard lock(m); lua_getglobal(L, "supports_set_wb"); 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)); - exit(1); + abort(); } bool ret = checkbool(L, -1); @@ -1433,7 +1391,7 @@ bool Theme::get_supports_set_wb(unsigned channel) void Theme::set_wb(unsigned channel, double r, double g, double b) { - unique_lock lock(m); + lock_guard lock(m); lua_getglobal(L, "set_wb"); lua_pushnumber(L, channel); lua_pushnumber(L, r); @@ -1441,7 +1399,7 @@ void Theme::set_wb(unsigned channel, double r, double g, double b) lua_pushnumber(L, b); if (lua_pcall(L, 4, 0, 0) != 0) { fprintf(stderr, "error running function `set_wb': %s\n", lua_tostring(L, -1)); - exit(1); + abort(); } assert(lua_gettop(L) == 0); @@ -1449,12 +1407,12 @@ void Theme::set_wb(unsigned channel, double r, double g, double b) vector Theme::get_transition_names(float t) { - unique_lock lock(m); + lock_guard lock(m); lua_getglobal(L, "get_transitions"); lua_pushnumber(L, t); if (lua_pcall(L, 1, 1, 0) != 0) { fprintf(stderr, "error running function `get_transitions': %s\n", lua_tostring(L, -1)); - exit(1); + abort(); } vector ret; @@ -1475,7 +1433,7 @@ int Theme::map_signal(int signal_num) return -1 - signal_num; } - unique_lock lock(map_m); + lock_guard lock(map_m); if (signal_to_card_mapping.count(signal_num)) { return signal_to_card_mapping[signal_num]; } @@ -1505,34 +1463,34 @@ int Theme::map_signal(int signal_num) void Theme::set_signal_mapping(int signal_num, int card_num) { - unique_lock lock(map_m); + lock_guard lock(map_m); assert(card_num < int(num_cards)); signal_to_card_mapping[signal_num] = card_num; } void Theme::transition_clicked(int transition_num, float t) { - unique_lock lock(m); + lock_guard lock(m); lua_getglobal(L, "transition_clicked"); lua_pushnumber(L, transition_num); lua_pushnumber(L, t); if (lua_pcall(L, 2, 0, 0) != 0) { fprintf(stderr, "error running function `transition_clicked': %s\n", lua_tostring(L, -1)); - exit(1); + abort(); } assert(lua_gettop(L) == 0); } void Theme::channel_clicked(int preview_num) { - unique_lock lock(m); + lock_guard lock(m); lua_getglobal(L, "channel_clicked"); lua_pushnumber(L, preview_num); if (lua_pcall(L, 1, 0, 0) != 0) { fprintf(stderr, "error running function `channel_clicked': %s\n", lua_tostring(L, -1)); - exit(1); + abort(); } assert(lua_gettop(L) == 0); } @@ -1568,10 +1526,10 @@ int Theme::set_theme_menu(lua_State *L) void Theme::theme_menu_entry_clicked(int lua_ref) { - unique_lock lock(m); + lock_guard lock(m); lua_rawgeti(L, LUA_REGISTRYINDEX, lua_ref); if (lua_pcall(L, 0, 0, 0) != 0) { fprintf(stderr, "error running menu callback: %s\n", lua_tostring(L, -1)); - exit(1); + abort(); } }