wibox: modify border color live
[awesome.git] / luaa.h
blobcb210920fd1cbdd692bd3f5f56f6893121aca094
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>
26 #include <lua.h>
27 #include <lauxlib.h>
29 #include "draw.h"
30 #include "common/util.h"
32 /** Type for Lua function */
33 typedef int luaA_ref;
35 #define deprecate() _warn(__LINE__, \
36 __FUNCTION__, \
37 "This function is deprecated and will be removed.")
39 #define DO_LUA_NEW(decl, type, prefix, lua_type, type_ref) \
40 decl int \
41 luaA_##prefix##_userdata_new(lua_State *L, type *p) \
42 { \
43 type **pp = lua_newuserdata(L, sizeof(type *)); \
44 *pp = p; \
45 type_ref(pp); \
46 return luaA_settype(L, lua_type); \
49 #define DO_LUA_GC(type, prefix, lua_type, type_unref) \
50 static int \
51 luaA_##prefix##_gc(lua_State *L) \
52 { \
53 type **p = luaA_checkudata(L, 1, lua_type); \
54 type_unref(p); \
55 *p = NULL; \
56 return 0; \
59 #define DO_LUA_EQ(type, prefix, lua_type) \
60 static int \
61 luaA_##prefix##_eq(lua_State *L) \
62 { \
63 type **p1 = luaA_checkudata(L, 1, lua_type); \
64 type **p2 = luaA_checkudata(L, 2, lua_type); \
65 lua_pushboolean(L, (*p1 == *p2)); \
66 return 1; \
69 #define luaA_dostring(L, cmd) \
70 do { \
71 if(a_strlen(cmd)) \
72 if(luaL_dostring(L, cmd)) \
73 warn("error executing Lua code: %s", \
74 lua_tostring(L, -1)); \
75 } while(0)
77 #define luaA_checktable(L, n) \
78 do { \
79 if(!lua_istable(L, n)) \
80 luaL_typerror(L, n, "table"); \
81 } while(0)
83 #define luaA_checkfunction(L, n) \
84 do { \
85 if(!lua_isfunction(L, n)) \
86 luaL_typerror(L, n, "function"); \
87 } while(0)
89 #define luaA_checkscreen(screen) \
90 do { \
91 if(screen < 0 || screen >= globalconf.nscreen) \
92 luaL_error(L, "invalid screen number: %d", screen + 1); \
93 } while(0)
95 /** Check that an object is not a NULL reference.
96 * \param L The Lua state.
97 * \param ud The index of the object in the stack.
98 * \param tname The type name.
99 * \return A pointer to the object.
101 static inline void *
102 luaA_checkudata(lua_State *L, int ud, const char *tname)
104 void **p = luaL_checkudata(L, ud, tname);
105 if(*p)
106 return p;
107 luaL_error(L, "invalid object");
108 return NULL;
111 static inline bool
112 luaA_checkboolean(lua_State *L, int n)
114 if(!lua_isboolean(L, n))
115 luaL_typerror(L, n, "boolean");
116 return lua_toboolean(L, n);
119 static inline bool
120 luaA_optboolean(lua_State *L, int idx, bool def)
122 return luaL_opt(L, luaA_checkboolean, idx, def);
125 static inline lua_Number
126 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
128 lua_getfield(L, idx, name);
129 return luaL_optnumber(L, -1, def);
132 static inline const char *
133 luaA_getopt_lstring(lua_State *L, int idx, const char *name, const char *def, size_t *len)
135 lua_getfield(L, idx, name);
136 return luaL_optlstring(L, -1, def, len);
139 static inline const char *
140 luaA_getopt_string(lua_State *L, int idx, const char *name, const char *def)
142 return luaA_getopt_lstring(L, idx, name, def, NULL);
145 static inline bool
146 luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
148 lua_getfield(L, idx, name);
149 return luaA_optboolean(L, -1, def);
152 static inline int
153 luaA_settype(lua_State *L, const char *type)
155 luaL_getmetatable(L, type);
156 lua_setmetatable(L, -2);
157 return 1;
160 /** Push a area type to a table on stack.
161 * \param L The Lua VM state.
162 * \param geometry The area geometry to push.
163 * \return The number of elements pushed on stack.
165 static inline int
166 luaA_pusharea(lua_State *L, area_t geometry)
168 lua_newtable(L);
169 lua_pushnumber(L, geometry.x);
170 lua_setfield(L, -2, "x");
171 lua_pushnumber(L, geometry.y);
172 lua_setfield(L, -2, "y");
173 lua_pushnumber(L, geometry.width);
174 lua_setfield(L, -2, "width");
175 lua_pushnumber(L, geometry.height);
176 lua_setfield(L, -2, "height");
177 return 1;
180 static inline int
181 luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
183 lua_getmetatable(L, idxobj);
184 lua_pushvalue(L, idxfield);
185 lua_rawget(L, -2);
186 if(!lua_isnil(L, -1))
188 lua_remove(L, -2);
189 return 1;
191 lua_pop(L, 2);
193 return 0;
196 /** Register a function.
197 * \param L The Lua stack.
198 * \param idx Index of the function in the stack.
199 * \param fct A luaA_ref address: it will be filled with the luaA_ref
200 * registered. If the adresse point to an already registered function, it will
201 * be unregistered.
202 * \return Always 0.
204 static inline int
205 luaA_registerfct(lua_State *L, int idx, luaA_ref *fct)
207 luaA_checkfunction(L, idx);
208 lua_pushvalue(L, idx);
209 if(*fct != LUA_REFNIL)
210 luaL_unref(L, LUA_REGISTRYINDEX, *fct);
211 *fct = luaL_ref(L, LUA_REGISTRYINDEX);
212 return 0;
215 /** Execute an Lua function.
216 * \param L The Lua stack.
217 * \param f The Lua function to execute.
218 * \param nargs The number of arguments for the Lua function.
219 * \param nret The number of returned value from the Lua function.
220 * \return True on no error, false otherwise.
222 static inline bool
223 luaA_dofunction(lua_State *L, luaA_ref f, int nargs, int nret)
225 if(f != LUA_REFNIL)
227 lua_rawgeti(L, LUA_REGISTRYINDEX, f);
228 if(nargs)
229 lua_insert(L, - (nargs + 1));
230 if(lua_pcall(L, nargs, nret, 0))
232 warn("error running function: %s",
233 lua_tostring(L, -1));
234 lua_pop(L, 1);
235 return false;
237 return true;
239 return false;
242 int luaA_otable_index(lua_State *);
244 /** Create a new object table with a metatable.
245 * This is useful to compare table with objects (udata) as keys.
246 * \param L The Lua stack.
247 * \return The number of elements pushed on stack.
249 static inline int
250 luaA_otable_new(lua_State *L)
252 /* Our object */
253 lua_newtable(L);
254 return luaA_settype(L, "otable");
257 void luaA_init(void);
258 bool luaA_parserc(const char *, bool);
259 void luaA_cs_init(void);
260 void luaA_cs_cleanup(void);
261 void luaA_on_timer(EV_P_ ev_timer *w, int revents);
262 void luaA_pushcolor(lua_State *, const xcolor_t *);
264 static inline int
265 luaA_generic_pairs(lua_State *L)
267 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
268 lua_pushvalue(L, 1); /* state, */
269 lua_pushnil(L); /* and initial value */
270 return 3;
273 #define hooks_property(c, prop) \
274 do { \
275 luaA_client_userdata_new(globalconf.L, c); \
276 lua_pushliteral(globalconf.L, prop); \
277 luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
278 } while(0);
280 #endif
281 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80