Merge branch 'fix-ldoc-set-spacing' of git://github.com/actionless/awesome
[awesome.git] / luaa.h
blob9576908bd68ea2dfa07776a9527881412927bc32
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 /** Print a warning about some Lua code.
43 * This is less mean than luaL_error() which setjmp via lua_error() and kills
44 * everything. This only warn, it's up to you to then do what's should be done.
45 * \param L The Lua VM state.
46 * \param fmt The warning message.
48 static inline void __attribute__ ((format(printf, 2, 3)))
49 luaA_warn(lua_State *L, const char *fmt, ...)
51 va_list ap;
52 luaL_where(L, 1);
53 fprintf(stderr, "%sW: ", lua_tostring(L, -1));
54 lua_pop(L, 1);
55 va_start(ap, fmt);
56 vfprintf(stderr, fmt, ap);
57 va_end(ap);
58 fprintf(stderr, "\n");
61 static inline int
62 luaA_typerror(lua_State *L, int narg, const char *tname)
64 const char *msg = lua_pushfstring(L, "%s expected, got %s",
65 tname, luaL_typename(L, narg));
66 return luaL_argerror(L, narg, msg);
69 static inline void
70 luaA_getuservalue(lua_State *L, int idx)
72 #if LUA_VERSION_NUM >= 502
73 lua_getuservalue(L, idx);
74 #else
75 lua_getfenv(L, idx);
76 #endif
79 static inline void
80 luaA_setuservalue(lua_State *L, int idx)
82 #if LUA_VERSION_NUM >= 502
83 lua_setuservalue(L, idx);
84 #else
85 lua_setfenv(L, idx);
86 #endif
89 static inline size_t
90 luaA_rawlen(lua_State *L, int idx)
92 #if LUA_VERSION_NUM >= 502
93 return lua_rawlen(L, idx);
94 #else
95 return lua_objlen(L, idx);
96 #endif
99 static inline void
100 luaA_registerlib(lua_State *L, const char *libname, const luaL_Reg *l)
102 #if LUA_VERSION_NUM >= 502
103 if (libname)
105 lua_newtable(L);
106 luaL_setfuncs(L, l, 0);
107 lua_pushvalue(L, -1);
108 lua_setglobal(L, libname);
110 else
111 luaL_setfuncs(L, l, 0);
112 #else
113 luaL_register(L, libname, l);
114 #endif
117 static inline bool
118 luaA_checkboolean(lua_State *L, int n)
120 if(!lua_isboolean(L, n))
121 luaA_typerror(L, n, "boolean");
122 return lua_toboolean(L, n);
125 static inline lua_Number
126 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
128 lua_getfield(L, idx, name);
129 if (lua_isnil(L, -1) || lua_isnumber(L, -1))
130 def = luaL_optnumber(L, -1, def);
131 lua_pop(L, 1);
132 return def;
135 /** Push a area type to a table on stack.
136 * \param L The Lua VM state.
137 * \param geometry The area geometry to push.
138 * \return The number of elements pushed on stack.
140 static inline int
141 luaA_pusharea(lua_State *L, area_t geometry)
143 lua_createtable(L, 0, 4);
144 lua_pushnumber(L, geometry.x);
145 lua_setfield(L, -2, "x");
146 lua_pushnumber(L, geometry.y);
147 lua_setfield(L, -2, "y");
148 lua_pushnumber(L, geometry.width);
149 lua_setfield(L, -2, "width");
150 lua_pushnumber(L, geometry.height);
151 lua_setfield(L, -2, "height");
152 return 1;
155 /** Register an Lua object.
156 * \param L The Lua stack.
157 * \param idx Index of the object in the stack.
158 * \param ref A int address: it will be filled with the int
159 * registered. If the address points to an already registered object, it will
160 * be unregistered.
161 * \return Always 0.
163 static inline int
164 luaA_register(lua_State *L, int idx, int *ref)
166 lua_pushvalue(L, idx);
167 if(*ref != LUA_REFNIL)
168 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
169 *ref = luaL_ref(L, LUA_REGISTRYINDEX);
170 return 0;
173 /** Unregister a Lua object.
174 * \param L The Lua stack.
175 * \param ref A reference to an Lua object.
177 static inline void
178 luaA_unregister(lua_State *L, int *ref)
180 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
181 *ref = LUA_REFNIL;
184 /** Register a function.
185 * \param L The Lua stack.
186 * \param idx Index of the function in the stack.
187 * \param fct A int address: it will be filled with the int
188 * registered. If the address points to an already registered function, it will
189 * be unregistered.
190 * \return luaA_register value.
192 static inline int
193 luaA_registerfct(lua_State *L, int idx, int *fct)
195 luaA_checkfunction(L, idx);
196 return luaA_register(L, idx, fct);
199 void luaA_init(xdgHandle *);
200 bool luaA_parserc(xdgHandle *, const char *, bool);
202 /** Global signals */
203 signal_array_t global_signals;
205 int luaA_class_index_miss_property(lua_State *, lua_object_t *);
206 int luaA_class_newindex_miss_property(lua_State *, lua_object_t *);
207 int luaA_default_index(lua_State *);
208 int luaA_default_newindex(lua_State *);
210 void luaA_systray_invalidate(void);
212 #endif
213 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80