[hooks] Fix timer resolution

Signed-off-by: Julien Danjou <julien@danjou.info>
This commit is contained in:
Julien Danjou 2008-05-31 23:43:28 +02:00
parent 01fe4e3331
commit b202b22314
3 changed files with 20 additions and 17 deletions

View File

@ -30,6 +30,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/un.h>
#include <fcntl.h>
#include <signal.h>
@ -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();

3
lua.c
View File

@ -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))
{

View File

@ -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