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.
31 #include "common/lualib.h"
32 #include "common/luaclass.h"
34 #define luaA_deprecate(L, repl) \
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); \
42 #define luaA_checkscreen(screen) \
44 if(screen < 0 || screen >= globalconf.screens.len) \
45 luaL_error(L, "invalid screen number: %d", screen + 1); \
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
, ...)
59 fprintf(stderr
, "%sW: ", lua_tostring(L
, -1));
62 vfprintf(stderr
, fmt
, ap
);
64 fprintf(stderr
, "\n");
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
);
76 luaA_getuservalue(lua_State
*L
, int idx
)
78 #if LUA_VERSION_NUM >= 502
79 lua_getuservalue(L
, idx
);
86 luaA_setuservalue(lua_State
*L
, int idx
)
88 #if LUA_VERSION_NUM >= 502
89 lua_setuservalue(L
, idx
);
96 luaA_rawlen(lua_State
*L
, int idx
)
98 #if LUA_VERSION_NUM >= 502
99 return lua_rawlen(L
, idx
);
101 return lua_objlen(L
, idx
);
106 luaA_registerlib(lua_State
*L
, const char *libname
, const luaL_Reg
*l
)
108 #if LUA_VERSION_NUM >= 502
112 luaL_setfuncs(L
, l
, 0);
113 lua_pushvalue(L
, -1);
114 lua_setglobal(L
, libname
);
117 luaL_setfuncs(L
, l
, 0);
119 luaL_register(L
, libname
, l
);
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
);
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
);
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
);
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
);
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.
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");
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
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
);
203 /** Unregister a Lua object.
204 * \param L The Lua stack.
205 * \param ref A reference to an Lua object.
208 luaA_unregister(lua_State
*L
, int *ref
)
210 luaL_unref(L
, LUA_REGISTRYINDEX
, *ref
);
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
220 * \return luaA_register value.
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.
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);
257 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80