]> icculus.org git repositories - dana/openbox.git/blob - otk/timer.cc
new timer infrastructure. takes a function pointer for the timeout, with a void*...
[dana/openbox.git] / otk / 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 "display.hh"
9 #include "util.hh"
10
11 namespace otk {
12
13 static timeval normalizeTimeval(const timeval &tm)
14 {
15   timeval ret = tm;
16
17   while (ret.tv_usec < 0) {
18     if (ret.tv_sec > 0) {
19       --ret.tv_sec;
20       ret.tv_usec += 1000000;
21     } else {
22       ret.tv_usec = 0;
23     }
24   }
25
26   if (ret.tv_usec >= 1000000) {
27     ret.tv_sec += ret.tv_usec / 1000000;
28     ret.tv_usec %= 1000000;
29   }
30
31   if (ret.tv_sec < 0) ret.tv_sec = 0;
32
33   return ret;
34 }
35
36
37 OBTimer::OBTimer(OBTimerQueueManager *m, OBTimeoutHandler h, OBTimeoutData d)
38 {
39   manager = m;
40   handler = h;
41   data = d;
42
43   recur = timing = False;
44 }
45
46
47 OBTimer::~OBTimer(void)
48 {
49   if (timing) stop();
50 }
51
52
53 void OBTimer::setTimeout(long t)
54 {
55   _timeout.tv_sec = t / 1000;
56   _timeout.tv_usec = t % 1000;
57   _timeout.tv_usec *= 1000;
58 }
59
60
61 void OBTimer::setTimeout(const timeval &t)
62 {
63   _timeout.tv_sec = t.tv_sec;
64   _timeout.tv_usec = t.tv_usec;
65 }
66
67
68 void OBTimer::start(void)
69 {
70   gettimeofday(&_start, 0);
71
72   if (! timing) {
73     timing = True;
74     manager->addTimer(this);
75   }
76 }
77
78
79 void OBTimer::stop(void)
80 {
81   timing = False;
82
83   manager->removeTimer(this);
84 }
85
86
87 void OBTimer::halt(void)
88 {
89   timing = False;
90 }
91
92
93 void OBTimer::fireTimeout(void)
94 {
95   if (handler)
96     handler(data);
97 }
98
99
100 timeval OBTimer::timeRemaining(const timeval &tm) const
101 {
102   timeval ret = endpoint();
103
104   ret.tv_sec  -= tm.tv_sec;
105   ret.tv_usec -= tm.tv_usec;
106
107   return normalizeTimeval(ret);
108 }
109
110
111 timeval OBTimer::endpoint(void) const
112 {
113   timeval ret;
114
115   ret.tv_sec = _start.tv_sec + _timeout.tv_sec;
116   ret.tv_usec = _start.tv_usec + _timeout.tv_usec;
117
118   return normalizeTimeval(ret);
119 }
120
121
122 bool OBTimer::shouldFire(const timeval &tm) const
123 {
124   timeval end = endpoint();
125
126   return ! ((tm.tv_sec < end.tv_sec) ||
127             (tm.tv_sec == end.tv_sec && tm.tv_usec < end.tv_usec));
128 }
129
130
131 void OBTimerQueueManager::fire()
132 {
133   fd_set rfds;
134   timeval now, tm, *timeout = (timeval *) 0;
135
136   const int xfd = ConnectionNumber(otk::OBDisplay::display);
137   
138   FD_ZERO(&rfds);
139   FD_SET(xfd, &rfds); // break on any x events
140
141   if (! timerList.empty()) {
142     const OBTimer* const timer = timerList.top();
143
144     gettimeofday(&now, 0);
145     tm = timer->timeRemaining(now);
146
147     timeout = &tm;
148   }
149
150   select(xfd + 1, &rfds, 0, 0, timeout);
151
152   // check for timer timeout
153   gettimeofday(&now, 0);
154
155   // there is a small chance for deadlock here:
156   // *IF* the timer list keeps getting refreshed *AND* the time between
157   // timer->start() and timer->shouldFire() is within the timer's period
158   // then the timer will keep firing.  This should be VERY near impossible.
159   while (! timerList.empty()) {
160     OBTimer *timer = timerList.top();
161     if (! timer->shouldFire(now))
162       break;
163
164     timerList.pop();
165
166     timer->fireTimeout();
167     timer->halt();
168     if (timer->isRecurring())
169       timer->start();
170   }
171 }
172
173
174 void OBTimerQueueManager::addTimer(OBTimer *timer)
175 {
176   assert(timer);
177   timerList.push(timer);
178 }
179
180 void OBTimerQueueManager::removeTimer(OBTimer* timer)
181 {
182   assert(timer);
183   timerList.release(timer);
184 }
185
186 }