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.
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", \
39 #define luaA_checkscreen(screen) \
41 if(screen < 0 || screen >= globalconf.screens.len) \
42 luaL_error(L, "invalid screen number: %d", screen + 1); \
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
);
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
);
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
);
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
);
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.
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");
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
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
);
125 /** Unregister a Lua object.
126 * \param L The Lua stack.
127 * \param ref A reference to an Lua object.
130 luaA_unregister(lua_State
*L
, int *ref
)
132 luaL_unref(L
, LUA_REGISTRYINDEX
, *ref
);
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
142 * \return luaA_register value.
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.
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
, ...)
176 fprintf(stderr
, "%sW: ", lua_tostring(L
, -1));
179 vfprintf(stderr
, fmt
, 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
)
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
);
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.
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");
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) \
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); \
243 /** Global signals */
244 signal_array_t global_signals
;
246 int luaA_class_index_miss_property(lua_State
*, lua_object_t
*);
247 int luaA_class_newindex_miss_property(lua_State
*, lua_object_t
*);
250 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80