Naughty: Remove some code duplication
[awesome.git] / hooks.c
blobf1241936adf0d6c13ee821f8a0b7b3a2b60a24c7
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 #define HANDLE_HOOK(L, h) \
25 do { \
26 if(lua_gettop(L) == 1) \
27 luaA_registerfct(L, 1, &h); \
28 lua_rawgeti(L, LUA_REGISTRYINDEX, h); \
29 return 1; \
30 } while(0)
32 /** Set the function called each time a client gets focus. This function is
33 * called with the client object as argument.
34 * \param L The Lua VM state.
35 * \return The number of elements pushed on stack.
36 * \luastack
37 * \lparam A function to call each time a client gets focus.
39 static int
40 luaA_hooks_focus(lua_State *L)
42 HANDLE_HOOK(L, globalconf.hooks.focus);
45 /** Set the function called each time a client loses focus. This function is
46 * called with the client object as argument.
47 * \param L The Lua VM state.
48 * \return The number of elements pushed on stack.
49 * \luastack
50 * \lparam A function to call each time a client loses focus.
52 static int
53 luaA_hooks_unfocus(lua_State *L)
55 HANDLE_HOOK(L, globalconf.hooks.unfocus);
58 /** Set the function called each time a new client appears. This function is
59 * called with the client object as argument, plus a boolean argument which is
60 * true if the client is managed at startup.
61 * \param L The Lua VM state.
62 * \return The number of elements pushed on stack.
63 * \luastack
64 * \lparam A function to call on each new client.
66 static int
67 luaA_hooks_manage(lua_State *L)
69 HANDLE_HOOK(L, globalconf.hooks.manage);
72 /** Set the function called each time a client goes away. This function is
73 * called with the client object as argument.
74 * \param L The Lua VM state.
75 * \return The number of elements pushed on stack.
76 * \luastack
77 * \lparam A function to call when a client goes away.
79 static int
80 luaA_hooks_unmanage(lua_State *L)
82 HANDLE_HOOK(L, globalconf.hooks.unmanage);
85 /** Set the function called each time the mouse enter a window. This
86 * function is called with the client object as argument.
87 * \param L The Lua VM state.
88 * \return The number of elements pushed on stack.
89 * \luastack
90 * \lparam A function to call each time a client gets mouse over it.
92 static int
93 luaA_hooks_mouse_enter(lua_State *L)
95 HANDLE_HOOK(L, globalconf.hooks.mouse_enter);
98 /** Set the function called each time the mouse leave a window. This
99 * function is called with the client object as argument.
100 * \param L The Lua VM state.
101 * \return The number of elements pushed on stack.
102 * \luastack
103 * \lparam A function to call each time a client gets mouse over it.
105 static int
106 luaA_hooks_mouse_leave(lua_State *L)
108 HANDLE_HOOK(L, globalconf.hooks.mouse_leave);
111 /** Set the function called on each client list change.
112 * This function is called without any argument.
113 * \param L The Lua VM state.
114 * \return The number of elements pushed on stack.
115 * \luastack
116 * \lparam A function to call on each client list change.
118 static int
119 luaA_hooks_clients(lua_State *L)
121 HANDLE_HOOK(L, globalconf.hooks.clients);
124 /** Set the function called on each screen tag list change.
125 * This function is called with a screen number as first argument,
126 * the tag object as second and the action (add, remove, select or unselect)
127 * as third.
128 * \param L The Lua VM state.
129 * \return The number of elements pushed on stack.
130 * \luastack
131 * \lparam A function to call on each tag list change.
133 static int
134 luaA_hooks_tags(lua_State *L)
136 HANDLE_HOOK(L, globalconf.hooks.tags);
139 /** Set the function called on each client's tags change.
140 * This function is called with the client and the tag as argument.
141 * \param L The Lua VM state.
142 * \return The number of elements pushed on stack.
143 * \luastack
144 * \lparam A function to call on each client's tags change.
146 static int
147 luaA_hooks_tagged(lua_State *L)
149 HANDLE_HOOK(L, globalconf.hooks.tagged);
152 /** Set the function called on each client's property change.
153 * This function is called with the client object as argument and the
154 * property name.
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 client property update.
160 static int
161 luaA_hooks_property(lua_State *L)
163 HANDLE_HOOK(L, globalconf.hooks.property);
166 /** Set the function called on each startup-notification events
167 * This function is called with a table and various fields set to describe the
168 * vents.
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 startup-notification event.
174 static int
175 luaA_hooks_startup_notification(lua_State *L)
177 HANDLE_HOOK(L, globalconf.hooks.startup_notification);
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 /** Set the function called on awesome exit
206 * \param L The Lua VM state.
207 * \return The number of elements pushed on stack.
208 * \luastack
209 * \lparam A function to call on awesome exit.
211 static int
212 luaA_hooks_exit(lua_State *L)
214 HANDLE_HOOK(L, globalconf.hooks.exit);
217 #ifdef WITH_DBUS
218 /** Set the function to be called when a D-Bus event is received.
219 * The first argument passed to this function is the type of the message we
220 * receive: signal, method_call, method_return or error.
221 * The second argument is the path.
222 * The other arguments are a variable list of arguments.
223 * The function can return values using pair of type, value.
224 * For example: return "s", "hello", "i", 32
225 * \param L The Lua VM state.
226 * \return The number of elements pushed on stack.
227 * \luastack
228 * \lparam A function to call on D-Bus events.
230 static int
231 luaA_hooks_dbus(lua_State *L)
233 HANDLE_HOOK(L, globalconf.hooks.dbus);
235 #endif
237 const struct luaL_reg awesome_hooks_lib[] =
239 { "focus", luaA_hooks_focus },
240 { "unfocus", luaA_hooks_unfocus },
241 { "manage", luaA_hooks_manage },
242 { "unmanage", luaA_hooks_unmanage },
243 { "mouse_enter", luaA_hooks_mouse_enter },
244 { "mouse_leave", luaA_hooks_mouse_leave },
245 { "property", luaA_hooks_property },
246 { "clients", luaA_hooks_clients },
247 { "tags", luaA_hooks_tags },
248 { "tagged", luaA_hooks_tagged },
249 { "startup_notification", luaA_hooks_startup_notification },
250 { "timer", luaA_hooks_timer },
251 { "exit", luaA_hooks_exit },
252 #ifdef WITH_DBUS
253 { "dbus", luaA_hooks_dbus },
254 #endif
255 { NULL, NULL }