[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;
struct sockaddr_un *addr;
client_t *c;
time_t lastrun = time(NULL);
struct timeval *tv = NULL;
static struct option long_options[] =
{
{"help", 0, NULL, 'h'},
@ -478,7 +480,7 @@ main(int argc, char **argv)
/* refresh everything before waiting events */
layout_refresh();
/* main event loop, also reads status text from socket */
/* main event loop */
while(running)
{
FD_ZERO(&rd);
@ -487,7 +489,15 @@ main(int argc, char **argv)
if(dbusfd >= 0)
FD_SET(dbusfd, &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)
continue;
@ -536,6 +546,12 @@ main(int argc, char **argv)
}
layout_refresh();
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();

20
lua.c
View file

@ -317,6 +317,25 @@ luaA_hooks_urgent(lua_State *L)
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.
* \param A string with a font name in Pango format.
*/
@ -396,6 +415,7 @@ luaA_parserc(const char *rcfile)
{ "arrange", luaA_hooks_arrange },
{ "titleupdate", luaA_hooks_titleupdate },
{ "urgent", luaA_hooks_urgent },
{ "timer", luaA_hooks_timer },
{ NULL, NULL }
};

View file

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