awesomerc: use getmaster()
[awesome.git] / luaa.h
blobafb0ba98e74cff9b0966416e09617464735e5fcd
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 fct 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 /** Register a function.
281 * \param L The Lua stack.
282 * \param idx Index of the function in the stack.
283 * \param fct A luaA_ref address: it will be filled with the luaA_ref
284 * registered. If the adresse point to an already registered function, it will
285 * be unregistered.
286 * \return luaA_register value.
288 static inline int
289 luaA_registerfct(lua_State *L, int idx, luaA_ref *fct)
291 luaA_checkfunction(L, idx);
292 return luaA_register(L, idx, fct);
295 /** Execute an Lua function.
296 * \param L The Lua stack.
297 * \param f The Lua function to execute.
298 * \param nargs The number of arguments for the Lua function.
299 * \param nret The number of returned value from the Lua function.
300 * \return True on no error, false otherwise.
302 static inline bool
303 luaA_dofunction(lua_State *L, luaA_ref f, int nargs, int nret)
305 if(f != LUA_REFNIL)
307 lua_rawgeti(L, LUA_REGISTRYINDEX, f);
308 if(nargs)
309 lua_insert(L, - (nargs + 1));
310 if(lua_pcall(L, nargs, nret, 0))
312 warn("error running function: %s",
313 lua_tostring(L, -1));
314 lua_pop(L, 1);
315 return false;
317 return true;
319 return false;
322 /** Print a warning about some Lua code.
323 * This is less mean than luaL_error() which setjmp via lua_error() and kills
324 * everything. This only warn, it's up to you to then do what's should be done.
325 * \param L The Lua VM state.
326 * \param fmt The warning message.
328 static inline void __attribute__ ((format(printf, 2, 3)))
329 luaA_warn(lua_State *L, const char *fmt, ...)
331 va_list ap;
332 luaL_where(L, 1);
333 fprintf(stderr, "%sW: ", lua_tostring(L, -1));
334 lua_pop(L, 1);
335 va_start(ap, fmt);
336 vfprintf(stderr, fmt, ap);
337 va_end(ap);
338 fprintf(stderr, "\n");
341 int luaA_otable_index(lua_State *);
343 /** Create a new object table with a metatable.
344 * This is useful to compare table with objects (udata) as keys.
345 * \param L The Lua stack.
346 * \return The number of elements pushed on stack.
348 static inline int
349 luaA_otable_new(lua_State *L)
351 /* Our object */
352 lua_newtable(L);
353 return luaA_settype(L, "otable");
356 void luaA_init(void);
357 bool luaA_parserc(const char *, bool);
358 void luaA_cs_init(void);
359 void luaA_cs_cleanup(void);
360 void luaA_on_timer(EV_P_ ev_timer *, int);
361 int luaA_pushcolor(lua_State *, const xcolor_t *);
362 bool luaA_hasitem(lua_State *, const void *);
363 void luaA_table2wtable(lua_State *);
364 int luaA_next(lua_State *, int);
365 bool luaA_isloop(lua_State *, int);
367 #define hooks_property(c, prop) \
368 do { \
369 luaA_client_userdata_new(globalconf.L, c); \
370 lua_pushliteral(globalconf.L, prop); \
371 luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
372 } while(0);
374 #endif
375 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80