]> Dogcows Code - chaz/openbox/blob - otk/timer.cc
80 cols
[chaz/openbox] / otk / timer.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
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
10 extern "C" {
11 #ifdef HAVE_SYS_SELECT_H
12 # include <sys/select.h>
13 #else
14 # ifdef HAVE_UNISTD_H
15 # include <sys/types.h>
16 # include <unistd.h>
17 # endif // HAVE_UNISTD_H
18 #endif // HAVE_SYS_SELECT_H
19 }
20
21 namespace otk {
22
23 timeval Timer::_nearest_timeout, Timer::_now;
24 Timer::TimerQ Timer::_q;
25
26 void Timer::timevalAdd(timeval &a, long msec)
27 {
28 a.tv_sec += msec / 1000;
29 a.tv_usec += (msec % 1000) * 1000;
30 a.tv_sec += a.tv_usec / 1000000;
31 a.tv_usec %= 1000000;
32 }
33
34 bool Timer::nearestTimeout(struct timeval &tm)
35 {
36 if (_q.empty())
37 return false;
38 tm.tv_sec = _nearest_timeout.tv_sec - _now.tv_sec;
39 tm.tv_usec = _nearest_timeout.tv_usec - _now.tv_usec;
40
41 while (tm.tv_usec < 0) {
42 tm.tv_usec += 1000000;
43 tm.tv_sec--;
44 }
45 tm.tv_sec += tm.tv_usec / 1000000;
46 tm.tv_usec %= 1000000;
47 if (tm.tv_sec < 0)
48 tm.tv_sec = 0;
49
50 return true;
51 }
52
53 void Timer::dispatchTimers(bool wait)
54 {
55 fd_set selset;
56 int fd;
57 timeval next;
58 Timer *curr;
59
60 gettimeofday(&_now, NULL);
61 _nearest_timeout = _now;
62 _nearest_timeout.tv_sec += 10000;
63
64 while (!_q.empty()) {
65 curr = _q.top();
66 /* since we overload the destructor to keep from removing from the middle of
67 the priority queue, set _del_me, we have to do our real delete in here.
68 */
69 if (curr->_del_me) {
70 _q.pop();
71 realDelete(curr);
72 continue;
73 }
74
75 // the queue is sorted, so if this timer shouldn't fire, none are ready
76 _nearest_timeout = curr->_timeout;
77 if (!timercmp(&_now, &_nearest_timeout, >))
78 break;
79
80 /* we set the last fired time to delay msec after the previous firing, then
81 re-insert. timers maintain their order and may trigger more than once
82 if they've waited more than one delay's worth of time.
83 */
84 _q.pop();
85 timevalAdd(curr->_last, curr->_delay);
86 curr->_action(curr->_data);
87 timevalAdd(curr->_timeout, curr->_delay);
88 _q.push(curr);
89 }
90
91 if (wait) {
92 // wait for the nearest trigger, or for X to do something interesting
93 fd = ConnectionNumber(**display);
94 FD_ZERO(&selset);
95 FD_SET(fd, &selset);
96 if (nearestTimeout(next))
97 select(fd + 1, &selset, NULL, NULL, &next);
98 else
99 select(fd + 1, &selset, NULL, NULL, NULL);
100 }
101 }
102
103 Timer::Timer(long delay, Timer::TimeoutHandler action, void *data)
104 : _delay(delay),
105 _action(action),
106 _data(data),
107 _del_me(false),
108 _last(_now),
109 _timeout(_now)
110 {
111 timevalAdd(_timeout, delay);
112 _q.push(this);
113 }
114
115 void Timer::operator delete(void *self)
116 {
117 Timer *t;
118 t = (Timer *)self;
119 t->_del_me = true;
120 }
121
122 void Timer::realDelete(Timer *me)
123 {
124 ::delete me;
125 }
126
127 void Timer::initialize(void)
128 {
129 gettimeofday(&_now, NULL);
130 _nearest_timeout.tv_sec = 100000;
131 _nearest_timeout.tv_usec = 0;
132 }
133
134 void Timer::destroy(void)
135 {
136 while(!_q.empty()) {
137 realDelete(_q.top());
138 _q.pop();
139 }
140 }
141
142 }
This page took 0.039477 seconds and 5 git commands to generate.