From e27268fc69b776f533a2082d2ca9c13fe863cd80 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sun, 5 Jun 2011 14:27:13 +0200 Subject: A rough version of resource implementation in lua I added a new class, LResourceManager, which should handle loading and saving of resources like the old ResourceManager, only it does that with the help of lua. I moved the common features of the two managers (interface + a few functions) to a common base class ResourceManager_base. I augmented the Resource_base class with two new functions (setFromLua and pushToLua) which are used by the lua RM instead of getString and setFromString. Parts of the new RM are written in lua. To avoid loading scripts from a file at runtime I decided to link compiled lua code straight into the executable. For this purpose I created a small script which converts a binary file into a declaration of a C array of bytes. --- luatoc.sh | 14 ++++ src/FbTk/LResource.cc | 163 +++++++++++++++++++++++++++++++++++++++++++ src/FbTk/LResource.hh | 59 ++++++++++++++++ src/FbTk/LResourceHelper.lua | 135 +++++++++++++++++++++++++++++++++++ src/FbTk/Makefile.am | 10 ++- src/FbTk/Resource.cc | 33 +++++---- src/FbTk/Resource.hh | 78 ++++++++++++++++----- 7 files changed, 457 insertions(+), 35 deletions(-) create mode 100755 luatoc.sh create mode 100644 src/FbTk/LResource.cc create mode 100644 src/FbTk/LResource.hh create mode 100644 src/FbTk/LResourceHelper.lua diff --git a/luatoc.sh b/luatoc.sh new file mode 100755 index 0000000..cd6a231 --- /dev/null +++ b/luatoc.sh @@ -0,0 +1,14 @@ +if [ $# -ne 3 ]; then + echo "Usage: `basename $0` " + exit 1 +fi + +{ + echo "extern const char $3[] = {" + od --format=x1 --output-duplicates $1 \ + | cut --delimiter=' ' --fields=2- --only-delimited \ + | sed -e 's/^/ /' -e 's/\>/,/g' -e 's/\ $2 diff --git a/src/FbTk/LResource.cc b/src/FbTk/LResource.cc new file mode 100644 index 0000000..fd8ef2f --- /dev/null +++ b/src/FbTk/LResource.cc @@ -0,0 +1,163 @@ +// LResource: Fluxbox Resource implementation in lua +// Copyright (C) 2010 - 2011 Pavel Labath +// +// 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. + + +#include + +#include "LResource.hh" + +#include "Resource.hh" +#include "Luamm.hh" + +extern const char LResourceHelper[]; +extern const unsigned int LResourceHelper_size; + +namespace FbTk { + +namespace { + const char make_root[] = "FbTk::make_root"; + const char register_resource[] = "FbTk::register_resource"; + const char dump_resources[] = "FbTk::dump_resources"; + const char resource_metatable[] = "FbTk::resource_metatable"; + + int readResource(lua::state *l) { + Resource_base *r = *static_cast(l->touserdata(-1)); + l->pop(); + + if(r != NULL) + r->pushToLua(*l); + else + l->pushnil(); + + return 1; + } + + int writeResource(lua::state *l) { + Resource_base *r = *static_cast(l->touserdata(-2)); + l->replace(-2); + + if(r != NULL) + r->setFromLua(*l); + else + l->pop(); + + return 0; + } +} + +void LResourceManager::initState(lua::state &l) { + l.checkstack(6); + lua::stack_sentry s(l); + + l.loadstring(LResourceHelper, LResourceHelper_size); + l.pushfunction(&readResource); + l.pushfunction(&writeResource); + l.newtable(); { + l.newtable(); { + l.pushvalue(-2); + l.setfield(-2, "__metatable"); + } l.setfield(lua::REGISTRYINDEX, resource_metatable); + } + l.call(3, 3); + l.setfield(lua::REGISTRYINDEX, dump_resources); + l.setfield(lua::REGISTRYINDEX, register_resource); + l.setfield(lua::REGISTRYINDEX, make_root); +} + +LResourceManager::LResourceManager(lua::state &l, const std::string &root) + : m_l(&l), m_root(root) { + l.checkstack(2); + lua::stack_sentry s(l); + + l.getfield(lua::REGISTRYINDEX, make_root); + l.pushstring(root); + l.call(1, 0); +} + +bool LResourceManager::save(const char *filename, const char *) { + m_l->checkstack(3); + lua::stack_sentry s(*m_l); + + m_l->getfield(lua::REGISTRYINDEX, dump_resources); + m_l->getfield(lua::GLOBALSINDEX, m_root.c_str()); + m_l->pushstring(filename); + m_l->call(2, 0); + + return true; // FIXME +} + +void LResourceManager::addResource(Resource_base &r) { + m_l->checkstack(5); + lua::stack_sentry s(*m_l); + + m_resourcelist.push_back(&r); + m_resourcelist.unique(); + + m_l->getfield(lua::REGISTRYINDEX, register_resource); + m_l->getfield(lua::GLOBALSINDEX, m_root.c_str()); + m_l->pushstring(r.name()); + m_l->createuserdata(&r); { + m_l->getfield(lua::REGISTRYINDEX, resource_metatable); + m_l->setmetatable(-2); + } + m_l->call(3, 0); +} + +void LResourceManager::removeResource(Resource_base &r) { + m_l->checkstack(5); + lua::stack_sentry s(*m_l); + + m_l->getfield(lua::REGISTRYINDEX, register_resource); + m_l->getfield(lua::GLOBALSINDEX, m_root.c_str()); + m_l->pushstring(r.name()); + r.pushToLua(*m_l); + m_l->call(3, 1); + *static_cast(m_l->touserdata(-1)) = NULL; + m_l->pop(); + + m_resourcelist.remove(&r); +} + +Resource_base *LResourceManager::findResource(const std::string &resname) { + // find resource name + ResourceList::const_iterator i = m_resourcelist.begin(); + ResourceList::const_iterator i_end = m_resourcelist.end(); + for (; i != i_end; ++i) { + if ((*i)->name() == resname || + (*i)->altName() == resname) + return *i; + } + return 0; +} + +const Resource_base *LResourceManager::findResource(const std::string &resname) const { + // find resource name + ResourceList::const_iterator i = m_resourcelist.begin(); + ResourceList::const_iterator i_end = m_resourcelist.end(); + for (; i != i_end; ++i) { + if ((*i)->name() == resname || + (*i)->altName() == resname) + return *i; + } + return 0; +} + +} // end namespace FbTk diff --git a/src/FbTk/LResource.hh b/src/FbTk/LResource.hh new file mode 100644 index 0000000..897575b --- /dev/null +++ b/src/FbTk/LResource.hh @@ -0,0 +1,59 @@ +// LResource: Fluxbox Resource implementation in lua +// Copyright (C) 2011 Pavel Labath +// +// 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. + +#ifndef FBTK_LRESOURCE_HH +#define FBTK_LRESOURCE_HH + +#include +#include +#include +#include + +#include "Resource.hh" + +namespace lua { + class state; +} + +namespace FbTk { + +class LResourceManager: public ResourceManager_base { +public: + static void initState(lua::state &l); + + LResourceManager(lua::state &l, const std::string &root); + virtual bool save(const char *filename, const char *); + virtual void addResource(Resource_base &r); + virtual void removeResource(Resource_base &r); + Resource_base *findResource(const std::string &resname); + const Resource_base *findResource(const std::string &resname) const; + +private: + typedef std::list ResourceList; + + ResourceList m_resourcelist; + lua::state *m_l; + std::string m_root; +}; + +} // end namespace FbTk + +#endif // FBTK_LRESOURCE_HH diff --git a/src/FbTk/LResourceHelper.lua b/src/FbTk/LResourceHelper.lua new file mode 100644 index 0000000..f336e05 --- /dev/null +++ b/src/FbTk/LResourceHelper.lua @@ -0,0 +1,135 @@ +read_resource, write_resource, res_magic = ...; + +local cat_magic = {}; + +local function myerror(table, msg) + error(getmetatable(table)._fullname .. ': ' .. msg); +end; + +local function check_arg(table, key) + if type(key) ~= 'string' then + myerror(table, 'expecting strings as keys.'); + end; + if string.match(key, "^_") then + myerror(table, 'resource names must not begin with "_".'); + end; + local t = getmetatable(table)[key]; + return t, getmetatable(t); +end; + +local new_cat; +local function newindex(table, key, value) + local meta = getmetatable(table); + local t, mt = check_arg(table, key); + + if type(value) == 'table' then + if mt == res_magic then + myerror(table, '"' .. key .. '" is a resource.'); + end; + if mt == nil or mt._magic ~= cat_magic then + t = new_cat(table, key); + end; + for k,v in pairs(value) do + t[k] = v; + end; + else + if mt ~= nil and mt._magic == cat_magic and mt._state == 1 then + myerror(table, '"' .. key .. '" is a category.'); + elseif mt == res_magic then + write_resource(t, value); + else + meta[key] = value; + end; + end; +end; + +local function index(table, key) + local t, mt = check_arg(table, key); + + if mt == res_magic then + return read_resource(t); + end; + + return t; +end; + +new_cat = function(table, key) + local meta = getmetatable(table); + local mt = { + __newindex = newindex, __index = index, + _magic = cat_magic, _fullname = meta._fullname .. '.' .. key, _state = 0 + }; + meta[key] = setmetatable({}, mt); + return meta[key]; +end; + +local function register_resource(root, name, object) + local meta = getmetatable(root); + meta._state = 1; + + local head, tail = string.match(name, '^(%a+)%.?(.*)'); + local t = meta[head]; + if tail == '' then + meta[head] = object; + if getmetatable(object) == res_magic then + write_resource(object, t); + end; + return t; + end; + + if t == nil then + t = new_cat(root, head); + end; + return register_resource(t, tail, object); +end; + +local function dump_value(val) + if type(val) == "string" then + return string.format('%q', val); + elseif type(val) == "number" then + return string.format('%g', val); + else + error('Unsupported value type: ' .. type(val)); + end; +end; + +local function dump_(root, fd) + local meta = getmetatable(root); + if not string.match(meta._fullname, "^[%a.]+$") then + error("Someone has been messing with metatables."); + end; + + for k, v in pairs(meta) do + if type(k) == "string" and string.match(k, "^%a+$") then + local mt = getmetatable(v); + + fd:write(meta._fullname, '.', k, ' = '); + if mt ~= nil and mt._magic == cat_magic then + fd:write('{}\n'); + dump(v); + fd:write('\n'); + else + if mt == res_magic then + v = read_resource(v); + end; + fd:write(dump_value(v), '\n'); + end; + end; + end; +end; + +local function dump(root, file) + local fd = io.open(file, 'w'); + dump_(root, fd); + fd:close(); +end; + +local function make_root(name) + local t = { + __newindex = newindex, __index = index, + _magic = cat_magic, _fullname = name, _state = 0 + }; + getfenv()[name] = setmetatable({}, t); +end; + +return make_root, register_resource, dump; diff --git a/src/FbTk/Makefile.am b/src/FbTk/Makefile.am index e4101e3..fc80dd4 100644 --- a/src/FbTk/Makefile.am +++ b/src/FbTk/Makefile.am @@ -2,6 +2,7 @@ noinst_LIBRARIES = libFbTk.a AM_CPPFLAGS=@CPPFLAGS@ INCLUDES = -I$(top_srcdir)/libs/lua/src +CLEANFILES = LResourceHelper-lua.cc LResourceHelper.luac if XFT xft_SOURCE= XftFontImp.hh XftFontImp.cc @@ -69,8 +70,15 @@ libFbTk_a_SOURCES = App.hh App.cc \ Slot.hh Signal.hh MemFun.hh SelectArg.hh \ Util.hh \ RelCalcHelper.hh RelCalcHelper.cc \ - Luamm.cc Luamm.hh \ + Luamm.cc Luamm.hh LResource.cc LResource.hh LResourceHelper-lua.cc \ ${xpm_SOURCE} \ ${xft_SOURCE} \ ${xmb_SOURCE} \ $(imlib2_SOURCE) + +%.luac: %.lua Makefile + $(top_builddir)/libs/lua/src/luac -o $@ $< + + +%-lua.cc: %.luac $(top_srcdir)/luatoc.sh Makefile + $(top_srcdir)/luatoc.sh $< $@ $(<:.luac=) diff --git a/src/FbTk/Resource.cc b/src/FbTk/Resource.cc index 85e78da..fad4bf3 100644 --- a/src/FbTk/Resource.cc +++ b/src/FbTk/Resource.cc @@ -37,6 +37,24 @@ using std::string; namespace FbTk { +ResourceManager_base::~ResourceManager_base() { +} + +string ResourceManager_base::resourceValue(const string &resname) const { + const Resource_base *res = findResource(resname); + if (res != 0) + return res->getString(); + + return ""; +} + +void ResourceManager_base::setResourceValue(const string &resname, const string &value) { + Resource_base *res = findResource(resname); + if (res != 0) + res->setFromString(value.c_str()); + +} + ResourceManager::ResourceManager(const char *filename, bool lock_db) : m_db_lock(0), m_database(0), @@ -186,21 +204,6 @@ const Resource_base *ResourceManager::findResource(const string &resname) const return 0; } -string ResourceManager::resourceValue(const string &resname) const { - const Resource_base *res = findResource(resname); - if (res != 0) - return res->getString(); - - return ""; -} - -void ResourceManager::setResourceValue(const string &resname, const string &value) { - Resource_base *res = findResource(resname); - if (res != 0) - res->setFromString(value.c_str()); - -} - ResourceManager &ResourceManager::lock() { ++m_db_lock; // if the lock was zero, then load the database diff --git a/src/FbTk/Resource.hh b/src/FbTk/Resource.hh index 55afe60..1c53bff 100644 --- a/src/FbTk/Resource.hh +++ b/src/FbTk/Resource.hh @@ -33,6 +33,10 @@ #include #include "XrmDatabaseHelper.hh" +namespace lua { + class state; +} + namespace FbTk { class ResourceException: public std::exception { @@ -62,6 +66,12 @@ public: /// get name of this resource const std::string& name() const { return m_name; } + // Sets the resource value using the value on top of lua stack. Pops the value. + virtual void setFromLua(lua::state &l) = 0; + + // pushes the value of the resource on the stack + virtual void pushToLua(lua::state &l) const = 0; + protected: Resource_base(const std::string &name, const std::string &altname): m_name(name), m_altname(altname) @@ -75,40 +85,31 @@ private: template class Resource; -class ResourceManager +class ResourceManager_base { public: typedef std::list ResourceList; - // lock specifies if the database should be opened with one level locked - // (useful for constructing inside initial set of constructors) - ResourceManager(const char *filename, bool lock_db); - virtual ~ResourceManager(); - - /// Load all resources registered to this class - /// @return true on success - virtual bool load(const char *filename); + virtual ~ResourceManager_base(); /// Save all resouces registered to this class /// @return true on success - virtual bool save(const char *filename, const char *mergefilename=0); + virtual bool save(const char *filename, const char *mergefilename=0) = 0; /// Add resource to list, only used in Resource - void addResource(Resource_base &r); + virtual void addResource(Resource_base &r) = 0; /// Remove a specific resource, only used in Resource - void removeResource(Resource_base &r) { - m_resourcelist.remove(&r); - } + virtual void removeResource(Resource_base &r) = 0; /// searches for the resource with the resourcename /// @return pointer to resource base on success, else 0. - Resource_base *findResource(const std::string &resourcename); + virtual Resource_base *findResource(const std::string &resourcename) = 0; /// searches for the resource with the resourcename /// @return pointer to resource base on success, else 0. - const Resource_base *findResource(const std::string &resourcename) const; + virtual const Resource_base *findResource(const std::string &resourcename) const = 0; std::string resourceValue(const std::string &resourcename) const; void setResourceValue(const std::string &resourcename, const std::string &value); @@ -120,6 +121,42 @@ public: */ template Resource &getResource(const std::string &resource); +}; + +class ResourceManager: public ResourceManager_base +{ +public: + typedef std::list ResourceList; + + // lock specifies if the database should be opened with one level locked + // (useful for constructing inside initial set of constructors) + ResourceManager(const char *filename, bool lock_db); + virtual ~ResourceManager(); + + /// Load all resources registered to this class + /// @return true on success + virtual bool load(const char *filename); + + /// Save all resouces registered to this class + /// @return true on success + virtual bool save(const char *filename, const char *mergefilename=0); + + + + /// Add resource to list, only used in Resource + virtual void addResource(Resource_base &r); + + /// Remove a specific resource, only used in Resource + virtual void removeResource(Resource_base &r) { + m_resourcelist.remove(&r); + } + + /// searches for the resource with the resourcename + /// @return pointer to resource base on success, else 0. + virtual Resource_base *findResource(const std::string &resourcename); + /// searches for the resource with the resourcename + /// @return pointer to resource base on success, else 0. + virtual const Resource_base *findResource(const std::string &resourcename) const; // this marks the database as "in use" and will avoid reloading // resources unless it is zero. @@ -164,7 +201,7 @@ template class Resource:public Resource_base, public Accessor { public: typedef T Type; - Resource(ResourceManager &rm, T val, const std::string &name, const std::string &altname): + Resource(ResourceManager_base &rm, T val, const std::string &name, const std::string &altname): Resource_base(name, altname), m_value(val), m_defaultval(val), m_rm(rm) { m_rm.addResource(*this); // add this to resource handler } @@ -180,6 +217,9 @@ public: /// @return string value of resource std::string getString() const; + virtual void setFromLua(lua::state &l); + virtual void pushToLua(lua::state &l) const; + operator T() const { return m_value; } T& get() { return m_value; } T& operator*() { return m_value; } @@ -188,13 +228,13 @@ public: const T *operator->() const { return &m_value; } private: T m_value, m_defaultval; - ResourceManager &m_rm; + ResourceManager_base &m_rm; }; template -Resource &ResourceManager::getResource(const std::string &resname) { +Resource &ResourceManager_base::getResource(const std::string &resname) { Resource_base *res = findResource(resname); if (res == 0) { throw ResourceException("Could not find resource \"" + -- cgit v0.11.2