awful.hooks.timer: fix timer removal
[awesome.git] / hooks.c
blobc5d8190ce5629e55828c8df1fb440b7da702a4af
1 /*
2 * hooks.c - Lua hooks management
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 #include "structs.h"
24 extern awesome_t globalconf;
26 #define HANDLE_HOOK(L, h) \
27 do { \
28 if(lua_gettop(L) == 1) \
29 luaA_registerfct(L, 1, &h); \
30 lua_rawgeti(L, LUA_REGISTRYINDEX, h); \
31 return 1; \
32 } while(0)
34 /** Set the function called each time a client gets focus. This function is
35 * called with the client object as argument.
36 * \param L The Lua VM state.
37 * \return The number of elements pushed on stack.
38 * \luastack
39 * \lparam A function to call each time a client gets focus.
41 static int
42 luaA_hooks_focus(lua_State *L)
44 HANDLE_HOOK(L, globalconf.hooks.focus);
47 /** Set the function called each time a client loses focus. This function is
48 * called with the client object as argument.
49 * \param L The Lua VM state.
50 * \return The number of elements pushed on stack.
51 * \luastack
52 * \lparam A function to call each time a client loses focus.
54 static int
55 luaA_hooks_unfocus(lua_State *L)
57 HANDLE_HOOK(L, globalconf.hooks.unfocus);
60 /** Set the function called each time a new client appears. This function is
61 * called with the client object as argument, plus a boolean argument which is
62 * true if the client is managed at startup.
63 * \param L The Lua VM state.
64 * \return The number of elements pushed on stack.
65 * \luastack
66 * \lparam A function to call on each new client.
68 static int
69 luaA_hooks_manage(lua_State *L)
71 HANDLE_HOOK(L, globalconf.hooks.manage);
74 /** Set the function called each time a client goes away. This function is
75 * called with the client object as argument.
76 * \param L The Lua VM state.
77 * \return The number of elements pushed on stack.
78 * \luastack
79 * \lparam A function to call when a client goes away.
81 static int
82 luaA_hooks_unmanage(lua_State *L)
84 HANDLE_HOOK(L, globalconf.hooks.unmanage);
87 /** Set the function called each time the mouse enter a window. This
88 * function is called with the client object as argument.
89 * \param L The Lua VM state.
90 * \return The number of elements pushed on stack.
91 * \luastack
92 * \lparam A function to call each time a client gets mouse over it.
94 static int
95 luaA_hooks_mouse_enter(lua_State *L)
97 HANDLE_HOOK(L, globalconf.hooks.mouse_enter);
100 /** Set the function called each time the mouse leave a window. This
101 * function is called with the client object as argument.
102 * \param L The Lua VM state.
103 * \return The number of elements pushed on stack.
104 * \luastack
105 * \lparam A function to call each time a client gets mouse over it.
107 static int
108 luaA_hooks_mouse_leave(lua_State *L)
110 HANDLE_HOOK(L, globalconf.hooks.mouse_leave);
113 /** Set the function called on each client list change.
114 * This function is called without any argument.
115 * \param L The Lua VM state.
116 * \return The number of elements pushed on stack.
117 * \luastack
118 * \lparam A function to call on each client list change.
120 static int
121 luaA_hooks_clients(lua_State *L)
123 HANDLE_HOOK(L, globalconf.hooks.clients);
126 /** Set the function called on each screen tag list change.
127 * This function is called with a screen number as first argument,
128 * the tag object as second and the action (add or remove) as third.
129 * \param L The Lua VM state.
130 * \return The number of elements pushed on stack.
131 * \luastack
132 * \lparam A function to call on each tag list change.
134 static int
135 luaA_hooks_tags(lua_State *L)
137 HANDLE_HOOK(L, globalconf.hooks.tags);
140 /** Set the function called on each client's tags change.
141 * This function is called with the client and the tag as argument.
142 * \param L The Lua VM state.
143 * \return The number of elements pushed on stack.
144 * \luastack
145 * \lparam A function to call on each client's tags change.
147 static int
148 luaA_hooks_tagged(lua_State *L)
150 HANDLE_HOOK(L, globalconf.hooks.tagged);
153 /** Set the function called on each screen arrange. This function is called
154 * with the screen number as argument.
155 * \param L The Lua VM state.
156 * \return The number of elements pushed on stack.
157 * \luastack
158 * \lparam A function to call on each screen arrange.
160 static int
161 luaA_hooks_arrange(lua_State *L)
163 HANDLE_HOOK(L, globalconf.hooks.arrange);
166 /** Set the function called on each client's property change.
167 * This function is called with the client object as argument and the
168 * property name.
169 * \param L The Lua VM state.
170 * \return The number of elements pushed on stack.
171 * \luastack
172 * \lparam A function to call on each client property update.
174 static int
175 luaA_hooks_property(lua_State *L)
177 HANDLE_HOOK(L, globalconf.hooks.property);
180 /** Set the function to be called every N seconds.
181 * \param L The Lua VM state.
182 * \return The number of elements pushed on stack.
183 * \luastack
184 * \lparam The number of seconds to run function every. Set 0 to disable.
185 * \lparam A function to call every N seconds (optional).
187 static int
188 luaA_hooks_timer(lua_State *L)
190 if(lua_gettop(L) >= 1)
192 globalconf.timer.repeat = luaL_checknumber(L, 1);
194 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
195 luaA_registerfct(L, 2, &globalconf.hooks.timer);
197 ev_timer_again(globalconf.loop, &globalconf.timer);
200 lua_rawgeti(L, LUA_REGISTRYINDEX, globalconf.hooks.timer);
202 return 1;
205 #ifdef WITH_DBUS
206 /** Set the function to be called when a D-Bus event is received.
207 * The first argument passed to this function is the type of the message we
208 * receive: signal, method_call, method_return or error.
209 * The second argument is the path.
210 * The other arguments are a variable list of arguments.
211 * The function can return values using pair of type, value.
212 * For example: return "s", "hello", "i", 32
213 * \param L The Lua VM state.
214 * \return The number of elements pushed on stack.
215 * \luastack
216 * \lparam A function to call on D-Bus events.
218 static int
219 luaA_hooks_dbus(lua_State *L)
221 HANDLE_HOOK(L, globalconf.hooks.dbus);
223 #endif
225 const struct luaL_reg awesome_hooks_lib[] =
227 { "focus", luaA_hooks_focus },
228 { "unfocus", luaA_hooks_unfocus },
229 { "manage", luaA_hooks_manage },
230 { "unmanage", luaA_hooks_unmanage },
231 { "mouse_enter", luaA_hooks_mouse_enter },
232 { "mouse_leave", luaA_hooks_mouse_leave },
233 { "property", luaA_hooks_property },
234 { "arrange", luaA_hooks_arrange },
235 { "clients", luaA_hooks_clients },
236 { "tags", luaA_hooks_tags },
237 { "tagged", luaA_hooks_tagged },
238 { "timer", luaA_hooks_timer },
239 #ifdef WITH_DBUS
240 { "dbus", luaA_hooks_dbus },
241 #endif
242 { NULL, NULL }