From 9d2f0d82bc305c081b54787fb49a90471a429000 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 --- nls/fluxbox-nls.hh | 6 +++++ src/FbCommands.cc | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/FbCommands.hh | 23 +++++++++++++++++- src/MenuCreator.cc | 2 ++ 4 files changed, 98 insertions(+), 1 deletion(-) diff --git a/nls/fluxbox-nls.hh b/nls/fluxbox-nls.hh index 13db6b6..6c3d5f0 100644 --- a/nls/fluxbox-nls.hh +++ b/nls/fluxbox-nls.hh @@ -143,6 +143,9 @@ enum { MenuReconfigure = 8, MenuRestart = 9, MenuErrorEndEncoding = 10, + MenuFieldNotString = 11, + MenuMenuNotTable = 12, + RememberSet = 11, RememberDecorations = 1, @@ -251,6 +254,9 @@ enum { LayerNormal = 5, LayerTop = 6, + FbCommandsSet = 21, + FbCommandsCantLoadMenu = 1, + dummy_not_used = 0 // just for the end }; // end enum diff --git a/src/FbCommands.cc b/src/FbCommands.cc index a3a0c27..9f3acdc 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" @@ -622,4 +623,71 @@ void ClientPatternTestCmd::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 d9639ad..bd377f5 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,7 +222,6 @@ private: Destination m_dest; }; - /// test client pattern class ClientPatternTestCmd: public FbTk::Command { public: @@ -228,6 +231,24 @@ private: std::string m_args; }; +/// 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 diff --git a/src/MenuCreator.cc b/src/MenuCreator.cc index 46f233d..3b7667a 100644 --- a/src/MenuCreator.cc +++ b/src/MenuCreator.cc @@ -236,6 +236,7 @@ createMenu_(FbTk::Menu &inject_into, lua::state &l, FbTk::StringConvertor &conv, void insertMenuItem(lua::state &l, FbTk::Menu &menu, FbTk::StringConvertor &parent_conv, FbTk::AutoReloadHelper *reloader) { + _FB_USES_NLS; lua::stack_sentry s(l, -1); l.checkstack(1); @@ -351,6 +352,7 @@ MenuCreator::createMenu(FbTk::Menu &inject_into, lua::state &l, FbTk::AutoReload lua::stack_sentry s(l, -1); if(l.type(-1) != lua::TTABLE) { + _FB_USES_NLS; cerr << _FB_CONSOLETEXT(Menu, MenuNotTable, "Warning: Menu is not a lua table", "Menu is not a lua table") << endl; return; -- cgit v0.11.2