luaa: merge tostring() with DO_LUA_NEW
[awesome.git] / luaa.h
blob515cd89a5b72ac4d767ac35117547193097a5714
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 deprecate() _warn(__LINE__, \
39 __FUNCTION__, \
40 "This function is deprecated and will be removed.")
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(a_strlen(cmd)) \
82 if(luaL_dostring(L, cmd)) \
83 warn("error executing Lua code: %s", \
84 lua_tostring(L, -1)); \
85 } while(0)
87 #define luaA_checktable(L, n) \
88 do { \
89 if(!lua_istable(L, n)) \
90 luaL_typerror(L, n, "table"); \
91 } while(0)
93 #define luaA_checkfunction(L, n) \
94 do { \
95 if(!lua_isfunction(L, n)) \
96 luaL_typerror(L, n, "function"); \
97 } while(0)
99 #define luaA_checkscreen(screen) \
100 do { \
101 if(screen < 0 || screen >= globalconf.nscreen) \
102 luaL_error(L, "invalid screen number: %d", screen + 1); \
103 } while(0)
105 /** Dump the Lua stack. Useful for debugging.
106 * \param L The Lua VM state.
108 static inline void
109 luaA_dumpstack(lua_State *L)
111 fprintf(stderr, "-------- Lua stack dump ---------\n");
112 for(int i = lua_gettop(L); i; i--)
114 int t = lua_type(L, i);
115 switch (t)
117 case LUA_TSTRING:
118 fprintf(stderr, "%d: string: `%s'\n", i, lua_tostring(L, i));
119 break;
120 case LUA_TBOOLEAN:
121 fprintf(stderr, "%d: bool: %s\n", i, lua_toboolean(L, i) ? "true" : "false");
122 break;
123 case LUA_TNUMBER:
124 fprintf(stderr, "%d: number: %g\n", i, lua_tonumber(L, i));
125 break;
126 case LUA_TNIL:
127 fprintf(stderr, "%d: nil\n", i);
128 break;
129 default:
130 fprintf(stderr, "%d: %s\t#%d\t%p\n", i, lua_typename(L, t),
131 (int) lua_objlen(L, i),
132 lua_topointer(L, i));
133 break;
136 fprintf(stderr, "------- Lua stack dump end ------\n");
139 /** Check that an object is not a NULL reference.
140 * \param L The Lua state.
141 * \param ud The index of the object in the stack.
142 * \param tname The type name.
143 * \return A pointer to the object.
145 static inline void *
146 luaA_checkudata(lua_State *L, int ud, const char *tname)
148 void **p = luaL_checkudata(L, ud, tname);
149 if(*p)
150 return p;
151 luaL_error(L, "invalid object");
152 return NULL;
155 /** Convert a object to a udata if possible.
156 * \param L The Lua VM state.
157 * \param ud The index.
158 * \param tname The type name.
159 * \return A pointer to the object, NULL otherwise.
161 static inline void *
162 luaA_toudata(lua_State *L, int ud, const char *tname)
164 void **p = lua_touserdata(L, ud);
165 if(p && *p) /* value is a userdata? */
166 if(lua_getmetatable(L, ud)) /* does it have a metatable? */
168 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
169 if(lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
171 lua_pop(L, 2); /* remove both metatables */
172 return p;
174 lua_pop(L, 2); /* remove both metatables */
176 return NULL;
179 static inline bool
180 luaA_checkboolean(lua_State *L, int n)
182 if(!lua_isboolean(L, n))
183 luaL_typerror(L, n, "boolean");
184 return lua_toboolean(L, n);
187 static inline bool
188 luaA_optboolean(lua_State *L, int idx, bool def)
190 return luaL_opt(L, luaA_checkboolean, idx, def);
193 static inline lua_Number
194 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
196 lua_getfield(L, idx, name);
197 return luaL_optnumber(L, -1, def);
200 static inline const char *
201 luaA_getopt_lstring(lua_State *L, int idx, const char *name, const char *def, size_t *len)
203 lua_getfield(L, idx, name);
204 return luaL_optlstring(L, -1, def, len);
207 static inline const char *
208 luaA_getopt_string(lua_State *L, int idx, const char *name, const char *def)
210 return luaA_getopt_lstring(L, idx, name, def, NULL);
213 static inline bool
214 luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
216 lua_getfield(L, idx, name);
217 return luaA_optboolean(L, -1, def);
220 static inline int
221 luaA_settype(lua_State *L, const char *type)
223 luaL_getmetatable(L, type);
224 lua_setmetatable(L, -2);
225 return 1;
228 /** Push a area type to a table on stack.
229 * \param L The Lua VM state.
230 * \param geometry The area geometry to push.
231 * \return The number of elements pushed on stack.
233 static inline int
234 luaA_pusharea(lua_State *L, area_t geometry)
236 lua_newtable(L);
237 lua_pushnumber(L, geometry.x);
238 lua_setfield(L, -2, "x");
239 lua_pushnumber(L, geometry.y);
240 lua_setfield(L, -2, "y");
241 lua_pushnumber(L, geometry.width);
242 lua_setfield(L, -2, "width");
243 lua_pushnumber(L, geometry.height);
244 lua_setfield(L, -2, "height");
245 return 1;
248 static inline int
249 luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
251 lua_getmetatable(L, idxobj);
252 lua_pushvalue(L, idxfield);
253 lua_rawget(L, -2);
254 if(!lua_isnil(L, -1))
256 lua_remove(L, -2);
257 return 1;
259 lua_pop(L, 2);
261 return 0;
264 /** Register an Lua object.
265 * \param L The Lua stack.
266 * \param idx Index of the object in the stack.
267 * \param fct A luaA_ref address: it will be filled with the luaA_ref
268 * registered. If the adresse point to an already registered object, it will
269 * be unregistered.
270 * \return Always 0.
272 static inline int
273 luaA_register(lua_State *L, int idx, luaA_ref *ref)
275 lua_pushvalue(L, idx);
276 if(*ref != LUA_REFNIL)
277 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
278 *ref = luaL_ref(L, LUA_REGISTRYINDEX);
279 return 0;
282 /** Register a function.
283 * \param L The Lua stack.
284 * \param idx Index of the function in the stack.
285 * \param fct A luaA_ref address: it will be filled with the luaA_ref
286 * registered. If the adresse point to an already registered function, it will
287 * be unregistered.
288 * \return luaA_register value.
290 static inline int
291 luaA_registerfct(lua_State *L, int idx, luaA_ref *fct)
293 luaA_checkfunction(L, idx);
294 return luaA_register(L, idx, fct);
297 /** Execute an Lua function.
298 * \param L The Lua stack.
299 * \param f The Lua function to execute.
300 * \param nargs The number of arguments for the Lua function.
301 * \param nret The number of returned value from the Lua function.
302 * \return True on no error, false otherwise.
304 static inline bool
305 luaA_dofunction(lua_State *L, luaA_ref f, int nargs, int nret)
307 if(f != LUA_REFNIL)
309 lua_rawgeti(L, LUA_REGISTRYINDEX, f);
310 if(nargs)
311 lua_insert(L, - (nargs + 1));
312 if(lua_pcall(L, nargs, nret, 0))
314 warn("error running function: %s",
315 lua_tostring(L, -1));
316 lua_pop(L, 1);
317 return false;
319 return true;
321 return false;
324 int luaA_otable_index(lua_State *);
326 /** Create a new object table with a metatable.
327 * This is useful to compare table with objects (udata) as keys.
328 * \param L The Lua stack.
329 * \return The number of elements pushed on stack.
331 static inline int
332 luaA_otable_new(lua_State *L)
334 /* Our object */
335 lua_newtable(L);
336 return luaA_settype(L, "otable");
339 void luaA_init(void);
340 bool luaA_parserc(const char *, bool);
341 void luaA_cs_init(void);
342 void luaA_cs_cleanup(void);
343 void luaA_on_timer(EV_P_ ev_timer *, int);
344 int luaA_pushcolor(lua_State *, const xcolor_t *);
345 bool luaA_hasitem(lua_State *, const void *);
346 void luaA_table2wtable(lua_State *);
347 int luaA_next(lua_State *, int);
349 #define hooks_property(c, prop) \
350 do { \
351 luaA_client_userdata_new(globalconf.L, c); \
352 lua_pushliteral(globalconf.L, prop); \
353 luaA_dofunction(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