diff options
author | Pavel Labath <pavelo@centrum.sk> | 2011-07-02 22:05:05 (GMT) |
---|---|---|
committer | Pavel Labath <pavelo@centrum.sk> | 2013-02-17 09:36:37 (GMT) |
commit | 9d2f0d82bc305c081b54787fb49a90471a429000 (patch) | |
tree | 8c2f946d86bdb2ba1d437372328e52ff251acb0c | |
parent | 240736c6f9e78e58f90a4291d7fe876a15e460c6 (diff) | |
download | fluxbox_pavel-9d2f0d82bc305c081b54787fb49a90471a429000.zip fluxbox_pavel-9d2f0d82bc305c081b54787fb49a90471a429000.tar.bz2 |
Add a new command which executes lua functions
-rw-r--r-- | nls/fluxbox-nls.hh | 6 | ||||
-rw-r--r-- | src/FbCommands.cc | 68 | ||||
-rw-r--r-- | src/FbCommands.hh | 23 | ||||
-rw-r--r-- | src/MenuCreator.cc | 2 |
4 files changed, 98 insertions, 1 deletions
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 { | |||
143 | MenuReconfigure = 8, | 143 | MenuReconfigure = 8, |
144 | MenuRestart = 9, | 144 | MenuRestart = 9, |
145 | MenuErrorEndEncoding = 10, | 145 | MenuErrorEndEncoding = 10, |
146 | MenuFieldNotString = 11, | ||
147 | MenuMenuNotTable = 12, | ||
148 | |||
146 | 149 | ||
147 | RememberSet = 11, | 150 | RememberSet = 11, |
148 | RememberDecorations = 1, | 151 | RememberDecorations = 1, |
@@ -251,6 +254,9 @@ enum { | |||
251 | LayerNormal = 5, | 254 | LayerNormal = 5, |
252 | LayerTop = 6, | 255 | LayerTop = 6, |
253 | 256 | ||
257 | FbCommandsSet = 21, | ||
258 | FbCommandsCantLoadMenu = 1, | ||
259 | |||
254 | dummy_not_used = 0 // just for the end | 260 | dummy_not_used = 0 // just for the end |
255 | 261 | ||
256 | }; // end enum | 262 | }; // 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 @@ | |||
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" |
@@ -622,4 +623,71 @@ void ClientPatternTestCmd::execute() { | |||
622 | } | 623 | } |
623 | 624 | ||
624 | 625 | ||
626 | REGISTER_UNTRUSTED_COMMAND_WITH_ARGS(lua, LuaCmd, void); | ||
627 | |||
628 | namespace { | ||
629 | const char LuaCmds[] = "FbCommands::LuaCmd"; | ||
630 | } | ||
631 | |||
632 | LuaCmd::LuaCmd(const std::string &chunk) { | ||
633 | lua::state &l = Fluxbox::instance()->lua(); | ||
634 | l.checkstack(1); | ||
635 | l.loadstring(chunk); | ||
636 | init(l); | ||
637 | } | ||
638 | |||
639 | LuaCmd::LuaCmd(lua::state &l) { | ||
640 | lua::stack_sentry s(l, -1); | ||
641 | if(l.isstring(-1)) { | ||
642 | const std::string &str = l.tostring(-1); | ||
643 | l.pop(); | ||
644 | l.loadstring(str); | ||
645 | } | ||
646 | init(l); | ||
647 | } | ||
648 | |||
649 | void LuaCmd::init(lua::state &l) { | ||
650 | lua::stack_sentry s(l, -1); | ||
651 | l.checkstack(2); | ||
652 | |||
653 | l.rawgetfield(lua::REGISTRYINDEX, LuaCmds); { | ||
654 | if(l.isnil(-1)) { | ||
655 | l.pop(); | ||
656 | l.newtable(); | ||
657 | |||
658 | l.pushvalue(-1); | ||
659 | l.rawsetfield(lua::REGISTRYINDEX, LuaCmds); | ||
660 | } | ||
661 | |||
662 | l.pushvalue(-2); | ||
663 | m_ref = l.ref(-2); | ||
664 | } l.pop(); | ||
665 | |||
666 | l.pop(); | ||
667 | } | ||
668 | |||
669 | LuaCmd::~LuaCmd() { | ||
670 | lua::state &l = Fluxbox::instance()->lua(); | ||
671 | l.checkstack(1); | ||
672 | lua::stack_sentry s(l); | ||
673 | |||
674 | l.rawgetfield(lua::REGISTRYINDEX, LuaCmds); { | ||
675 | l.unref(-1, m_ref); | ||
676 | } l.pop(); | ||
677 | } | ||
678 | |||
679 | void LuaCmd::execute() { | ||
680 | lua::state &l = Fluxbox::instance()->lua(); | ||
681 | l.checkstack(1); | ||
682 | lua::stack_sentry s(l); | ||
683 | |||
684 | l.rawgetfield(lua::REGISTRYINDEX, LuaCmds); { | ||
685 | assert(l.istable(-1)); | ||
686 | |||
687 | l.rawgeti(-1, m_ref); { | ||
688 | assert(! l.isnil(-1)); | ||
689 | } l.call(0, 0); | ||
690 | } l.pop(); | ||
691 | } | ||
692 | |||
625 | } // end namespace FbCommands | 693 | } // 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 @@ | |||
32 | #include "ClientPattern.hh" | 32 | #include "ClientPattern.hh" |
33 | #include "FocusableList.hh" | 33 | #include "FocusableList.hh" |
34 | 34 | ||
35 | namespace lua { | ||
36 | class state; | ||
37 | } | ||
38 | |||
35 | namespace FbCommands { | 39 | namespace FbCommands { |
36 | 40 | ||
37 | /// executes a system command | 41 | /// executes a system command |
@@ -218,7 +222,6 @@ private: | |||
218 | Destination m_dest; | 222 | Destination m_dest; |
219 | }; | 223 | }; |
220 | 224 | ||
221 | |||
222 | /// test client pattern | 225 | /// test client pattern |
223 | class ClientPatternTestCmd: public FbTk::Command<void> { | 226 | class ClientPatternTestCmd: public FbTk::Command<void> { |
224 | public: | 227 | public: |
@@ -228,6 +231,24 @@ private: | |||
228 | std::string m_args; | 231 | std::string m_args; |
229 | }; | 232 | }; |
230 | 233 | ||
234 | /// Command that runs a lua function | ||
235 | class LuaCmd: public FbTk::Command<void> { | ||
236 | public: | ||
237 | /// parses the string as a lua chunk | ||
238 | LuaCmd(const std::string &chunk); | ||
239 | |||
240 | /// expect the function to be on the lua stack (as a function or a string) | ||
241 | LuaCmd(lua::state &l); | ||
242 | |||
243 | ~LuaCmd(); | ||
244 | |||
245 | void execute(); | ||
246 | private: | ||
247 | int m_ref; | ||
248 | |||
249 | void init(lua::state &l); | ||
250 | }; | ||
251 | |||
231 | } // end namespace FbCommands | 252 | } // end namespace FbCommands |
232 | 253 | ||
233 | #endif // FBCOMMANDS_HH | 254 | #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, | |||
236 | void | 236 | void |
237 | insertMenuItem(lua::state &l, FbTk::Menu &menu, FbTk::StringConvertor &parent_conv, | 237 | insertMenuItem(lua::state &l, FbTk::Menu &menu, FbTk::StringConvertor &parent_conv, |
238 | FbTk::AutoReloadHelper *reloader) { | 238 | FbTk::AutoReloadHelper *reloader) { |
239 | _FB_USES_NLS; | ||
239 | lua::stack_sentry s(l, -1); | 240 | lua::stack_sentry s(l, -1); |
240 | l.checkstack(1); | 241 | l.checkstack(1); |
241 | 242 | ||
@@ -351,6 +352,7 @@ MenuCreator::createMenu(FbTk::Menu &inject_into, lua::state &l, FbTk::AutoReload | |||
351 | lua::stack_sentry s(l, -1); | 352 | lua::stack_sentry s(l, -1); |
352 | 353 | ||
353 | if(l.type(-1) != lua::TTABLE) { | 354 | if(l.type(-1) != lua::TTABLE) { |
355 | _FB_USES_NLS; | ||
354 | cerr << _FB_CONSOLETEXT(Menu, MenuNotTable, "Warning: Menu is not a lua table", | 356 | cerr << _FB_CONSOLETEXT(Menu, MenuNotTable, "Warning: Menu is not a lua table", |
355 | "Menu is not a lua table") << endl; | 357 | "Menu is not a lua table") << endl; |
356 | return; | 358 | return; |