2 * luaa.h - Lua configuration management header
4 * Copyright © 2008-2009 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.
34 #define luaA_deprecate(L, repl) \
35 luaA_warn(L, "%s: This function is deprecated and will be removed, see %s", \
38 #define DO_LUA_TOSTRING(type, prefix, lua_type) \
40 luaA_##prefix##_tostring(lua_State *L) \
42 type *p = luaL_checkudata(L, 1, lua_type); \
43 lua_pushfstring(L, lua_type ": %p", p); \
47 #define luaA_checktable(L, n) \
49 if(!lua_istable(L, n)) \
50 luaL_typerror(L, n, "table"); \
53 #define luaA_checkfunction(L, n) \
55 if(!lua_isfunction(L, n)) \
56 luaL_typerror(L, n, "function"); \
59 #define luaA_checkscreen(screen) \
61 if(screen < 0 || screen >= globalconf.screens.len) \
62 luaL_error(L, "invalid screen number: %d", screen + 1); \
65 /** Dump the Lua stack. Useful for debugging.
66 * \param L The Lua VM state.
69 luaA_dumpstack(lua_State
*L
)
71 fprintf(stderr
, "-------- Lua stack dump ---------\n");
72 for(int i
= lua_gettop(L
); i
; i
--)
74 int t
= lua_type(L
, i
);
78 fprintf(stderr
, "%d: string: `%s'\n", i
, lua_tostring(L
, i
));
81 fprintf(stderr
, "%d: bool: %s\n", i
, lua_toboolean(L
, i
) ? "true" : "false");
84 fprintf(stderr
, "%d: number: %g\n", i
, lua_tonumber(L
, i
));
87 fprintf(stderr
, "%d: nil\n", i
);
90 fprintf(stderr
, "%d: %s\t#%d\t%p\n", i
, lua_typename(L
, t
),
91 (int) lua_objlen(L
, i
),
96 fprintf(stderr
, "------- Lua stack dump end ------\n");
99 /** Convert a object to a udata if possible.
100 * \param L The Lua VM state.
101 * \param ud The index.
102 * \param tname The type name.
103 * \return A pointer to the object, NULL otherwise.
106 luaA_toudata(lua_State
*L
, int ud
, const char *tname
)
108 void *p
= lua_touserdata(L
, ud
);
109 if(p
) /* value is a userdata? */
110 if(lua_getmetatable(L
, ud
)) /* does it have a metatable? */
112 lua_getfield(L
, LUA_REGISTRYINDEX
, tname
); /* get correct metatable */
113 if(!lua_rawequal(L
, -1, -2)) /* does it have the correct mt? */
115 lua_pop(L
, 2); /* remove both metatables */
121 luaA_checkboolean(lua_State
*L
, int n
)
123 if(!lua_isboolean(L
, n
))
124 luaL_typerror(L
, n
, "boolean");
125 return lua_toboolean(L
, n
);
129 luaA_optboolean(lua_State
*L
, int idx
, bool def
)
131 return luaL_opt(L
, luaA_checkboolean
, idx
, def
);
134 static inline lua_Number
135 luaA_getopt_number(lua_State
*L
, int idx
, const char *name
, lua_Number def
)
137 lua_getfield(L
, idx
, name
);
138 lua_Number n
= luaL_optnumber(L
, -1, def
);
143 static inline const char *
144 luaA_getopt_lstring(lua_State
*L
, int idx
, const char *name
, const char *def
, size_t *len
)
146 lua_getfield(L
, idx
, name
);
147 const char *s
= luaL_optlstring(L
, -1, def
, len
);
153 luaA_getopt_boolean(lua_State
*L
, int idx
, const char *name
, bool def
)
155 lua_getfield(L
, idx
, name
);
156 bool b
= luaA_optboolean(L
, -1, def
);
161 /** Push a area type to a table on stack.
162 * \param L The Lua VM state.
163 * \param geometry The area geometry to push.
164 * \return The number of elements pushed on stack.
167 luaA_pusharea(lua_State
*L
, area_t geometry
)
169 lua_createtable(L
, 0, 4);
170 lua_pushnumber(L
, geometry
.x
);
171 lua_setfield(L
, -2, "x");
172 lua_pushnumber(L
, geometry
.y
);
173 lua_setfield(L
, -2, "y");
174 lua_pushnumber(L
, geometry
.width
);
175 lua_setfield(L
, -2, "width");
176 lua_pushnumber(L
, geometry
.height
);
177 lua_setfield(L
, -2, "height");
182 luaA_usemetatable(lua_State
*L
, int idxobj
, int idxfield
)
184 lua_getmetatable(L
, idxobj
);
185 lua_pushvalue(L
, idxfield
);
187 if(!lua_isnil(L
, -1))
197 /** Register an Lua object.
198 * \param L The Lua stack.
199 * \param idx Index of the object in the stack.
200 * \param ref A luaA_ref address: it will be filled with the luaA_ref
201 * registered. If the adresse point to an already registered object, it will
206 luaA_register(lua_State
*L
, int idx
, luaA_ref
*ref
)
208 lua_pushvalue(L
, idx
);
209 if(*ref
!= LUA_REFNIL
)
210 luaL_unref(L
, LUA_REGISTRYINDEX
, *ref
);
211 *ref
= luaL_ref(L
, LUA_REGISTRYINDEX
);
215 /** Unregister a Lua object.
216 * \param L The Lua stack.
217 * \param ref A reference to an Lua object.
220 luaA_unregister(lua_State
*L
, luaA_ref
*ref
)
222 luaL_unref(L
, LUA_REGISTRYINDEX
, *ref
);
226 /** Register a function.
227 * \param L The Lua stack.
228 * \param idx Index of the function in the stack.
229 * \param fct A luaA_ref address: it will be filled with the luaA_ref
230 * registered. If the adresse point to an already registered function, it will
232 * \return luaA_register value.
235 luaA_registerfct(lua_State
*L
, int idx
, luaA_ref
*fct
)
237 luaA_checkfunction(L
, idx
);
238 return luaA_register(L
, idx
, fct
);
241 /** Execute an Lua function.
242 * \param L The Lua stack.
243 * \param f The Lua function to execute.
244 * \param nargs The number of arguments for the Lua function.
245 * \param nret The number of returned value from the Lua function.
246 * \return True on no error, false otherwise.
249 luaA_dofunction(lua_State
*L
, luaA_ref f
, int nargs
, int nret
)
251 lua_rawgeti(L
, LUA_REGISTRYINDEX
, f
);
253 lua_insert(L
, - (nargs
+ 1));
254 if(lua_pcall(L
, nargs
, nret
, 0))
256 warn("error running function: %s",
257 lua_tostring(L
, -1));
264 /** Print a warning about some Lua code.
265 * This is less mean than luaL_error() which setjmp via lua_error() and kills
266 * everything. This only warn, it's up to you to then do what's should be done.
267 * \param L The Lua VM state.
268 * \param fmt The warning message.
270 static inline void __attribute__ ((format(printf
, 2, 3)))
271 luaA_warn(lua_State
*L
, const char *fmt
, ...)
275 fprintf(stderr
, "%sW: ", lua_tostring(L
, -1));
278 vfprintf(stderr
, fmt
, ap
);
280 fprintf(stderr
, "\n");
283 /** Get an optional padding table from a Lua table.
284 * \param L The Lua VM state.
285 * \param idx The table index on the stack.
286 * \param dpadding The default padding value to use.
288 static inline padding_t
289 luaA_getopt_padding(lua_State
*L
, int idx
, padding_t
*dpadding
)
293 luaA_checktable(L
, 2);
295 padding
.right
= luaA_getopt_number(L
, 2, "right", dpadding
->right
);
296 padding
.left
= luaA_getopt_number(L
, 2, "left", dpadding
->left
);
297 padding
.top
= luaA_getopt_number(L
, 2, "top", dpadding
->top
);
298 padding
.bottom
= luaA_getopt_number(L
, 2, "bottom", dpadding
->bottom
);
304 /** Push a padding structure into a table on the Lua stack.
305 * \param L The Lua VM state.
306 * \param padding The padding struct pointer.
307 * \return The number of elements pushed on stack.
310 luaA_pushpadding(lua_State
*L
, padding_t
*padding
)
312 lua_createtable(L
, 0, 4);
313 lua_pushnumber(L
, padding
->right
);
314 lua_setfield(L
, -2, "right");
315 lua_pushnumber(L
, padding
->left
);
316 lua_setfield(L
, -2, "left");
317 lua_pushnumber(L
, padding
->top
);
318 lua_setfield(L
, -2, "top");
319 lua_pushnumber(L
, padding
->bottom
);
320 lua_setfield(L
, -2, "bottom");
324 void luaA_init(xdgHandle
*);
325 bool luaA_parserc(xdgHandle
*, const char *, bool);
326 void luaA_on_timer(EV_P_ ev_timer
*, int);
327 int luaA_pushxcolor(lua_State
*, const xcolor_t
*);
328 int luaA_pushcolor(lua_State
*, const color_t
*);
329 bool luaA_hasitem(lua_State
*, const void *);
330 void luaA_table2wtable(lua_State
*);
331 int luaA_next(lua_State
*, int);
332 bool luaA_isloop(lua_State
*, int);
334 #define hook_property(type, obj, prop) \
336 if(globalconf.hooks.property != LUA_REFNIL) \
338 type##_push(globalconf.L, obj); \
339 lua_pushliteral(globalconf.L, prop); \
340 luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
345 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80