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.
35 #include "common/util.h"
36 #include "common/luaobject.h"
38 #define luaA_deprecate(L, repl) \
39 luaA_warn(L, "%s: This function is deprecated and will be removed, see %s", \
42 #define DO_LUA_TOSTRING(type, prefix, lua_type) \
44 luaA_##prefix##_tostring(lua_State *L) \
46 type *p = luaL_checkudata(L, 1, lua_type); \
47 lua_pushfstring(L, lua_type ": %p", p); \
51 #define luaA_dostring(L, cmd) \
53 if(luaL_dostring(L, cmd)) \
54 warn("error executing Lua code: %s", \
55 lua_tostring(L, -1)); \
58 #define luaA_checktable(L, n) \
60 if(!lua_istable(L, n)) \
61 luaL_typerror(L, n, "table"); \
64 #define luaA_checkfunction(L, n) \
66 if(!lua_isfunction(L, n)) \
67 luaL_typerror(L, n, "function"); \
70 #define luaA_checkscreen(screen) \
72 if(screen < 0 || screen >= globalconf.nscreen) \
73 luaL_error(L, "invalid screen number: %d", screen + 1); \
76 /** Dump the Lua stack. Useful for debugging.
77 * \param L The Lua VM state.
80 luaA_dumpstack(lua_State
*L
)
82 fprintf(stderr
, "-------- Lua stack dump ---------\n");
83 for(int i
= lua_gettop(L
); i
; i
--)
85 int t
= lua_type(L
, i
);
89 fprintf(stderr
, "%d: string: `%s'\n", i
, lua_tostring(L
, i
));
92 fprintf(stderr
, "%d: bool: %s\n", i
, lua_toboolean(L
, i
) ? "true" : "false");
95 fprintf(stderr
, "%d: number: %g\n", i
, lua_tonumber(L
, i
));
98 fprintf(stderr
, "%d: nil\n", i
);
101 fprintf(stderr
, "%d: %s\t#%d\t%p\n", i
, lua_typename(L
, t
),
102 (int) lua_objlen(L
, i
),
103 lua_topointer(L
, i
));
107 fprintf(stderr
, "------- Lua stack dump end ------\n");
110 /** Convert a object to a udata if possible.
111 * \param L The Lua VM state.
112 * \param ud The index.
113 * \param tname The type name.
114 * \return A pointer to the object, NULL otherwise.
117 luaA_toudata(lua_State
*L
, int ud
, const char *tname
)
119 void *p
= lua_touserdata(L
, ud
);
120 if(p
) /* value is a userdata? */
121 if(lua_getmetatable(L
, ud
)) /* does it have a metatable? */
123 lua_getfield(L
, LUA_REGISTRYINDEX
, tname
); /* get correct metatable */
124 if(!lua_rawequal(L
, -1, -2)) /* does it have the correct mt? */
126 lua_pop(L
, 2); /* remove both metatables */
132 luaA_checkboolean(lua_State
*L
, int n
)
134 if(!lua_isboolean(L
, n
))
135 luaL_typerror(L
, n
, "boolean");
136 return lua_toboolean(L
, n
);
140 luaA_optboolean(lua_State
*L
, int idx
, bool def
)
142 return luaL_opt(L
, luaA_checkboolean
, idx
, def
);
145 static inline lua_Number
146 luaA_getopt_number(lua_State
*L
, int idx
, const char *name
, lua_Number def
)
148 lua_getfield(L
, idx
, name
);
149 lua_Number n
= luaL_optnumber(L
, -1, def
);
154 static inline const char *
155 luaA_getopt_lstring(lua_State
*L
, int idx
, const char *name
, const char *def
, size_t *len
)
157 lua_getfield(L
, idx
, name
);
158 const char *s
= luaL_optlstring(L
, -1, def
, len
);
164 luaA_getopt_boolean(lua_State
*L
, int idx
, const char *name
, bool def
)
166 lua_getfield(L
, idx
, name
);
167 bool b
= luaA_optboolean(L
, -1, def
);
172 /** Push a area type to a table on stack.
173 * \param L The Lua VM state.
174 * \param geometry The area geometry to push.
175 * \return The number of elements pushed on stack.
178 luaA_pusharea(lua_State
*L
, area_t geometry
)
181 lua_pushnumber(L
, geometry
.x
);
182 lua_setfield(L
, -2, "x");
183 lua_pushnumber(L
, geometry
.y
);
184 lua_setfield(L
, -2, "y");
185 lua_pushnumber(L
, geometry
.width
);
186 lua_setfield(L
, -2, "width");
187 lua_pushnumber(L
, geometry
.height
);
188 lua_setfield(L
, -2, "height");
193 luaA_usemetatable(lua_State
*L
, int idxobj
, int idxfield
)
195 lua_getmetatable(L
, idxobj
);
196 lua_pushvalue(L
, idxfield
);
198 if(!lua_isnil(L
, -1))
208 /** Register an Lua object.
209 * \param L The Lua stack.
210 * \param idx Index of the object in the stack.
211 * \param ref A luaA_ref address: it will be filled with the luaA_ref
212 * registered. If the adresse point to an already registered object, it will
217 luaA_register(lua_State
*L
, int idx
, luaA_ref
*ref
)
219 lua_pushvalue(L
, idx
);
220 if(*ref
!= LUA_REFNIL
)
221 luaL_unref(L
, LUA_REGISTRYINDEX
, *ref
);
222 *ref
= luaL_ref(L
, LUA_REGISTRYINDEX
);
226 /** Unregister a Lua object.
227 * \param L The Lua stack.
228 * \param ref A reference to an Lua object.
231 luaA_unregister(lua_State
*L
, luaA_ref
*ref
)
233 luaL_unref(L
, LUA_REGISTRYINDEX
, *ref
);
237 /** Register a function.
238 * \param L The Lua stack.
239 * \param idx Index of the function in the stack.
240 * \param fct A luaA_ref address: it will be filled with the luaA_ref
241 * registered. If the adresse point to an already registered function, it will
243 * \return luaA_register value.
246 luaA_registerfct(lua_State
*L
, int idx
, luaA_ref
*fct
)
248 luaA_checkfunction(L
, idx
);
249 return luaA_register(L
, idx
, fct
);
252 /** Execute an Lua function.
253 * \param L The Lua stack.
254 * \param f The Lua function to execute.
255 * \param nargs The number of arguments for the Lua function.
256 * \param nret The number of returned value from the Lua function.
257 * \return True on no error, false otherwise.
260 luaA_dofunction(lua_State
*L
, luaA_ref f
, int nargs
, int nret
)
262 lua_rawgeti(L
, LUA_REGISTRYINDEX
, f
);
264 lua_insert(L
, - (nargs
+ 1));
265 if(lua_pcall(L
, nargs
, nret
, 0))
267 warn("error running function: %s",
268 lua_tostring(L
, -1));
275 /** Print a warning about some Lua code.
276 * This is less mean than luaL_error() which setjmp via lua_error() and kills
277 * everything. This only warn, it's up to you to then do what's should be done.
278 * \param L The Lua VM state.
279 * \param fmt The warning message.
281 static inline void __attribute__ ((format(printf
, 2, 3)))
282 luaA_warn(lua_State
*L
, const char *fmt
, ...)
286 fprintf(stderr
, "%sW: ", lua_tostring(L
, -1));
289 vfprintf(stderr
, fmt
, ap
);
291 fprintf(stderr
, "\n");
294 /** Get an optional padding table from a Lua table.
295 * \param L The Lua VM state.
296 * \param idx The table index on the stack.
297 * \param dpadding The default padding value to use.
299 static inline padding_t
300 luaA_getopt_padding(lua_State
*L
, int idx
, padding_t
*dpadding
)
304 luaA_checktable(L
, 2);
306 padding
.right
= luaA_getopt_number(L
, 2, "right", dpadding
->right
);
307 padding
.left
= luaA_getopt_number(L
, 2, "left", dpadding
->left
);
308 padding
.top
= luaA_getopt_number(L
, 2, "top", dpadding
->top
);
309 padding
.bottom
= luaA_getopt_number(L
, 2, "bottom", dpadding
->bottom
);
315 /** Push a padding structure into a table on the Lua stack.
316 * \param L The Lua VM state.
317 * \param padding The padding struct pointer.
318 * \return The number of elements pushed on stack.
321 luaA_pushpadding(lua_State
*L
, padding_t
*padding
)
324 lua_pushnumber(L
, padding
->right
);
325 lua_setfield(L
, -2, "right");
326 lua_pushnumber(L
, padding
->left
);
327 lua_setfield(L
, -2, "left");
328 lua_pushnumber(L
, padding
->top
);
329 lua_setfield(L
, -2, "top");
330 lua_pushnumber(L
, padding
->bottom
);
331 lua_setfield(L
, -2, "bottom");
335 void luaA_init(xdgHandle
);
336 bool luaA_parserc(xdgHandle
, const char *, bool);
337 void luaA_on_timer(EV_P_ ev_timer
*, int);
338 int luaA_pushcolor(lua_State
*, const xcolor_t
*);
339 bool luaA_hasitem(lua_State
*, const void *);
340 void luaA_table2wtable(lua_State
*);
341 int luaA_next(lua_State
*, int);
342 bool luaA_isloop(lua_State
*, int);
344 #define hooks_property(c, prop) \
346 if(globalconf.hooks.property != LUA_REFNIL) \
348 client_push(globalconf.L, c); \
349 lua_pushliteral(globalconf.L, prop); \
350 luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
355 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80