diff --git a/src/gui/lua/Makefile.am b/src/gui/lua/Makefile.am index 291a551a4..00d8e81ca 100644 --- a/src/gui/lua/Makefile.am +++ b/src/gui/lua/Makefile.am @@ -39,4 +39,5 @@ libneutrino_gui_lua_a_SOURCES = \ lua_hintbox.cpp \ lua_menue.cpp \ lua_messagebox.cpp \ + lua_misc.cpp \ lua_video.cpp diff --git a/src/gui/lua/lua_misc.cpp b/src/gui/lua/lua_misc.cpp new file mode 100644 index 000000000..e1c02bf76 --- /dev/null +++ b/src/gui/lua/lua_misc.cpp @@ -0,0 +1,199 @@ +/* + * lua misc functions + * + * (C) 2014-2015 M. Liebmann (micha-bbg) + * (C) 2014 Sven Hoefer (svenhoefer) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include "luainstance.h" +#include "lua_misc.h" + +CLuaInstMisc* CLuaInstMisc::getInstance() +{ + static CLuaInstMisc* LuaInstMisc = NULL; + + if(!LuaInstMisc) + LuaInstMisc = new CLuaInstMisc(); + return LuaInstMisc; +} + +CLuaData *CLuaInstMisc::CheckData(lua_State *L, int narg) +{ + luaL_checktype(L, narg, LUA_TUSERDATA); + void *ud = luaL_checkudata(L, narg, LUA_CLASSNAME); + if (!ud) + fprintf(stderr, "[CLuaInstMisc::%s] wrong type %p, %d, %s\n", __func__, L, narg, LUA_CLASSNAME); + return *(CLuaData **)ud; // unbox pointer +} + +int CLuaInstMisc::strFind_old(lua_State *L) +{ + int numargs = lua_gettop(L); + if (numargs < 3) { + printf("CLuaInstMisc::%s: not enough arguments (%d, expected 2 (or 3 or 4))\n", __func__, numargs); + lua_pushnil(L); + return 1; + } + const char *s1; + const char *s2; + int pos=0, n=0, ret=0; + s1 = luaL_checkstring(L, 2); + s2 = luaL_checkstring(L, 3); + if (numargs > 3) + pos = luaL_checkint(L, 4); + if (numargs > 4) + n = luaL_checkint(L, 5); + + std::string str(s1); + if (numargs > 4) + ret = str.find(s2, pos, n); + else + ret = str.find(s2, pos); + +// printf("####[%s:%d] str_len: %d, s2: %s, pos: %d, n: %d, ret: %d\n", __func__, __LINE__, str.length(), s2, pos, n, ret); + if (ret == (int)std::string::npos) + lua_pushnil(L); + else + lua_pushinteger(L, ret); + return 1; +} + +int CLuaInstMisc::strSub_old(lua_State *L) +{ + int numargs = lua_gettop(L); + if (numargs < 3) { + printf("CLuaInstMisc::%s: not enough arguments (%d, expected 2 (or 3))\n", __func__, numargs); + lua_pushstring(L, ""); + return 1; + } + const char *s1; + int pos=0, len=std::string::npos; + std::string ret=""; + s1 = luaL_checkstring(L, 2); + pos = luaL_checkint(L, 3); + if (numargs > 3) + len = luaL_checkint(L, 4); + + std::string str(s1); + ret = str.substr(pos, len); + +// printf("####[%s:%d] str_len: %d, pos: %d, len: %d, ret_len: %d\n", __func__, __LINE__, str.length(), pos, len, ret.length()); + lua_pushstring(L, ret.c_str()); + return 1; +} + +int CLuaInstMisc::createChannelIDfromUrl_old(lua_State *L) +{ + int numargs = lua_gettop(L); + if (numargs < 2) { + printf("CLuaInstMisc::%s: no arguments\n", __func__); + lua_pushnil(L); + return 1; + } + + const char *url = luaL_checkstring(L, 2); + if (strlen(url) < 1 ) { + lua_pushnil(L); + return 1; + } + + t_channel_id id = CREATE_CHANNEL_ID(0, 0, 0, url); + char id_str[17]; + snprintf(id_str, sizeof(id_str), "%" PRIx64, id); + + lua_pushstring(L, id_str); + return 1; +} + +int CLuaInstMisc::enableInfoClock_old(lua_State *L) +{ + bool enable = true; + int numargs = lua_gettop(L); + if (numargs > 1) + enable = _luaL_checkbool(L, 2); + CInfoClock::getInstance()->enableInfoClock(enable); + return 0; +} + +int CLuaInstMisc::runScriptExt_old(lua_State *L) +{ + CLuaData *W = CheckData(L, 1); + if (!W) return 0; + + int numargs = lua_gettop(L); + const char *script = luaL_checkstring(L, 2); + std::vector args; + for (int i = 3; i <= numargs; i++) { + std::string arg = luaL_checkstring(L, i); + if (!arg.empty()) + args.push_back(arg); + } + + CLuaInstance *lua = new CLuaInstance(); + lua->runScript(script, &args); + args.clear(); + delete lua; + return 0; +} + +int CLuaInstMisc::GetRevision_old(lua_State *L) +{ + unsigned int rev = 0; + std::string hw = ""; +#if HAVE_COOL_HARDWARE + hw = "Coolstream"; +#endif + rev = cs_get_revision(); + lua_pushinteger(L, rev); + lua_pushstring(L, hw.c_str()); + return 2; +} + +int CLuaInstMisc::checkVersion_old(lua_State *L) +{ + int numargs = lua_gettop(L); + if (numargs < 3) { + printf("CLuaInstMisc::%s: not enough arguments (%d, expected 2)\n", __func__, numargs); + lua_pushnil(L); + return 1; + } + int major=0, minor=0; + major = luaL_checkint(L, 2); + minor = luaL_checkint(L, 3); + if ((major > LUA_API_VERSION_MAJOR) || ((major == LUA_API_VERSION_MAJOR) && (minor > LUA_API_VERSION_MINOR))) { + char msg[1024]; + snprintf(msg, sizeof(msg)-1, "%s (v%d.%d)\n%s v%d.%d", + g_Locale->getText(LOCALE_LUA_VERSIONSCHECK1), + LUA_API_VERSION_MAJOR, LUA_API_VERSION_MINOR, + g_Locale->getText(LOCALE_LUA_VERSIONSCHECK2), + major, minor); + luaL_error(L, msg); + } + lua_pushinteger(L, 1); /* for backward compatibility */ + return 1; +} diff --git a/src/gui/lua/lua_misc.h b/src/gui/lua/lua_misc.h new file mode 100644 index 000000000..44c41f467 --- /dev/null +++ b/src/gui/lua/lua_misc.h @@ -0,0 +1,54 @@ +/* + * lua misc functions + * + * (C) 2014-2015 M. Liebmann (micha-bbg) + * (C) 2014 Sven Hoefer (svenhoefer) + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef _LUAMISCFUNCS_H +#define _LUAMISCFUNCS_H + +/* +class CLuaMisc +{ + public: + CLuaMisc() {}; + ~CLuaMisc() {}; +}; +*/ + +class CLuaInstMisc +{ + static CLuaData *CheckData(lua_State *L, int narg); + public: + CLuaInstMisc() {}; + ~CLuaInstMisc() {}; + static CLuaInstMisc* getInstance(); +// static void MiscRegister(lua_State *L); + + static int strFind_old(lua_State *L); + static int strSub_old(lua_State *L); + static int createChannelIDfromUrl_old(lua_State *L); + static int enableInfoClock_old(lua_State *L); + static int runScriptExt_old(lua_State *L); + static int GetRevision_old(lua_State *L); + static int checkVersion_old(lua_State *L); + +// private: +// static CLuaMisc *MiscCheck(lua_State *L, int n); +}; + +#endif //_LUAMISCFUNCS_H diff --git a/src/gui/lua/luainstance.cpp b/src/gui/lua/luainstance.cpp index 826b72ee6..215269acc 100644 --- a/src/gui/lua/luainstance.cpp +++ b/src/gui/lua/luainstance.cpp @@ -31,10 +31,8 @@ #include #include #include -#include #include #include -#include #include #include "luainstance.h" @@ -46,6 +44,7 @@ #include "lua_hintbox.h" #include "lua_menue.h" #include "lua_messagebox.h" +#include "lua_misc.h" #include "lua_video.h" static void set_lua_variables(lua_State *L) @@ -471,7 +470,6 @@ void CLuaInstance::abortScript() const luaL_Reg CLuaInstance::methods[] = { - { "GetRevision", CLuaInstance::GetRevision }, { "PaintBox", CLuaInstance::PaintBox }, { "paintHLine", CLuaInstance::paintHLineRel }, { "paintVLine", CLuaInstance::paintVLineRel }, @@ -487,14 +485,20 @@ const luaL_Reg CLuaInstance::methods[] = { "DisplayImage", CLuaInstance::DisplayImage }, { "Blit", CLuaInstance::Blit }, { "GetLanguage", CLuaInstance::GetLanguage }, - { "runScript", CLuaInstance::runScriptExt }, - { "strFind", CLuaInstance::strFind }, - { "strSub", CLuaInstance::strSub }, - { "checkVersion", CLuaInstance::checkVersion }, - { "createChannelIDfromUrl", CLuaInstance::createChannelIDfromUrl }, - { "enableInfoClock", CLuaInstance::enableInfoClock }, { "getDynFont", CLuaInstance::getDynFont }, + /* + lua_misc.cpp + Deprecated, for the future separate class for misc functions + */ + { "strFind", CLuaInstMisc::getInstance()->strFind_old }, + { "strSub", CLuaInstMisc::getInstance()->strSub_old }, + { "createChannelIDfromUrl", CLuaInstMisc::getInstance()->createChannelIDfromUrl_old }, + { "enableInfoClock", CLuaInstMisc::getInstance()->enableInfoClock_old }, + { "runScript", CLuaInstMisc::getInstance()->runScriptExt_old }, + { "GetRevision", CLuaInstMisc::getInstance()->GetRevision_old }, + { "checkVersion", CLuaInstMisc::getInstance()->checkVersion_old }, + /* lua_video.cpp Deprecated, for the future separate class for video @@ -658,19 +662,6 @@ int CLuaInstance::deleteSavedScreen(lua_State *L) return 0; } -int CLuaInstance::GetRevision(lua_State *L) -{ - unsigned int rev = 0; - std::string hw = ""; -#if HAVE_COOL_HARDWARE - hw = "Coolstream"; -#endif - rev = cs_get_revision(); - lua_pushinteger(L, rev); - lua_pushstring(L, hw.c_str()); - return 2; -} - int CLuaInstance::PaintBox(lua_State *L) { int count = lua_gettop(L); @@ -808,62 +799,6 @@ int CLuaInstance::DisplayImage(lua_State *L) return 0; } -int CLuaInstance::strFind(lua_State *L) -{ - int numargs = lua_gettop(L); - if (numargs < 3) { - printf("CLuaInstance::%s: not enough arguments (%d, expected 2 (or 3 or 4))\n", __func__, numargs); - lua_pushnil(L); - return 1; - } - const char *s1; - const char *s2; - int pos=0, n=0, ret=0; - s1 = luaL_checkstring(L, 2); - s2 = luaL_checkstring(L, 3); - if (numargs > 3) - pos = luaL_checkint(L, 4); - if (numargs > 4) - n = luaL_checkint(L, 5); - - std::string str(s1); - if (numargs > 4) - ret = str.find(s2, pos, n); - else - ret = str.find(s2, pos); - -// printf("####[%s:%d] str_len: %d, s2: %s, pos: %d, n: %d, ret: %d\n", __func__, __LINE__, str.length(), s2, pos, n, ret); - if (ret == (int)std::string::npos) - lua_pushnil(L); - else - lua_pushinteger(L, ret); - return 1; -} - -int CLuaInstance::strSub(lua_State *L) -{ - int numargs = lua_gettop(L); - if (numargs < 3) { - printf("CLuaInstance::%s: not enough arguments (%d, expected 2 (or 3))\n", __func__, numargs); - lua_pushstring(L, ""); - return 1; - } - const char *s1; - int pos=0, len=std::string::npos; - std::string ret=""; - s1 = luaL_checkstring(L, 2); - pos = luaL_checkint(L, 3); - if (numargs > 3) - len = luaL_checkint(L, 4); - - std::string str(s1); - ret = str.substr(pos, len); - -// printf("####[%s:%d] str_len: %d, pos: %d, len: %d, ret_len: %d\n", __func__, __LINE__, str.length(), pos, len, ret.length()); - lua_pushstring(L, ret.c_str()); - return 1; -} - int CLuaInstance::GetSize(lua_State *L) { LUA_DEBUG("CLuaInstance::%s %d\n", __func__, lua_gettop(L)); @@ -1126,90 +1061,6 @@ int CLuaInstance::GetLanguage(lua_State *L) return 1; } -int CLuaInstance::runScriptExt(lua_State *L) -{ - CLuaData *W = CheckData(L, 1); - if (!W) return 0; - - int numargs = lua_gettop(L); - const char *script = luaL_checkstring(L, 2); - std::vector args; - for (int i = 3; i <= numargs; i++) { - std::string arg = luaL_checkstring(L, i); - if (!arg.empty()) - args.push_back(arg); - } - - CLuaInstance *lua = new CLuaInstance(); - lua->runScript(script, &args); - args.clear(); - delete lua; - return 0; -} - -int CLuaInstance::checkVersion(lua_State *L) -{ - int numargs = lua_gettop(L); - if (numargs < 3) { - printf("CLuaInstance::%s: not enough arguments (%d, expected 2)\n", __func__, numargs); - lua_pushnil(L); - return 1; - } - int major=0, minor=0; - major = luaL_checkint(L, 2); - minor = luaL_checkint(L, 3); - if ((major > LUA_API_VERSION_MAJOR) || ((major == LUA_API_VERSION_MAJOR) && (minor > LUA_API_VERSION_MINOR))) { - char msg[1024]; - snprintf(msg, sizeof(msg)-1, "%s (v%d.%d)\n%s v%d.%d", - g_Locale->getText(LOCALE_LUA_VERSIONSCHECK1), - LUA_API_VERSION_MAJOR, LUA_API_VERSION_MINOR, - g_Locale->getText(LOCALE_LUA_VERSIONSCHECK2), - major, minor); - luaL_error(L, msg); - } - lua_pushinteger(L, 1); /* for backward compatibility */ - return 1; -} - -// -------------------------------------------------------------------------------- - -int CLuaInstance::createChannelIDfromUrl(lua_State *L) -{ - int numargs = lua_gettop(L); - if (numargs < 2) { - printf("CLuaInstance::%s: no arguments\n", __func__); - lua_pushnil(L); - return 1; - } - - const char *url = luaL_checkstring(L, 2); - if (strlen(url) < 1 ) { - lua_pushnil(L); - return 1; - } - - t_channel_id id = CREATE_CHANNEL_ID(0, 0, 0, url); - char id_str[17]; - snprintf(id_str, sizeof(id_str), "%" PRIx64, id); - - lua_pushstring(L, id_str); - return 1; -} - -// -------------------------------------------------------------------------------- - -int CLuaInstance::enableInfoClock(lua_State *L) -{ - bool enable = true; - int numargs = lua_gettop(L); - if (numargs > 1) - enable = _luaL_checkbool(L, 2); - CInfoClock::getInstance()->enableInfoClock(enable); - return 0; -} - -// -------------------------------------------------------------------------------- - int CLuaInstance::getDynFont(lua_State *L) { int numargs = lua_gettop(L); @@ -1260,5 +1111,3 @@ int CLuaInstance::getDynFont(lua_State *L) lua_pushinteger(L, DYNFONT_NO_ERROR); return 2; } - -// -------------------------------------------------------------------------------- diff --git a/src/gui/lua/luainstance.h b/src/gui/lua/luainstance.h index d93111f04..aa71ebd2c 100644 --- a/src/gui/lua/luainstance.h +++ b/src/gui/lua/luainstance.h @@ -62,7 +62,6 @@ private: lua_State* lua; void registerFunctions(); - static int GetRevision(lua_State *L); static int NewWindow(lua_State *L); static int saveScreen(lua_State *L); static int restoreScreen(lua_State *L); @@ -78,18 +77,9 @@ private: static int GCWindow(lua_State *L); static int Blit(lua_State *L); static int GetLanguage(lua_State *L); - static int runScriptExt(lua_State *L); static int GetSize(lua_State *L); static int DisplayImage(lua_State *L); - - static int strFind(lua_State *L); - static int strSub(lua_State *L); - - static int checkVersion(lua_State *L); - static int createChannelIDfromUrl(lua_State *L); - static int enableInfoClock(lua_State *L); static int getDynFont(lua_State *L); - }; #endif /* _LUAINSTANCE_H */