imagebox: remove valign
[awesome.git] / luaa.h
blobe6ef709fe6dea96a1acba3e429a631c71e18849d
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 <ev.h>
27 #include <lua.h>
28 #include <lauxlib.h>
30 #include <basedir.h>
32 #include "draw.h"
34 #define luaA_deprecate(L, repl) \
35 luaA_warn(L, "%s: This function is deprecated and will be removed, see %s", \
36 __FUNCTION__, repl)
38 #define DO_LUA_TOSTRING(type, prefix, lua_type) \
39 static int \
40 luaA_##prefix##_tostring(lua_State *L) \
41 { \
42 type *p = luaL_checkudata(L, 1, lua_type); \
43 lua_pushfstring(L, lua_type ": %p", p); \
44 return 1; \
47 #define luaA_checktable(L, n) \
48 do { \
49 if(!lua_istable(L, n)) \
50 luaL_typerror(L, n, "table"); \
51 } while(0)
53 #define luaA_checkscreen(screen) \
54 do { \
55 if(screen < 0 || screen >= globalconf.screens.len) \
56 luaL_error(L, "invalid screen number: %d", screen + 1); \
57 } while(0)
59 static inline bool
60 luaA_checkboolean(lua_State *L, int n)
62 if(!lua_isboolean(L, n))
63 luaL_typerror(L, n, "boolean");
64 return lua_toboolean(L, n);
67 static inline bool
68 luaA_optboolean(lua_State *L, int idx, bool def)
70 return luaL_opt(L, luaA_checkboolean, idx, def);
73 static inline lua_Number
74 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
76 lua_getfield(L, idx, name);
77 if (lua_isnil(L, -1) || lua_isnumber(L, -1))
78 def = luaL_optnumber(L, -1, def);
79 lua_pop(L, 1);
80 return def;
83 static inline const char *
84 luaA_getopt_lstring(lua_State *L, int idx, const char *name, const char *def, size_t *len)
86 lua_getfield(L, idx, name);
87 const char *s = luaL_optlstring(L, -1, def, len);
88 lua_pop(L, 1);
89 return s;
92 static inline bool
93 luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
95 lua_getfield(L, idx, name);
96 bool b = luaA_optboolean(L, -1, def);
97 lua_pop(L, 1);
98 return b;
101 /** Push a area type to a table on stack.
102 * \param L The Lua VM state.
103 * \param geometry The area geometry to push.
104 * \return The number of elements pushed on stack.
106 static inline int
107 luaA_pusharea(lua_State *L, area_t geometry)
109 lua_createtable(L, 0, 4);
110 lua_pushnumber(L, geometry.x);
111 lua_setfield(L, -2, "x");
112 lua_pushnumber(L, geometry.y);
113 lua_setfield(L, -2, "y");
114 lua_pushnumber(L, geometry.width);
115 lua_setfield(L, -2, "width");
116 lua_pushnumber(L, geometry.height);
117 lua_setfield(L, -2, "height");
118 return 1;
121 static inline int
122 luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
124 lua_getmetatable(L, idxobj);
125 lua_pushvalue(L, idxfield);
126 lua_rawget(L, -2);
127 if(!lua_isnil(L, -1))
129 lua_remove(L, -2);
130 return 1;
132 lua_pop(L, 2);
134 return 0;
137 /** Register an Lua object.
138 * \param L The Lua stack.
139 * \param idx Index of the object in the stack.
140 * \param ref A int address: it will be filled with the int
141 * registered. If the adresse point to an already registered object, it will
142 * be unregistered.
143 * \return Always 0.
145 static inline int
146 luaA_register(lua_State *L, int idx, int *ref)
148 lua_pushvalue(L, idx);
149 if(*ref != LUA_REFNIL)
150 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
151 *ref = luaL_ref(L, LUA_REGISTRYINDEX);
152 return 0;
155 /** Unregister a Lua object.
156 * \param L The Lua stack.
157 * \param ref A reference to an Lua object.
159 static inline void
160 luaA_unregister(lua_State *L, int *ref)
162 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
163 *ref = LUA_REFNIL;
166 /** Register a function.
167 * \param L The Lua stack.
168 * \param idx Index of the function in the stack.
169 * \param fct A int address: it will be filled with the int
170 * registered. If the adresse point to an already registered function, it will
171 * be unregistered.
172 * \return luaA_register value.
174 static inline int
175 luaA_registerfct(lua_State *L, int idx, int *fct)
177 luaA_checkfunction(L, idx);
178 return luaA_register(L, idx, fct);
181 /** Grab a function from the registry and execute it.
182 * \param L The Lua stack.
183 * \param ref The function reference.
184 * \param nargs The number of arguments for the Lua function.
185 * \param nret The number of returned value from the Lua function.
186 * \return True on no error, false otherwise.
188 static inline bool
189 luaA_dofunction_from_registry(lua_State *L, int ref, int nargs, int nret)
191 lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
192 return luaA_dofunction(L, nargs, nret);
195 /** Print a warning about some Lua code.
196 * This is less mean than luaL_error() which setjmp via lua_error() and kills
197 * everything. This only warn, it's up to you to then do what's should be done.
198 * \param L The Lua VM state.
199 * \param fmt The warning message.
201 static inline void __attribute__ ((format(printf, 2, 3)))
202 luaA_warn(lua_State *L, const char *fmt, ...)
204 va_list ap;
205 luaL_where(L, 1);
206 fprintf(stderr, "%sW: ", lua_tostring(L, -1));
207 lua_pop(L, 1);
208 va_start(ap, fmt);
209 vfprintf(stderr, fmt, ap);
210 va_end(ap);
211 fprintf(stderr, "\n");
214 /** Get an optional padding table from a Lua table.
215 * \param L The Lua VM state.
216 * \param idx The table index on the stack.
217 * \param dpadding The default padding value to use.
219 static inline padding_t
220 luaA_getopt_padding(lua_State *L, int idx, padding_t *dpadding)
222 padding_t padding;
224 luaA_checktable(L, idx);
226 padding.right = luaA_getopt_number(L, idx, "right", dpadding->right);
227 padding.left = luaA_getopt_number(L, idx, "left", dpadding->left);
228 padding.top = luaA_getopt_number(L, idx, "top", dpadding->top);
229 padding.bottom = luaA_getopt_number(L, idx, "bottom", dpadding->bottom);
231 return padding;
235 /** Push a padding structure into a table on the Lua stack.
236 * \param L The Lua VM state.
237 * \param padding The padding struct pointer.
238 * \return The number of elements pushed on stack.
240 static inline int
241 luaA_pushpadding(lua_State *L, padding_t *padding)
243 lua_createtable(L, 0, 4);
244 lua_pushnumber(L, padding->right);
245 lua_setfield(L, -2, "right");
246 lua_pushnumber(L, padding->left);
247 lua_setfield(L, -2, "left");
248 lua_pushnumber(L, padding->top);
249 lua_setfield(L, -2, "top");
250 lua_pushnumber(L, padding->bottom);
251 lua_setfield(L, -2, "bottom");
252 return 1;
255 void luaA_init(xdgHandle *);
256 bool luaA_parserc(xdgHandle *, const char *, bool);
257 void luaA_on_timer(EV_P_ ev_timer *, int);
258 bool luaA_hasitem(lua_State *, const void *);
259 void luaA_table2wtable(lua_State *);
260 int luaA_next(lua_State *, int);
261 bool luaA_isloop(lua_State *, int);
263 #define hook_property(type, obj, prop) \
264 do { \
265 if(globalconf.hooks.property != LUA_REFNIL) \
267 type##_push(globalconf.L, obj); \
268 lua_pushliteral(globalconf.L, prop); \
269 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.property, 2, 0); \
271 } while(0);
273 #endif
274 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80