1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
// LResource: Fluxbox Resource implementation in lua
// Copyright (C) 2010 - 2011 Pavel Labath
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#include <config.h>
#include "LResource.hh"
#include "I18n.hh"
#include "LuaUtil.hh"
#include "Resource.hh"
extern const char LResourceHelper[];
extern const unsigned int LResourceHelper_size;
namespace FbTk {
namespace {
const char make_root[] = "FbTk::make_root";
const char register_resource[] = "FbTk::register_resource";
const char dump_resources[] = "FbTk::dump_resources";
const char resource_metatable[] = "FbTk::resource_metatable";
int readResource(lua::state *l) {
Resource_base *r = *static_cast<Resource_base **>(l->touserdata(-1));
l->pop();
if(r != NULL)
r->pushToLua(*l);
else
l->pushnil();
return 1;
}
int writeResource(lua::state *l) {
Resource_base *r = *static_cast<Resource_base **>(l->touserdata(-2));
l->replace(-2);
if(r != NULL)
r->setFromLua(*l);
else
l->pop();
return 0;
}
void initState(Lua &l) {
l.checkstack(6);
lua::stack_sentry s(l);
l.loadstring(LResourceHelper, LResourceHelper_size);
l.pushfunction(&readResource);
l.pushfunction(&writeResource);
l.newtable(); {
l.newtable(); {
l.pushvalue(-2);
l.setfield(-2, "__metatable");
} l.setfield(lua::REGISTRYINDEX, resource_metatable);
}
l.call(3, 3);
l.setfield(lua::REGISTRYINDEX, dump_resources);
l.setfield(lua::REGISTRYINDEX, register_resource);
l.setfield(lua::REGISTRYINDEX, make_root);
}
Lua::RegisterInitFunction register_init_state(&initState);
} // anonymous namespace
void LResourceManager::convert(ResourceManager &old, const std::string &new_file) {
Lua l;
LResourceManager new_rm(old.root(), l);
for(ResourceList::const_iterator i = old.begin(); i != old.end(); ++i) {
// adding the resource to new_rm will set it to default value
// we save the value to a temp variable so we can restore it later
const std::string &t = (*i)->getString();
new_rm.addResource(**i);
(*i)->setFromString(t.c_str());
}
new_rm.save(new_file.c_str(), NULL);
}
LResourceManager::LResourceManager(const std::string &root, Lua &l)
: ResourceManager_base(root), m_l(&l) {
setLua(l);
}
void LResourceManager::load(const std::string &filename, const std::string &fallback) {
_FB_USES_NLS;
m_l->checkstack(1);
lua::stack_sentry s(*m_l);
m_filename = filename;
try {
m_l->loadfile(filename.c_str());
m_l->call(0, 0);
}
catch(lua::exception &e) {
std::cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFile, "Failed to load database",
"Failed trying to read rc file") << ":" << filename << std::endl;
std::cerr << "Fluxbox: " << e.what() << std::endl;
std::cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFileTrying, "Retrying with",
"Retrying rc file loading with (the following file)")
<< ": " << fallback << std::endl;
try {
m_l->loadfile(fallback.c_str());
m_l->call(0, 0);
}
catch(lua::exception &e) {
std::cerr << _FB_CONSOLETEXT(Fluxbox, CantLoadRCFile, "Failed to load database", "")
<< ": " << fallback << std::endl;
std::cerr << "Fluxbox: " << e.what() << std::endl;
}
}
}
bool LResourceManager::save(const char *filename, const char *) {
m_l->checkstack(3);
lua::stack_sentry s(*m_l);
if(filename == NULL)
filename = m_filename.c_str();
m_l->getfield(lua::REGISTRYINDEX, dump_resources);
m_l->getfield(lua::GLOBALSINDEX, m_root.c_str());
m_l->pushstring(filename);
m_l->call(2, 0);
return true; // FIXME
}
void LResourceManager::addResource(Resource_base &r) {
ResourceManager_base::addResource(r);
try {
doAddResource(r);
}
catch(...) {
ResourceManager_base::removeResource(r);
throw;
}
}
void LResourceManager::doAddResource(Resource_base &r) {
m_l->checkstack(5);
lua::stack_sentry s(*m_l);
m_l->getfield(lua::REGISTRYINDEX, register_resource);
m_l->getfield(lua::GLOBALSINDEX, m_root.c_str());
m_l->pushstring(r.name());
m_l->createuserdata<Resource_base *>(&r); {
m_l->getfield(lua::REGISTRYINDEX, resource_metatable);
m_l->setmetatable(-2);
}
m_l->call(3, 0);
}
void LResourceManager::removeResource(Resource_base &r) {
doRemoveResource(r);
ResourceManager_base::removeResource(r);
}
void LResourceManager::doRemoveResource(Resource_base &r) {
m_l->checkstack(4);
lua::stack_sentry s(*m_l);
m_l->getfield(lua::REGISTRYINDEX, register_resource);
m_l->getfield(lua::GLOBALSINDEX, m_root.c_str());
m_l->pushstring(r.name());
r.pushToLua(*m_l);
m_l->call(3, 1);
*static_cast<Resource_base **>(m_l->touserdata(-1)) = NULL;
m_l->pop();
}
void LResourceManager::setLua(Lua &l) {
l.checkstack(2);
lua::stack_sentry s(l);
for(ResourceList::const_iterator i = begin(); i != end(); ++i)
doRemoveResource(**i);
l.getfield(lua::REGISTRYINDEX, make_root);
l.pushstring(m_root);
l.call(1, 1);
l.readOnlySetField(lua::GLOBALSINDEX, m_root.c_str());
m_l = &l;
for(ResourceList::const_iterator i = begin(); i != end(); ++i)
doAddResource(**i);
}
} // end namespace FbTk
|