1 // -*- mode: C++; indent-tabs-mode: nil; -*-
4 # include "../config.h"
5 #endif // HAVE_CONFIG_H
7 #include "otk/display.hh"
14 OBTimer::OBTimer(TimeoutHandler *h) {
17 recur = timing = False;
21 OBTimer::~OBTimer(void) {
26 void OBTimer::setTimeout(long t) {
27 _timeout.tv_sec = t / 1000;
28 _timeout.tv_usec = t % 1000;
29 _timeout.tv_usec *= 1000;
33 void OBTimer::setTimeout(const timeval &t) {
34 _timeout.tv_sec = t.tv_sec;
35 _timeout.tv_usec = t.tv_usec;
39 void OBTimer::start(void) {
40 gettimeofday(&_start, 0);
44 Openbox::instance->timerManager()->addTimer(this);
49 void OBTimer::stop(void) {
52 Openbox::instance->timerManager()->removeTimer(this);
56 void OBTimer::halt(void) {
61 void OBTimer::fireTimeout(void) {
67 timeval OBTimer::timeRemaining(const timeval &tm) const {
68 timeval ret = endpoint();
70 ret.tv_sec -= tm.tv_sec;
71 ret.tv_usec -= tm.tv_usec;
73 return normalizeTimeval(ret);
77 timeval OBTimer::endpoint(void) const {
80 ret.tv_sec = _start.tv_sec + _timeout.tv_sec;
81 ret.tv_usec = _start.tv_usec + _timeout.tv_usec;
83 return normalizeTimeval(ret);
87 bool OBTimer::shouldFire(const timeval &tm) const {
88 timeval end = endpoint();
90 return ! ((tm.tv_sec < end.tv_sec) ||
91 (tm.tv_sec == end.tv_sec && tm.tv_usec < end.tv_usec));
95 void OBTimerQueueManager::fire()
98 timeval now, tm, *timeout = (timeval *) 0;
100 const int xfd = ConnectionNumber(otk::OBDisplay::display);
103 FD_SET(xfd, &rfds); // break on any x events
105 if (! timerList.empty()) {
106 const OBTimer* const timer = timerList.top();
108 gettimeofday(&now, 0);
109 tm = timer->timeRemaining(now);
114 select(xfd + 1, &rfds, 0, 0, timeout);
116 // check for timer timeout
117 gettimeofday(&now, 0);
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))
130 timer->fireTimeout();
132 if (timer->isRecurring())
138 void OBTimerQueueManager::addTimer(OBTimer *timer)
141 timerList.push(timer);
144 void OBTimerQueueManager::removeTimer(OBTimer* timer)
147 timerList.release(timer);