1
// Timer.cc for FbTk - Fluxbox Toolkit
2
// Copyright (c) 2003 - 2005 Henrik Kinnunen (fluxgen at fluxbox dot org)
4
// Timer.cc for Blackbox - An X11 Window Manager
5
// Copyright (c) 1997 - 2000 Brad Hughes (bhughes at tcac.net)
7
// Permission is hereby granted, free of charge, to any person obtaining a
8
// copy of this software and associated documentation files (the "Software"),
9
// to deal in the Software without restriction, including without limitation
10
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
// and/or sell copies of the Software, and to permit persons to whom the
12
// Software is furnished to do so, subject to the following conditions:
14
// The above copyright notice and this permission notice shall be included in
15
// all copies or substantial portions of the Software.
17
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23
// DEALINGS IN THE SOFTWARE.
36
#endif // HAVE_CONFIG_H
39
#include <sys/types.h>
49
Timer::TimerList Timer::m_timerlist;
51
Timer::Timer():m_timing(false), m_once(false) {
55
Timer::Timer(RefCount<Command> &handler):
63
if (isTiming()) stop();
67
void Timer::setTimeout(time_t t) {
68
m_timeout.tv_sec = t / 1000;
69
m_timeout.tv_usec = t;
70
m_timeout.tv_usec -= (m_timeout.tv_sec * 1000);
71
m_timeout.tv_usec *= 1000;
75
void Timer::setTimeout(timeval t) {
76
m_timeout.tv_sec = t.tv_sec;
77
m_timeout.tv_usec = t.tv_usec;
80
void Timer::setCommand(RefCount<Command> &cmd) {
85
gettimeofday(&m_start, 0);
87
// only add Timers that actually DO something
88
if (! m_timing && *m_handler) {
90
addTimer(this); //add us to the list
97
removeTimer(this); //remove us from the list
101
void Timer::fireTimeout() {
103
m_handler->execute();
106
void Timer::updateTimers(int fd) {
108
timeval now, tm, *timeout = 0;
114
if (m_timerlist.size() > 0) {
115
gettimeofday(&now, 0);
117
tm.tv_sec = tm.tv_usec = 0l;
119
Timer *timer = m_timerlist.front();
121
tm.tv_sec = timer->getStartTime().tv_sec +
122
timer->getTimeout().tv_sec - now.tv_sec;
123
tm.tv_usec = timer->getStartTime().tv_usec +
124
timer->getTimeout().tv_usec - now.tv_usec;
126
while (tm.tv_usec >= 1000000) {
128
tm.tv_usec -= 1000000;
131
while (tm.tv_usec < 0) {
134
tm.tv_usec += 1000000;
144
select(fd + 1, &rfds, 0, 0, timeout);
146
TimerList::iterator it;
148
// someone set the date of the machine BACK
149
// so we have to adjust the start_time
150
static time_t last_time = time(0);
151
if (time(0) < last_time) {
153
time_t delta = time(0) - last_time;
155
for (it = m_timerlist.begin(); it != m_timerlist.end(); it++) {
156
(*it)->m_start.tv_sec += delta;
161
// check for timer timeout
162
gettimeofday(&now, 0);
164
//must check end ...the timer might remove
165
//it self from the list (should be fixed in the future)
166
for(it = m_timerlist.begin(); it != m_timerlist.end(); ) {
167
//This is to make sure we don't get an invalid iterator
168
//when we do fireTimeout
170
tm.tv_sec = t.getStartTime().tv_sec +
171
t.getTimeout().tv_sec;
172
tm.tv_usec = t.getStartTime().tv_usec +
173
t.getTimeout().tv_usec;
175
if (((now.tv_sec < tm.tv_sec) ||
176
(now.tv_sec == tm.tv_sec && now.tv_usec < tm.tv_usec)))
180
// restart the current timer so that the start time is updated
184
// Note that this mustn't be done if we're deleting the
185
// entry from the list, so therefore it's not in the update
186
// section of the for loop
189
// Since the default stop behaviour results in the timer
190
// being removed, we must remove it here, so that the iterator
191
// lives well. Another option would be to add it to another
192
// list, and then just go through that list and stop them all.
193
it = m_timerlist.erase(it);
201
void Timer::addTimer(Timer *timer) {
204
TimerList::iterator it = m_timerlist.begin();
205
TimerList::iterator it_end = m_timerlist.end();
207
for (; it != it_end; ++it, ++index) {
208
if (((*it)->getTimeout().tv_sec > timer->getTimeout().tv_sec) ||
209
(((*it)->getTimeout().tv_sec == timer->getTimeout().tv_sec) &&
210
((*it)->getTimeout().tv_usec >= timer->getTimeout().tv_usec))) {
214
m_timerlist.insert(it, timer);
218
void Timer::removeTimer(Timer *timer) {
220
m_timerlist.remove(timer);
223
}; // end namespace FbTk