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.
33 #include "common/util.h"
35 /** Type for Lua function */
38 #define deprecate(L) luaA_warn(L, "%s: This function is deprecated and will be removed.", \
41 #define DO_LUA_NEW(decl, type, prefix, lua_type, type_ref) \
43 luaA_##prefix##_userdata_new(lua_State *L, type *p) \
45 type **pp = lua_newuserdata(L, sizeof(type *)); \
48 return luaA_settype(L, lua_type); \
51 luaA_##prefix##_tostring(lua_State *L) \
53 type **p = luaA_checkudata(L, 1, lua_type); \
54 lua_pushfstring(L, lua_type ": %p", *p); \
58 #define DO_LUA_GC(type, prefix, lua_type, type_unref) \
60 luaA_##prefix##_gc(lua_State *L) \
62 type **p = luaA_checkudata(L, 1, lua_type); \
68 #define DO_LUA_EQ(type, prefix, lua_type) \
70 luaA_##prefix##_eq(lua_State *L) \
72 type **p1 = luaA_checkudata(L, 1, lua_type); \
73 type **p2 = luaA_checkudata(L, 2, lua_type); \
74 lua_pushboolean(L, (*p1 == *p2)); \
78 #define luaA_dostring(L, cmd) \
81 if(luaL_dostring(L, cmd)) \
82 warn("error executing Lua code: %s", \
83 lua_tostring(L, -1)); \
86 #define luaA_checktable(L, n) \
88 if(!lua_istable(L, n)) \
89 luaL_typerror(L, n, "table"); \
92 #define luaA_checkfunction(L, n) \
94 if(!lua_isfunction(L, n)) \
95 luaL_typerror(L, n, "function"); \
98 #define luaA_checkscreen(screen) \
100 if(screen < 0 || screen >= globalconf.nscreen) \
101 luaL_error(L, "invalid screen number: %d", screen + 1); \
104 /** Dump the Lua stack. Useful for debugging.
105 * \param L The Lua VM state.
108 luaA_dumpstack(lua_State
*L
)
110 fprintf(stderr
, "-------- Lua stack dump ---------\n");
111 for(int i
= lua_gettop(L
); i
; i
--)
113 int t
= lua_type(L
, i
);
117 fprintf(stderr
, "%d: string: `%s'\n", i
, lua_tostring(L
, i
));
120 fprintf(stderr
, "%d: bool: %s\n", i
, lua_toboolean(L
, i
) ? "true" : "false");
123 fprintf(stderr
, "%d: number: %g\n", i
, lua_tonumber(L
, i
));
126 fprintf(stderr
, "%d: nil\n", i
);
129 fprintf(stderr
, "%d: %s\t#%d\t%p\n", i
, lua_typename(L
, t
),
130 (int) lua_objlen(L
, i
),
131 lua_topointer(L
, i
));
135 fprintf(stderr
, "------- Lua stack dump end ------\n");
138 /** Check that an object is not a NULL reference.
139 * \param L The Lua state.
140 * \param ud The index of the object in the stack.
141 * \param tname The type name.
142 * \return A pointer to the object.
145 luaA_checkudata(lua_State
*L
, int ud
, const char *tname
)
147 void **p
= luaL_checkudata(L
, ud
, tname
);
150 luaL_error(L
, "invalid object");
154 /** Convert a object to a udata if possible.
155 * \param L The Lua VM state.
156 * \param ud The index.
157 * \param tname The type name.
158 * \return A pointer to the object, NULL otherwise.
161 luaA_toudata(lua_State
*L
, int ud
, const char *tname
)
163 void **p
= lua_touserdata(L
, ud
);
164 if(p
&& *p
) /* value is a userdata? */
165 if(lua_getmetatable(L
, ud
)) /* does it have a metatable? */
167 lua_getfield(L
, LUA_REGISTRYINDEX
, tname
); /* get correct metatable */
168 if(lua_rawequal(L
, -1, -2)) /* does it have the correct mt? */
170 lua_pop(L
, 2); /* remove both metatables */
173 lua_pop(L
, 2); /* remove both metatables */
179 luaA_checkboolean(lua_State
*L
, int n
)
181 if(!lua_isboolean(L
, n
))
182 luaL_typerror(L
, n
, "boolean");
183 return lua_toboolean(L
, n
);
187 luaA_optboolean(lua_State
*L
, int idx
, bool def
)
189 return luaL_opt(L
, luaA_checkboolean
, idx
, def
);
192 static inline lua_Number
193 luaA_getopt_number(lua_State
*L
, int idx
, const char *name
, lua_Number def
)
195 lua_getfield(L
, idx
, name
);
196 return luaL_optnumber(L
, -1, def
);
199 static inline const char *
200 luaA_getopt_lstring(lua_State
*L
, int idx
, const char *name
, const char *def
, size_t *len
)
202 lua_getfield(L
, idx
, name
);
203 return luaL_optlstring(L
, -1, def
, len
);
206 static inline const char *
207 luaA_getopt_string(lua_State
*L
, int idx
, const char *name
, const char *def
)
209 return luaA_getopt_lstring(L
, idx
, name
, def
, NULL
);
213 luaA_getopt_boolean(lua_State
*L
, int idx
, const char *name
, bool def
)
215 lua_getfield(L
, idx
, name
);
216 return luaA_optboolean(L
, -1, def
);
220 luaA_settype(lua_State
*L
, const char *type
)
222 luaL_getmetatable(L
, type
);
223 lua_setmetatable(L
, -2);
227 /** Push a area type to a table on stack.
228 * \param L The Lua VM state.
229 * \param geometry The area geometry to push.
230 * \return The number of elements pushed on stack.
233 luaA_pusharea(lua_State
*L
, area_t geometry
)
236 lua_pushnumber(L
, geometry
.x
);
237 lua_setfield(L
, -2, "x");
238 lua_pushnumber(L
, geometry
.y
);
239 lua_setfield(L
, -2, "y");
240 lua_pushnumber(L
, geometry
.width
);
241 lua_setfield(L
, -2, "width");
242 lua_pushnumber(L
, geometry
.height
);
243 lua_setfield(L
, -2, "height");
248 luaA_usemetatable(lua_State
*L
, int idxobj
, int idxfield
)
250 lua_getmetatable(L
, idxobj
);
251 lua_pushvalue(L
, idxfield
);
253 if(!lua_isnil(L
, -1))
263 /** Register an Lua object.
264 * \param L The Lua stack.
265 * \param idx Index of the object in the stack.
266 * \param fct A luaA_ref address: it will be filled with the luaA_ref
267 * registered. If the adresse point to an already registered object, it will
272 luaA_register(lua_State
*L
, int idx
, luaA_ref
*ref
)
274 lua_pushvalue(L
, idx
);
275 if(*ref
!= LUA_REFNIL
)
276 luaL_unref(L
, LUA_REGISTRYINDEX
, *ref
);
277 *ref
= luaL_ref(L
, LUA_REGISTRYINDEX
);
281 /** Register a function.
282 * \param L The Lua stack.
283 * \param idx Index of the function in the stack.
284 * \param fct A luaA_ref address: it will be filled with the luaA_ref
285 * registered. If the adresse point to an already registered function, it will
287 * \return luaA_register value.
290 luaA_registerfct(lua_State
*L
, int idx
, luaA_ref
*fct
)
292 luaA_checkfunction(L
, idx
);
293 return luaA_register(L
, idx
, fct
);
296 /** Execute an Lua function.
297 * \param L The Lua stack.
298 * \param f The Lua function to execute.
299 * \param nargs The number of arguments for the Lua function.
300 * \param nret The number of returned value from the Lua function.
301 * \return True on no error, false otherwise.
304 luaA_dofunction(lua_State
*L
, luaA_ref f
, int nargs
, int nret
)
308 lua_rawgeti(L
, LUA_REGISTRYINDEX
, f
);
310 lua_insert(L
, - (nargs
+ 1));
311 if(lua_pcall(L
, nargs
, nret
, 0))
313 warn("error running function: %s",
314 lua_tostring(L
, -1));
323 /** Print a warning about some Lua code.
324 * This is less mean than luaL_error() which setjmp via lua_error() and kills
325 * everything. This only warn, it's up to you to then do what's should be done.
326 * \param L The Lua VM state.
327 * \param fmt The warning message.
329 static inline void __attribute__ ((format(printf
, 2, 3)))
330 luaA_warn(lua_State
*L
, const char *fmt
, ...)
334 fprintf(stderr
, "%sW: ", lua_tostring(L
, -1));
337 vfprintf(stderr
, fmt
, ap
);
339 fprintf(stderr
, "\n");
342 int luaA_otable_index(lua_State
*);
344 /** Create a new object table with a metatable.
345 * This is useful to compare table with objects (udata) as keys.
346 * \param L The Lua stack.
347 * \return The number of elements pushed on stack.
350 luaA_otable_new(lua_State
*L
)
354 return luaA_settype(L
, "otable");
357 void luaA_init(void);
358 bool luaA_parserc(const char *, bool);
359 void luaA_cs_init(void);
360 void luaA_cs_cleanup(void);
361 void luaA_on_timer(EV_P_ ev_timer
*, int);
362 int luaA_pushcolor(lua_State
*, const xcolor_t
*);
363 bool luaA_hasitem(lua_State
*, const void *);
364 void luaA_table2wtable(lua_State
*);
365 int luaA_next(lua_State
*, int);
366 bool luaA_isloop(lua_State
*, int);
368 #define hooks_property(c, prop) \
370 luaA_client_userdata_new(globalconf.L, c); \
371 lua_pushliteral(globalconf.L, prop); \
372 luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
376 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80