keygrabber: use XStringToKeysym()
[awesome.git] / luaa.h
blob3eaad1bbee8cf17f409c6a1e45cfacf5b6ec4084
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_dostring(L, cmd) \
48 do { \
49 if(luaL_dostring(L, cmd)) \
50 warn("error executing Lua code: %s", \
51 lua_tostring(L, -1)); \
52 } while(0)
54 #define luaA_checktable(L, n) \
55 do { \
56 if(!lua_istable(L, n)) \
57 luaL_typerror(L, n, "table"); \
58 } while(0)
60 #define luaA_checkfunction(L, n) \
61 do { \
62 if(!lua_isfunction(L, n)) \
63 luaL_typerror(L, n, "function"); \
64 } while(0)
66 #define luaA_checkscreen(screen) \
67 do { \
68 if(screen < 0 || screen >= globalconf.screens.len) \
69 luaL_error(L, "invalid screen number: %d", screen + 1); \
70 } while(0)
72 /** Dump the Lua stack. Useful for debugging.
73 * \param L The Lua VM state.
75 static inline void
76 luaA_dumpstack(lua_State *L)
78 fprintf(stderr, "-------- Lua stack dump ---------\n");
79 for(int i = lua_gettop(L); i; i--)
81 int t = lua_type(L, i);
82 switch (t)
84 case LUA_TSTRING:
85 fprintf(stderr, "%d: string: `%s'\n", i, lua_tostring(L, i));
86 break;
87 case LUA_TBOOLEAN:
88 fprintf(stderr, "%d: bool: %s\n", i, lua_toboolean(L, i) ? "true" : "false");
89 break;
90 case LUA_TNUMBER:
91 fprintf(stderr, "%d: number: %g\n", i, lua_tonumber(L, i));
92 break;
93 case LUA_TNIL:
94 fprintf(stderr, "%d: nil\n", i);
95 break;
96 default:
97 fprintf(stderr, "%d: %s\t#%d\t%p\n", i, lua_typename(L, t),
98 (int) lua_objlen(L, i),
99 lua_topointer(L, i));
100 break;
103 fprintf(stderr, "------- Lua stack dump end ------\n");
106 /** Convert a object to a udata if possible.
107 * \param L The Lua VM state.
108 * \param ud The index.
109 * \param tname The type name.
110 * \return A pointer to the object, NULL otherwise.
112 static inline void *
113 luaA_toudata(lua_State *L, int ud, const char *tname)
115 void *p = lua_touserdata(L, ud);
116 if(p) /* value is a userdata? */
117 if(lua_getmetatable(L, ud)) /* does it have a metatable? */
119 lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
120 if(!lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
121 p = NULL;
122 lua_pop(L, 2); /* remove both metatables */
124 return p;
127 static inline bool
128 luaA_checkboolean(lua_State *L, int n)
130 if(!lua_isboolean(L, n))
131 luaL_typerror(L, n, "boolean");
132 return lua_toboolean(L, n);
135 static inline bool
136 luaA_optboolean(lua_State *L, int idx, bool def)
138 return luaL_opt(L, luaA_checkboolean, idx, def);
141 static inline lua_Number
142 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
144 lua_getfield(L, idx, name);
145 lua_Number n = luaL_optnumber(L, -1, def);
146 lua_pop(L, 1);
147 return n;
150 static inline const char *
151 luaA_getopt_lstring(lua_State *L, int idx, const char *name, const char *def, size_t *len)
153 lua_getfield(L, idx, name);
154 const char *s = luaL_optlstring(L, -1, def, len);
155 lua_pop(L, 1);
156 return s;
159 static inline bool
160 luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
162 lua_getfield(L, idx, name);
163 bool b = luaA_optboolean(L, -1, def);
164 lua_pop(L, 1);
165 return b;
168 /** Push a area type to a table on stack.
169 * \param L The Lua VM state.
170 * \param geometry The area geometry to push.
171 * \return The number of elements pushed on stack.
173 static inline int
174 luaA_pusharea(lua_State *L, area_t geometry)
176 lua_createtable(L, 0, 4);
177 lua_pushnumber(L, geometry.x);
178 lua_setfield(L, -2, "x");
179 lua_pushnumber(L, geometry.y);
180 lua_setfield(L, -2, "y");
181 lua_pushnumber(L, geometry.width);
182 lua_setfield(L, -2, "width");
183 lua_pushnumber(L, geometry.height);
184 lua_setfield(L, -2, "height");
185 return 1;
188 static inline int
189 luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
191 lua_getmetatable(L, idxobj);
192 lua_pushvalue(L, idxfield);
193 lua_rawget(L, -2);
194 if(!lua_isnil(L, -1))
196 lua_remove(L, -2);
197 return 1;
199 lua_pop(L, 2);
201 return 0;
204 /** Register an Lua object.
205 * \param L The Lua stack.
206 * \param idx Index of the object in the stack.
207 * \param ref A luaA_ref address: it will be filled with the luaA_ref
208 * registered. If the adresse point to an already registered object, it will
209 * be unregistered.
210 * \return Always 0.
212 static inline int
213 luaA_register(lua_State *L, int idx, luaA_ref *ref)
215 lua_pushvalue(L, idx);
216 if(*ref != LUA_REFNIL)
217 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
218 *ref = luaL_ref(L, LUA_REGISTRYINDEX);
219 return 0;
222 /** Unregister a Lua object.
223 * \param L The Lua stack.
224 * \param ref A reference to an Lua object.
226 static inline void
227 luaA_unregister(lua_State *L, luaA_ref *ref)
229 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
230 *ref = LUA_REFNIL;
233 /** Register a function.
234 * \param L The Lua stack.
235 * \param idx Index of the function in the stack.
236 * \param fct A luaA_ref address: it will be filled with the luaA_ref
237 * registered. If the adresse point to an already registered function, it will
238 * be unregistered.
239 * \return luaA_register value.
241 static inline int
242 luaA_registerfct(lua_State *L, int idx, luaA_ref *fct)
244 luaA_checkfunction(L, idx);
245 return luaA_register(L, idx, fct);
248 /** Execute an Lua function.
249 * \param L The Lua stack.
250 * \param f The Lua function to execute.
251 * \param nargs The number of arguments for the Lua function.
252 * \param nret The number of returned value from the Lua function.
253 * \return True on no error, false otherwise.
255 static inline bool
256 luaA_dofunction(lua_State *L, luaA_ref f, int nargs, int nret)
258 lua_rawgeti(L, LUA_REGISTRYINDEX, f);
259 if(nargs)
260 lua_insert(L, - (nargs + 1));
261 if(lua_pcall(L, nargs, nret, 0))
263 warn("error running function: %s",
264 lua_tostring(L, -1));
265 lua_pop(L, 1);
266 return false;
268 return true;
271 /** Print a warning about some Lua code.
272 * This is less mean than luaL_error() which setjmp via lua_error() and kills
273 * everything. This only warn, it's up to you to then do what's should be done.
274 * \param L The Lua VM state.
275 * \param fmt The warning message.
277 static inline void __attribute__ ((format(printf, 2, 3)))
278 luaA_warn(lua_State *L, const char *fmt, ...)
280 va_list ap;
281 luaL_where(L, 1);
282 fprintf(stderr, "%sW: ", lua_tostring(L, -1));
283 lua_pop(L, 1);
284 va_start(ap, fmt);
285 vfprintf(stderr, fmt, ap);
286 va_end(ap);
287 fprintf(stderr, "\n");
290 /** Get an optional padding table from a Lua table.
291 * \param L The Lua VM state.
292 * \param idx The table index on the stack.
293 * \param dpadding The default padding value to use.
295 static inline padding_t
296 luaA_getopt_padding(lua_State *L, int idx, padding_t *dpadding)
298 padding_t padding;
300 luaA_checktable(L, 2);
302 padding.right = luaA_getopt_number(L, 2, "right", dpadding->right);
303 padding.left = luaA_getopt_number(L, 2, "left", dpadding->left);
304 padding.top = luaA_getopt_number(L, 2, "top", dpadding->top);
305 padding.bottom = luaA_getopt_number(L, 2, "bottom", dpadding->bottom);
307 return padding;
311 /** Push a padding structure into a table on the Lua stack.
312 * \param L The Lua VM state.
313 * \param padding The padding struct pointer.
314 * \return The number of elements pushed on stack.
316 static inline int
317 luaA_pushpadding(lua_State *L, padding_t *padding)
319 lua_createtable(L, 0, 4);
320 lua_pushnumber(L, padding->right);
321 lua_setfield(L, -2, "right");
322 lua_pushnumber(L, padding->left);
323 lua_setfield(L, -2, "left");
324 lua_pushnumber(L, padding->top);
325 lua_setfield(L, -2, "top");
326 lua_pushnumber(L, padding->bottom);
327 lua_setfield(L, -2, "bottom");
328 return 1;
331 void luaA_init(xdgHandle *);
332 bool luaA_parserc(xdgHandle *, const char *, bool);
333 void luaA_on_timer(EV_P_ ev_timer *, int);
334 int luaA_pushxcolor(lua_State *, const xcolor_t *);
335 int luaA_pushcolor(lua_State *, const color_t *);
336 bool luaA_hasitem(lua_State *, const void *);
337 void luaA_table2wtable(lua_State *);
338 int luaA_next(lua_State *, int);
339 bool luaA_isloop(lua_State *, int);
341 #define hooks_property(c, prop) \
342 do { \
343 if(globalconf.hooks.property != LUA_REFNIL) \
345 client_push(globalconf.L, c); \
346 lua_pushliteral(globalconf.L, prop); \
347 luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
349 } while(0);
351 #endif
352 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80