Fix warnings from LDoc
[awesome.git] / luaa.h
blobd871964a19d93fbf3531797d679246e5bd1f3d16
1 /*
2 * luaa.h - Lua configuration management header
4 * Copyright © 2008-2009 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 <basedir.h>
32 #include "draw.h"
33 #include "common/lualib.h"
34 #include "common/luaclass.h"
36 #define luaA_deprecate(L, repl) \
37 do { \
38 luaA_warn(L, "%s: This function is deprecated and will be removed, see %s", \
39 __FUNCTION__, repl); \
40 lua_pushlstring(L, __FUNCTION__, sizeof(__FUNCTION__)); \
41 signal_object_emit(L, &global_signals, "debug::deprecation", 1); \
42 } while(0)
44 #define luaA_checkscreen(screen) \
45 do { \
46 if(screen < 0 || screen >= globalconf.screens.len) \
47 luaL_error(L, "invalid screen number: %d", screen + 1); \
48 } while(0)
50 /** Print a warning about some Lua code.
51 * This is less mean than luaL_error() which setjmp via lua_error() and kills
52 * everything. This only warn, it's up to you to then do what's should be done.
53 * \param L The Lua VM state.
54 * \param fmt The warning message.
56 static inline void __attribute__ ((format(printf, 2, 3)))
57 luaA_warn(lua_State *L, const char *fmt, ...)
59 va_list ap;
60 luaL_where(L, 1);
61 fprintf(stderr, "%sW: ", lua_tostring(L, -1));
62 lua_pop(L, 1);
63 va_start(ap, fmt);
64 vfprintf(stderr, fmt, ap);
65 va_end(ap);
66 fprintf(stderr, "\n");
69 static inline int
70 luaA_typerror(lua_State *L, int narg, const char *tname)
72 const char *msg = lua_pushfstring(L, "%s expected, got %s",
73 tname, luaL_typename(L, narg));
74 return luaL_argerror(L, narg, msg);
77 static inline void
78 luaA_getuservalue(lua_State *L, int idx)
80 #if LUA_VERSION_NUM >= 502
81 lua_getuservalue(L, idx);
82 #else
83 lua_getfenv(L, idx);
84 #endif
87 static inline void
88 luaA_setuservalue(lua_State *L, int idx)
90 #if LUA_VERSION_NUM >= 502
91 lua_setuservalue(L, idx);
92 #else
93 lua_setfenv(L, idx);
94 #endif
97 static inline size_t
98 luaA_rawlen(lua_State *L, int idx)
100 #if LUA_VERSION_NUM >= 502
101 return lua_rawlen(L, idx);
102 #else
103 return lua_objlen(L, idx);
104 #endif
107 static inline void
108 luaA_registerlib(lua_State *L, const char *libname, const luaL_Reg *l)
110 #if LUA_VERSION_NUM >= 502
111 if (libname)
113 lua_newtable(L);
114 luaL_setfuncs(L, l, 0);
115 lua_pushvalue(L, -1);
116 lua_setglobal(L, libname);
118 else
119 luaL_setfuncs(L, l, 0);
120 #else
121 luaL_register(L, libname, l);
122 #endif
125 static inline bool
126 luaA_checkboolean(lua_State *L, int n)
128 if(!lua_isboolean(L, n))
129 luaA_typerror(L, n, "boolean");
130 return lua_toboolean(L, n);
133 static inline bool
134 luaA_optboolean(lua_State *L, int idx, bool def)
136 return luaL_opt(L, luaA_checkboolean, idx, def);
139 static inline lua_Number
140 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
142 lua_getfield(L, idx, name);
143 if (lua_isnil(L, -1) || lua_isnumber(L, -1))
144 def = luaL_optnumber(L, -1, def);
145 lua_pop(L, 1);
146 return def;
149 static inline const char *
150 luaA_getopt_lstring(lua_State *L, int idx, const char *name, const char *def, size_t *len)
152 lua_getfield(L, idx, name);
153 const char *s = luaL_optlstring(L, -1, def, len);
154 lua_pop(L, 1);
155 return s;
158 static inline bool
159 luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
161 lua_getfield(L, idx, name);
162 bool b = luaA_optboolean(L, -1, def);
163 lua_pop(L, 1);
164 return b;
167 /** Push a area type to a table on stack.
168 * \param L The Lua VM state.
169 * \param geometry The area geometry to push.
170 * \return The number of elements pushed on stack.
172 static inline int
173 luaA_pusharea(lua_State *L, area_t geometry)
175 lua_createtable(L, 0, 4);
176 lua_pushnumber(L, geometry.x);
177 lua_setfield(L, -2, "x");
178 lua_pushnumber(L, geometry.y);
179 lua_setfield(L, -2, "y");
180 lua_pushnumber(L, geometry.width);
181 lua_setfield(L, -2, "width");
182 lua_pushnumber(L, geometry.height);
183 lua_setfield(L, -2, "height");
184 return 1;
187 /** Register an Lua object.
188 * \param L The Lua stack.
189 * \param idx Index of the object in the stack.
190 * \param ref A int address: it will be filled with the int
191 * registered. If the address points to an already registered object, it will
192 * be unregistered.
193 * \return Always 0.
195 static inline int
196 luaA_register(lua_State *L, int idx, int *ref)
198 lua_pushvalue(L, idx);
199 if(*ref != LUA_REFNIL)
200 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
201 *ref = luaL_ref(L, LUA_REGISTRYINDEX);
202 return 0;
205 /** Unregister a Lua object.
206 * \param L The Lua stack.
207 * \param ref A reference to an Lua object.
209 static inline void
210 luaA_unregister(lua_State *L, int *ref)
212 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
213 *ref = LUA_REFNIL;
216 /** Register a function.
217 * \param L The Lua stack.
218 * \param idx Index of the function in the stack.
219 * \param fct A int address: it will be filled with the int
220 * registered. If the address points to an already registered function, it will
221 * be unregistered.
222 * \return luaA_register value.
224 static inline int
225 luaA_registerfct(lua_State *L, int idx, int *fct)
227 luaA_checkfunction(L, idx);
228 return luaA_register(L, idx, fct);
231 /** Grab a function from the registry and execute it.
232 * \param L The Lua stack.
233 * \param ref The function reference.
234 * \param nargs The number of arguments for the Lua function.
235 * \param nret The number of returned value from the Lua function.
236 * \return True on no error, false otherwise.
238 static inline bool
239 luaA_dofunction_from_registry(lua_State *L, int ref, int nargs, int nret)
241 lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
242 return luaA_dofunction(L, nargs, nret);
245 void luaA_init(xdgHandle *);
246 bool luaA_parserc(xdgHandle *, const char *, bool);
247 bool luaA_hasitem(lua_State *, const void *);
248 bool luaA_isloop(lua_State *, int);
250 /** Global signals */
251 signal_array_t global_signals;
253 int luaA_class_index_miss_property(lua_State *, lua_object_t *);
254 int luaA_class_newindex_miss_property(lua_State *, lua_object_t *);
256 void luaA_systray_invalidate(void);
258 #endif
259 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80