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.
29 #include "common/util.h"
30 #include "common/draw.h"
35 AWESOME_TYPE_STATUSBAR
= 1,
39 /** Type for Lua function */
40 typedef int luaA_function
;
42 #define DO_LUA_NEW(decl, type, prefix, lua_type, type_ref) \
44 luaA_##prefix##_userdata_new(lua_State *L, type *p) \
46 type **pp = lua_newuserdata(L, sizeof(type *)); \
49 return luaA_settype(L, lua_type); \
52 #define DO_LUA_GC(type, prefix, lua_type, type_unref) \
54 luaA_##prefix##_gc(lua_State *L) \
56 type **p = luaA_checkudata(L, 1, lua_type); \
62 #define DO_LUA_EQ(type, prefix, lua_type) \
64 luaA_##prefix##_eq(lua_State *L) \
66 type **p1 = luaA_checkudata(L, 1, lua_type); \
67 type **p2 = luaA_checkudata(L, 2, lua_type); \
68 lua_pushboolean(L, (*p1 == *p2)); \
72 #define luaA_dostring(L, cmd) \
75 if(luaL_dostring(L, cmd)) \
76 warn("error executing Lua code: %s", \
77 lua_tostring(L, -1)); \
80 #define luaA_dofunction(L, f, n, r) \
84 lua_rawgeti(L, LUA_REGISTRYINDEX, f); \
86 lua_insert(L, - (n + 1)); \
87 if(lua_pcall(L, n, r, 0)) \
89 warn("error running function: %s", \
90 lua_tostring(L, -1)); \
96 #define luaA_checktable(L, n) \
98 if(!lua_istable(L, n)) \
99 luaL_typerror(L, n, "table"); \
102 #define luaA_checkfunction(L, n) \
104 if(!lua_isfunction(L, n)) \
105 luaL_typerror(L, n, "function"); \
108 #define luaA_checkscreen(screen) \
110 if(screen < 0 || screen >= globalconf.screens_info->nscreen) \
111 luaL_error(L, "invalid screen number: %d\n", screen); \
114 /** Check that an object is not a NULL reference.
115 * \param L The Lua state.
116 * \param ud The index of the object in the stack.
117 * \param tname The type name.
118 * \return A pointer to the object.
121 luaA_checkudata(lua_State
*L
, int ud
, const char *tname
)
123 void **p
= luaL_checkudata(L
, ud
, tname
);
126 luaL_error(L
, "invalid object");
131 luaA_checkboolean(lua_State
*L
, int n
)
133 if(!lua_isboolean(L
, n
))
134 luaL_typerror(L
, n
, "boolean");
135 return lua_toboolean(L
, n
);
139 luaA_optboolean(lua_State
*L
, int idx
, bool def
)
141 return luaL_opt(L
, luaA_checkboolean
, idx
, def
);
144 static inline lua_Number
145 luaA_getopt_number(lua_State
*L
, int idx
, const char *name
, lua_Number def
)
147 lua_getfield(L
, idx
, name
);
148 return luaL_optnumber(L
, -1, def
);
151 static inline const char *
152 luaA_getopt_lstring(lua_State
*L
, int idx
, const char *name
, const char *def
, size_t *len
)
154 lua_getfield(L
, idx
, name
);
155 return luaL_optlstring(L
, -1, def
, len
);
158 static inline const char *
159 luaA_getopt_string(lua_State
*L
, int idx
, const char *name
, const char *def
)
161 return luaA_getopt_lstring(L
, idx
, name
, def
, NULL
);
165 luaA_getopt_boolean(lua_State
*L
, int idx
, const char *name
, bool def
)
167 lua_getfield(L
, idx
, name
);
168 return luaA_optboolean(L
, -1, def
);
172 luaA_settype(lua_State
*L
, const char *type
)
174 luaL_getmetatable(L
, type
);
175 lua_setmetatable(L
, -2);
180 luaA_usemetatable(lua_State
*L
, int idxobj
, int idxfield
)
182 lua_getmetatable(L
, idxobj
);
183 lua_pushvalue(L
, idxfield
);
185 if (!lua_isnil(L
, -1))
196 luaA_registerfct(lua_State
*L
, luaA_function
*fct
)
198 luaA_checkfunction(L
, -1);
199 if(*fct
!= LUA_REFNIL
)
200 luaL_unref(L
, LUA_REGISTRYINDEX
, *fct
);
201 *fct
= luaL_ref(L
, LUA_REGISTRYINDEX
);
205 void luaA_init(void);
206 void luaA_parserc(const char *);
207 void luaA_pushpointer(lua_State
*, void *, awesome_type_t
);
208 void luaA_cs_init(void);
209 void luaA_cs_cleanup(void);
210 void luaA_on_timer(EV_P_ ev_timer
*w
, int revents
);
211 void luaA_pushcolor(lua_State
*, const xcolor_t
*c
);
214 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80