diff --git a/awesome.c b/awesome.c index b997e9760..532ba044e 100644 --- a/awesome.c +++ b/awesome.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include #include @@ -285,8 +286,7 @@ main(int argc, char **argv) xcb_generic_event_t *ev; struct sockaddr_un *addr; client_t *c; - time_t lastrun = 0; - struct timeval *tv = NULL; + struct timeval select_timeout, hook_lastrun, now, hook_nextrun; static struct option long_options[] = { {"help", 0, NULL, 'h'}, @@ -488,15 +488,23 @@ main(int argc, char **argv) if(dbusfd >= 0) FD_SET(dbusfd, &rd); FD_SET(xfd, &rd); - if(globalconf.stimeout) + if(timerisset(&globalconf.timer)) { - if(!tv) - tv = p_new(struct timeval, 1); - tv->tv_sec = MAX(globalconf.stimeout - (time(NULL) - lastrun), 1); + gettimeofday(&now, NULL); + timeradd(&hook_lastrun, &globalconf.timer, &hook_nextrun); + /* Need to do 2 tests, <= does not work with timercmp() */ + if(timercmp(&hook_nextrun, &now, <) || (timercmp(&hook_nextrun, &now, ==))) + { + gettimeofday(&hook_lastrun, NULL); + luaA_dofunction(globalconf.L, globalconf.hooks.timer, 0); + select_timeout = globalconf.timer; + } + else + timersub(&hook_nextrun, &now, &select_timeout); } - else - p_delete(&tv); - if(select(MAX(MAX(xfd, csfd), dbusfd) + 1, &rd, NULL, NULL, tv) == -1) + if(select(MAX(MAX(xfd, csfd), dbusfd) + 1, &rd, + NULL, NULL, + timerisset(&globalconf.timer) ? &select_timeout : NULL) == -1) { if(errno == EINTR) continue; @@ -547,12 +555,6 @@ main(int argc, char **argv) layout_refresh(); statusbar_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 220d8830c..9e241a308 100644 --- a/lua.c +++ b/lua.c @@ -324,7 +324,8 @@ luaA_hooks_urgent(lua_State *L) static int luaA_hooks_timer(lua_State *L) { - globalconf.stimeout = luaL_checknumber(L, 1); + globalconf.timer.tv_usec = 0; + globalconf.timer.tv_sec = luaL_checknumber(L, 1); if(lua_gettop(L) == 2 && lua_isfunction(L, 2)) { diff --git a/structs.h b/structs.h index eb4854b15..874bee1cd 100644 --- a/structs.h +++ b/structs.h @@ -434,7 +434,7 @@ struct awesome_t luaA_function timer; } hooks; /** The timeout after which we need to stop select() */ - long stimeout; + struct timeval timer; }; #endif