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) \
80 if(luaL_dostring(L, cmd)) \
81 warn("error executing Lua code: %s", \
82 lua_tostring(L, -1)); \
85 #define luaA_checktable(L, n) \
87 if(!lua_istable(L, n)) \
88 luaL_typerror(L, n, "table"); \
91 #define luaA_checkfunction(L, n) \
93 if(!lua_isfunction(L, n)) \
94 luaL_typerror(L, n, "function"); \
97 #define luaA_checkscreen(screen) \
99 if(screen < 0 || screen >= globalconf.nscreen) \
100 luaL_error(L, "invalid screen number: %d", screen + 1); \
103 /** Dump the Lua stack. Useful for debugging.
104 * \param L The Lua VM state.
107 luaA_dumpstack(lua_State
*L
)
109 fprintf(stderr
, "-------- Lua stack dump ---------\n");
110 for(int i
= lua_gettop(L
); i
; i
--)
112 int t
= lua_type(L
, i
);
116 fprintf(stderr
, "%d: string: `%s'\n", i
, lua_tostring(L
, i
));
119 fprintf(stderr
, "%d: bool: %s\n", i
, lua_toboolean(L
, i
) ? "true" : "false");
122 fprintf(stderr
, "%d: number: %g\n", i
, lua_tonumber(L
, i
));
125 fprintf(stderr
, "%d: nil\n", i
);
128 fprintf(stderr
, "%d: %s\t#%d\t%p\n", i
, lua_typename(L
, t
),
129 (int) lua_objlen(L
, i
),
130 lua_topointer(L
, i
));
134 fprintf(stderr
, "------- Lua stack dump end ------\n");
137 /** Check that an object is not a NULL reference.
138 * \param L The Lua state.
139 * \param ud The index of the object in the stack.
140 * \param tname The type name.
141 * \return A pointer to the object.
144 luaA_checkudata(lua_State
*L
, int ud
, const char *tname
)
146 void **p
= luaL_checkudata(L
, ud
, tname
);
149 luaL_error(L
, "invalid object");
153 /** Convert a object to a udata if possible.
154 * \param L The Lua VM state.
155 * \param ud The index.
156 * \param tname The type name.
157 * \return A pointer to the object, NULL otherwise.
160 luaA_toudata(lua_State
*L
, int ud
, const char *tname
)
162 void **p
= lua_touserdata(L
, ud
);
163 if(p
&& *p
) /* value is a userdata? */
164 if(lua_getmetatable(L
, ud
)) /* does it have a metatable? */
166 lua_getfield(L
, LUA_REGISTRYINDEX
, tname
); /* get correct metatable */
167 if(lua_rawequal(L
, -1, -2)) /* does it have the correct mt? */
169 lua_pop(L
, 2); /* remove both metatables */
172 lua_pop(L
, 2); /* remove both metatables */
178 luaA_checkboolean(lua_State
*L
, int n
)
180 if(!lua_isboolean(L
, n
))
181 luaL_typerror(L
, n
, "boolean");
182 return lua_toboolean(L
, n
);
186 luaA_optboolean(lua_State
*L
, int idx
, bool def
)
188 return luaL_opt(L
, luaA_checkboolean
, idx
, def
);
191 static inline lua_Number
192 luaA_getopt_number(lua_State
*L
, int idx
, const char *name
, lua_Number def
)
194 lua_getfield(L
, idx
, name
);
195 return luaL_optnumber(L
, -1, def
);
198 static inline const char *
199 luaA_getopt_lstring(lua_State
*L
, int idx
, const char *name
, const char *def
, size_t *len
)
201 lua_getfield(L
, idx
, name
);
202 return luaL_optlstring(L
, -1, def
, len
);
205 static inline const char *
206 luaA_getopt_string(lua_State
*L
, int idx
, const char *name
, const char *def
)
208 return luaA_getopt_lstring(L
, idx
, name
, def
, NULL
);
212 luaA_getopt_boolean(lua_State
*L
, int idx
, const char *name
, bool def
)
214 lua_getfield(L
, idx
, name
);
215 return luaA_optboolean(L
, -1, def
);
219 luaA_settype(lua_State
*L
, const char *type
)
221 luaL_getmetatable(L
, type
);
222 lua_setmetatable(L
, -2);
226 /** Push a area type to a table on stack.
227 * \param L The Lua VM state.
228 * \param geometry The area geometry to push.
229 * \return The number of elements pushed on stack.
232 luaA_pusharea(lua_State
*L
, area_t geometry
)
235 lua_pushnumber(L
, geometry
.x
);
236 lua_setfield(L
, -2, "x");
237 lua_pushnumber(L
, geometry
.y
);
238 lua_setfield(L
, -2, "y");
239 lua_pushnumber(L
, geometry
.width
);
240 lua_setfield(L
, -2, "width");
241 lua_pushnumber(L
, geometry
.height
);
242 lua_setfield(L
, -2, "height");
247 luaA_usemetatable(lua_State
*L
, int idxobj
, int idxfield
)
249 lua_getmetatable(L
, idxobj
);
250 lua_pushvalue(L
, idxfield
);
252 if(!lua_isnil(L
, -1))
262 /** Register an Lua object.
263 * \param L The Lua stack.
264 * \param idx Index of the object in the stack.
265 * \param ref A luaA_ref address: it will be filled with the luaA_ref
266 * registered. If the adresse point to an already registered object, it will
271 luaA_register(lua_State
*L
, int idx
, luaA_ref
*ref
)
273 lua_pushvalue(L
, idx
);
274 if(*ref
!= LUA_REFNIL
)
275 luaL_unref(L
, LUA_REGISTRYINDEX
, *ref
);
276 *ref
= luaL_ref(L
, LUA_REGISTRYINDEX
);
280 /** Unregister a Lua object.
281 * \param L The Lua stack.
282 * \param ref A reference to an Lua object.
285 luaA_unregister(lua_State
*L
, luaA_ref
*ref
)
287 luaL_unref(L
, LUA_REGISTRYINDEX
, *ref
);
291 /** Register a function.
292 * \param L The Lua stack.
293 * \param idx Index of the function in the stack.
294 * \param fct A luaA_ref address: it will be filled with the luaA_ref
295 * registered. If the adresse point to an already registered function, it will
297 * \return luaA_register value.
300 luaA_registerfct(lua_State
*L
, int idx
, luaA_ref
*fct
)
302 luaA_checkfunction(L
, idx
);
303 return luaA_register(L
, idx
, fct
);
306 /** Execute an Lua function.
307 * \param L The Lua stack.
308 * \param f The Lua function to execute.
309 * \param nargs The number of arguments for the Lua function.
310 * \param nret The number of returned value from the Lua function.
311 * \return True on no error, false otherwise.
314 luaA_dofunction(lua_State
*L
, luaA_ref f
, int nargs
, int nret
)
318 lua_rawgeti(L
, LUA_REGISTRYINDEX
, f
);
320 lua_insert(L
, - (nargs
+ 1));
321 if(lua_pcall(L
, nargs
, nret
, 0))
323 warn("error running function: %s",
324 lua_tostring(L
, -1));
333 /** Print a warning about some Lua code.
334 * This is less mean than luaL_error() which setjmp via lua_error() and kills
335 * everything. This only warn, it's up to you to then do what's should be done.
336 * \param L The Lua VM state.
337 * \param fmt The warning message.
339 static inline void __attribute__ ((format(printf
, 2, 3)))
340 luaA_warn(lua_State
*L
, const char *fmt
, ...)
344 fprintf(stderr
, "%sW: ", lua_tostring(L
, -1));
347 vfprintf(stderr
, fmt
, ap
);
349 fprintf(stderr
, "\n");
352 int luaA_otable_index(lua_State
*);
354 /** Create a new object table with a metatable.
355 * This is useful to compare table with objects (udata) as keys.
356 * \param L The Lua stack.
357 * \return The number of elements pushed on stack.
360 luaA_otable_new(lua_State
*L
)
364 return luaA_settype(L
, "otable");
367 void luaA_init(void);
368 bool luaA_parserc(const char *, bool);
369 void luaA_cs_init(void);
370 void luaA_cs_cleanup(void);
371 void luaA_on_timer(EV_P_ ev_timer
*, int);
372 int luaA_pushcolor(lua_State
*, const xcolor_t
*);
373 bool luaA_hasitem(lua_State
*, const void *);
374 void luaA_table2wtable(lua_State
*);
375 int luaA_next(lua_State
*, int);
376 bool luaA_isloop(lua_State
*, int);
378 #define hooks_property(c, prop) \
380 luaA_client_userdata_new(globalconf.L, c); \
381 lua_pushliteral(globalconf.L, prop); \
382 luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
386 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80