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 fct 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 /** Register a function.
281 * \param L The Lua stack.
282 * \param idx Index of the function in the stack.
283 * \param fct A luaA_ref address: it will be filled with the luaA_ref
284 * registered. If the adresse point to an already registered function, it will
286 * \return luaA_register value.
289 luaA_registerfct(lua_State
*L
, int idx
, luaA_ref
*fct
)
291 luaA_checkfunction(L
, idx
);
292 return luaA_register(L
, idx
, fct
);
295 /** Execute an Lua function.
296 * \param L The Lua stack.
297 * \param f The Lua function to execute.
298 * \param nargs The number of arguments for the Lua function.
299 * \param nret The number of returned value from the Lua function.
300 * \return True on no error, false otherwise.
303 luaA_dofunction(lua_State
*L
, luaA_ref f
, int nargs
, int nret
)
307 lua_rawgeti(L
, LUA_REGISTRYINDEX
, f
);
309 lua_insert(L
, - (nargs
+ 1));
310 if(lua_pcall(L
, nargs
, nret
, 0))
312 warn("error running function: %s",
313 lua_tostring(L
, -1));
322 /** Print a warning about some Lua code.
323 * This is less mean than luaL_error() which setjmp via lua_error() and kills
324 * everything. This only warn, it's up to you to then do what's should be done.
325 * \param L The Lua VM state.
326 * \param fmt The warning message.
328 static inline void __attribute__ ((format(printf
, 2, 3)))
329 luaA_warn(lua_State
*L
, const char *fmt
, ...)
333 fprintf(stderr
, "%sW: ", lua_tostring(L
, -1));
336 vfprintf(stderr
, fmt
, ap
);
338 fprintf(stderr
, "\n");
341 int luaA_otable_index(lua_State
*);
343 /** Create a new object table with a metatable.
344 * This is useful to compare table with objects (udata) as keys.
345 * \param L The Lua stack.
346 * \return The number of elements pushed on stack.
349 luaA_otable_new(lua_State
*L
)
353 return luaA_settype(L
, "otable");
356 void luaA_init(void);
357 bool luaA_parserc(const char *, bool);
358 void luaA_cs_init(void);
359 void luaA_cs_cleanup(void);
360 void luaA_on_timer(EV_P_ ev_timer
*, int);
361 int luaA_pushcolor(lua_State
*, const xcolor_t
*);
362 bool luaA_hasitem(lua_State
*, const void *);
363 void luaA_table2wtable(lua_State
*);
364 int luaA_next(lua_State
*, int);
365 bool luaA_isloop(lua_State
*, int);
367 #define hooks_property(c, prop) \
369 luaA_client_userdata_new(globalconf.L, c); \
370 lua_pushliteral(globalconf.L, prop); \
371 luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
375 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80