awful.menu: import
[awesome.git] / luaa.h
blob6e8238c32f1297f19c6d50135b770d1978f554ad
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); \
52 #define DO_LUA_GC(type, prefix, lua_type, type_unref) \
53 static int \
54 luaA_##prefix##_gc(lua_State *L) \
55 { \
56 type **p = luaA_checkudata(L, 1, lua_type); \
57 type_unref(p); \
58 *p = NULL; \
59 return 0; \
62 #define DO_LUA_EQ(type, prefix, lua_type) \
63 static int \
64 luaA_##prefix##_eq(lua_State *L) \
65 { \
66 type **p1 = luaA_checkudata(L, 1, lua_type); \
67 type **p2 = luaA_checkudata(L, 2, lua_type); \
68 lua_pushboolean(L, (*p1 == *p2)); \
69 return 1; \
72 #define luaA_dostring(L, cmd) \
73 do { \
74 if(a_strlen(cmd)) \
75 if(luaL_dostring(L, cmd)) \
76 warn("error executing Lua code: %s", \
77 lua_tostring(L, -1)); \
78 } while(0)
80 #define luaA_checktable(L, n) \
81 do { \
82 if(!lua_istable(L, n)) \
83 luaL_typerror(L, n, "table"); \
84 } while(0)
86 #define luaA_checkfunction(L, n) \
87 do { \
88 if(!lua_isfunction(L, n)) \
89 luaL_typerror(L, n, "function"); \
90 } while(0)
92 #define luaA_checkscreen(screen) \
93 do { \
94 if(screen < 0 || screen >= globalconf.nscreen) \
95 luaL_error(L, "invalid screen number: %d", screen + 1); \
96 } while(0)
98 /** Dump the Lua stack. Useful for debugging.
99 * \param L The Lua VM state.
101 static inline void
102 luaA_dumpstack(lua_State *L)
104 fprintf(stderr, "-------- Lua stack dump ---------\n");
105 for(int i = lua_gettop(L); i; i--)
107 int t = lua_type(L, i);
108 switch (t)
110 case LUA_TSTRING:
111 fprintf(stderr, "%d: string: `%s'\n", i, lua_tostring(L, i));
112 break;
113 case LUA_TBOOLEAN:
114 fprintf(stderr, "%d: bool: %s\n", i, lua_toboolean(L, i) ? "true" : "false");
115 break;
116 case LUA_TNUMBER:
117 fprintf(stderr, "%d: number: %g\n", i, lua_tonumber(L, i));
118 break;
119 case LUA_TNIL:
120 fprintf(stderr, "%d: nil\n", i);
121 break;
122 default:
123 fprintf(stderr, "%d: %s\t#%d\t%p\n", i, lua_typename(L, t),
124 (int) lua_objlen(L, i),
125 lua_topointer(L, i));
126 break;
129 fprintf(stderr, "------- Lua stack dump end ------\n");
132 /** Check that an object is not a NULL reference.
133 * \param L The Lua state.
134 * \param ud The index of the object in the stack.
135 * \param tname The type name.
136 * \return A pointer to the object.
138 static inline void *
139 luaA_checkudata(lua_State *L, int ud, const char *tname)
141 void **p = luaL_checkudata(L, ud, tname);
142 if(*p)
143 return p;
144 luaL_error(L, "invalid object");
145 return NULL;
148 /** Convert a object to a udata if possible.
149 * \param L The Lua VM state.
150 * \param ud The index.
151 * \param tname The type name.
152 * \return A pointer to the object, NULL otherwise.
154 static inline void *
155 luaA_toudata(lua_State *L, int ud, const char *tname)
157 void **p = lua_touserdata(L, ud);
158 if(p && *p) /* value is a userdata? */
159 if(lua_getmetatable(L, ud)) /* does it have a metatable? */
161 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
162 if(lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
164 lua_pop(L, 2); /* remove both metatables */
165 return p;
167 lua_pop(L, 2); /* remove both metatables */
169 return NULL;
172 static inline bool
173 luaA_checkboolean(lua_State *L, int n)
175 if(!lua_isboolean(L, n))
176 luaL_typerror(L, n, "boolean");
177 return lua_toboolean(L, n);
180 static inline bool
181 luaA_optboolean(lua_State *L, int idx, bool def)
183 return luaL_opt(L, luaA_checkboolean, idx, def);
186 static inline lua_Number
187 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
189 lua_getfield(L, idx, name);
190 return luaL_optnumber(L, -1, def);
193 static inline const char *
194 luaA_getopt_lstring(lua_State *L, int idx, const char *name, const char *def, size_t *len)
196 lua_getfield(L, idx, name);
197 return luaL_optlstring(L, -1, def, len);
200 static inline const char *
201 luaA_getopt_string(lua_State *L, int idx, const char *name, const char *def)
203 return luaA_getopt_lstring(L, idx, name, def, NULL);
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 fct 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 /** Register a function.
276 * \param L The Lua stack.
277 * \param idx Index of the function in the stack.
278 * \param fct A luaA_ref address: it will be filled with the luaA_ref
279 * registered. If the adresse point to an already registered function, it will
280 * be unregistered.
281 * \return luaA_register value.
283 static inline int
284 luaA_registerfct(lua_State *L, int idx, luaA_ref *fct)
286 luaA_checkfunction(L, idx);
287 return luaA_register(L, idx, fct);
290 /** Execute an Lua function.
291 * \param L The Lua stack.
292 * \param f The Lua function to execute.
293 * \param nargs The number of arguments for the Lua function.
294 * \param nret The number of returned value from the Lua function.
295 * \return True on no error, false otherwise.
297 static inline bool
298 luaA_dofunction(lua_State *L, luaA_ref f, int nargs, int nret)
300 if(f != LUA_REFNIL)
302 lua_rawgeti(L, LUA_REGISTRYINDEX, f);
303 if(nargs)
304 lua_insert(L, - (nargs + 1));
305 if(lua_pcall(L, nargs, nret, 0))
307 warn("error running function: %s",
308 lua_tostring(L, -1));
309 lua_pop(L, 1);
310 return false;
312 return true;
314 return false;
317 int luaA_otable_index(lua_State *);
319 /** Create a new object table with a metatable.
320 * This is useful to compare table with objects (udata) as keys.
321 * \param L The Lua stack.
322 * \return The number of elements pushed on stack.
324 static inline int
325 luaA_otable_new(lua_State *L)
327 /* Our object */
328 lua_newtable(L);
329 return luaA_settype(L, "otable");
332 void luaA_init(void);
333 bool luaA_parserc(const char *, bool);
334 void luaA_cs_init(void);
335 void luaA_cs_cleanup(void);
336 void luaA_on_timer(EV_P_ ev_timer *, int);
337 int luaA_pushcolor(lua_State *, const xcolor_t *);
338 bool luaA_hasitem(lua_State *, const void *);
339 void luaA_table2wtable(lua_State *);
340 int luaA_next(lua_State *, int);
342 #define hooks_property(c, prop) \
343 do { \
344 luaA_client_userdata_new(globalconf.L, c); \
345 lua_pushliteral(globalconf.L, prop); \
346 luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
347 } while(0);
349 #endif
350 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80