Revert "client: fix windows managed on startup"
[awesome.git] / lua.h
blobbb672b8aa04874f1636fb629d78c559fd8c462e0
1 /*
2 * lua.h - Lua configuration management header
4 * Copyright © 2008 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>
26 #include <lua.h>
27 #include <lauxlib.h>
29 #include "common/util.h"
30 #include "common/draw.h"
32 /** Object types */
33 typedef enum
35 AWESOME_TYPE_STATUSBAR = 1,
36 AWESOME_TYPE_TITLEBAR
37 } awesome_type_t;
39 /** Type for Lua function */
40 typedef int luaA_ref;
42 #define DO_LUA_NEW(decl, type, prefix, lua_type, type_ref) \
43 decl int \
44 luaA_##prefix##_userdata_new(lua_State *L, type *p) \
45 { \
46 type **pp = lua_newuserdata(L, sizeof(type *)); \
47 *pp = p; \
48 type_ref(pp); \
49 return luaA_settype(L, lua_type); \
52 #define DO_LUA_GC(type, prefix, lua_type, type_unref) \
53 static int \
54 luaA_##prefix##_gc(lua_State *L) \
55 { \
56 type **p = luaA_checkudata(L, 1, lua_type); \
57 type_unref(p); \
58 *p = NULL; \
59 return 0; \
62 #define DO_LUA_EQ(type, prefix, lua_type) \
63 static int \
64 luaA_##prefix##_eq(lua_State *L) \
65 { \
66 type **p1 = luaA_checkudata(L, 1, lua_type); \
67 type **p2 = luaA_checkudata(L, 2, lua_type); \
68 lua_pushboolean(L, (*p1 == *p2)); \
69 return 1; \
72 #define luaA_dostring(L, cmd) \
73 do { \
74 if(a_strlen(cmd)) \
75 if(luaL_dostring(L, cmd)) \
76 warn("error executing Lua code: %s", \
77 lua_tostring(L, -1)); \
78 } while(0)
80 #define luaA_checktable(L, n) \
81 do { \
82 if(!lua_istable(L, n)) \
83 luaL_typerror(L, n, "table"); \
84 } while(0)
86 #define luaA_checkfunction(L, n) \
87 do { \
88 if(!lua_isfunction(L, n)) \
89 luaL_typerror(L, n, "function"); \
90 } while(0)
92 #define luaA_checkscreen(screen) \
93 do { \
94 if(screen < 0 || screen >= globalconf.screens_info->nscreen) \
95 luaL_error(L, "invalid screen number: %d", screen); \
96 } while(0)
98 /** Check that an object is not a NULL reference.
99 * \param L The Lua state.
100 * \param ud The index of the object in the stack.
101 * \param tname The type name.
102 * \return A pointer to the object.
104 static inline void *
105 luaA_checkudata(lua_State *L, int ud, const char *tname)
107 void **p = luaL_checkudata(L, ud, tname);
108 if(*p)
109 return p;
110 luaL_error(L, "invalid object");
111 return NULL;
114 static inline bool
115 luaA_checkboolean(lua_State *L, int n)
117 if(!lua_isboolean(L, n))
118 luaL_typerror(L, n, "boolean");
119 return lua_toboolean(L, n);
122 static inline bool
123 luaA_optboolean(lua_State *L, int idx, bool def)
125 return luaL_opt(L, luaA_checkboolean, idx, def);
128 static inline lua_Number
129 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
131 lua_getfield(L, idx, name);
132 return luaL_optnumber(L, -1, def);
135 static inline const char *
136 luaA_getopt_lstring(lua_State *L, int idx, const char *name, const char *def, size_t *len)
138 lua_getfield(L, idx, name);
139 return luaL_optlstring(L, -1, def, len);
142 static inline const char *
143 luaA_getopt_string(lua_State *L, int idx, const char *name, const char *def)
145 return luaA_getopt_lstring(L, idx, name, def, NULL);
148 static inline bool
149 luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
151 lua_getfield(L, idx, name);
152 return luaA_optboolean(L, -1, def);
155 static inline int
156 luaA_settype(lua_State *L, const char *type)
158 luaL_getmetatable(L, type);
159 lua_setmetatable(L, -2);
160 return 1;
163 static inline int
164 luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
166 lua_getmetatable(L, idxobj);
167 lua_pushvalue(L, idxfield);
168 lua_rawget(L, -2);
169 if (!lua_isnil(L, -1))
171 lua_remove(L, -2);
172 return 1;
174 lua_pop(L, 2);
176 return 0;
179 /** Register a function.
180 * \param L The Lua stack.
181 * \param fct A luaA_ref address: it will be filled with the luaA_ref
182 * registered. If the adresse point to an already registered function, it will
183 * be unregistered.
184 * \return Always 0.
186 static inline int
187 luaA_registerfct(lua_State *L, luaA_ref *fct)
189 luaA_checkfunction(L, -1);
190 if(*fct != LUA_REFNIL)
191 luaL_unref(L, LUA_REGISTRYINDEX, *fct);
192 *fct = luaL_ref(L, LUA_REGISTRYINDEX);
193 return 0;
196 /** Execute an Lua function.
197 * \param L The Lua stack.
198 * \param f The Lua function to execute.
199 * \param nargs The number of arguments for the Lua function.
200 * \param nret The number of returned value from the Lua function.
201 * \return True on no error, false otherwise.
203 static inline bool
204 luaA_dofunction(lua_State *L, luaA_ref f, int nargs, int nret)
206 if(f != LUA_REFNIL)
208 lua_rawgeti(L, LUA_REGISTRYINDEX, f);
209 if(nargs)
210 lua_insert(L, - (nargs + 1));
211 if(lua_pcall(L, nargs, nret, 0))
213 warn("error running function: %s",
214 lua_tostring(L, -1));
215 lua_pop(L, 1);
216 return false;
218 return true;
220 return false;
223 int luaA_otable_index(lua_State *);
225 /** Create a new object table with a metatable.
226 * This is useful to compare table with objects (udata) as keys.
227 * \param L The Lua stack.
228 * \return The number of elements pushed on stack.
230 static inline int
231 luaA_otable_new(lua_State *L)
233 /* Our object */
234 lua_newtable(L);
235 /* The meta table */
236 lua_newtable(L);
237 lua_pushcfunction(L, luaA_otable_index);
238 /* Register index into the metatable */
239 lua_setfield(L, -2, "__index");
240 /* Set the meta table */
241 lua_setmetatable(L, -2);
242 return 1;
245 void luaA_init(void);
246 void luaA_parserc(const char *);
247 void luaA_pushpointer(lua_State *, void *, awesome_type_t);
248 void luaA_cs_init(void);
249 void luaA_cs_cleanup(void);
250 void luaA_on_timer(EV_P_ ev_timer *w, int revents);
251 void luaA_pushcolor(lua_State *, const xcolor_t *);
253 static inline int
254 luaA_generic_pairs(lua_State *L)
256 lua_pushvalue(L, lua_upvalueindex(1)); /* return generator, */
257 lua_pushvalue(L, 1); /* state, */
258 lua_pushnil(L); /* and initial value */
259 return 3;
262 #endif
263 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80