From 356f64a70206d4fcc828480bdd8ba99bda7f2fcb Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Sun, 3 Jul 2011 00:05:05 +0200 Subject: Add a new command which executes lua functions --- src/FbCommands.cc | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/FbCommands.hh | 22 ++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/src/FbCommands.cc b/src/FbCommands.cc index d366164..0d7c1df 100644 --- a/src/FbCommands.cc +++ b/src/FbCommands.cc @@ -31,6 +31,7 @@ #include "MenuCreator.hh" #include "FbTk/I18n.hh" +#include "FbTk/Luamm.hh" #include "FbTk/Theme.hh" #include "FbTk/Menu.hh" #include "FbTk/CommandParser.hh" @@ -542,4 +543,71 @@ void DeiconifyCmd::execute() { }; } +REGISTER_UNTRUSTED_COMMAND_WITH_ARGS(lua, LuaCmd, void); + +namespace { + const char LuaCmds[] = "FbCommands::LuaCmd"; +} + +LuaCmd::LuaCmd(const std::string &chunk) { + lua::state &l = Fluxbox::instance()->lua(); + l.checkstack(1); + l.loadstring(chunk); + init(l); +} + +LuaCmd::LuaCmd(lua::state &l) { + lua::stack_sentry s(l, -1); + if(l.isstring(-1)) { + const std::string &str = l.tostring(-1); + l.pop(); + l.loadstring(str); + } + init(l); +} + +void LuaCmd::init(lua::state &l) { + lua::stack_sentry s(l, -1); + l.checkstack(2); + + l.rawgetfield(lua::REGISTRYINDEX, LuaCmds); { + if(l.isnil(-1)) { + l.pop(); + l.newtable(); + + l.pushvalue(-1); + l.rawsetfield(lua::REGISTRYINDEX, LuaCmds); + } + + l.pushvalue(-2); + m_ref = l.ref(-2); + } l.pop(); + + l.pop(); +} + +LuaCmd::~LuaCmd() { + lua::state &l = Fluxbox::instance()->lua(); + l.checkstack(1); + lua::stack_sentry s(l); + + l.rawgetfield(lua::REGISTRYINDEX, LuaCmds); { + l.unref(-1, m_ref); + } l.pop(); +} + +void LuaCmd::execute() { + lua::state &l = Fluxbox::instance()->lua(); + l.checkstack(1); + lua::stack_sentry s(l); + + l.rawgetfield(lua::REGISTRYINDEX, LuaCmds); { + assert(l.istable(-1)); + + l.rawgeti(-1, m_ref); { + assert(! l.isnil(-1)); + } l.call(0, 0); + } l.pop(); +} + } // end namespace FbCommands diff --git a/src/FbCommands.hh b/src/FbCommands.hh index b6b1f7f..ef9812b 100644 --- a/src/FbCommands.hh +++ b/src/FbCommands.hh @@ -32,6 +32,10 @@ #include "ClientPattern.hh" #include "FocusableList.hh" +namespace lua { +class state; +} + namespace FbCommands { /// executes a system command @@ -218,6 +222,24 @@ private: Destination m_dest; }; +/// Command that runs a lua function +class LuaCmd: public FbTk::Command { +public: + /// parses the string as a lua chunk + LuaCmd(const std::string &chunk); + + /// expect the function to be on the lua stack (as a function or a string) + LuaCmd(lua::state &l); + + ~LuaCmd(); + + void execute(); +private: + int m_ref; + + void init(lua::state &l); +}; + } // end namespace FbCommands #endif // FBCOMMANDS_HH -- cgit v0.11.2