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.
33 #include "common/lualib.h"
34 #include "common/luaclass.h"
36 #define luaA_deprecate(L, repl) \
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); \
44 #define luaA_checkscreen(screen) \
46 if(screen < 0 || screen >= globalconf.screens.len) \
47 luaL_error(L, "invalid screen number: %d", screen + 1); \
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
, ...)
61 fprintf(stderr
, "%sW: ", lua_tostring(L
, -1));
64 vfprintf(stderr
, fmt
, ap
);
66 fprintf(stderr
, "\n");
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
);
78 luaA_getuservalue(lua_State
*L
, int idx
)
80 #if LUA_VERSION_NUM >= 502
81 lua_getuservalue(L
, idx
);
88 luaA_setuservalue(lua_State
*L
, int idx
)
90 #if LUA_VERSION_NUM >= 502
91 lua_setuservalue(L
, idx
);
98 luaA_rawlen(lua_State
*L
, int idx
)
100 #if LUA_VERSION_NUM >= 502
101 return lua_rawlen(L
, idx
);
103 return lua_objlen(L
, idx
);
108 luaA_registerlib(lua_State
*L
, const char *libname
, const luaL_Reg
*l
)
110 #if LUA_VERSION_NUM >= 502
114 luaL_setfuncs(L
, l
, 0);
115 lua_pushvalue(L
, -1);
116 lua_setglobal(L
, libname
);
119 luaL_setfuncs(L
, l
, 0);
121 luaL_register(L
, libname
, l
);
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
);
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
);
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
);
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
);
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.
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");
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
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
);
205 /** Unregister a Lua object.
206 * \param L The Lua stack.
207 * \param ref A reference to an Lua object.
210 luaA_unregister(lua_State
*L
, int *ref
)
212 luaL_unref(L
, LUA_REGISTRYINDEX
, *ref
);
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
222 * \return luaA_register value.
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.
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);
259 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80