manage signal handler: use awesome.startup
[awesome.git] / luaa.h
blob79927975945818ebb20c2d38f9ba09cc439158f2
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 lua_Number
132 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
134 lua_getfield(L, idx, name);
135 if (lua_isnil(L, -1) || lua_isnumber(L, -1))
136 def = luaL_optnumber(L, -1, def);
137 lua_pop(L, 1);
138 return def;
141 /** Push a area type to a table on stack.
142 * \param L The Lua VM state.
143 * \param geometry The area geometry to push.
144 * \return The number of elements pushed on stack.
146 static inline int
147 luaA_pusharea(lua_State *L, area_t geometry)
149 lua_createtable(L, 0, 4);
150 lua_pushnumber(L, geometry.x);
151 lua_setfield(L, -2, "x");
152 lua_pushnumber(L, geometry.y);
153 lua_setfield(L, -2, "y");
154 lua_pushnumber(L, geometry.width);
155 lua_setfield(L, -2, "width");
156 lua_pushnumber(L, geometry.height);
157 lua_setfield(L, -2, "height");
158 return 1;
161 /** Register an Lua object.
162 * \param L The Lua stack.
163 * \param idx Index of the object in the stack.
164 * \param ref A int address: it will be filled with the int
165 * registered. If the address points to an already registered object, it will
166 * be unregistered.
167 * \return Always 0.
169 static inline int
170 luaA_register(lua_State *L, int idx, int *ref)
172 lua_pushvalue(L, idx);
173 if(*ref != LUA_REFNIL)
174 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
175 *ref = luaL_ref(L, LUA_REGISTRYINDEX);
176 return 0;
179 /** Unregister a Lua object.
180 * \param L The Lua stack.
181 * \param ref A reference to an Lua object.
183 static inline void
184 luaA_unregister(lua_State *L, int *ref)
186 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
187 *ref = LUA_REFNIL;
190 /** Register a function.
191 * \param L The Lua stack.
192 * \param idx Index of the function in the stack.
193 * \param fct A int address: it will be filled with the int
194 * registered. If the address points to an already registered function, it will
195 * be unregistered.
196 * \return luaA_register value.
198 static inline int
199 luaA_registerfct(lua_State *L, int idx, int *fct)
201 luaA_checkfunction(L, idx);
202 return luaA_register(L, idx, fct);
205 void luaA_init(xdgHandle *);
206 bool luaA_parserc(xdgHandle *, const char *, bool);
208 /** Global signals */
209 signal_array_t global_signals;
211 int luaA_class_index_miss_property(lua_State *, lua_object_t *);
212 int luaA_class_newindex_miss_property(lua_State *, lua_object_t *);
213 int luaA_default_index(lua_State *);
214 int luaA_default_newindex(lua_State *);
216 void luaA_systray_invalidate(void);
218 #endif
219 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80