forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathluaa.h
More file actions
386 lines (349 loc) · 10.5 KB
/
Copy pathluaa.h
File metadata and controls
386 lines (349 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* lua.h - Lua configuration management header
*
* Copyright © 2008 Julien Danjou <julien@danjou.info>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#ifndef AWESOME_LUA_H
#define AWESOME_LUA_H
#include <ev.h>
#include <lua.h>
#include <lauxlib.h>
#include <stdio.h>
#include "draw.h"
#include "common/util.h"
/** Type for Lua function */
typedef int luaA_ref;
#define deprecate(L, repl) \
luaA_warn(L, "%s: This function is deprecated and will be removed, see %s", \
__FUNCTION__, repl)
#define DO_LUA_NEW(decl, type, prefix, lua_type, type_ref) \
decl int \
luaA_##prefix##_userdata_new(lua_State *L, type *p) \
{ \
type **pp = lua_newuserdata(L, sizeof(type *)); \
*pp = p; \
type_ref(pp); \
return luaA_settype(L, lua_type); \
} \
static int \
luaA_##prefix##_tostring(lua_State *L) \
{ \
type **p = luaA_checkudata(L, 1, lua_type); \
lua_pushfstring(L, lua_type ": %p", *p); \
return 1; \
}
#define DO_LUA_GC(type, prefix, lua_type, type_unref) \
static int \
luaA_##prefix##_gc(lua_State *L) \
{ \
type **p = luaA_checkudata(L, 1, lua_type); \
type_unref(p); \
*p = NULL; \
return 0; \
}
#define DO_LUA_EQ(type, prefix, lua_type) \
static int \
luaA_##prefix##_eq(lua_State *L) \
{ \
type **p1 = luaA_checkudata(L, 1, lua_type); \
type **p2 = luaA_checkudata(L, 2, lua_type); \
lua_pushboolean(L, (*p1 == *p2)); \
return 1; \
}
#define luaA_dostring(L, cmd) \
do { \
if(luaL_dostring(L, cmd)) \
warn("error executing Lua code: %s", \
lua_tostring(L, -1)); \
} while(0)
#define luaA_checktable(L, n) \
do { \
if(!lua_istable(L, n)) \
luaL_typerror(L, n, "table"); \
} while(0)
#define luaA_checkfunction(L, n) \
do { \
if(!lua_isfunction(L, n)) \
luaL_typerror(L, n, "function"); \
} while(0)
#define luaA_checkscreen(screen) \
do { \
if(screen < 0 || screen >= globalconf.nscreen) \
luaL_error(L, "invalid screen number: %d", screen + 1); \
} while(0)
/** Dump the Lua stack. Useful for debugging.
* \param L The Lua VM state.
*/
static inline void
luaA_dumpstack(lua_State *L)
{
fprintf(stderr, "-------- Lua stack dump ---------\n");
for(int i = lua_gettop(L); i; i--)
{
int t = lua_type(L, i);
switch (t)
{
case LUA_TSTRING:
fprintf(stderr, "%d: string: `%s'\n", i, lua_tostring(L, i));
break;
case LUA_TBOOLEAN:
fprintf(stderr, "%d: bool: %s\n", i, lua_toboolean(L, i) ? "true" : "false");
break;
case LUA_TNUMBER:
fprintf(stderr, "%d: number: %g\n", i, lua_tonumber(L, i));
break;
case LUA_TNIL:
fprintf(stderr, "%d: nil\n", i);
break;
default:
fprintf(stderr, "%d: %s\t#%d\t%p\n", i, lua_typename(L, t),
(int) lua_objlen(L, i),
lua_topointer(L, i));
break;
}
}
fprintf(stderr, "------- Lua stack dump end ------\n");
}
/** Check that an object is not a NULL reference.
* \param L The Lua state.
* \param ud The index of the object in the stack.
* \param tname The type name.
* \return A pointer to the object.
*/
static inline void *
luaA_checkudata(lua_State *L, int ud, const char *tname)
{
void **p = luaL_checkudata(L, ud, tname);
if(*p)
return p;
luaL_error(L, "invalid object");
return NULL;
}
/** Convert a object to a udata if possible.
* \param L The Lua VM state.
* \param ud The index.
* \param tname The type name.
* \return A pointer to the object, NULL otherwise.
*/
static inline void *
luaA_toudata(lua_State *L, int ud, const char *tname)
{
void **p = lua_touserdata(L, ud);
if(p && *p) /* value is a userdata? */
if(lua_getmetatable(L, ud)) /* does it have a metatable? */
{
lua_getfield(L, LUA_REGISTRYINDEX, tname); /* get correct metatable */
if(lua_rawequal(L, -1, -2)) /* does it have the correct mt? */
{
lua_pop(L, 2); /* remove both metatables */
return p;
}
lua_pop(L, 2); /* remove both metatables */
}
return NULL;
}
static inline bool
luaA_checkboolean(lua_State *L, int n)
{
if(!lua_isboolean(L, n))
luaL_typerror(L, n, "boolean");
return lua_toboolean(L, n);
}
static inline bool
luaA_optboolean(lua_State *L, int idx, bool def)
{
return luaL_opt(L, luaA_checkboolean, idx, def);
}
static inline lua_Number
luaA_getopt_number(lua_State *L, int idx, const char *name, lua_Number def)
{
lua_getfield(L, idx, name);
return luaL_optnumber(L, -1, def);
}
static inline const char *
luaA_getopt_lstring(lua_State *L, int idx, const char *name, const char *def, size_t *len)
{
lua_getfield(L, idx, name);
return luaL_optlstring(L, -1, def, len);
}
static inline const char *
luaA_getopt_string(lua_State *L, int idx, const char *name, const char *def)
{
return luaA_getopt_lstring(L, idx, name, def, NULL);
}
static inline bool
luaA_getopt_boolean(lua_State *L, int idx, const char *name, bool def)
{
lua_getfield(L, idx, name);
return luaA_optboolean(L, -1, def);
}
static inline int
luaA_settype(lua_State *L, const char *type)
{
luaL_getmetatable(L, type);
lua_setmetatable(L, -2);
return 1;
}
/** Push a area type to a table on stack.
* \param L The Lua VM state.
* \param geometry The area geometry to push.
* \return The number of elements pushed on stack.
*/
static inline int
luaA_pusharea(lua_State *L, area_t geometry)
{
lua_newtable(L);
lua_pushnumber(L, geometry.x);
lua_setfield(L, -2, "x");
lua_pushnumber(L, geometry.y);
lua_setfield(L, -2, "y");
lua_pushnumber(L, geometry.width);
lua_setfield(L, -2, "width");
lua_pushnumber(L, geometry.height);
lua_setfield(L, -2, "height");
return 1;
}
static inline int
luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
{
lua_getmetatable(L, idxobj);
lua_pushvalue(L, idxfield);
lua_rawget(L, -2);
if(!lua_isnil(L, -1))
{
lua_remove(L, -2);
return 1;
}
lua_pop(L, 2);
return 0;
}
/** Register an Lua object.
* \param L The Lua stack.
* \param idx Index of the object in the stack.
* \param ref A luaA_ref address: it will be filled with the luaA_ref
* registered. If the adresse point to an already registered object, it will
* be unregistered.
* \return Always 0.
*/
static inline int
luaA_register(lua_State *L, int idx, luaA_ref *ref)
{
lua_pushvalue(L, idx);
if(*ref != LUA_REFNIL)
luaL_unref(L, LUA_REGISTRYINDEX, *ref);
*ref = luaL_ref(L, LUA_REGISTRYINDEX);
return 0;
}
/** Unregister a Lua object.
* \param L The Lua stack.
* \param ref A reference to an Lua object.
*/
static inline void
luaA_unregister(lua_State *L, luaA_ref *ref)
{
luaL_unref(L, LUA_REGISTRYINDEX, *ref);
*ref = LUA_REFNIL;
}
/** Register a function.
* \param L The Lua stack.
* \param idx Index of the function in the stack.
* \param fct A luaA_ref address: it will be filled with the luaA_ref
* registered. If the adresse point to an already registered function, it will
* be unregistered.
* \return luaA_register value.
*/
static inline int
luaA_registerfct(lua_State *L, int idx, luaA_ref *fct)
{
luaA_checkfunction(L, idx);
return luaA_register(L, idx, fct);
}
/** Execute an Lua function.
* \param L The Lua stack.
* \param f The Lua function to execute.
* \param nargs The number of arguments for the Lua function.
* \param nret The number of returned value from the Lua function.
* \return True on no error, false otherwise.
*/
static inline bool
luaA_dofunction(lua_State *L, luaA_ref f, int nargs, int nret)
{
lua_rawgeti(L, LUA_REGISTRYINDEX, f);
if(nargs)
lua_insert(L, - (nargs + 1));
if(lua_pcall(L, nargs, nret, 0))
{
warn("error running function: %s",
lua_tostring(L, -1));
lua_pop(L, 1);
return false;
}
return true;
}
/** Print a warning about some Lua code.
* This is less mean than luaL_error() which setjmp via lua_error() and kills
* everything. This only warn, it's up to you to then do what's should be done.
* \param L The Lua VM state.
* \param fmt The warning message.
*/
static inline void __attribute__ ((format(printf, 2, 3)))
luaA_warn(lua_State *L, const char *fmt, ...)
{
va_list ap;
luaL_where(L, 1);
fprintf(stderr, "%sW: ", lua_tostring(L, -1));
lua_pop(L, 1);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
}
int luaA_otable_index(lua_State *);
/** Create a new object table with a metatable.
* This is useful to compare table with objects (udata) as keys.
* \param L The Lua stack.
* \return The number of elements pushed on stack.
*/
static inline int
luaA_otable_new(lua_State *L)
{
/* Our object */
lua_newtable(L);
return luaA_settype(L, "otable");
}
void luaA_init(void);
bool luaA_parserc(const char *, bool);
void luaA_cs_init(void);
void luaA_cs_cleanup(void);
void luaA_on_timer(EV_P_ ev_timer *, int);
int luaA_pushcolor(lua_State *, const xcolor_t *);
bool luaA_hasitem(lua_State *, const void *);
void luaA_table2wtable(lua_State *);
int luaA_next(lua_State *, int);
bool luaA_isloop(lua_State *, int);
#define hooks_property(c, prop) \
do { \
if(globalconf.hooks.property != LUA_REFNIL) \
{ \
luaA_client_userdata_new(globalconf.L, c); \
lua_pushliteral(globalconf.L, prop); \
luaA_dofunction(globalconf.L, globalconf.hooks.property, 2, 0); \
} \
} while(0);
#endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80