awful.rules: add does_match and matching_rules functions (FS#1224)
[awesome.git] / luaa.h
blob7cef3403d95a368ee3d70e50027feb139ae8aaf4
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 <lua.h>
26 #include <lauxlib.h>
28 #include <basedir.h>
30 #include "draw.h"
31 #include "common/lualib.h"
32 #include "common/luaclass.h"
34 #define luaA_deprecate(L, repl) \
35 do { \
36 luaA_warn(L, "%s: This function is deprecated and will be removed, see %s", \
37 __FUNCTION__, repl); \
38 lua_pushlstring(L, __FUNCTION__, sizeof(__FUNCTION__)); \
39 signal_object_emit(L, &global_signals, "debug::deprecation", 1); \
40 } while(0)
42 #define luaA_checkscreen(screen) \
43 do { \
44 if(screen < 0 || screen >= globalconf.screens.len) \
45 luaL_error(L, "invalid screen number: %d", screen + 1); \
46 } while(0)
48 /** Print a warning about some Lua code.
49 * This is less mean than luaL_error() which setjmp via lua_error() and kills
50 * everything. This only warn, it's up to you to then do what's should be done.
51 * \param L The Lua VM state.
52 * \param fmt The warning message.
54 static inline void __attribute__ ((format(printf, 2, 3)))
55 luaA_warn(lua_State *L, const char *fmt, ...)
57 va_list ap;
58 luaL_where(L, 1);
59 fprintf(stderr, "%sW: ", lua_tostring(L, -1));
60 lua_pop(L, 1);
61 va_start(ap, fmt);
62 vfprintf(stderr, fmt, ap);
63 va_end(ap);
64 fprintf(stderr, "\n");
67 static inline int
68 luaA_typerror(lua_State *L, int narg, const char *tname)
70 const char *msg = lua_pushfstring(L, "%s expected, got %s",
71 tname, luaL_typename(L, narg));
72 return luaL_argerror(L, narg, msg);
75 static inline void
76 luaA_getuservalue(lua_State *L, int idx)
78 #if LUA_VERSION_NUM >= 502
79 lua_getuservalue(L, idx);
80 #else
81 lua_getfenv(L, idx);
82 #endif
85 static inline void
86 luaA_setuservalue(lua_State *L, int idx)
88 #if LUA_VERSION_NUM >= 502
89 lua_setuservalue(L, idx);
90 #else
91 lua_setfenv(L, idx);
92 #endif
95 static inline size_t
96 luaA_rawlen(lua_State *L, int idx)
98 #if LUA_VERSION_NUM >= 502
99 return lua_rawlen(L, idx);
100 #else
101 return lua_objlen(L, idx);
102 #endif
105 static inline void
106 luaA_registerlib(lua_State *L, const char *libname, const luaL_Reg *l)
108 #if LUA_VERSION_NUM >= 502
109 if (libname)
111 lua_newtable(L);
112 luaL_setfuncs(L, l, 0);
113 lua_pushvalue(L, -1);
114 lua_setglobal(L, libname);
116 else
117 luaL_setfuncs(L, l, 0);
118 #else
119 luaL_register(L, libname, l);
120 #endif
123 static inline bool
124 luaA_checkboolean(lua_State *L, int n)
126 if(!lua_isboolean(L, n))
127 luaA_typerror(L, n, "boolean");
128 return lua_toboolean(L, n);
131 static inline bool
132 luaA_optboolean(lua_State *L, int idx, bool def)
134 return luaL_opt(L, luaA_checkboolean, idx, def);
137 static inline lua_Number
138 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
140 lua_getfield(L, idx, name);
141 if (lua_isnil(L, -1) || lua_isnumber(L, -1))
142 def = luaL_optnumber(L, -1, def);
143 lua_pop(L, 1);
144 return def;
147 static inline const char *
148 luaA_getopt_lstring(lua_State *L, int idx, const char *name, const char *def, size_t *len)
150 lua_getfield(L, idx, name);
151 const char *s = luaL_optlstring(L, -1, def, len);
152 lua_pop(L, 1);
153 return s;
156 static inline bool
157 luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
159 lua_getfield(L, idx, name);
160 bool b = luaA_optboolean(L, -1, def);
161 lua_pop(L, 1);
162 return b;
165 /** Push a area type to a table on stack.
166 * \param L The Lua VM state.
167 * \param geometry The area geometry to push.
168 * \return The number of elements pushed on stack.
170 static inline int
171 luaA_pusharea(lua_State *L, area_t geometry)
173 lua_createtable(L, 0, 4);
174 lua_pushnumber(L, geometry.x);
175 lua_setfield(L, -2, "x");
176 lua_pushnumber(L, geometry.y);
177 lua_setfield(L, -2, "y");
178 lua_pushnumber(L, geometry.width);
179 lua_setfield(L, -2, "width");
180 lua_pushnumber(L, geometry.height);
181 lua_setfield(L, -2, "height");
182 return 1;
185 /** Register an Lua object.
186 * \param L The Lua stack.
187 * \param idx Index of the object in the stack.
188 * \param ref A int address: it will be filled with the int
189 * registered. If the address points to an already registered object, it will
190 * be unregistered.
191 * \return Always 0.
193 static inline int
194 luaA_register(lua_State *L, int idx, int *ref)
196 lua_pushvalue(L, idx);
197 if(*ref != LUA_REFNIL)
198 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
199 *ref = luaL_ref(L, LUA_REGISTRYINDEX);
200 return 0;
203 /** Unregister a Lua object.
204 * \param L The Lua stack.
205 * \param ref A reference to an Lua object.
207 static inline void
208 luaA_unregister(lua_State *L, int *ref)
210 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
211 *ref = LUA_REFNIL;
214 /** Register a function.
215 * \param L The Lua stack.
216 * \param idx Index of the function in the stack.
217 * \param fct A int address: it will be filled with the int
218 * registered. If the address points to an already registered function, it will
219 * be unregistered.
220 * \return luaA_register value.
222 static inline int
223 luaA_registerfct(lua_State *L, int idx, int *fct)
225 luaA_checkfunction(L, idx);
226 return luaA_register(L, idx, fct);
229 /** Grab a function from the registry and execute it.
230 * \param L The Lua stack.
231 * \param ref The function reference.
232 * \param nargs The number of arguments for the Lua function.
233 * \param nret The number of returned value from the Lua function.
234 * \return True on no error, false otherwise.
236 static inline bool
237 luaA_dofunction_from_registry(lua_State *L, int ref, int nargs, int nret)
239 lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
240 return luaA_dofunction(L, nargs, nret);
243 void luaA_init(xdgHandle *);
244 bool luaA_parserc(xdgHandle *, const char *, bool);
245 bool luaA_hasitem(lua_State *, const void *);
246 bool luaA_isloop(lua_State *, int);
248 /** Global signals */
249 signal_array_t global_signals;
251 int luaA_class_index_miss_property(lua_State *, lua_object_t *);
252 int luaA_class_newindex_miss_property(lua_State *, lua_object_t *);
254 void luaA_systray_invalidate(void);
256 #endif
257 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80