add usage example for keygrabber API
[awesome.git] / luaa.h
blob4ed896f58c4cc46e38f65616cbc65915977e32e7
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 do { \
37 luaA_warn(L, "%s: This function is deprecated and will be removed, see %s", \
38 __FUNCTION__, repl); \
39 lua_pushlstring(L, __FUNCTION__, sizeof(__FUNCTION__)); \
40 signal_object_emit(L, &global_signals, "debug::deprecation", 1); \
41 } while(0)
43 #define luaA_checkscreen(screen) \
44 do { \
45 if(screen < 0 || screen >= globalconf.screens.len) \
46 luaL_error(L, "invalid screen number: %d", screen + 1); \
47 } while(0)
49 static inline bool
50 luaA_checkboolean(lua_State *L, int n)
52 if(!lua_isboolean(L, n))
53 luaL_typerror(L, n, "boolean");
54 return lua_toboolean(L, n);
57 static inline bool
58 luaA_optboolean(lua_State *L, int idx, bool def)
60 return luaL_opt(L, luaA_checkboolean, idx, def);
63 static inline lua_Number
64 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
66 lua_getfield(L, idx, name);
67 if (lua_isnil(L, -1) || lua_isnumber(L, -1))
68 def = luaL_optnumber(L, -1, def);
69 lua_pop(L, 1);
70 return def;
73 static inline const char *
74 luaA_getopt_lstring(lua_State *L, int idx, const char *name, const char *def, size_t *len)
76 lua_getfield(L, idx, name);
77 const char *s = luaL_optlstring(L, -1, def, len);
78 lua_pop(L, 1);
79 return s;
82 static inline bool
83 luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
85 lua_getfield(L, idx, name);
86 bool b = luaA_optboolean(L, -1, def);
87 lua_pop(L, 1);
88 return b;
91 /** Push a area type to a table on stack.
92 * \param L The Lua VM state.
93 * \param geometry The area geometry to push.
94 * \return The number of elements pushed on stack.
96 static inline int
97 luaA_pusharea(lua_State *L, area_t geometry)
99 lua_createtable(L, 0, 4);
100 lua_pushnumber(L, geometry.x);
101 lua_setfield(L, -2, "x");
102 lua_pushnumber(L, geometry.y);
103 lua_setfield(L, -2, "y");
104 lua_pushnumber(L, geometry.width);
105 lua_setfield(L, -2, "width");
106 lua_pushnumber(L, geometry.height);
107 lua_setfield(L, -2, "height");
108 return 1;
111 /** Register an Lua object.
112 * \param L The Lua stack.
113 * \param idx Index of the object in the stack.
114 * \param ref A int address: it will be filled with the int
115 * registered. If the address points to an already registered object, it will
116 * be unregistered.
117 * \return Always 0.
119 static inline int
120 luaA_register(lua_State *L, int idx, int *ref)
122 lua_pushvalue(L, idx);
123 if(*ref != LUA_REFNIL)
124 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
125 *ref = luaL_ref(L, LUA_REGISTRYINDEX);
126 return 0;
129 /** Unregister a Lua object.
130 * \param L The Lua stack.
131 * \param ref A reference to an Lua object.
133 static inline void
134 luaA_unregister(lua_State *L, int *ref)
136 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
137 *ref = LUA_REFNIL;
140 /** Register a function.
141 * \param L The Lua stack.
142 * \param idx Index of the function in the stack.
143 * \param fct A int address: it will be filled with the int
144 * registered. If the address points to an already registered function, it will
145 * be unregistered.
146 * \return luaA_register value.
148 static inline int
149 luaA_registerfct(lua_State *L, int idx, int *fct)
151 luaA_checkfunction(L, idx);
152 return luaA_register(L, idx, fct);
155 /** Grab a function from the registry and execute it.
156 * \param L The Lua stack.
157 * \param ref The function reference.
158 * \param nargs The number of arguments for the Lua function.
159 * \param nret The number of returned value from the Lua function.
160 * \return True on no error, false otherwise.
162 static inline bool
163 luaA_dofunction_from_registry(lua_State *L, int ref, int nargs, int nret)
165 lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
166 return luaA_dofunction(L, nargs, nret);
169 /** Print a warning about some Lua code.
170 * This is less mean than luaL_error() which setjmp via lua_error() and kills
171 * everything. This only warn, it's up to you to then do what's should be done.
172 * \param L The Lua VM state.
173 * \param fmt The warning message.
175 static inline void __attribute__ ((format(printf, 2, 3)))
176 luaA_warn(lua_State *L, const char *fmt, ...)
178 va_list ap;
179 luaL_where(L, 1);
180 fprintf(stderr, "%sW: ", lua_tostring(L, -1));
181 lua_pop(L, 1);
182 va_start(ap, fmt);
183 vfprintf(stderr, fmt, ap);
184 va_end(ap);
185 fprintf(stderr, "\n");
188 void luaA_init(xdgHandle *);
189 bool luaA_parserc(xdgHandle *, const char *, bool);
190 void luaA_on_timer(EV_P_ ev_timer *, int);
191 bool luaA_hasitem(lua_State *, const void *);
192 void luaA_table2wtable(lua_State *);
193 int luaA_next(lua_State *, int);
194 bool luaA_isloop(lua_State *, int);
196 #define hook_property(obj, prop) \
197 do { \
198 if(globalconf.hooks.property != LUA_REFNIL) \
200 luaA_object_push(globalconf.L, obj); \
201 lua_pushliteral(globalconf.L, prop); \
202 luaA_dofunction_from_registry(globalconf.L, globalconf.hooks.property, 2, 0); \
204 } while(0);
206 /** Global signals */
207 signal_array_t global_signals;
209 int luaA_class_index_miss_property(lua_State *, lua_object_t *);
210 int luaA_class_newindex_miss_property(lua_State *, lua_object_t *);
212 #endif
213 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80