client: check for focus on unmanage before removing client
[awesome.git] / lua.h
blob0e54ef320234a34b1444d7ba6074592c02bbdc50
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_function;
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(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_dofunction(L, f, n, r) \
81 do { \
82 if(f != LUA_REFNIL) \
83 { \
84 lua_rawgeti(L, LUA_REGISTRYINDEX, f); \
85 if(n) \
86 lua_insert(L, - (n + 1)); \
87 if(lua_pcall(L, n, r, 0)) \
88 { \
89 warn("error running function: %s", \
90 lua_tostring(L, -1)); \
91 lua_pop(L, 1); \
92 } \
93 } \
94 } while(0)
96 #define luaA_checktable(L, n) \
97 do { \
98 if(!lua_istable(L, n)) \
99 luaL_typerror(L, n, "table"); \
100 } while(0)
102 #define luaA_checkfunction(L, n) \
103 do { \
104 if(!lua_isfunction(L, n)) \
105 luaL_typerror(L, n, "function"); \
106 } while(0)
108 #define luaA_checkscreen(screen) \
109 do { \
110 if(screen < 0 || screen >= globalconf.screens_info->nscreen) \
111 luaL_error(L, "invalid screen number: %d\n", screen); \
112 } while(0)
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.
120 static inline void *
121 luaA_checkudata(lua_State *L, int ud, const char *tname)
123 void **p = luaL_checkudata(L, ud, tname);
124 if(*p)
125 return p;
126 luaL_error(L, "invalid object");
127 return NULL;
130 static inline bool
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);
138 static inline bool
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);
164 static inline bool
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);
171 static inline int
172 luaA_settype(lua_State *L, const char *type)
174 luaL_getmetatable(L, type);
175 lua_setmetatable(L, -2);
176 return 1;
179 static inline int
180 luaA_usemetatable(lua_State *L, int idxobj, int idxfield)
182 lua_getmetatable(L, idxobj);
183 lua_pushvalue(L, idxfield);
184 lua_rawget(L, -2);
185 if (!lua_isnil(L, -1))
187 lua_remove(L, -2);
188 return 1;
190 lua_pop(L, 2);
192 return 0;
195 static inline int
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);
202 return 0;
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);
213 #endif
214 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80