2 * lua.h - Lua configuration management header
4 * Copyright © 2008 Julien Danjou <julien@danjou.info>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
30 #include "common/util.h"
32 /** Type for Lua function */
35 #define deprecate() _warn(__LINE__, \
37 "This function is deprecated and will be removed.")
39 #define DO_LUA_NEW(decl, type, prefix, lua_type, type_ref) \
41 luaA_##prefix##_userdata_new(lua_State *L, type *p) \
43 type **pp = lua_newuserdata(L, sizeof(type *)); \
46 return luaA_settype(L, lua_type); \
49 #define DO_LUA_GC(type, prefix, lua_type, type_unref) \
51 luaA_##prefix##_gc(lua_State *L) \
53 type **p = luaA_checkudata(L, 1, lua_type); \
59 #define DO_LUA_EQ(type, prefix, lua_type) \
61 luaA_##prefix##_eq(lua_State *L) \
63 type **p1 = luaA_checkudata(L, 1, lua_type); \
64 type **p2 = luaA_checkudata(L, 2, lua_type); \
65 lua_pushboolean(L, (*p1 == *p2)); \
69 #define luaA_dostring(L, cmd) \
72 if(luaL_dostring(L, cmd)) \
73 warn("error executing Lua code: %s", \
74 lua_tostring(L, -1)); \
77 #define luaA_checktable(L, n) \
79 if(!lua_istable(L, n)) \
80 luaL_typerror(L, n, "table"); \
83 #define luaA_checkfunction(L, n) \
85 if(!lua_isfunction(L, n)) \
86 luaL_typerror(L, n, "function"); \
89 #define luaA_checkscreen(screen) \
91 if(screen < 0 || screen >= globalconf.nscreen) \
92 luaL_error(L, "invalid screen number: %d", screen + 1); \
95 /** Check that an object is not a NULL reference.
96 * \param L The Lua state.
97 * \param ud The index of the object in the stack.
98 * \param tname The type name.
99 * \return A pointer to the object.
102 luaA_checkudata(lua_State
*L
, int ud
, const char *tname
)
104 void **p
= luaL_checkudata(L
, ud
, tname
);
107 luaL_error(L
, "invalid object");
112 luaA_checkboolean(lua_State
*L
, int n
)
114 if(!lua_isboolean(L
, n
))
115 luaL_typerror(L
, n
, "boolean");
116 return lua_toboolean(L
, n
);
120 luaA_optboolean(lua_State
*L
, int idx
, bool def
)
122 return luaL_opt(L
, luaA_checkboolean
, idx
, def
);
125 static inline lua_Number
126 luaA_getopt_number(lua_State
*L
, int idx
, const char *name
, lua_Number def
)
128 lua_getfield(L
, idx
, name
);
129 return luaL_optnumber(L
, -1, def
);
132 static inline const char *
133 luaA_getopt_lstring(lua_State
*L
, int idx
, const char *name
, const char *def
, size_t *len
)
135 lua_getfield(L
, idx
, name
);
136 return luaL_optlstring(L
, -1, def
, len
);
139 static inline const char *
140 luaA_getopt_string(lua_State
*L
, int idx
, const char *name
, const char *def
)
142 return luaA_getopt_lstring(L
, idx
, name
, def
, NULL
);
146 luaA_getopt_boolean(lua_State
*L
, int idx
, const char *name
, bool def
)
148 lua_getfield(L
, idx
, name
);
149 return luaA_optboolean(L
, -1, def
);
153 luaA_settype(lua_State
*L
, const char *type
)
155 luaL_getmetatable(L
, type
);
156 lua_setmetatable(L
, -2);
160 /** Push a area type to a table on stack.
161 * \param L The Lua VM state.
162 * \param geometry The area geometry to push.
163 * \return The number of elements pushed on stack.
166 luaA_pusharea(lua_State
*L
, area_t geometry
)
169 lua_pushnumber(L
, geometry
.x
);
170 lua_setfield(L
, -2, "x");
171 lua_pushnumber(L
, geometry
.y
);
172 lua_setfield(L
, -2, "y");
173 lua_pushnumber(L
, geometry
.width
);
174 lua_setfield(L
, -2, "width");
175 lua_pushnumber(L
, geometry
.height
);
176 lua_setfield(L
, -2, "height");
181 luaA_usemetatable(lua_State
*L
, int idxobj
, int idxfield
)
183 lua_getmetatable(L
, idxobj
);
184 lua_pushvalue(L
, idxfield
);
186 if(!lua_isnil(L
, -1))
196 /** Register a function.
197 * \param L The Lua stack.
198 * \param idx Index of the function in the stack.
199 * \param fct A luaA_ref address: it will be filled with the luaA_ref
200 * registered. If the adresse point to an already registered function, it will
205 luaA_registerfct(lua_State
*L
, int idx
, luaA_ref
*fct
)
207 luaA_checkfunction(L
, idx
);
208 lua_pushvalue(L
, idx
);
209 if(*fct
!= LUA_REFNIL
)
210 luaL_unref(L
, LUA_REGISTRYINDEX
, *fct
);
211 *fct
= luaL_ref(L
, LUA_REGISTRYINDEX
);
215 /** Execute an Lua function.
216 * \param L The Lua stack.
217 * \param f The Lua function to execute.
218 * \param nargs The number of arguments for the Lua function.
219 * \param nret The number of returned value from the Lua function.
220 * \return True on no error, false otherwise.
223 luaA_dofunction(lua_State
*L
, luaA_ref f
, int nargs
, int nret
)
227 lua_rawgeti(L
, LUA_REGISTRYINDEX
, f
);
229 lua_insert(L
, - (nargs
+ 1));
230 if(lua_pcall(L
, nargs
, nret
, 0))
232 warn("error running function: %s",
233 lua_tostring(L
, -1));
242 int luaA_otable_index(lua_State
*);
244 /** Create a new object table with a metatable.
245 * This is useful to compare table with objects (udata) as keys.
246 * \param L The Lua stack.
247 * \return The number of elements pushed on stack.
250 luaA_otable_new(lua_State
*L
)
254 return luaA_settype(L
, "otable");
257 void luaA_init(void);
258 bool luaA_parserc(const char *, bool);
259 void luaA_cs_init(void);
260 void luaA_cs_cleanup(void);
261 void luaA_on_timer(EV_P_ ev_timer
*w
, int revents
);
262 void luaA_pushcolor(lua_State
*, const xcolor_t
*);
265 luaA_generic_pairs(lua_State
*L
)
267 lua_pushvalue(L
, lua_upvalueindex(1)); /* return generator, */
268 lua_pushvalue(L
, 1); /* state, */
269 lua_pushnil(L
); /* and initial value */
273 #define hooks_property(c, prop) \
275 luaA_client_userdata_new(globalconf.L, c); \
276 lua_pushliteral(globalconf.L, prop); \
277 luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
281 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80