diff --git a/src/Lib/Script/CRTPLuaScriptObject.hh b/src/Lib/Script/CRTPLuaScriptObject.hh index 311acb65c66067ed04d6e64411440208b3ed17cb..6772bb5a779fd79e8354e9c37499a71a47494b7f 100644 --- a/src/Lib/Script/CRTPLuaScriptObject.hh +++ b/src/Lib/Script/CRTPLuaScriptObject.hh @@ -17,10 +17,15 @@ namespace Vivy::Script #define LUA_RETURN_NOTHING(L) { lua_settop(L, 0); return 0; } // clang-format on +#define method_list static constexpr inline auto #define script_class(theClassName) \ class theClassName final : public CRTPLuaScriptObject<theClassName> -#define method_list static constexpr inline auto +#define LUA_DECL_GETTER(type, luaName, value) \ + static int luaName(lua_State *const L) noexcept \ + { \ + return luaPushValue<type>(L, CHECK(L, 1)->value); \ + } #define LUA_SCRIPTABLE_CLASS_MANUAL_CTOR(theClassName) \ VIVY_UNMOVABLE_OBJECT(theClassName) \ @@ -62,6 +67,30 @@ protected: exit(EXIT_FAILURE); // lua_error should not return anything } + // Push something onto the stack, must be specialized + template <typename T> static int luaPushValue(lua_State *const L, const T &thing) noexcept; + using std_str = std::string; + using std_strv = std::string_view; + + template <> static int luaPushValue<int>(lua_State *const L, const int &thing) noexcept + { + lua_pushinteger(L, thing); + return 1; + } + + template <> + static int luaPushValue<std_strv>(lua_State *const L, const std_strv &thing) noexcept + { + lua_pushlstring(L, thing.data(), thing.size()); + return 1; + } + + template <> static int luaPushValue<std_str>(lua_State *const L, const std_str &thing) noexcept + { + lua_pushstring(L, thing.c_str()); + return 1; + } + static bool PushFunctionFromRegistry(lua_State *const L, const int key) noexcept { if (key >= 0) { @@ -335,13 +364,22 @@ script_class (FreeFunctions) { static int print(lua_State *const) noexcept; static int getModule(lua_State *const) noexcept; - static int start(lua_State *const) noexcept; - static int finish(lua_State *const) noexcept; + + LUA_DECL_GETTER(int, start, audioStart) + LUA_DECL_GETTER(int, finish, audioFinish) + LUA_DECL_GETTER(int, width, videoWidth) + LUA_DECL_GETTER(int, height, videoHeight) method_list metaMethods = { luaRegDefaultGC }; method_list methods = { LUA_DECL_METHOD(FreeFunctions, getModule), LUA_DECL_METHOD(FreeFunctions, start), LUA_DECL_METHOD(FreeFunctions, finish), - LUA_DECL_METHOD(FreeFunctions, print), LUA_DECL_CREATE(FreeFunctions) }; + LUA_DECL_METHOD(FreeFunctions, width), + LUA_DECL_METHOD(FreeFunctions, height), + LUA_DECL_METHOD(FreeFunctions, print), + LUA_DECL_CREATE(FreeFunctions) }; + + int videoWidth{ 0 }, videoHeight{ 0 }; + int audioStart{ 0 }, audioFinish{ 0 }; }; } diff --git a/src/Lib/Script/CRTPLuaScriptObject/FreeFunctions.cc b/src/Lib/Script/CRTPLuaScriptObject/FreeFunctions.cc index 9192f1d1f73cc355505ee4f479a565b8b955107a..36c9cee70d84ed5ad0de6ccae9d6c000d0358bfe 100644 --- a/src/Lib/Script/CRTPLuaScriptObject/FreeFunctions.cc +++ b/src/Lib/Script/CRTPLuaScriptObject/FreeFunctions.cc @@ -1,5 +1,9 @@ #include "../CRTPLuaScriptObject.hh" #include "../LuaContext.hh" +#include "../../Document/CRTPSubDocument.hh" +#include "../../Document/VivyDocument.hh" +#include "../../Audio.hh" +#include "../../Video.hh" #include "../../../VivyApplication.hh" using namespace Vivy::Script; @@ -11,6 +15,22 @@ FreeFunctions::FreeFunctions(lua_State *const L) noexcept std::shared_ptr<VivyDocument> vivyDoc = context->getAttachedVivyDocument(); if (vivyDoc) { + if (!(vivyDoc->checkDocumentCapabilities(VivyDocument::AudioAble) && + vivyDoc->checkDocumentCapabilities(VivyDocument::AssAble) && + vivyDoc->checkDocumentCapabilities(VivyDocument::AudioAble))) { + err(context) << "The VivyDocument is not a complete document, " + << "can't load it for scripting"; + luaGlobalError(L, "Attached VivyDocument is not complete"); + } + + out(context) << "Initializing the FreeFunctions lua object with the VivyDocument"; + std::shared_ptr<VideoStream> video = vivyDoc->getVideoSubDocument()->getDefaultStream(); + [[maybe_unused]] std::shared_ptr<AudioStream> audio = + vivyDoc->getAudioSubDocument()->getDefaultStream(); + + videoWidth = video->getWidth(); + videoHeight = video->getWidth(); + TODO(Set getters values for audio) } else { @@ -34,19 +54,3 @@ FreeFunctions::getModule(lua_State *const L) noexcept [[maybe_unused]] const ModuleDeclaration *const mod = context->getModule(modName); return 1; } - -int -FreeFunctions::start(lua_State *const L) noexcept -{ - lua_settop(L, 0); - lua_pushinteger(L, 0); - return 1; -} - -int -FreeFunctions::finish(lua_State *const L) noexcept -{ - lua_settop(L, 0); - lua_pushinteger(L, 0); - return 1; -}