scan: fix array indice
[awesome.git] / lua.h
blob93e715e03d9a267a418d7bdf3d18edaabd20c34e
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 "common/util.h"
30 #include "common/draw.h"
32 /** Object types */
33 typedef enum
35 AWESOME_TYPE_STATUSBAR = 1,
36 AWESOME_TYPE_TITLEBAR
37 } awesome_type_t;
39 /** Type for Lua function */
40 typedef int luaA_ref;
42 #define deprecate(string, ...) _warn(__LINE__, \
43 __FUNCTION__, \
44 "This function is deprecated and will be removed.")
46 #define DO_LUA_NEW(decl, type, prefix, lua_type, type_ref) \
47 decl int \
48 luaA_##prefix##_userdata_new(lua_State *L, type *p) \
49 { \
50 type **pp = lua_newuserdata(L, sizeof(type *)); \
51 *pp = p; \
52 type_ref(pp); \
53 return luaA_settype(L, lua_type); \
56 #define DO_LUA_GC(type, prefix, lua_type, type_unref) \
57 static int \
58 luaA_##prefix##_gc(lua_State *L) \
59 { \
60 type **p = luaA_checkudata(L, 1, lua_type); \
61 type_unref(p); \
62 *p = NULL; \
63 return 0; \
66 #define DO_LUA_EQ(type, prefix, lua_type) \
67 static int \
68 luaA_##prefix##_eq(lua_State *L) \
69 { \
70 type **p1 = luaA_checkudata(L, 1, lua_type); \
71 type **p2 = luaA_checkudata(L, 2, lua_type); \
72 lua_pushboolean(L, (*p1 == *p2)); \
73 return 1; \
76 #define luaA_dostring(L, cmd) \
77 do { \
78 if(a_strlen(cmd)) \
79 if(luaL_dostring(L, cmd)) \
80 warn("error executing Lua code: %s", \
81 lua_tostring(L, -1)); \
82 } while(0)
84 #define luaA_checktable(L, n) \
85 do { \
86 if(!lua_istable(L, n)) \
87 luaL_typerror(L, n, "table"); \
88 } while(0)
90 #define luaA_checkfunction(L, n) \
91 do { \
92 if(!lua_isfunction(L, n)) \
93 luaL_typerror(L, n, "function"); \
94 } while(0)
96 #define luaA_checkscreen(screen) \
97 do { \
98 if(screen < 0 || screen >= globalconf.screens_info->nscreen) \
99 luaL_error(L, "invalid screen number: %d", screen + 1); \
100 } while(0)
102 /** Check that an object is not a NULL reference.
103 * \param L The Lua state.
104 * \param ud The index of the object in the stack.
105 * \param tname The type name.
106 * \return A pointer to the object.
108 static inline void *
109 luaA_checkudata(lua_State *L, int ud, const char *tname)
111 void **p = luaL_checkudata(L, ud, tname);
112 if(*p)
113 return p;
114 luaL_error(L, "invalid object");
115 return NULL;
118 static inline bool
119 luaA_checkboolean(lua_State *L, int n)
121 if(!lua_isboolean(L, n))
122 luaL_typerror(L, n, "boolean");
123 return lua_toboolean(L, n);
126 static inline bool
127 luaA_optboolean(lua_State *L, int idx, bool def)
129 return luaL_opt(L, luaA_checkboolean, idx, def);
132 static inline lua_Number
133 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
135 lua_getfield(L, idx, name);
136 return luaL_optnumber(L, -1, def);
139 static inline const char *
140 luaA_getopt_lstring(lua_State *L, int idx, const char *name, const char *def, size_t *len)
142 lua_getfield(L, idx, name);
143 return luaL_optlstring(L, -1, def, len);
146 static inline const char *
147 luaA_getopt_string(lua_State *L, int idx, const char *name, const char *def)
149 return luaA_getopt_lstring(L, idx, name, def, NULL);
152 static inline bool
153 luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
155 lua_getfield(L, idx, name);
156 return luaA_optboolean(L, -1, def);
159 static inline int
160 luaA_settype(lua_State *L, const char *type)
162 luaL_getmetatable(L, type);
163 lua_setmetatable(L, -2);
164 return 1;
167 static inline int
168 luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
170 lua_getmetatable(L, idxobj);
171 lua_pushvalue(L, idxfield);
172 lua_rawget(L, -2);
173 if(!lua_isnil(L, -1))
175 lua_remove(L, -2);
176 return 1;
178 lua_pop(L, 2);
180 return 0;
183 /** Register a function.
184 * \param L The Lua stack.
185 * \param fct A luaA_ref address: it will be filled with the luaA_ref
186 * registered. If the adresse point to an already registered function, it will
187 * be unregistered.
188 * \return Always 0.
190 static inline int
191 luaA_registerfct(lua_State *L, luaA_ref *fct)
193 luaA_checkfunction(L, -1);
194 if(*fct != LUA_REFNIL)
195 luaL_unref(L, LUA_REGISTRYINDEX, *fct);
196 *fct = luaL_ref(L, LUA_REGISTRYINDEX);
197 return 0;
200 /** Execute an Lua function.
201 * \param L The Lua stack.
202 * \param f The Lua function to execute.
203 * \param nargs The number of arguments for the Lua function.
204 * \param nret The number of returned value from the Lua function.
205 * \return True on no error, false otherwise.
207 static inline bool
208 luaA_dofunction(lua_State *L, luaA_ref f, int nargs, int nret)
210 if(f != LUA_REFNIL)
212 lua_rawgeti(L, LUA_REGISTRYINDEX, f);
213 if(nargs)
214 lua_insert(L, - (nargs + 1));
215 if(lua_pcall(L, nargs, nret, 0))
217 warn("error running function: %s",
218 lua_tostring(L, -1));
219 lua_pop(L, 1);
220 return false;
222 return true;
224 return false;
227 int luaA_otable_index(lua_State *);
229 /** Create a new object table with a metatable.
230 * This is useful to compare table with objects (udata) as keys.
231 * \param L The Lua stack.
232 * \return The number of elements pushed on stack.
234 static inline int
235 luaA_otable_new(lua_State *L)
237 /* Our object */
238 lua_newtable(L);
239 return luaA_settype(L, "otable");
242 void luaA_init(void);
243 void luaA_parserc(const char *);
244 void luaA_pushpointer(lua_State *, void *, awesome_type_t);
245 void luaA_cs_init(void);
246 void luaA_cs_cleanup(void);
247 void luaA_on_timer(EV_P_ ev_timer *w, int revents);
248 void luaA_pushcolor(lua_State *, const xcolor_t *);
250 static inline int
251 luaA_generic_pairs(lua_State *L)
253 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
254 lua_pushvalue(L, 1); /* state, */
255 lua_pushnil(L); /* and initial value */
256 return 3;
259 #endif
260 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80