button, key: emit events
[awesome.git] / hooks.c
blob54055c6dc5ea20ebda04b05803d978810d52cdba
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 to be called every N seconds.
167 * \param L The Lua VM state.
168 * \return The number of elements pushed on stack.
169 * \luastack
170 * \lparam The number of seconds to run function every. Set 0 to disable.
171 * \lparam A function to call every N seconds (optional).
173 static int
174 luaA_hooks_timer(lua_State *L)
176 if(lua_gettop(L) >= 1)
178 globalconf.timer.repeat = luaL_checknumber(L, 1);
180 if(lua_gettop(L) == 2 && !lua_isnil(L, 2))
181 luaA_registerfct(L, 2, &globalconf.hooks.timer);
183 ev_timer_again(globalconf.loop, &globalconf.timer);
186 lua_rawgeti(L, LUA_REGISTRYINDEX, globalconf.hooks.timer);
188 return 1;
191 /** Set the function called on awesome exit
192 * \param L The Lua VM state.
193 * \return The number of elements pushed on stack.
194 * \luastack
195 * \lparam A function to call on awesome exit.
197 static int
198 luaA_hooks_exit(lua_State *L)
200 HANDLE_HOOK(L, globalconf.hooks.exit);
203 const struct luaL_reg awesome_hooks_lib[] =
205 { "focus", luaA_hooks_focus },
206 { "unfocus", luaA_hooks_unfocus },
207 { "manage", luaA_hooks_manage },
208 { "unmanage", luaA_hooks_unmanage },
209 { "mouse_enter", luaA_hooks_mouse_enter },
210 { "mouse_leave", luaA_hooks_mouse_leave },
211 { "property", luaA_hooks_property },
212 { "clients", luaA_hooks_clients },
213 { "tags", luaA_hooks_tags },
214 { "tagged", luaA_hooks_tagged },
215 { "timer", luaA_hooks_timer },
216 { "exit", luaA_hooks_exit },
217 { NULL, NULL }