X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=nageru%2Ftheme.cpp;h=71a59a24525a8c123da024212e40d9395f5d1769;hb=021fa9fa5ce103ae5843647fee3d5d1d7038d32f;hp=71cb2c322c0a0a03c39fc0d1a0f37de8e75fb693;hpb=7dff1387e5089754b58b29258a64b5d53f866295;p=nageru diff --git a/nageru/theme.cpp b/nageru/theme.cpp index 71cb2c3..71a59a2 100644 --- a/nageru/theme.cpp +++ b/nageru/theme.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -36,6 +37,7 @@ #include "flags.h" #include "image_input.h" #include "input_state.h" +#include "lua_utils.h" #include "pbo_frame_allocator.h" class Mixer; @@ -55,6 +57,20 @@ Theme *get_theme_updata(lua_State* L) return (Theme *)lua_touserdata(L, lua_upvalueindex(1)); } +void print_warning(lua_State* L, const char *format, ...) +{ + char buf[4096]; + va_list ap; + va_start(ap, format); + vsnprintf(buf, sizeof(buf), format, ap); + va_end(ap); + + lua_Debug ar; + lua_getstack(L, 1, &ar); + lua_getinfo(L, "nSl", &ar); + fprintf(stderr, "WARNING: %s:%d: %s", ar.source, ar.currentline, buf); +} + int ThemeMenu_set(lua_State *L) { Theme *theme = get_theme_updata(L); @@ -100,59 +116,6 @@ InputStateInfo::InputStateInfo(const InputState &input_state) } } -class LuaRefWithDeleter { -public: - LuaRefWithDeleter(mutex *m, lua_State *L, int ref) : m(m), L(L), ref(ref) {} - ~LuaRefWithDeleter() { - lock_guard lock(*m); - luaL_unref(L, LUA_REGISTRYINDEX, ref); - } - int get() const { return ref; } - -private: - LuaRefWithDeleter(const LuaRefWithDeleter &) = delete; - - mutex *m; - lua_State *L; - int ref; -}; - -template -int wrap_lua_object(lua_State* L, const char *class_name, Args&&... args) -{ - // Construct the C++ object and put it on the stack. - void *mem = lua_newuserdata(L, sizeof(T)); - new(mem) T(forward(args)...); - - // Look up the metatable named , and set it on the new object. - luaL_getmetatable(L, class_name); - lua_setmetatable(L, -2); - - return 1; -} - -// Like wrap_lua_object, but the object is not owned by Lua; ie. it's not freed -// by Lua GC. This is typically the case for Effects, which are owned by EffectChain -// and expected to be destructed by it. The object will be of type T** instead of T* -// when exposed to Lua. -// -// Note that we currently leak if you allocate an Effect in this way and never call -// add_effect. We should see if there's a way to e.g. set __gc on it at construction time -// and then release that once add_effect() takes ownership. -template -int wrap_lua_object_nonowned(lua_State* L, const char *class_name, Args&&... args) -{ - // Construct the pointer ot the C++ object and put it on the stack. - T **obj = (T **)lua_newuserdata(L, sizeof(T *)); - *obj = new T(forward(args)...); - - // Look up the metatable named , and set it on the new object. - luaL_getmetatable(L, class_name); - lua_setmetatable(L, -2); - - return 1; -} - enum EffectType { WHITE_BALANCE_EFFECT, RESAMPLE_EFFECT, @@ -457,11 +420,7 @@ int LiveInputWrapper_connect_signal(lua_State* L) int signal_num = luaL_checknumber(L, 2); bool success = (*input)->connect_signal(signal_num); if (!success) { - lua_Debug ar; - lua_getstack(L, 1, &ar); - lua_getinfo(L, "nSl", &ar); - fprintf(stderr, "ERROR: %s:%d: Calling connect_signal() on a video or HTML input. Ignoring.\n", - ar.source, ar.currentline); + print_warning(L, "Calling connect_signal() on a video or HTML input. Ignoring.\n"); } return 0; } @@ -480,8 +439,7 @@ int VideoInput_new(lua_State* L) int pixel_format = luaL_checknumber(L, 2); if (pixel_format != bmusb::PixelFormat_8BitYCbCrPlanar && pixel_format != bmusb::PixelFormat_8BitBGRA) { - fprintf(stderr, "WARNING: Invalid enum %d used for video format, choosing Y'CbCr.\n", - pixel_format); + print_warning(L, "Invalid enum %d used for video format, choosing Y'CbCr.\n", pixel_format); pixel_format = bmusb::PixelFormat_8BitYCbCrPlanar; } int ret = wrap_lua_object_nonowned(L, "VideoInput", filename, global_flags.width, global_flags.height); @@ -1528,14 +1486,14 @@ int Theme::map_signal(int signal_num) ++card_index; } if (signal_num >= int(num_cards - 1)) { - fprintf(stderr, "WARNING: Theme asked for input %d, but we only have %u input card(s) (card %d is busy with output).\n", + print_warning(L, "Theme asked for input %d, but we only have %u input card(s) (card %d is busy with output).\n", signal_num, num_cards - 1, global_flags.output_card); fprintf(stderr, "Mapping to card %d instead.\n", card_index); } } else { card_index = signal_num % num_cards; if (signal_num >= int(num_cards)) { - fprintf(stderr, "WARNING: Theme asked for input %d, but we only have %u card(s).\n", signal_num, num_cards); + print_warning(L, "Theme asked for input %d, but we only have %u card(s).\n", signal_num, num_cards); fprintf(stderr, "Mapping to card %d instead.\n", card_index); } }