aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-07-08 13:35:42 (GMT)
committerPavel Labath <pavelo@centrum.sk>2013-02-17 09:36:38 (GMT)
commit084d43e7901efc121f48cb8bd838579ca916bef0 (patch)
tree4832f88b0eb951b9efbb785b851597cfd0368a7d
parent7ea91f6e9974e210a2002de339074bb9e5ac3a48 (diff)
downloadfluxbox_pavel-084d43e7901efc121f48cb8bd838579ca916bef0.zip
fluxbox_pavel-084d43e7901efc121f48cb8bd838579ca916bef0.tar.bz2
use Lua::registerInitFunction to simplify some parts of code
-rw-r--r--src/FbCommands.cc19
-rw-r--r--src/FbTk/LResource.cc47
-rw-r--r--src/FbTk/LResource.hh11
-rw-r--r--src/MenuCreator.cc1
-rw-r--r--src/Screen.cc2
-rw-r--r--src/fluxbox.cc3
-rw-r--r--src/fluxbox.hh4
7 files changed, 42 insertions, 45 deletions
diff --git a/src/FbCommands.cc b/src/FbCommands.cc
index 9f3acdc..c789b6b 100644
--- a/src/FbCommands.cc
+++ b/src/FbCommands.cc
@@ -31,7 +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/LuaUtil.hh"
35#include "FbTk/Theme.hh" 35#include "FbTk/Theme.hh"
36#include "FbTk/Menu.hh" 36#include "FbTk/Menu.hh"
37#include "FbTk/CommandParser.hh" 37#include "FbTk/CommandParser.hh"
@@ -627,6 +627,15 @@ REGISTER_UNTRUSTED_COMMAND_WITH_ARGS(lua, LuaCmd, void);
627 627
628namespace { 628namespace {
629 const char LuaCmds[] = "FbCommands::LuaCmd"; 629 const char LuaCmds[] = "FbCommands::LuaCmd";
630
631 void initLuaCmds(FbTk::Lua &l) {
632 l.checkstack(1);
633
634 l.newtable();
635 l.rawsetfield(lua::REGISTRYINDEX, LuaCmds);
636 }
637
638 FbTk::Lua::RegisterInitFunction register_init_lua_cmds(&initLuaCmds);
630} 639}
631 640
632LuaCmd::LuaCmd(const std::string &chunk) { 641LuaCmd::LuaCmd(const std::string &chunk) {
@@ -651,14 +660,6 @@ void LuaCmd::init(lua::state &l) {
651 l.checkstack(2); 660 l.checkstack(2);
652 661
653 l.rawgetfield(lua::REGISTRYINDEX, LuaCmds); { 662 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 l.pushvalue(-2);
663 m_ref = l.ref(-2); 664 m_ref = l.ref(-2);
664 } l.pop(); 665 } l.pop();
diff --git a/src/FbTk/LResource.cc b/src/FbTk/LResource.cc
index b91f0d8..6671b89 100644
--- a/src/FbTk/LResource.cc
+++ b/src/FbTk/LResource.cc
@@ -24,8 +24,8 @@
24 24
25#include "LResource.hh" 25#include "LResource.hh"
26 26
27#include "LuaUtil.hh"
27#include "Resource.hh" 28#include "Resource.hh"
28#include "Luamm.hh"
29 29
30extern const char LResourceHelper[]; 30extern const char LResourceHelper[];
31extern const unsigned int LResourceHelper_size; 31extern const unsigned int LResourceHelper_size;
@@ -61,30 +61,32 @@ namespace {
61 61
62 return 0; 62 return 0;
63 } 63 }
64}
65 64
66void LResourceManager::initState(lua::state &l) { 65 void initState(Lua &l) {
67 l.checkstack(6); 66 l.checkstack(6);
68 lua::stack_sentry s(l); 67 lua::stack_sentry s(l);
69 68
70 l.loadstring(LResourceHelper, LResourceHelper_size); 69 l.loadstring(LResourceHelper, LResourceHelper_size);
71 l.pushfunction(&readResource); 70 l.pushfunction(&readResource);
72 l.pushfunction(&writeResource); 71 l.pushfunction(&writeResource);
73 l.newtable(); {
74 l.newtable(); { 72 l.newtable(); {
75 l.pushvalue(-2); 73 l.newtable(); {
76 l.setfield(-2, "__metatable"); 74 l.pushvalue(-2);
77 } l.setfield(lua::REGISTRYINDEX, resource_metatable); 75 l.setfield(-2, "__metatable");
76 } l.setfield(lua::REGISTRYINDEX, resource_metatable);
77 }
78 l.call(3, 3);
79 l.setfield(lua::REGISTRYINDEX, dump_resources);
80 l.setfield(lua::REGISTRYINDEX, register_resource);
81 l.setfield(lua::REGISTRYINDEX, make_root);
78 } 82 }
79 l.call(3, 3); 83
80 l.setfield(lua::REGISTRYINDEX, dump_resources); 84 Lua::RegisterInitFunction register_init_state(&initState);
81 l.setfield(lua::REGISTRYINDEX, register_resource); 85
82 l.setfield(lua::REGISTRYINDEX, make_root); 86} // anonymous namespace
83}
84 87
85void LResourceManager::convert(ResourceManager &old, const std::string &new_file) { 88void LResourceManager::convert(ResourceManager &old, const std::string &new_file) {
86 lua::state l; 89 Lua l;
87 initState(l);
88 90
89 LResourceManager new_rm(old.root(), l); 91 LResourceManager new_rm(old.root(), l);
90 for(ResourceList::const_iterator i = old.begin(); i != old.end(); ++i) { 92 for(ResourceList::const_iterator i = old.begin(); i != old.end(); ++i) {
@@ -98,17 +100,12 @@ void LResourceManager::convert(ResourceManager &old, const std::string &new_file
98 new_rm.save(new_file.c_str(), NULL); 100 new_rm.save(new_file.c_str(), NULL);
99} 101}
100 102
101LResourceManager::LResourceManager(const std::string &root, lua::state &l) 103LResourceManager::LResourceManager(const std::string &root, Lua &l)
102 : ResourceManager_base(root), m_l(&l) { 104 : ResourceManager_base(root), m_l(&l) {
103 l.checkstack(2); 105 l.checkstack(2);
104 lua::stack_sentry s(l); 106 lua::stack_sentry s(l);
105 107
106 l.getfield(lua::REGISTRYINDEX, make_root); 108 l.getfield(lua::REGISTRYINDEX, make_root);
107 if(l.isnil(-1)) {
108 l.pop();
109 initState(l);
110 l.getfield(lua::REGISTRYINDEX, make_root);
111 }
112 l.pushstring(root); 109 l.pushstring(root);
113 l.call(1, 0); 110 l.call(1, 0);
114} 111}
diff --git a/src/FbTk/LResource.hh b/src/FbTk/LResource.hh
index 1125707..1567269 100644
--- a/src/FbTk/LResource.hh
+++ b/src/FbTk/LResource.hh
@@ -29,25 +29,22 @@
29 29
30#include "Resource.hh" 30#include "Resource.hh"
31 31
32namespace lua {
33 class state;
34}
35
36namespace FbTk { 32namespace FbTk {
37 33
34class Lua;
35
38class LResourceManager: public ResourceManager_base { 36class LResourceManager: public ResourceManager_base {
39public: 37public:
40 static void initState(lua::state &l);
41 static void convert(ResourceManager &old, const std::string &new_file); 38 static void convert(ResourceManager &old, const std::string &new_file);
42 39
43 LResourceManager(const std::string &root, lua::state &l); 40 LResourceManager(const std::string &root, Lua &l);
44 virtual bool save(const char *filename, const char *); 41 virtual bool save(const char *filename, const char *);
45 virtual void addResource(Resource_base &r); 42 virtual void addResource(Resource_base &r);
46 virtual void removeResource(Resource_base &r); 43 virtual void removeResource(Resource_base &r);
47 44
48private: 45private:
49 46
50 lua::state *m_l; 47 Lua *m_l;
51}; 48};
52 49
53} // end namespace FbTk 50} // end namespace FbTk
diff --git a/src/MenuCreator.cc b/src/MenuCreator.cc
index 8619b00..a120644 100644
--- a/src/MenuCreator.cc
+++ b/src/MenuCreator.cc
@@ -43,6 +43,7 @@
43#include "RootCmdMenuItem.hh" 43#include "RootCmdMenuItem.hh"
44 44
45#include "FbTk/I18n.hh" 45#include "FbTk/I18n.hh"
46#include "FbTk/LuaUtil.hh"
46#include "FbTk/MultiButtonMenuItem.hh" 47#include "FbTk/MultiButtonMenuItem.hh"
47#include "FbTk/BoolMenuItem.hh" 48#include "FbTk/BoolMenuItem.hh"
48#include "FbTk/RefCount.hh" 49#include "FbTk/RefCount.hh"
diff --git a/src/Screen.cc b/src/Screen.cc
index 8e3091d..93de6ca 100644
--- a/src/Screen.cc
+++ b/src/Screen.cc
@@ -75,7 +75,7 @@
75#include "FbTk/STLUtil.hh" 75#include "FbTk/STLUtil.hh"
76#include "FbTk/KeyUtil.hh" 76#include "FbTk/KeyUtil.hh"
77#include "FbTk/Util.hh" 77#include "FbTk/Util.hh"
78#include "FbTk/Luamm.hh" 78#include "FbTk/LuaUtil.hh"
79 79
80//use GNU extensions 80//use GNU extensions
81#ifndef _GNU_SOURCE 81#ifndef _GNU_SOURCE
diff --git a/src/fluxbox.cc b/src/fluxbox.cc
index 86171ad..7bec04b 100644
--- a/src/fluxbox.cc
+++ b/src/fluxbox.cc
@@ -43,6 +43,7 @@
43#include "FbTk/FileUtil.hh" 43#include "FbTk/FileUtil.hh"
44#include "FbTk/ImageControl.hh" 44#include "FbTk/ImageControl.hh"
45#include "FbTk/EventManager.hh" 45#include "FbTk/EventManager.hh"
46#include "FbTk/LuaUtil.hh"
46#include "FbTk/StringUtil.hh" 47#include "FbTk/StringUtil.hh"
47#include "FbTk/Util.hh" 48#include "FbTk/Util.hh"
48#include "FbTk/LResource.hh" 49#include "FbTk/LResource.hh"
@@ -236,7 +237,7 @@ Fluxbox::Fluxbox(int argc, char **argv,
236 const std::string& dpy_name, 237 const std::string& dpy_name,
237 const std::string& rc_path, const std::string& rc_filename, bool xsync) 238 const std::string& rc_path, const std::string& rc_filename, bool xsync)
238 : FbTk::App(dpy_name.c_str()), 239 : FbTk::App(dpy_name.c_str()),
239 m_l(new lua::state()), 240 m_l(new Lua),
240 m_fbatoms(FbAtoms::instance()), 241 m_fbatoms(FbAtoms::instance()),
241 m_resourcemanager("session", *m_l), 242 m_resourcemanager("session", *m_l),
242 243
diff --git a/src/fluxbox.hh b/src/fluxbox.hh
index e744260..e433c46 100644
--- a/src/fluxbox.hh
+++ b/src/fluxbox.hh
@@ -173,7 +173,7 @@ public:
173 /// get screen from number 173 /// get screen from number
174 BScreen *findScreen(int num); 174 BScreen *findScreen(int num);
175 175
176 lua::state &lua() { return *m_l; } 176 FbTk::Lua& lua() { return *m_l; }
177 177
178 typedef std::list<BScreen *> ScreenList; 178 typedef std::list<BScreen *> ScreenList;
179 const ScreenList screenList() const { return m_screen_list; } 179 const ScreenList screenList() const { return m_screen_list; }
@@ -226,7 +226,7 @@ private:
226 /// Called when a window layer changes 226 /// Called when a window layer changes
227 void windowLayerChanged(FluxboxWindow &win); 227 void windowLayerChanged(FluxboxWindow &win);
228 228
229 std::auto_ptr<lua::state> m_l; 229 std::auto_ptr<FbTk::Lua> m_l;
230 std::auto_ptr<FbAtoms> m_fbatoms; 230 std::auto_ptr<FbAtoms> m_fbatoms;
231 231
232 FbTk::LResourceManager m_resourcemanager; 232 FbTk::LResourceManager m_resourcemanager;