]> icculus.org git repositories - dana/openbox.git/blob - src/timer.cc
WE DONT USE BASE DISPLAY FOR ANYTHING ANY MORE!!@^!*@*!! YAY
[dana/openbox.git] / src / timer.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2
3 #ifdef    HAVE_CONFIG_H
4 #  include "../config.h"
5 #endif // HAVE_CONFIG_H
6
7 #include "timer.hh"
8 #include "util.hh"
9
10 namespace ob {
11
12 BTimer::BTimer(TimerQueueManager *m, TimeoutHandler *h) {
13   manager = m;
14   handler = h;
15
16   recur = timing = False;
17 }
18
19
20 BTimer::~BTimer(void) {
21   if (timing) stop();
22 }
23
24
25 void BTimer::setTimeout(long t) {
26   _timeout.tv_sec = t / 1000;
27   _timeout.tv_usec = t % 1000;
28   _timeout.tv_usec *= 1000;
29 }
30
31
32 void BTimer::setTimeout(const timeval &t) {
33   _timeout.tv_sec = t.tv_sec;
34   _timeout.tv_usec = t.tv_usec;
35 }
36
37
38 void BTimer::start(void) {
39   gettimeofday(&_start, 0);
40
41   if (! timing) {
42     timing = True;
43     manager->addTimer(this);
44   }
45 }
46
47
48 void BTimer::stop(void) {
49   timing = False;
50
51   manager->removeTimer(this);
52 }
53
54
55 void BTimer::halt(void) {
56   timing = False;
57 }
58
59
60 void BTimer::fireTimeout(void) {
61   if (handler)
62     handler->timeout();
63 }
64
65
66 timeval BTimer::timeRemaining(const timeval &tm) const {
67   timeval ret = endpoint();
68
69   ret.tv_sec  -= tm.tv_sec;
70   ret.tv_usec -= tm.tv_usec;
71
72   return normalizeTimeval(ret);
73 }
74
75
76 timeval BTimer::endpoint(void) const {
77   timeval ret;
78
79   ret.tv_sec = _start.tv_sec + _timeout.tv_sec;
80   ret.tv_usec = _start.tv_usec + _timeout.tv_usec;
81
82   return normalizeTimeval(ret);
83 }
84
85
86 bool BTimer::shouldFire(const timeval &tm) const {
87   timeval end = endpoint();
88
89   return ! ((tm.tv_sec < end.tv_sec) ||
90             (tm.tv_sec == end.tv_sec && tm.tv_usec < end.tv_usec));
91 }
92
93 }