diff options
author | Pavel Labath <pavelo@centrum.sk> | 2011-06-05 12:27:13 (GMT) |
---|---|---|
committer | Pavel Labath <pavelo@centrum.sk> | 2011-06-15 23:07:49 (GMT) |
commit | de1e05cec6b26f6a7f398a9ea64f38fa0943f9c0 (patch) | |
tree | 6c5cb813d5cf4bad3c620fb42a2fb4236b9f9ec1 /src/FbTk/LResource.cc | |
parent | f7981d4a85985b4862af0036b7728f8543706374 (diff) | |
download | fluxbox_pavel-de1e05cec6b26f6a7f398a9ea64f38fa0943f9c0.zip fluxbox_pavel-de1e05cec6b26f6a7f398a9ea64f38fa0943f9c0.tar.bz2 |
A rough version of resource implementation in lua
I added a new class, LResourceManager, which should handle loading and saving of resources like
the old ResourceManager, only it does that with the help of lua. I moved the common features of
the two managers (interface + a few functions) to a common base class ResourceManager_base. I
augmented the Resource_base class with two new functions (setFromLua and pushToLua) which are
used by the lua RM instead of getString and setFromString.
Parts of the new RM are written in lua. To avoid loading scripts from a file at runtime I decided
to link compiled lua code straight into the executable. For this purpose I created a small script
which converts a binary file into a declaration of a C array of bytes.
Diffstat (limited to 'src/FbTk/LResource.cc')
-rw-r--r-- | src/FbTk/LResource.cc | 163 |
1 files changed, 163 insertions, 0 deletions
diff --git a/src/FbTk/LResource.cc b/src/FbTk/LResource.cc new file mode 100644 index 0000000..fd8ef2f --- /dev/null +++ b/src/FbTk/LResource.cc | |||
@@ -0,0 +1,163 @@ | |||
1 | // LResource: Fluxbox Resource implementation in lua | ||
2 | // Copyright (C) 2010 - 2011 Pavel Labath | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | |||
23 | #include <config.h> | ||
24 | |||
25 | #include "LResource.hh" | ||
26 | |||
27 | #include "Resource.hh" | ||
28 | #include "Luamm.hh" | ||
29 | |||
30 | extern const char LResourceHelper[]; | ||
31 | extern const unsigned int LResourceHelper_size; | ||
32 | |||
33 | namespace FbTk { | ||
34 | |||
35 | namespace { | ||
36 | const char make_root[] = "FbTk::make_root"; | ||
37 | const char register_resource[] = "FbTk::register_resource"; | ||
38 | const char dump_resources[] = "FbTk::dump_resources"; | ||
39 | const char resource_metatable[] = "FbTk::resource_metatable"; | ||
40 | |||
41 | int readResource(lua::state *l) { | ||
42 | Resource_base *r = *static_cast<Resource_base **>(l->touserdata(-1)); | ||
43 | l->pop(); | ||
44 | |||
45 | if(r != NULL) | ||
46 | r->pushToLua(*l); | ||
47 | else | ||
48 | l->pushnil(); | ||
49 | |||
50 | return 1; | ||
51 | } | ||
52 | |||
53 | int writeResource(lua::state *l) { | ||
54 | Resource_base *r = *static_cast<Resource_base **>(l->touserdata(-2)); | ||
55 | l->replace(-2); | ||
56 | |||
57 | if(r != NULL) | ||
58 | r->setFromLua(*l); | ||
59 | else | ||
60 | l->pop(); | ||
61 | |||
62 | return 0; | ||
63 | } | ||
64 | } | ||
65 | |||
66 | void LResourceManager::initState(lua::state &l) { | ||
67 | l.checkstack(6); | ||
68 | lua::stack_sentry s(l); | ||
69 | |||
70 | l.loadstring(LResourceHelper, LResourceHelper_size); | ||
71 | l.pushfunction(&readResource); | ||
72 | l.pushfunction(&writeResource); | ||
73 | l.newtable(); { | ||
74 | l.newtable(); { | ||
75 | l.pushvalue(-2); | ||
76 | l.setfield(-2, "__metatable"); | ||
77 | } l.setfield(lua::REGISTRYINDEX, resource_metatable); | ||
78 | } | ||
79 | l.call(3, 3); | ||
80 | l.setfield(lua::REGISTRYINDEX, dump_resources); | ||
81 | l.setfield(lua::REGISTRYINDEX, register_resource); | ||
82 | l.setfield(lua::REGISTRYINDEX, make_root); | ||
83 | } | ||
84 | |||
85 | LResourceManager::LResourceManager(lua::state &l, const std::string &root) | ||
86 | : m_l(&l), m_root(root) { | ||
87 | l.checkstack(2); | ||
88 | lua::stack_sentry s(l); | ||
89 | |||
90 | l.getfield(lua::REGISTRYINDEX, make_root); | ||
91 | l.pushstring(root); | ||
92 | l.call(1, 0); | ||
93 | } | ||
94 | |||
95 | bool LResourceManager::save(const char *filename, const char *) { | ||
96 | m_l->checkstack(3); | ||
97 | lua::stack_sentry s(*m_l); | ||
98 | |||
99 | m_l->getfield(lua::REGISTRYINDEX, dump_resources); | ||
100 | m_l->getfield(lua::GLOBALSINDEX, m_root.c_str()); | ||
101 | m_l->pushstring(filename); | ||
102 | m_l->call(2, 0); | ||
103 | |||
104 | return true; // FIXME | ||
105 | } | ||
106 | |||
107 | void LResourceManager::addResource(Resource_base &r) { | ||
108 | m_l->checkstack(5); | ||
109 | lua::stack_sentry s(*m_l); | ||
110 | |||
111 | m_resourcelist.push_back(&r); | ||
112 | m_resourcelist.unique(); | ||
113 | |||
114 | m_l->getfield(lua::REGISTRYINDEX, register_resource); | ||
115 | m_l->getfield(lua::GLOBALSINDEX, m_root.c_str()); | ||
116 | m_l->pushstring(r.name()); | ||
117 | m_l->createuserdata<Resource_base *>(&r); { | ||
118 | m_l->getfield(lua::REGISTRYINDEX, resource_metatable); | ||
119 | m_l->setmetatable(-2); | ||
120 | } | ||
121 | m_l->call(3, 0); | ||
122 | } | ||
123 | |||
124 | void LResourceManager::removeResource(Resource_base &r) { | ||
125 | m_l->checkstack(5); | ||
126 | lua::stack_sentry s(*m_l); | ||
127 | |||
128 | m_l->getfield(lua::REGISTRYINDEX, register_resource); | ||
129 | m_l->getfield(lua::GLOBALSINDEX, m_root.c_str()); | ||
130 | m_l->pushstring(r.name()); | ||
131 | r.pushToLua(*m_l); | ||
132 | m_l->call(3, 1); | ||
133 | *static_cast<Resource_base **>(m_l->touserdata(-1)) = NULL; | ||
134 | m_l->pop(); | ||
135 | |||
136 | m_resourcelist.remove(&r); | ||
137 | } | ||
138 | |||
139 | Resource_base *LResourceManager::findResource(const std::string &resname) { | ||
140 | // find resource name | ||
141 | ResourceList::const_iterator i = m_resourcelist.begin(); | ||
142 | ResourceList::const_iterator i_end = m_resourcelist.end(); | ||
143 | for (; i != i_end; ++i) { | ||
144 | if ((*i)->name() == resname || | ||
145 | (*i)->altName() == resname) | ||
146 | return *i; | ||
147 | } | ||
148 | return 0; | ||
149 | } | ||
150 | |||
151 | const Resource_base *LResourceManager::findResource(const std::string &resname) const { | ||
152 | // find resource name | ||
153 | ResourceList::const_iterator i = m_resourcelist.begin(); | ||
154 | ResourceList::const_iterator i_end = m_resourcelist.end(); | ||
155 | for (; i != i_end; ++i) { | ||
156 | if ((*i)->name() == resname || | ||
157 | (*i)->altName() == resname) | ||
158 | return *i; | ||
159 | } | ||
160 | return 0; | ||
161 | } | ||
162 | |||
163 | } // end namespace FbTk | ||