From 994f7abbc1403d6643c2dde3a5b4bd035b608b53 Mon Sep 17 00:00:00 2001 From: Julien Danjou Date: Thu, 29 May 2008 13:47:11 +0200 Subject: [PATCH] [lua] Add a timed hook Signed-off-by: Julien Danjou --- awesome.c | 20 ++++++++++++++++++-- lua.c | 20 ++++++++++++++++++++ structs.h | 4 ++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/awesome.c b/awesome.c index e172da64..e1d99c50 100644 --- a/awesome.c +++ b/awesome.c @@ -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(); diff --git a/lua.c b/lua.c index b6e3152d..332393e9 100644 --- a/lua.c +++ b/lua.c @@ -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 } }; diff --git a/structs.h b/structs.h index e5b3f550..0fc16c79 100644 --- a/structs.h +++ b/structs.h @@ -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