Add imlibsetroot setter to awsetbg script.
[awesome.git] / luaa.h
blobfcfdcc60bdf15462cd02ade345f60ade3b18d23c
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"
34 #include "common/luaclass.h"
36 #define luaA_deprecate(L, repl) \
37 do { \
38 luaA_warn(L, "%s: This function is deprecated and will be removed, see %s", \
39 __FUNCTION__, repl); \
40 lua_pushlstring(L, __FUNCTION__, sizeof(__FUNCTION__)); \
41 signal_object_emit(L, &global_signals, "debug::deprecation", 1); \
42 } while(0)
44 #define luaA_checkscreen(screen) \
45 do { \
46 if(screen < 0 || screen >= globalconf.screens.len) \
47 luaL_error(L, "invalid screen number: %d", screen + 1); \
48 } while(0)
50 static inline bool
51 luaA_checkboolean(lua_State *L, int n)
53 if(!lua_isboolean(L, n))
54 luaL_typerror(L, n, "boolean");
55 return lua_toboolean(L, n);
58 static inline bool
59 luaA_optboolean(lua_State *L, int idx, bool def)
61 return luaL_opt(L, luaA_checkboolean, idx, def);
64 static inline lua_Number
65 luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
67 lua_getfield(L, idx, name);
68 if (lua_isnil(L, -1) || lua_isnumber(L, -1))
69 def = luaL_optnumber(L, -1, def);
70 lua_pop(L, 1);
71 return def;
74 static inline const char *
75 luaA_getopt_lstring(lua_State *L, int idx, const char *name, const char *def, size_t *len)
77 lua_getfield(L, idx, name);
78 const char *s = luaL_optlstring(L, -1, def, len);
79 lua_pop(L, 1);
80 return s;
83 static inline bool
84 luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
86 lua_getfield(L, idx, name);
87 bool b = luaA_optboolean(L, -1, def);
88 lua_pop(L, 1);
89 return b;
92 /** Push a area type to a table on stack.
93 * \param L The Lua VM state.
94 * \param geometry The area geometry to push.
95 * \return The number of elements pushed on stack.
97 static inline int
98 luaA_pusharea(lua_State *L, area_t geometry)
100 lua_createtable(L, 0, 4);
101 lua_pushnumber(L, geometry.x);
102 lua_setfield(L, -2, "x");
103 lua_pushnumber(L, geometry.y);
104 lua_setfield(L, -2, "y");
105 lua_pushnumber(L, geometry.width);
106 lua_setfield(L, -2, "width");
107 lua_pushnumber(L, geometry.height);
108 lua_setfield(L, -2, "height");
109 return 1;
112 /** Register an Lua object.
113 * \param L The Lua stack.
114 * \param idx Index of the object in the stack.
115 * \param ref A int address: it will be filled with the int
116 * registered. If the address points to an already registered object, it will
117 * be unregistered.
118 * \return Always 0.
120 static inline int
121 luaA_register(lua_State *L, int idx, int *ref)
123 lua_pushvalue(L, idx);
124 if(*ref != LUA_REFNIL)
125 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
126 *ref = luaL_ref(L, LUA_REGISTRYINDEX);
127 return 0;
130 /** Unregister a Lua object.
131 * \param L The Lua stack.
132 * \param ref A reference to an Lua object.
134 static inline void
135 luaA_unregister(lua_State *L, int *ref)
137 luaL_unref(L, LUA_REGISTRYINDEX, *ref);
138 *ref = LUA_REFNIL;
141 /** Register a function.
142 * \param L The Lua stack.
143 * \param idx Index of the function in the stack.
144 * \param fct A int address: it will be filled with the int
145 * registered. If the address points to an already registered function, it will
146 * be unregistered.
147 * \return luaA_register value.
149 static inline int
150 luaA_registerfct(lua_State *L, int idx, int *fct)
152 luaA_checkfunction(L, idx);
153 return luaA_register(L, idx, fct);
156 /** Grab a function from the registry and execute it.
157 * \param L The Lua stack.
158 * \param ref The function reference.
159 * \param nargs The number of arguments for the Lua function.
160 * \param nret The number of returned value from the Lua function.
161 * \return True on no error, false otherwise.
163 static inline bool
164 luaA_dofunction_from_registry(lua_State *L, int ref, int nargs, int nret)
166 lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
167 return luaA_dofunction(L, nargs, nret);
170 /** Print a warning about some Lua code.
171 * This is less mean than luaL_error() which setjmp via lua_error() and kills
172 * everything. This only warn, it's up to you to then do what's should be done.
173 * \param L The Lua VM state.
174 * \param fmt The warning message.
176 static inline void __attribute__ ((format(printf, 2, 3)))
177 luaA_warn(lua_State *L, const char *fmt, ...)
179 va_list ap;
180 luaL_where(L, 1);
181 fprintf(stderr, "%sW: ", lua_tostring(L, -1));
182 lua_pop(L, 1);
183 va_start(ap, fmt);
184 vfprintf(stderr, fmt, ap);
185 va_end(ap);
186 fprintf(stderr, "\n");
189 void luaA_init(xdgHandle *);
190 bool luaA_parserc(xdgHandle *, const char *, bool);
191 bool luaA_hasitem(lua_State *, const void *);
192 bool luaA_isloop(lua_State *, int);
194 /** Global signals */
195 signal_array_t global_signals;
197 int luaA_class_index_miss_property(lua_State *, lua_object_t *);
198 int luaA_class_newindex_miss_property(lua_State *, lua_object_t *);
200 void luaA_systray_invalidate(void);
202 #endif
203 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80