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_dostring(L, cmd) \
49 if(luaL_dostring(L, cmd)) \
50 warn("error executing Lua code: %s", \
51 lua_tostring(L, -1)); \
54 #define luaA_checktable(L, n) \
56 if(!lua_istable(L, n)) \
57 luaL_typerror(L, n, "table"); \
60 #define luaA_checkfunction(L, n) \
62 if(!lua_isfunction(L, n)) \
63 luaL_typerror(L, n, "function"); \
66 #define luaA_checkscreen(screen) \
68 if(screen < 0 || screen >= globalconf.screens.len) \
69 luaL_error(L, "invalid screen number: %d", screen + 1); \
72 /** Dump the Lua stack. Useful for debugging.
73 * \param L The Lua VM state.
76 luaA_dumpstack(lua_State
*L
)
78 fprintf(stderr
, "-------- Lua stack dump ---------\n");
79 for(int i
= lua_gettop(L
); i
; i
--)
81 int t
= lua_type(L
, i
);
85 fprintf(stderr
, "%d: string: `%s'\n", i
, lua_tostring(L
, i
));
88 fprintf(stderr
, "%d: bool: %s\n", i
, lua_toboolean(L
, i
) ? "true" : "false");
91 fprintf(stderr
, "%d: number: %g\n", i
, lua_tonumber(L
, i
));
94 fprintf(stderr
, "%d: nil\n", i
);
97 fprintf(stderr
, "%d: %s\t#%d\t%p\n", i
, lua_typename(L
, t
),
98 (int) lua_objlen(L
, i
),
103 fprintf(stderr
, "------- Lua stack dump end ------\n");
106 /** Convert a object to a udata if possible.
107 * \param L The Lua VM state.
108 * \param ud The index.
109 * \param tname The type name.
110 * \return A pointer to the object, NULL otherwise.
113 luaA_toudata(lua_State
*L
, int ud
, const char *tname
)
115 void *p
= lua_touserdata(L
, ud
);
116 if(p
) /* value is a userdata? */
117 if(lua_getmetatable(L
, ud
)) /* does it have a metatable? */
119 lua_getfield(L
, LUA_REGISTRYINDEX
, tname
); /* get correct metatable */
120 if(!lua_rawequal(L
, -1, -2)) /* does it have the correct mt? */
122 lua_pop(L
, 2); /* remove both metatables */
128 luaA_checkboolean(lua_State
*L
, int n
)
130 if(!lua_isboolean(L
, n
))
131 luaL_typerror(L
, n
, "boolean");
132 return lua_toboolean(L
, n
);
136 luaA_optboolean(lua_State
*L
, int idx
, bool def
)
138 return luaL_opt(L
, luaA_checkboolean
, idx
, def
);
141 static inline lua_Number
142 luaA_getopt_number(lua_State
*L
, int idx
, const char *name
, lua_Number def
)
144 lua_getfield(L
, idx
, name
);
145 lua_Number n
= luaL_optnumber(L
, -1, def
);
150 static inline const char *
151 luaA_getopt_lstring(lua_State
*L
, int idx
, const char *name
, const char *def
, size_t *len
)
153 lua_getfield(L
, idx
, name
);
154 const char *s
= luaL_optlstring(L
, -1, def
, len
);
160 luaA_getopt_boolean(lua_State
*L
, int idx
, const char *name
, bool def
)
162 lua_getfield(L
, idx
, name
);
163 bool b
= luaA_optboolean(L
, -1, def
);
168 /** Push a area type to a table on stack.
169 * \param L The Lua VM state.
170 * \param geometry The area geometry to push.
171 * \return The number of elements pushed on stack.
174 luaA_pusharea(lua_State
*L
, area_t geometry
)
176 lua_createtable(L
, 0, 4);
177 lua_pushnumber(L
, geometry
.x
);
178 lua_setfield(L
, -2, "x");
179 lua_pushnumber(L
, geometry
.y
);
180 lua_setfield(L
, -2, "y");
181 lua_pushnumber(L
, geometry
.width
);
182 lua_setfield(L
, -2, "width");
183 lua_pushnumber(L
, geometry
.height
);
184 lua_setfield(L
, -2, "height");
189 luaA_usemetatable(lua_State
*L
, int idxobj
, int idxfield
)
191 lua_getmetatable(L
, idxobj
);
192 lua_pushvalue(L
, idxfield
);
194 if(!lua_isnil(L
, -1))
204 /** Register an Lua object.
205 * \param L The Lua stack.
206 * \param idx Index of the object in the stack.
207 * \param ref A luaA_ref address: it will be filled with the luaA_ref
208 * registered. If the adresse point to an already registered object, it will
213 luaA_register(lua_State
*L
, int idx
, luaA_ref
*ref
)
215 lua_pushvalue(L
, idx
);
216 if(*ref
!= LUA_REFNIL
)
217 luaL_unref(L
, LUA_REGISTRYINDEX
, *ref
);
218 *ref
= luaL_ref(L
, LUA_REGISTRYINDEX
);
222 /** Unregister a Lua object.
223 * \param L The Lua stack.
224 * \param ref A reference to an Lua object.
227 luaA_unregister(lua_State
*L
, luaA_ref
*ref
)
229 luaL_unref(L
, LUA_REGISTRYINDEX
, *ref
);
233 /** Register a function.
234 * \param L The Lua stack.
235 * \param idx Index of the function in the stack.
236 * \param fct A luaA_ref address: it will be filled with the luaA_ref
237 * registered. If the adresse point to an already registered function, it will
239 * \return luaA_register value.
242 luaA_registerfct(lua_State
*L
, int idx
, luaA_ref
*fct
)
244 luaA_checkfunction(L
, idx
);
245 return luaA_register(L
, idx
, fct
);
248 /** Execute an Lua function.
249 * \param L The Lua stack.
250 * \param f The Lua function to execute.
251 * \param nargs The number of arguments for the Lua function.
252 * \param nret The number of returned value from the Lua function.
253 * \return True on no error, false otherwise.
256 luaA_dofunction(lua_State
*L
, luaA_ref f
, int nargs
, int nret
)
258 lua_rawgeti(L
, LUA_REGISTRYINDEX
, f
);
260 lua_insert(L
, - (nargs
+ 1));
261 if(lua_pcall(L
, nargs
, nret
, 0))
263 warn("error running function: %s",
264 lua_tostring(L
, -1));
271 /** Print a warning about some Lua code.
272 * This is less mean than luaL_error() which setjmp via lua_error() and kills
273 * everything. This only warn, it's up to you to then do what's should be done.
274 * \param L The Lua VM state.
275 * \param fmt The warning message.
277 static inline void __attribute__ ((format(printf
, 2, 3)))
278 luaA_warn(lua_State
*L
, const char *fmt
, ...)
282 fprintf(stderr
, "%sW: ", lua_tostring(L
, -1));
285 vfprintf(stderr
, fmt
, ap
);
287 fprintf(stderr
, "\n");
290 /** Get an optional padding table from a Lua table.
291 * \param L The Lua VM state.
292 * \param idx The table index on the stack.
293 * \param dpadding The default padding value to use.
295 static inline padding_t
296 luaA_getopt_padding(lua_State
*L
, int idx
, padding_t
*dpadding
)
300 luaA_checktable(L
, 2);
302 padding
.right
= luaA_getopt_number(L
, 2, "right", dpadding
->right
);
303 padding
.left
= luaA_getopt_number(L
, 2, "left", dpadding
->left
);
304 padding
.top
= luaA_getopt_number(L
, 2, "top", dpadding
->top
);
305 padding
.bottom
= luaA_getopt_number(L
, 2, "bottom", dpadding
->bottom
);
311 /** Push a padding structure into a table on the Lua stack.
312 * \param L The Lua VM state.
313 * \param padding The padding struct pointer.
314 * \return The number of elements pushed on stack.
317 luaA_pushpadding(lua_State
*L
, padding_t
*padding
)
319 lua_createtable(L
, 0, 4);
320 lua_pushnumber(L
, padding
->right
);
321 lua_setfield(L
, -2, "right");
322 lua_pushnumber(L
, padding
->left
);
323 lua_setfield(L
, -2, "left");
324 lua_pushnumber(L
, padding
->top
);
325 lua_setfield(L
, -2, "top");
326 lua_pushnumber(L
, padding
->bottom
);
327 lua_setfield(L
, -2, "bottom");
331 void luaA_init(xdgHandle
*);
332 bool luaA_parserc(xdgHandle
*, const char *, bool);
333 void luaA_on_timer(EV_P_ ev_timer
*, int);
334 int luaA_pushxcolor(lua_State
*, const xcolor_t
*);
335 int luaA_pushcolor(lua_State
*, const color_t
*);
336 bool luaA_hasitem(lua_State
*, const void *);
337 void luaA_table2wtable(lua_State
*);
338 int luaA_next(lua_State
*, int);
339 bool luaA_isloop(lua_State
*, int);
341 #define hooks_property(c, prop) \
343 if(globalconf.hooks.property != LUA_REFNIL) \
345 client_push(globalconf.L, c); \
346 lua_pushliteral(globalconf.L, prop); \
347 luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
352 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80