new OBTimer interface
This commit is contained in:
parent
578bedc289
commit
7ebccbf39a
5 changed files with 37 additions and 38 deletions
36
otk/timer.cc
36
otk/timer.cc
|
@ -35,17 +35,17 @@ static timeval normalizeTimeval(const timeval &tm)
|
||||||
|
|
||||||
OBTimer::OBTimer(OBTimerQueueManager *m, OBTimeoutHandler h, OBTimeoutData d)
|
OBTimer::OBTimer(OBTimerQueueManager *m, OBTimeoutHandler h, OBTimeoutData d)
|
||||||
{
|
{
|
||||||
manager = m;
|
_manager = m;
|
||||||
handler = h;
|
_handler = h;
|
||||||
data = d;
|
_data = d;
|
||||||
|
|
||||||
recur = timing = false;
|
_recur = _timing = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
OBTimer::~OBTimer(void)
|
OBTimer::~OBTimer(void)
|
||||||
{
|
{
|
||||||
if (timing) stop();
|
if (_timing) stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -68,33 +68,33 @@ void OBTimer::start(void)
|
||||||
{
|
{
|
||||||
gettimeofday(&_start, 0);
|
gettimeofday(&_start, 0);
|
||||||
|
|
||||||
if (! timing) {
|
if (! _timing) {
|
||||||
timing = true;
|
_timing = true;
|
||||||
manager->addTimer(this);
|
_manager->addTimer(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void OBTimer::stop(void)
|
void OBTimer::stop(void)
|
||||||
{
|
{
|
||||||
if (timing) {
|
if (_timing) {
|
||||||
timing = false;
|
_timing = false;
|
||||||
|
|
||||||
manager->removeTimer(this);
|
_manager->removeTimer(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void OBTimer::fireTimeout(void)
|
void OBTimer::fire(void)
|
||||||
{
|
{
|
||||||
if (handler)
|
if (_handler)
|
||||||
handler(data);
|
_handler(_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
timeval OBTimer::timeRemaining(const timeval &tm) const
|
timeval OBTimer::remainingTime(const timeval &tm) const
|
||||||
{
|
{
|
||||||
timeval ret = endpoint();
|
timeval ret = endTime();
|
||||||
|
|
||||||
ret.tv_sec -= tm.tv_sec;
|
ret.tv_sec -= tm.tv_sec;
|
||||||
ret.tv_usec -= tm.tv_usec;
|
ret.tv_usec -= tm.tv_usec;
|
||||||
|
@ -103,7 +103,7 @@ timeval OBTimer::timeRemaining(const timeval &tm) const
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
timeval OBTimer::endpoint(void) const
|
timeval OBTimer::endTime(void) const
|
||||||
{
|
{
|
||||||
timeval ret;
|
timeval ret;
|
||||||
|
|
||||||
|
@ -116,7 +116,7 @@ timeval OBTimer::endpoint(void) const
|
||||||
|
|
||||||
bool OBTimer::shouldFire(const timeval &tm) const
|
bool OBTimer::shouldFire(const timeval &tm) const
|
||||||
{
|
{
|
||||||
timeval end = endpoint();
|
timeval end = endTime();
|
||||||
|
|
||||||
return ! ((tm.tv_sec < end.tv_sec) ||
|
return ! ((tm.tv_sec < end.tv_sec) ||
|
||||||
(tm.tv_sec == end.tv_sec && tm.tv_usec < end.tv_usec));
|
(tm.tv_sec == end.tv_sec && tm.tv_usec < end.tv_usec));
|
||||||
|
|
28
otk/timer.hh
28
otk/timer.hh
|
@ -32,15 +32,15 @@ typedef void (*OBTimeoutHandler)(OBTimeoutData);
|
||||||
class OBTimer {
|
class OBTimer {
|
||||||
private:
|
private:
|
||||||
//! The manager which to add ourself to and remove ourself after we are done
|
//! The manager which to add ourself to and remove ourself after we are done
|
||||||
OBTimerQueueManager *manager;
|
OBTimerQueueManager *_manager;
|
||||||
//! The function to call when the time elapses
|
//! The function to call when the time elapses
|
||||||
OBTimeoutHandler handler;
|
OBTimeoutHandler _handler;
|
||||||
//! The data which gets passed along to the OBTimeoutHandler
|
//! The data which gets passed along to the OBTimeoutHandler
|
||||||
OBTimeoutData data;
|
OBTimeoutData _data;
|
||||||
//! Determines if the timer is currently started
|
//! Determines if the timer is currently started
|
||||||
bool timing;
|
bool _timing;
|
||||||
//! When this is true, the timer will reset itself to fire again every time
|
//! When this is true, the timer will reset itself to fire again every time
|
||||||
bool recur;
|
bool _recur;
|
||||||
|
|
||||||
//! The time at which the timer started
|
//! The time at which the timer started
|
||||||
timeval _start;
|
timeval _start;
|
||||||
|
@ -65,32 +65,32 @@ public:
|
||||||
virtual ~OBTimer();
|
virtual ~OBTimer();
|
||||||
|
|
||||||
//! Fires the timer, calling its OBTimeoutHandler
|
//! Fires the timer, calling its OBTimeoutHandler
|
||||||
void fireTimeout();
|
void fire();
|
||||||
|
|
||||||
//! Returns if the OBTimer is started and timing
|
//! Returns if the OBTimer is started and timing
|
||||||
inline bool isTiming() const { return timing; }
|
inline bool timing() const { return _timing; }
|
||||||
//! Returns if the OBTimer is going to repeat
|
//! Returns if the OBTimer is going to repeat
|
||||||
inline bool isRecurring() const { return recur; }
|
inline bool recurring() const { return _recur; }
|
||||||
|
|
||||||
//! Gets the amount of time the OBTimer should last before firing
|
//! Gets the amount of time the OBTimer should last before firing
|
||||||
inline const timeval &getTimeout() const { return _timeout; }
|
inline const timeval &timeout() const { return _timeout; }
|
||||||
//! Gets the time at which the OBTimer started
|
//! Gets the time at which the OBTimer started
|
||||||
inline const timeval &getStartTime() const { return _start; }
|
inline const timeval &startTime() const { return _start; }
|
||||||
|
|
||||||
//! Gets the amount of time left before the OBTimer fires
|
//! Gets the amount of time left before the OBTimer fires
|
||||||
timeval timeRemaining(const timeval &tm) const;
|
timeval remainingTime(const timeval &tm) const;
|
||||||
//! Returns if the OBTimer is past its timeout time, and should fire
|
//! Returns if the OBTimer is past its timeout time, and should fire
|
||||||
bool shouldFire(const timeval &tm) const;
|
bool shouldFire(const timeval &tm) const;
|
||||||
|
|
||||||
//! Gets the time at which the OBTimer will fire
|
//! Gets the time at which the OBTimer will fire
|
||||||
timeval endpoint() const;
|
timeval endTime() const;
|
||||||
|
|
||||||
//! Sets the OBTimer to repeat or not
|
//! Sets the OBTimer to repeat or not
|
||||||
/*!
|
/*!
|
||||||
@param b If true, the timer is set to repeat; otherwise, it will fire only
|
@param b If true, the timer is set to repeat; otherwise, it will fire only
|
||||||
once
|
once
|
||||||
*/
|
*/
|
||||||
inline void recurring(bool b) { recur = b; }
|
inline void setRecurring(bool b) { _recur = b; }
|
||||||
|
|
||||||
//! Sets the amount of time for the OBTimer to last in milliseconds
|
//! Sets the amount of time for the OBTimer to last in milliseconds
|
||||||
/*!
|
/*!
|
||||||
|
@ -124,7 +124,7 @@ public:
|
||||||
@return true if this OBTimer will fire before 'other'; otherwise, false
|
@return true if this OBTimer will fire before 'other'; otherwise, false
|
||||||
*/
|
*/
|
||||||
bool operator<(const OBTimer& other) const
|
bool operator<(const OBTimer& other) const
|
||||||
{ return shouldFire(other.endpoint()); }
|
{ return shouldFire(other.endTime()); }
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,7 +23,7 @@ void OBTimerQueueManager::fire()
|
||||||
const OBTimer* const timer = timerList.top();
|
const OBTimer* const timer = timerList.top();
|
||||||
|
|
||||||
gettimeofday(&now, 0);
|
gettimeofday(&now, 0);
|
||||||
tm = timer->timeRemaining(now);
|
tm = timer->remainingTime(now);
|
||||||
|
|
||||||
timeout = &tm;
|
timeout = &tm;
|
||||||
}
|
}
|
||||||
|
@ -44,8 +44,8 @@ void OBTimerQueueManager::fire()
|
||||||
|
|
||||||
timerList.pop();
|
timerList.pop();
|
||||||
|
|
||||||
timer->fireTimeout();
|
timer->fire();
|
||||||
if (timer->isRecurring())
|
if (timer->recurring())
|
||||||
timer->start();
|
timer->start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1132,7 +1132,7 @@ void Blackbox::reconfigure(void) {
|
||||||
|
|
||||||
reconfigure_wait = True;
|
reconfigure_wait = True;
|
||||||
|
|
||||||
if (! timer->isTiming()) timer->start();
|
if (! timer->timing()) timer->start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -3828,7 +3828,6 @@ void BlackboxWindow::leaveNotifyEvent(const XCrossingEvent*) {
|
||||||
|
|
||||||
installColormap(False);
|
installColormap(False);
|
||||||
|
|
||||||
if (timer->isTiming())
|
|
||||||
timer->stop();
|
timer->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue