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