awful.widget.taglist: remove needless taglist_squares conditions
[awesome.git] / luaa.h
blob8833195efb034ffc0e0f5fe34c3223a523222774
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 <stdio.h>
34 #include "draw.h"
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", \
40 __FUNCTION__, repl)
42 #define DO_LUA_TOSTRING(type, prefix, lua_type) \
43 static int \
44 luaA_##prefix##_tostring(lua_State *L) \
45 { \
46 type *p = luaL_checkudata(L, 1, lua_type); \
47 lua_pushfstring(L, lua_type ": %p", p); \
48 return 1; \
51 #define luaA_dostring(L, cmd) \
52 do { \
53 if(luaL_dostring(L, cmd)) \
54 warn("error executing Lua code: %s", \
55 lua_tostring(L, -1)); \
56 } while(0)
58 #define luaA_checktable(L, n) \
59 do { \
60 if(!lua_istable(L, n)) \
61 luaL_typerror(L, n, "table"); \
62 } while(0)
64 #define luaA_checkfunction(L, n) \
65 do { \
66 if(!lua_isfunction(L, n)) \
67 luaL_typerror(L, n, "function"); \
68 } while(0)
70 #define luaA_checkscreen(screen) \
71 do { \
72 if(screen < 0 || screen >= globalconf.nscreen) \
73 luaL_error(L, "invalid screen number: %d", screen + 1); \
74 } while(0)
76 /** Dump the Lua stack. Useful for debugging.
77 * \param L The Lua VM state.
79 static inline void
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);
86 switch (t)
88 case LUA_TSTRING:
89 fprintf(stderr, "%d: string: `%s'\n", i, lua_tostring(L, i));
90 break;
91 case LUA_TBOOLEAN:
92 fprintf(stderr, "%d: bool: %s\n", i, lua_toboolean(L, i) ? "true" : "false");
93 break;
94 case LUA_TNUMBER:
95 fprintf(stderr, "%d: number: %g\n", i, lua_tonumber(L, i));
96 break;
97 case LUA_TNIL:
98 fprintf(stderr, "%d: nil\n", i);
99 break;
100 default:
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));
104 break;
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.
116 static inline void *
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? */
125 p = NULL;
126 lua_pop(L, 2); /* remove both metatables */
128 return p;
131 static inline bool
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);
139 static inline bool
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);
150 lua_pop(L, 1);
151 return n;
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);
159 lua_pop(L, 1);
160 return s;
163 static inline bool
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);
168 lua_pop(L, 1);
169 return b;
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.
177 static inline int
178 luaA_pusharea(lua_State *L, area_t geometry)
180 lua_newtable(L);
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");
189 return 1;
192 static inline int
193 luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
195 lua_getmetatable(L, idxobj);
196 lua_pushvalue(L, idxfield);
197 lua_rawget(L, -2);
198 if(!lua_isnil(L, -1))
200 lua_remove(L, -2);
201 return 1;
203 lua_pop(L, 2);
205 return 0;
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
213 * be unregistered.
214 * \return Always 0.
216 static inline int
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);
223 return 0;
226 /** Unregister a Lua object.
227 * \param L The Lua stack.
228 * \param ref A reference to an Lua object.
230 static inline void
231 luaA_unregister(lua_State *L, luaA_ref *ref)
233 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
234 *ref = LUA_REFNIL;
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
242 * be unregistered.
243 * \return luaA_register value.
245 static inline int
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.
259 static inline bool
260 luaA_dofunction(lua_State *L, luaA_ref f, int nargs, int nret)
262 lua_rawgeti(L, LUA_REGISTRYINDEX, f);
263 if(nargs)
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));
269 lua_pop(L, 1);
270 return false;
272 return true;
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, ...)
284 va_list ap;
285 luaL_where(L, 1);
286 fprintf(stderr, "%sW: ", lua_tostring(L, -1));
287 lua_pop(L, 1);
288 va_start(ap, fmt);
289 vfprintf(stderr, fmt, ap);
290 va_end(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)
302 padding_t padding;
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);
311 return padding;
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.
320 static inline int
321 luaA_pushpadding(lua_State *L, padding_t *padding)
323 lua_newtable(L);
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");
332 return 1;
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) \
345 do { \
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); \
352 } while(0);
354 #endif
355 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80