aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/LResource.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/FbTk/LResource.cc')
-rw-r--r--src/FbTk/LResource.cc163
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
30extern const char LResourceHelper[];
31extern const unsigned int LResourceHelper_size;
32
33namespace FbTk {
34
35namespace {
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
66void 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
85LResourceManager::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
95bool 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
107void 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
124void 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
139Resource_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
151const 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