[lua] Add a timed hook

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-05-29 13:47:11 +02:00
parent 14c29006f2
commit 994f7abbc1
3 changed files with 42 additions and 2 deletions

View File

@ -285,6 +285,8 @@ main(int argc, char **argv)
xcb_generic_event_t *ev; xcb_generic_event_t *ev;
struct sockaddr_un *addr; struct sockaddr_un *addr;
client_t *c; client_t *c;
time_t lastrun = time(NULL);
struct timeval *tv = NULL;
static struct option long_options[] = static struct option long_options[] =
{ {
{"help", 0, NULL, 'h'}, {"help", 0, NULL, 'h'},
@ -478,7 +480,7 @@ main(int argc, char **argv)
/* refresh everything before waiting events */ /* refresh everything before waiting events */
layout_refresh(); layout_refresh();
/* main event loop, also reads status text from socket */ /* main event loop */
while(running) while(running)
{ {
FD_ZERO(&rd); FD_ZERO(&rd);
@ -487,7 +489,15 @@ main(int argc, char **argv)
if(dbusfd >= 0) if(dbusfd >= 0)
FD_SET(dbusfd, &rd); FD_SET(dbusfd, &rd);
FD_SET(xfd, &rd); FD_SET(xfd, &rd);
if(select(MAX(MAX(xfd, csfd), dbusfd) + 1, &rd, NULL, NULL, NULL) == -1) if(globalconf.stimeout)
{
if(!tv)
tv = p_new(struct timeval, 1);
tv->tv_sec = MAX(globalconf.stimeout - (time(NULL) - lastrun), 1);
}
else
p_delete(&tv);
if(select(MAX(MAX(xfd, csfd), dbusfd) + 1, &rd, NULL, NULL, tv) == -1)
{ {
if(errno == EINTR) if(errno == EINTR)
continue; continue;
@ -536,6 +546,12 @@ main(int argc, char **argv)
} }
layout_refresh(); layout_refresh();
xcb_aux_sync(globalconf.connection); xcb_aux_sync(globalconf.connection);
if(tv && lastrun + globalconf.stimeout <= time(NULL))
{
luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0);
lastrun = time(NULL);
}
} }
a_dbus_cleanup(); a_dbus_cleanup();

20
lua.c
View File

@ -317,6 +317,25 @@ luaA_hooks_urgent(lua_State *L)
return 0; return 0;
} }
/** Set the function to be called every N seconds.
* \param The number of seconds to run function every. Set 0 to disable.
* \param A function to call every N seconds (optionnal).
*/
static int
luaA_hooks_timer(lua_State *L)
{
globalconf.stimeout = luaL_checknumber(L, 1);
if(lua_gettop(L) == 2 && lua_isfunction(L, 2))
{
if(globalconf.hooks.timer)
luaL_unref(L, LUA_REGISTRYINDEX, globalconf.hooks.timer);
globalconf.hooks.timer = luaL_ref(L, LUA_REGISTRYINDEX);
}
return 0;
}
/** Set default font. /** Set default font.
* \param A string with a font name in Pango format. * \param A string with a font name in Pango format.
*/ */
@ -396,6 +415,7 @@ luaA_parserc(const char *rcfile)
{ "arrange", luaA_hooks_arrange }, { "arrange", luaA_hooks_arrange },
{ "titleupdate", luaA_hooks_titleupdate }, { "titleupdate", luaA_hooks_titleupdate },
{ "urgent", luaA_hooks_urgent }, { "urgent", luaA_hooks_urgent },
{ "timer", luaA_hooks_timer },
{ NULL, NULL } { NULL, NULL }
}; };

View File

@ -438,7 +438,11 @@ struct awesome_t
luaA_function titleupdate; luaA_function titleupdate;
/** Command to run on urgent flag */ /** Command to run on urgent flag */
luaA_function urgent; luaA_function urgent;
/** Command to run on time */
luaA_function timer;
} hooks; } hooks;
/** The timeout after which we need to stop select() */
long stimeout;
}; };
#endif #endif