luaa: split dofunction()
[awesome.git] / luaa.h
blobd95a5c1f238ec999fb84102f7522554e52fe2492
1 /*
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.
22 #ifndef AWESOME_LUA_H
23 #define AWESOME_LUA_H
25 #include <ev.h>
27 #include <lua.h>
28 #include <lauxlib.h>
30 #include <basedir.h>
32 #include "draw.h"
34 #define luaA_deprecate(L, repl) \
35 luaA_warn(L, "%s: This function is deprecated and will be removed, see %s", \
36 __FUNCTION__, repl)
38 #define DO_LUA_TOSTRING(type, prefix, lua_type) \
39 static int \
40 luaA_##prefix##_tostring(lua_State *L) \
41 { \
42 type *p = luaL_checkudata(L, 1, lua_type); \
43 lua_pushfstring(L, lua_type ": %p", p); \
44 return 1; \
47 #define luaA_checktable(L, n) \
48 do { \
49 if(!lua_istable(L, n)) \
50 luaL_typerror(L, n, "table"); \
51 } while(0)
53 #define luaA_checkfunction(L, n) \
54 do { \
55 if(!lua_isfunction(L, n)) \
56 luaL_typerror(L, n, "function"); \
57 } while(0)
59 #define luaA_checkscreen(screen) \
60 do { \
61 if(screen < 0 || screen >= globalconf.screens.len) \
62 luaL_error(L, "invalid screen number: %d", screen + 1); \
63 } while(0)
65 /** Dump the Lua stack. Useful for debugging.
66 * \param L The Lua VM state.
68 static inline void
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);
75 switch (t)
77 case LUA_TSTRING:
78 fprintf(stderr, "%d: string: `%s'\n", i, lua_tostring(L, i));
79 break;
80 case LUA_TBOOLEAN:
81 fprintf(stderr, "%d: bool: %s\n", i, lua_toboolean(L, i) ? "true" : "false");
82 break;
83 case LUA_TNUMBER:
84 fprintf(stderr, "%d: number: %g\n", i, lua_tonumber(L, i));
85 break;
86 case LUA_TNIL:
87 fprintf(stderr, "%d: nil\n", i);
88 break;
89 default:
90 fprintf(stderr, "%d: %s\t#%d\t%p\n", i, lua_typename(L, t),
91 (int) lua_objlen(L, i),
92 lua_topointer(L, i));
93 break;
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.
105 static inline void *
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? */
114 p = NULL;
115 lua_pop(L, 2); /* remove both metatables */
117 return p;
120 static inline bool
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);
128 static inline bool
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);
139 lua_pop(L, 1);
140 return n;
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);
148 lua_pop(L, 1);
149 return s;
152 static inline bool
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);
157 lua_pop(L, 1);
158 return b;
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.
166 static inline int
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");
178 return 1;
181 static inline int
182 luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
184 lua_getmetatable(L, idxobj);
185 lua_pushvalue(L, idxfield);
186 lua_rawget(L, -2);
187 if(!lua_isnil(L, -1))
189 lua_remove(L, -2);
190 return 1;
192 lua_pop(L, 2);
194 return 0;
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
202 * be unregistered.
203 * \return Always 0.
205 static inline int
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);
212 return 0;
215 /** Unregister a Lua object.
216 * \param L The Lua stack.
217 * \param ref A reference to an Lua object.
219 static inline void
220 luaA_unregister(lua_State *L, luaA_ref *ref)
222 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
223 *ref = LUA_REFNIL;
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
231 * be unregistered.
232 * \return luaA_register value.
234 static inline int
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 on top of the stack.
242 * \param L The Lua stack.
243 * \param nargs The number of arguments for the Lua function.
244 * \param nret The number of returned value from the Lua function.
245 * \return True on no error, false otherwise.
247 static inline bool
248 luaA_dofunction(lua_State *L, int nargs, int nret)
250 if(nargs)
251 lua_insert(L, - (nargs + 1));
252 if(lua_pcall(L, nargs, nret, 0))
254 warn("error running function: %s",
255 lua_tostring(L, -1));
256 lua_pop(L, 1);
257 return false;
259 return true;
262 /** Grab a function from the registry and execute it.
263 * \param L The Lua stack.
264 * \param ref The function reference.
265 * \param nargs The number of arguments for the Lua function.
266 * \param nret The number of returned value from the Lua function.
267 * \return True on no error, false otherwise.
269 static inline bool
270 luaA_dofunction_from_registry(lua_State *L, luaA_ref ref, int nargs, int nret)
272 lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
273 return luaA_dofunction(L, nargs, nret);
276 /** Print a warning about some Lua code.
277 * This is less mean than luaL_error() which setjmp via lua_error() and kills
278 * everything. This only warn, it's up to you to then do what's should be done.
279 * \param L The Lua VM state.
280 * \param fmt The warning message.
282 static inline void __attribute__ ((format(printf, 2, 3)))
283 luaA_warn(lua_State *L, const char *fmt, ...)
285 va_list ap;
286 luaL_where(L, 1);
287 fprintf(stderr, "%sW: ", lua_tostring(L, -1));
288 lua_pop(L, 1);
289 va_start(ap, fmt);
290 vfprintf(stderr, fmt, ap);
291 va_end(ap);
292 fprintf(stderr, "\n");
295 /** Get an optional padding table from a Lua table.
296 * \param L The Lua VM state.
297 * \param idx The table index on the stack.
298 * \param dpadding The default padding value to use.
300 static inline padding_t
301 luaA_getopt_padding(lua_State *L, int idx, padding_t *dpadding)
303 padding_t padding;
305 luaA_checktable(L, 2);
307 padding.right = luaA_getopt_number(L, 2, "right", dpadding->right);
308 padding.left = luaA_getopt_number(L, 2, "left", dpadding->left);
309 padding.top = luaA_getopt_number(L, 2, "top", dpadding->top);
310 padding.bottom = luaA_getopt_number(L, 2, "bottom", dpadding->bottom);
312 return padding;
316 /** Push a padding structure into a table on the Lua stack.
317 * \param L The Lua VM state.
318 * \param padding The padding struct pointer.
319 * \return The number of elements pushed on stack.
321 static inline int
322 luaA_pushpadding(lua_State *L, padding_t *padding)
324 lua_createtable(L, 0, 4);
325 lua_pushnumber(L, padding->right);
326 lua_setfield(L, -2, "right");
327 lua_pushnumber(L, padding->left);
328 lua_setfield(L, -2, "left");
329 lua_pushnumber(L, padding->top);
330 lua_setfield(L, -2, "top");
331 lua_pushnumber(L, padding->bottom);
332 lua_setfield(L, -2, "bottom");
333 return 1;
336 void luaA_init(xdgHandle *);
337 bool luaA_parserc(xdgHandle *, const char *, bool);
338 void luaA_on_timer(EV_P_ ev_timer *, int);
339 int luaA_pushxcolor(lua_State *, const xcolor_t *);
340 int luaA_pushcolor(lua_State *, const color_t *);
341 bool luaA_hasitem(lua_State *, const void *);
342 void luaA_table2wtable(lua_State *);
343 int luaA_next(lua_State *, int);
344 bool luaA_isloop(lua_State *, int);
346 #define hook_property(type, obj, prop) \
347 do { \
348 if(globalconf.hooks.property != LUA_REFNIL) \
350 type##_push(globalconf.L, obj); \
351 lua_pushliteral(globalconf.L, prop); \
352 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.property, 2, 0); \
354 } while(0);
356 #endif
357 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80