naughty: remove useless hooks
[awesome.git] / luaa.h
blob62389dbab2afe16e9d1ac604a3346a21924b13f9
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"
33 #include "common/lualib.h"
35 #define luaA_deprecate(L, repl) \
36 luaA_warn(L, "%s: This function is deprecated and will be removed, see %s", \
37 __FUNCTION__, repl)
39 #define luaA_checkscreen(screen) \
40 do { \
41 if(screen < 0 || screen >= globalconf.screens.len) \
42 luaL_error(L, "invalid screen number: %d", screen + 1); \
43 } while(0)
45 static inline bool
46 luaA_checkboolean(lua_State *L, int n)
48 if(!lua_isboolean(L, n))
49 luaL_typerror(L, n, "boolean");
50 return lua_toboolean(L, n);
53 static inline bool
54 luaA_optboolean(lua_State *L, int idx, bool def)
56 return luaL_opt(L, luaA_checkboolean, idx, def);
59 static inline lua_Number
60 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
62 lua_getfield(L, idx, name);
63 if (lua_isnil(L, -1) || lua_isnumber(L, -1))
64 def = luaL_optnumber(L, -1, def);
65 lua_pop(L, 1);
66 return def;
69 static inline const char *
70 luaA_getopt_lstring(lua_State *L, int idx, const char *name, const char *def, size_t *len)
72 lua_getfield(L, idx, name);
73 const char *s = luaL_optlstring(L, -1, def, len);
74 lua_pop(L, 1);
75 return s;
78 static inline bool
79 luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
81 lua_getfield(L, idx, name);
82 bool b = luaA_optboolean(L, -1, def);
83 lua_pop(L, 1);
84 return b;
87 /** Push a area type to a table on stack.
88 * \param L The Lua VM state.
89 * \param geometry The area geometry to push.
90 * \return The number of elements pushed on stack.
92 static inline int
93 luaA_pusharea(lua_State *L, area_t geometry)
95 lua_createtable(L, 0, 4);
96 lua_pushnumber(L, geometry.x);
97 lua_setfield(L, -2, "x");
98 lua_pushnumber(L, geometry.y);
99 lua_setfield(L, -2, "y");
100 lua_pushnumber(L, geometry.width);
101 lua_setfield(L, -2, "width");
102 lua_pushnumber(L, geometry.height);
103 lua_setfield(L, -2, "height");
104 return 1;
107 /** Register an Lua object.
108 * \param L The Lua stack.
109 * \param idx Index of the object in the stack.
110 * \param ref A int address: it will be filled with the int
111 * registered. If the adresse point to an already registered object, it will
112 * be unregistered.
113 * \return Always 0.
115 static inline int
116 luaA_register(lua_State *L, int idx, int *ref)
118 lua_pushvalue(L, idx);
119 if(*ref != LUA_REFNIL)
120 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
121 *ref = luaL_ref(L, LUA_REGISTRYINDEX);
122 return 0;
125 /** Unregister a Lua object.
126 * \param L The Lua stack.
127 * \param ref A reference to an Lua object.
129 static inline void
130 luaA_unregister(lua_State *L, int *ref)
132 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
133 *ref = LUA_REFNIL;
136 /** Register a function.
137 * \param L The Lua stack.
138 * \param idx Index of the function in the stack.
139 * \param fct A int address: it will be filled with the int
140 * registered. If the adresse point to an already registered function, it will
141 * be unregistered.
142 * \return luaA_register value.
144 static inline int
145 luaA_registerfct(lua_State *L, int idx, int *fct)
147 luaA_checkfunction(L, idx);
148 return luaA_register(L, idx, fct);
151 /** Grab a function from the registry and execute it.
152 * \param L The Lua stack.
153 * \param ref The function reference.
154 * \param nargs The number of arguments for the Lua function.
155 * \param nret The number of returned value from the Lua function.
156 * \return True on no error, false otherwise.
158 static inline bool
159 luaA_dofunction_from_registry(lua_State *L, int ref, int nargs, int nret)
161 lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
162 return luaA_dofunction(L, nargs, nret);
165 /** Print a warning about some Lua code.
166 * This is less mean than luaL_error() which setjmp via lua_error() and kills
167 * everything. This only warn, it's up to you to then do what's should be done.
168 * \param L The Lua VM state.
169 * \param fmt The warning message.
171 static inline void __attribute__ ((format(printf, 2, 3)))
172 luaA_warn(lua_State *L, const char *fmt, ...)
174 va_list ap;
175 luaL_where(L, 1);
176 fprintf(stderr, "%sW: ", lua_tostring(L, -1));
177 lua_pop(L, 1);
178 va_start(ap, fmt);
179 vfprintf(stderr, fmt, ap);
180 va_end(ap);
181 fprintf(stderr, "\n");
184 /** Get an optional padding table from a Lua table.
185 * \param L The Lua VM state.
186 * \param idx The table index on the stack.
187 * \param dpadding The default padding value to use.
189 static inline padding_t
190 luaA_getopt_padding(lua_State *L, int idx, padding_t *dpadding)
192 padding_t padding;
194 luaA_checktable(L, idx);
196 padding.right = luaA_getopt_number(L, idx, "right", dpadding->right);
197 padding.left = luaA_getopt_number(L, idx, "left", dpadding->left);
198 padding.top = luaA_getopt_number(L, idx, "top", dpadding->top);
199 padding.bottom = luaA_getopt_number(L, idx, "bottom", dpadding->bottom);
201 return padding;
205 /** Push a padding structure into a table on the Lua stack.
206 * \param L The Lua VM state.
207 * \param padding The padding struct pointer.
208 * \return The number of elements pushed on stack.
210 static inline int
211 luaA_pushpadding(lua_State *L, padding_t *padding)
213 lua_createtable(L, 0, 4);
214 lua_pushnumber(L, padding->right);
215 lua_setfield(L, -2, "right");
216 lua_pushnumber(L, padding->left);
217 lua_setfield(L, -2, "left");
218 lua_pushnumber(L, padding->top);
219 lua_setfield(L, -2, "top");
220 lua_pushnumber(L, padding->bottom);
221 lua_setfield(L, -2, "bottom");
222 return 1;
225 void luaA_init(xdgHandle *);
226 bool luaA_parserc(xdgHandle *, const char *, bool);
227 void luaA_on_timer(EV_P_ ev_timer *, int);
228 bool luaA_hasitem(lua_State *, const void *);
229 void luaA_table2wtable(lua_State *);
230 int luaA_next(lua_State *, int);
231 bool luaA_isloop(lua_State *, int);
233 #define hook_property(obj, prop) \
234 do { \
235 if(globalconf.hooks.property != LUA_REFNIL) \
237 luaA_object_push(globalconf.L, obj); \
238 lua_pushliteral(globalconf.L, prop); \
239 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.property, 2, 0); \
241 } while(0);
243 /** Global signals */
244 signal_array_t global_signals;
246 #endif
247 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80