-// Timer.cc for Openbox
-// Copyright (c) 2001 Sean 'Shaleh' Perry <shaleh@debian.org>
+// -*- mode: C++; indent-tabs-mode: nil; -*-
+// Timer.cc for Blackbox - An X11 Window Manager
+// Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
// Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
-// stupid macros needed to access some functions in version 2 of the GNU C
-// library
-#ifndef _GNU_SOURCE
-# define _GNU_SOURCE
-#endif // _GNU_SOURCE
-
#ifdef HAVE_CONFIG_H
# include "../config.h"
#endif // HAVE_CONFIG_H
-#include "BaseDisplay.h"
-#include "Timer.h"
+#include "BaseDisplay.hh"
+#include "Timer.hh"
+#include "Util.hh"
-BTimer::BTimer(BaseDisplay *d, TimeoutHandler *h) {
- display = d;
+BTimer::BTimer(TimerQueueManager *m, TimeoutHandler *h) {
+ manager = m;
handler = h;
- once = timing = False;
+ recur = timing = False;
}
+
BTimer::~BTimer(void) {
if (timing) stop();
}
+
void BTimer::setTimeout(long t) {
_timeout.tv_sec = t / 1000;
- _timeout.tv_usec = t;
- _timeout.tv_usec -= (_timeout.tv_sec * 1000);
+ _timeout.tv_usec = t % 1000;
_timeout.tv_usec *= 1000;
}
-void BTimer::setTimeout(timeval t) {
+
+void BTimer::setTimeout(const timeval &t) {
_timeout.tv_sec = t.tv_sec;
_timeout.tv_usec = t.tv_usec;
}
+
void BTimer::start(void) {
gettimeofday(&_start, 0);
if (! timing) {
timing = True;
- display->addTimer(this);
+ manager->addTimer(this);
}
}
+
void BTimer::stop(void) {
timing = False;
- display->removeTimer(this);
+ manager->removeTimer(this);
}
+
+void BTimer::halt(void) {
+ timing = False;
+}
+
+
void BTimer::fireTimeout(void) {
- if (handler) handler->timeout();
+ if (handler)
+ handler->timeout();
+}
+
+
+timeval BTimer::timeRemaining(const timeval &tm) const {
+ timeval ret = endpoint();
+
+ ret.tv_sec -= tm.tv_sec;
+ ret.tv_usec -= tm.tv_usec;
+
+ return normalizeTimeval(ret);
+}
+
+
+timeval BTimer::endpoint(void) const {
+ timeval ret;
+
+ ret.tv_sec = _start.tv_sec + _timeout.tv_sec;
+ ret.tv_usec = _start.tv_usec + _timeout.tv_usec;
+
+ return normalizeTimeval(ret);
+}
+
+
+bool BTimer::shouldFire(const timeval &tm) const {
+ timeval end = endpoint();
+
+ return ! ((tm.tv_sec < end.tv_sec) ||
+ (tm.tv_sec == end.tv_sec && tm.tv_usec < end.tv_usec));
}