awful.placement: fix floating detection
[awesome.git] / luaa.h
blobc1301779c092637711f17b29bf85d73efae35ba2
1 /*
2 * lua.h - Lua configuration management header
4 * Copyright © 2008 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 <stdio.h>
32 #include "draw.h"
33 #include "common/util.h"
35 /** Type for Lua function */
36 typedef int luaA_ref;
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_NEW(decl, type, prefix, lua_type, type_ref) \
43 decl int \
44 luaA_##prefix##_userdata_new(lua_State *L, type *p) \
45 { \
46 type **pp = lua_newuserdata(L, sizeof(type *)); \
47 *pp = p; \
48 type_ref(pp); \
49 return luaA_settype(L, lua_type); \
50 } \
51 static int \
52 luaA_##prefix##_tostring(lua_State *L) \
53 { \
54 type **p = luaA_checkudata(L, 1, lua_type); \
55 lua_pushfstring(L, lua_type ": %p", *p); \
56 return 1; \
59 #define DO_LUA_GC(type, prefix, lua_type, type_unref) \
60 static int \
61 luaA_##prefix##_gc(lua_State *L) \
62 { \
63 type **p = luaA_checkudata(L, 1, lua_type); \
64 type_unref(p); \
65 *p = NULL; \
66 return 0; \
69 #define DO_LUA_EQ(type, prefix, lua_type) \
70 static int \
71 luaA_##prefix##_eq(lua_State *L) \
72 { \
73 type **p1 = luaA_checkudata(L, 1, lua_type); \
74 type **p2 = luaA_checkudata(L, 2, lua_type); \
75 lua_pushboolean(L, (*p1 == *p2)); \
76 return 1; \
79 #define luaA_dostring(L, cmd) \
80 do { \
81 if(luaL_dostring(L, cmd)) \
82 warn("error executing Lua code: %s", \
83 lua_tostring(L, -1)); \
84 } while(0)
86 #define luaA_checktable(L, n) \
87 do { \
88 if(!lua_istable(L, n)) \
89 luaL_typerror(L, n, "table"); \
90 } while(0)
92 #define luaA_checkfunction(L, n) \
93 do { \
94 if(!lua_isfunction(L, n)) \
95 luaL_typerror(L, n, "function"); \
96 } while(0)
98 #define luaA_checkscreen(screen) \
99 do { \
100 if(screen < 0 || screen >= globalconf.nscreen) \
101 luaL_error(L, "invalid screen number: %d", screen + 1); \
102 } while(0)
104 /** Dump the Lua stack. Useful for debugging.
105 * \param L The Lua VM state.
107 static inline void
108 luaA_dumpstack(lua_State *L)
110 fprintf(stderr, "-------- Lua stack dump ---------\n");
111 for(int i = lua_gettop(L); i; i--)
113 int t = lua_type(L, i);
114 switch (t)
116 case LUA_TSTRING:
117 fprintf(stderr, "%d: string: `%s'\n", i, lua_tostring(L, i));
118 break;
119 case LUA_TBOOLEAN:
120 fprintf(stderr, "%d: bool: %s\n", i, lua_toboolean(L, i) ? "true" : "false");
121 break;
122 case LUA_TNUMBER:
123 fprintf(stderr, "%d: number: %g\n", i, lua_tonumber(L, i));
124 break;
125 case LUA_TNIL:
126 fprintf(stderr, "%d: nil\n", i);
127 break;
128 default:
129 fprintf(stderr, "%d: %s\t#%d\t%p\n", i, lua_typename(L, t),
130 (int) lua_objlen(L, i),
131 lua_topointer(L, i));
132 break;
135 fprintf(stderr, "------- Lua stack dump end ------\n");
138 /** Check that an object is not a NULL reference.
139 * \param L The Lua state.
140 * \param ud The index of the object in the stack.
141 * \param tname The type name.
142 * \return A pointer to the object.
144 static inline void *
145 luaA_checkudata(lua_State *L, int ud, const char *tname)
147 void **p = luaL_checkudata(L, ud, tname);
148 if(*p)
149 return p;
150 luaL_error(L, "invalid object");
151 return NULL;
154 /** Convert a object to a udata if possible.
155 * \param L The Lua VM state.
156 * \param ud The index.
157 * \param tname The type name.
158 * \return A pointer to the object, NULL otherwise.
160 static inline void *
161 luaA_toudata(lua_State *L, int ud, const char *tname)
163 void **p = lua_touserdata(L, ud);
164 if(p && *p) /* value is a userdata? */
165 if(lua_getmetatable(L, ud)) /* does it have a metatable? */
167 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
168 if(lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
170 lua_pop(L, 2); /* remove both metatables */
171 return p;
173 lua_pop(L, 2); /* remove both metatables */
175 return NULL;
178 static inline bool
179 luaA_checkboolean(lua_State *L, int n)
181 if(!lua_isboolean(L, n))
182 luaL_typerror(L, n, "boolean");
183 return lua_toboolean(L, n);
186 static inline bool
187 luaA_optboolean(lua_State *L, int idx, bool def)
189 return luaL_opt(L, luaA_checkboolean, idx, def);
192 static inline lua_Number
193 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
195 lua_getfield(L, idx, name);
196 return luaL_optnumber(L, -1, def);
199 static inline const char *
200 luaA_getopt_lstring(lua_State *L, int idx, const char *name, const char *def, size_t *len)
202 lua_getfield(L, idx, name);
203 return luaL_optlstring(L, -1, def, len);
206 static inline bool
207 luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
209 lua_getfield(L, idx, name);
210 return luaA_optboolean(L, -1, def);
213 static inline int
214 luaA_settype(lua_State *L, const char *type)
216 luaL_getmetatable(L, type);
217 lua_setmetatable(L, -2);
218 return 1;
221 /** Push a area type to a table on stack.
222 * \param L The Lua VM state.
223 * \param geometry The area geometry to push.
224 * \return The number of elements pushed on stack.
226 static inline int
227 luaA_pusharea(lua_State *L, area_t geometry)
229 lua_newtable(L);
230 lua_pushnumber(L, geometry.x);
231 lua_setfield(L, -2, "x");
232 lua_pushnumber(L, geometry.y);
233 lua_setfield(L, -2, "y");
234 lua_pushnumber(L, geometry.width);
235 lua_setfield(L, -2, "width");
236 lua_pushnumber(L, geometry.height);
237 lua_setfield(L, -2, "height");
238 return 1;
241 static inline int
242 luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
244 lua_getmetatable(L, idxobj);
245 lua_pushvalue(L, idxfield);
246 lua_rawget(L, -2);
247 if(!lua_isnil(L, -1))
249 lua_remove(L, -2);
250 return 1;
252 lua_pop(L, 2);
254 return 0;
257 /** Register an Lua object.
258 * \param L The Lua stack.
259 * \param idx Index of the object in the stack.
260 * \param ref A luaA_ref address: it will be filled with the luaA_ref
261 * registered. If the adresse point to an already registered object, it will
262 * be unregistered.
263 * \return Always 0.
265 static inline int
266 luaA_register(lua_State *L, int idx, luaA_ref *ref)
268 lua_pushvalue(L, idx);
269 if(*ref != LUA_REFNIL)
270 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
271 *ref = luaL_ref(L, LUA_REGISTRYINDEX);
272 return 0;
275 /** Unregister a Lua object.
276 * \param L The Lua stack.
277 * \param ref A reference to an Lua object.
279 static inline void
280 luaA_unregister(lua_State *L, luaA_ref *ref)
282 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
283 *ref = LUA_REFNIL;
286 /** Register a function.
287 * \param L The Lua stack.
288 * \param idx Index of the function in the stack.
289 * \param fct A luaA_ref address: it will be filled with the luaA_ref
290 * registered. If the adresse point to an already registered function, it will
291 * be unregistered.
292 * \return luaA_register value.
294 static inline int
295 luaA_registerfct(lua_State *L, int idx, luaA_ref *fct)
297 luaA_checkfunction(L, idx);
298 return luaA_register(L, idx, fct);
301 /** Execute an Lua function.
302 * \param L The Lua stack.
303 * \param f The Lua function to execute.
304 * \param nargs The number of arguments for the Lua function.
305 * \param nret The number of returned value from the Lua function.
306 * \return True on no error, false otherwise.
308 static inline bool
309 luaA_dofunction(lua_State *L, luaA_ref f, int nargs, int nret)
311 lua_rawgeti(L, LUA_REGISTRYINDEX, f);
312 if(nargs)
313 lua_insert(L, - (nargs + 1));
314 if(lua_pcall(L, nargs, nret, 0))
316 warn("error running function: %s",
317 lua_tostring(L, -1));
318 lua_pop(L, 1);
319 return false;
321 return true;
324 /** Print a warning about some Lua code.
325 * This is less mean than luaL_error() which setjmp via lua_error() and kills
326 * everything. This only warn, it's up to you to then do what's should be done.
327 * \param L The Lua VM state.
328 * \param fmt The warning message.
330 static inline void __attribute__ ((format(printf, 2, 3)))
331 luaA_warn(lua_State *L, const char *fmt, ...)
333 va_list ap;
334 luaL_where(L, 1);
335 fprintf(stderr, "%sW: ", lua_tostring(L, -1));
336 lua_pop(L, 1);
337 va_start(ap, fmt);
338 vfprintf(stderr, fmt, ap);
339 va_end(ap);
340 fprintf(stderr, "\n");
343 int luaA_otable_index(lua_State *);
345 /** Create a new object table with a metatable.
346 * This is useful to compare table with objects (udata) as keys.
347 * \param L The Lua stack.
348 * \return The number of elements pushed on stack.
350 static inline int
351 luaA_otable_new(lua_State *L)
353 /* Our object */
354 lua_newtable(L);
355 return luaA_settype(L, "otable");
358 /** Get an optional padding table from a Lua table.
359 * \param L The Lua VM state.
360 * \param idx The table index on the stack.
361 * \param dpadding The default padding value to use.
363 static inline padding_t
364 luaA_getopt_padding(lua_State *L, int idx, padding_t *dpadding)
366 padding_t padding;
368 luaA_checktable(L, 2);
370 padding.right = luaA_getopt_number(L, 2, "right", dpadding->right);
371 padding.left = luaA_getopt_number(L, 2, "left", dpadding->left);
372 padding.top = luaA_getopt_number(L, 2, "top", dpadding->top);
373 padding.bottom = luaA_getopt_number(L, 2, "bottom", dpadding->bottom);
375 return padding;
379 /** Push a padding structure into a table on the Lua stack.
380 * \param L The Lua VM state.
381 * \param padding The padding struct pointer.
382 * \return The number of elements pushed on stack.
384 static inline int
385 luaA_pushpadding(lua_State *L, padding_t *padding)
387 lua_newtable(L);
388 lua_pushnumber(L, padding->right);
389 lua_setfield(L, -2, "right");
390 lua_pushnumber(L, padding->left);
391 lua_setfield(L, -2, "left");
392 lua_pushnumber(L, padding->top);
393 lua_setfield(L, -2, "top");
394 lua_pushnumber(L, padding->bottom);
395 lua_setfield(L, -2, "bottom");
396 return 1;
399 void luaA_init(void);
400 bool luaA_parserc(const char *, bool);
401 void luaA_cs_init(void);
402 void luaA_cs_cleanup(void);
403 void luaA_on_timer(EV_P_ ev_timer *, int);
404 int luaA_pushcolor(lua_State *, const xcolor_t *);
405 bool luaA_hasitem(lua_State *, const void *);
406 void luaA_table2wtable(lua_State *);
407 int luaA_next(lua_State *, int);
408 bool luaA_isloop(lua_State *, int);
410 #define hooks_property(c, prop) \
411 do { \
412 if(globalconf.hooks.property != LUA_REFNIL) \
414 luaA_client_userdata_new(globalconf.L, c); \
415 lua_pushliteral(globalconf.L, prop); \
416 luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
418 } while(0);
420 #endif
421 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80