aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-07-02 22:05:05 (GMT)
committerPavel Labath <pavelo@centrum.sk>2011-07-02 22:05:05 (GMT)
commit356f64a70206d4fcc828480bdd8ba99bda7f2fcb (patch)
tree45cecd018604ff6ed92110ccccb86e76f5a5446c /src
parent4f1c55ab980262e218dbb5ab73497c61096c868a (diff)
downloadfluxbox_pavel-356f64a70206d4fcc828480bdd8ba99bda7f2fcb.zip
fluxbox_pavel-356f64a70206d4fcc828480bdd8ba99bda7f2fcb.tar.bz2
Add a new command which executes lua functions
Diffstat (limited to 'src')
-rw-r--r--src/FbCommands.cc68
-rw-r--r--src/FbCommands.hh22
2 files changed, 90 insertions, 0 deletions
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 @@
31#include "MenuCreator.hh" 31#include "MenuCreator.hh"
32 32
33#include "FbTk/I18n.hh" 33#include "FbTk/I18n.hh"
34#include "FbTk/Luamm.hh"
34#include "FbTk/Theme.hh" 35#include "FbTk/Theme.hh"
35#include "FbTk/Menu.hh" 36#include "FbTk/Menu.hh"
36#include "FbTk/CommandParser.hh" 37#include "FbTk/CommandParser.hh"
@@ -542,4 +543,71 @@ void DeiconifyCmd::execute() {
542 }; 543 };
543} 544}
544 545
546REGISTER_UNTRUSTED_COMMAND_WITH_ARGS(lua, LuaCmd, void);
547
548namespace {
549 const char LuaCmds[] = "FbCommands::LuaCmd";
550}
551
552LuaCmd::LuaCmd(const std::string &chunk) {
553 lua::state &l = Fluxbox::instance()->lua();
554 l.checkstack(1);
555 l.loadstring(chunk);
556 init(l);
557}
558
559LuaCmd::LuaCmd(lua::state &l) {
560 lua::stack_sentry s(l, -1);
561 if(l.isstring(-1)) {
562 const std::string &str = l.tostring(-1);
563 l.pop();
564 l.loadstring(str);
565 }
566 init(l);
567}
568
569void LuaCmd::init(lua::state &l) {
570 lua::stack_sentry s(l, -1);
571 l.checkstack(2);
572
573 l.rawgetfield(lua::REGISTRYINDEX, LuaCmds); {
574 if(l.isnil(-1)) {
575 l.pop();
576 l.newtable();
577
578 l.pushvalue(-1);
579 l.rawsetfield(lua::REGISTRYINDEX, LuaCmds);
580 }
581
582 l.pushvalue(-2);
583 m_ref = l.ref(-2);
584 } l.pop();
585
586 l.pop();
587}
588
589LuaCmd::~LuaCmd() {
590 lua::state &l = Fluxbox::instance()->lua();
591 l.checkstack(1);
592 lua::stack_sentry s(l);
593
594 l.rawgetfield(lua::REGISTRYINDEX, LuaCmds); {
595 l.unref(-1, m_ref);
596 } l.pop();
597}
598
599void LuaCmd::execute() {
600 lua::state &l = Fluxbox::instance()->lua();
601 l.checkstack(1);
602 lua::stack_sentry s(l);
603
604 l.rawgetfield(lua::REGISTRYINDEX, LuaCmds); {
605 assert(l.istable(-1));
606
607 l.rawgeti(-1, m_ref); {
608 assert(! l.isnil(-1));
609 } l.call(0, 0);
610 } l.pop();
611}
612
545} // end namespace FbCommands 613} // 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 @@
32#include "ClientPattern.hh" 32#include "ClientPattern.hh"
33#include "FocusableList.hh" 33#include "FocusableList.hh"
34 34
35namespace lua {
36class state;
37}
38
35namespace FbCommands { 39namespace FbCommands {
36 40
37/// executes a system command 41/// executes a system command
@@ -218,6 +222,24 @@ private:
218 Destination m_dest; 222 Destination m_dest;
219}; 223};
220 224
225/// Command that runs a lua function
226class LuaCmd: public FbTk::Command<void> {
227public:
228 /// parses the string as a lua chunk
229 LuaCmd(const std::string &chunk);
230
231 /// expect the function to be on the lua stack (as a function or a string)
232 LuaCmd(lua::state &l);
233
234 ~LuaCmd();
235
236 void execute();
237private:
238 int m_ref;
239
240 void init(lua::state &l);
241};
242
221} // end namespace FbCommands 243} // end namespace FbCommands
222 244
223#endif // FBCOMMANDS_HH 245#endif // FBCOMMANDS_HH