luaaA: add luaA_unregister()
[awesome.git] / luaa.h
blob167b56a559831a96f588cbba5cdf3dd41c7b0f53
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(L) luaA_warn(L, "%s: This function is deprecated and will be removed.", \
39 __FUNCTION__)
41 #define DO_LUA_NEW(decl, type, prefix, lua_type, type_ref) \
42 decl int \
43 luaA_##prefix##_userdata_new(lua_State *L, type *p) \
44 { \
45 type **pp = lua_newuserdata(L, sizeof(type *)); \
46 *pp = p; \
47 type_ref(pp); \
48 return luaA_settype(L, lua_type); \
49 } \
50 static int \
51 luaA_##prefix##_tostring(lua_State *L) \
52 { \
53 type **p = luaA_checkudata(L, 1, lua_type); \
54 lua_pushfstring(L, lua_type ": %p", *p); \
55 return 1; \
58 #define DO_LUA_GC(type, prefix, lua_type, type_unref) \
59 static int \
60 luaA_##prefix##_gc(lua_State *L) \
61 { \
62 type **p = luaA_checkudata(L, 1, lua_type); \
63 type_unref(p); \
64 *p = NULL; \
65 return 0; \
68 #define DO_LUA_EQ(type, prefix, lua_type) \
69 static int \
70 luaA_##prefix##_eq(lua_State *L) \
71 { \
72 type **p1 = luaA_checkudata(L, 1, lua_type); \
73 type **p2 = luaA_checkudata(L, 2, lua_type); \
74 lua_pushboolean(L, (*p1 == *p2)); \
75 return 1; \
78 #define luaA_dostring(L, cmd) \
79 do { \
80 if(luaL_dostring(L, cmd)) \
81 warn("error executing Lua code: %s", \
82 lua_tostring(L, -1)); \
83 } while(0)
85 #define luaA_checktable(L, n) \
86 do { \
87 if(!lua_istable(L, n)) \
88 luaL_typerror(L, n, "table"); \
89 } while(0)
91 #define luaA_checkfunction(L, n) \
92 do { \
93 if(!lua_isfunction(L, n)) \
94 luaL_typerror(L, n, "function"); \
95 } while(0)
97 #define luaA_checkscreen(screen) \
98 do { \
99 if(screen < 0 || screen >= globalconf.nscreen) \
100 luaL_error(L, "invalid screen number: %d", screen + 1); \
101 } while(0)
103 /** Dump the Lua stack. Useful for debugging.
104 * \param L The Lua VM state.
106 static inline void
107 luaA_dumpstack(lua_State *L)
109 fprintf(stderr, "-------- Lua stack dump ---------\n");
110 for(int i = lua_gettop(L); i; i--)
112 int t = lua_type(L, i);
113 switch (t)
115 case LUA_TSTRING:
116 fprintf(stderr, "%d: string: `%s'\n", i, lua_tostring(L, i));
117 break;
118 case LUA_TBOOLEAN:
119 fprintf(stderr, "%d: bool: %s\n", i, lua_toboolean(L, i) ? "true" : "false");
120 break;
121 case LUA_TNUMBER:
122 fprintf(stderr, "%d: number: %g\n", i, lua_tonumber(L, i));
123 break;
124 case LUA_TNIL:
125 fprintf(stderr, "%d: nil\n", i);
126 break;
127 default:
128 fprintf(stderr, "%d: %s\t#%d\t%p\n", i, lua_typename(L, t),
129 (int) lua_objlen(L, i),
130 lua_topointer(L, i));
131 break;
134 fprintf(stderr, "------- Lua stack dump end ------\n");
137 /** Check that an object is not a NULL reference.
138 * \param L The Lua state.
139 * \param ud The index of the object in the stack.
140 * \param tname The type name.
141 * \return A pointer to the object.
143 static inline void *
144 luaA_checkudata(lua_State *L, int ud, const char *tname)
146 void **p = luaL_checkudata(L, ud, tname);
147 if(*p)
148 return p;
149 luaL_error(L, "invalid object");
150 return NULL;
153 /** Convert a object to a udata if possible.
154 * \param L The Lua VM state.
155 * \param ud The index.
156 * \param tname The type name.
157 * \return A pointer to the object, NULL otherwise.
159 static inline void *
160 luaA_toudata(lua_State *L, int ud, const char *tname)
162 void **p = lua_touserdata(L, ud);
163 if(p && *p) /* value is a userdata? */
164 if(lua_getmetatable(L, ud)) /* does it have a metatable? */
166 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
167 if(lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
169 lua_pop(L, 2); /* remove both metatables */
170 return p;
172 lua_pop(L, 2); /* remove both metatables */
174 return NULL;
177 static inline bool
178 luaA_checkboolean(lua_State *L, int n)
180 if(!lua_isboolean(L, n))
181 luaL_typerror(L, n, "boolean");
182 return lua_toboolean(L, n);
185 static inline bool
186 luaA_optboolean(lua_State *L, int idx, bool def)
188 return luaL_opt(L, luaA_checkboolean, idx, def);
191 static inline lua_Number
192 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
194 lua_getfield(L, idx, name);
195 return luaL_optnumber(L, -1, def);
198 static inline const char *
199 luaA_getopt_lstring(lua_State *L, int idx, const char *name, const char *def, size_t *len)
201 lua_getfield(L, idx, name);
202 return luaL_optlstring(L, -1, def, len);
205 static inline const char *
206 luaA_getopt_string(lua_State *L, int idx, const char *name, const char *def)
208 return luaA_getopt_lstring(L, idx, name, def, NULL);
211 static inline bool
212 luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
214 lua_getfield(L, idx, name);
215 return luaA_optboolean(L, -1, def);
218 static inline int
219 luaA_settype(lua_State *L, const char *type)
221 luaL_getmetatable(L, type);
222 lua_setmetatable(L, -2);
223 return 1;
226 /** Push a area type to a table on stack.
227 * \param L The Lua VM state.
228 * \param geometry The area geometry to push.
229 * \return The number of elements pushed on stack.
231 static inline int
232 luaA_pusharea(lua_State *L, area_t geometry)
234 lua_newtable(L);
235 lua_pushnumber(L, geometry.x);
236 lua_setfield(L, -2, "x");
237 lua_pushnumber(L, geometry.y);
238 lua_setfield(L, -2, "y");
239 lua_pushnumber(L, geometry.width);
240 lua_setfield(L, -2, "width");
241 lua_pushnumber(L, geometry.height);
242 lua_setfield(L, -2, "height");
243 return 1;
246 static inline int
247 luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
249 lua_getmetatable(L, idxobj);
250 lua_pushvalue(L, idxfield);
251 lua_rawget(L, -2);
252 if(!lua_isnil(L, -1))
254 lua_remove(L, -2);
255 return 1;
257 lua_pop(L, 2);
259 return 0;
262 /** Register an Lua object.
263 * \param L The Lua stack.
264 * \param idx Index of the object in the stack.
265 * \param ref A luaA_ref address: it will be filled with the luaA_ref
266 * registered. If the adresse point to an already registered object, it will
267 * be unregistered.
268 * \return Always 0.
270 static inline int
271 luaA_register(lua_State *L, int idx, luaA_ref *ref)
273 lua_pushvalue(L, idx);
274 if(*ref != LUA_REFNIL)
275 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
276 *ref = luaL_ref(L, LUA_REGISTRYINDEX);
277 return 0;
280 /** Unregister a Lua object.
281 * \param L The Lua stack.
282 * \param ref A reference to an Lua object.
284 static inline void
285 luaA_unregister(lua_State *L, luaA_ref *ref)
287 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
288 *ref = LUA_REFNIL;
291 /** Register a function.
292 * \param L The Lua stack.
293 * \param idx Index of the function in the stack.
294 * \param fct A luaA_ref address: it will be filled with the luaA_ref
295 * registered. If the adresse point to an already registered function, it will
296 * be unregistered.
297 * \return luaA_register value.
299 static inline int
300 luaA_registerfct(lua_State *L, int idx, luaA_ref *fct)
302 luaA_checkfunction(L, idx);
303 return luaA_register(L, idx, fct);
306 /** Execute an Lua function.
307 * \param L The Lua stack.
308 * \param f The Lua function to execute.
309 * \param nargs The number of arguments for the Lua function.
310 * \param nret The number of returned value from the Lua function.
311 * \return True on no error, false otherwise.
313 static inline bool
314 luaA_dofunction(lua_State *L, luaA_ref f, int nargs, int nret)
316 if(f != LUA_REFNIL)
318 lua_rawgeti(L, LUA_REGISTRYINDEX, f);
319 if(nargs)
320 lua_insert(L, - (nargs + 1));
321 if(lua_pcall(L, nargs, nret, 0))
323 warn("error running function: %s",
324 lua_tostring(L, -1));
325 lua_pop(L, 1);
326 return false;
328 return true;
330 return false;
333 /** Print a warning about some Lua code.
334 * This is less mean than luaL_error() which setjmp via lua_error() and kills
335 * everything. This only warn, it's up to you to then do what's should be done.
336 * \param L The Lua VM state.
337 * \param fmt The warning message.
339 static inline void __attribute__ ((format(printf, 2, 3)))
340 luaA_warn(lua_State *L, const char *fmt, ...)
342 va_list ap;
343 luaL_where(L, 1);
344 fprintf(stderr, "%sW: ", lua_tostring(L, -1));
345 lua_pop(L, 1);
346 va_start(ap, fmt);
347 vfprintf(stderr, fmt, ap);
348 va_end(ap);
349 fprintf(stderr, "\n");
352 int luaA_otable_index(lua_State *);
354 /** Create a new object table with a metatable.
355 * This is useful to compare table with objects (udata) as keys.
356 * \param L The Lua stack.
357 * \return The number of elements pushed on stack.
359 static inline int
360 luaA_otable_new(lua_State *L)
362 /* Our object */
363 lua_newtable(L);
364 return luaA_settype(L, "otable");
367 void luaA_init(void);
368 bool luaA_parserc(const char *, bool);
369 void luaA_cs_init(void);
370 void luaA_cs_cleanup(void);
371 void luaA_on_timer(EV_P_ ev_timer *, int);
372 int luaA_pushcolor(lua_State *, const xcolor_t *);
373 bool luaA_hasitem(lua_State *, const void *);
374 void luaA_table2wtable(lua_State *);
375 int luaA_next(lua_State *, int);
376 bool luaA_isloop(lua_State *, int);
378 #define hooks_property(c, prop) \
379 do { \
380 luaA_client_userdata_new(globalconf.L, c); \
381 lua_pushliteral(globalconf.L, prop); \
382 luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
383 } while(0);
385 #endif
386 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80