moved the update timer routines from BaseDisplay to BTimer and fixed indentation
This commit is contained in:
parent
6c1f079fbd
commit
9291eb0911
2 changed files with 159 additions and 67 deletions
140
src/Timer.cc
140
src/Timer.cc
|
@ -21,62 +21,152 @@
|
||||||
|
|
||||||
// stupid macros needed to access some functions in version 2 of the GNU C
|
// stupid macros needed to access some functions in version 2 of the GNU C
|
||||||
// library
|
// library
|
||||||
#ifndef _GNU_SOURCE
|
#ifndef _GNU_SOURCE
|
||||||
# define _GNU_SOURCE
|
# define _GNU_SOURCE
|
||||||
#endif // _GNU_SOURCE
|
#endif // _GNU_SOURCE
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
#ifdef HAVE_CONFIG_H
|
||||||
# include "../config.h"
|
# include "../config.h"
|
||||||
#endif // HAVE_CONFIG_H
|
#endif // HAVE_CONFIG_H
|
||||||
|
|
||||||
#include "BaseDisplay.hh"
|
#include "BaseDisplay.hh"
|
||||||
#include "Timer.hh"
|
#include "Timer.hh"
|
||||||
|
|
||||||
|
//static var
|
||||||
|
BTimer::TimerList BTimer::m_timerlist;
|
||||||
|
|
||||||
BTimer::BTimer(BaseDisplay *d, TimeoutHandler *h) {
|
BTimer::BTimer(TimeoutHandler *h):
|
||||||
display = d;
|
m_handler(h),
|
||||||
handler = h;
|
m_timing(false) ,
|
||||||
|
m_once(false) {
|
||||||
once = timing = False;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
BTimer::~BTimer(void) {
|
BTimer::~BTimer(void) {
|
||||||
if (isTiming()) stop();
|
if (isTiming()) stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void BTimer::setTimeout(long t) {
|
void BTimer::setTimeout(long t) {
|
||||||
_timeout.tv_sec = t / 1000;
|
m_timeout.tv_sec = t / 1000;
|
||||||
_timeout.tv_usec = t;
|
m_timeout.tv_usec = t;
|
||||||
_timeout.tv_usec -= (_timeout.tv_sec * 1000);
|
m_timeout.tv_usec -= (m_timeout.tv_sec * 1000);
|
||||||
_timeout.tv_usec *= 1000;
|
m_timeout.tv_usec *= 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void BTimer::setTimeout(timeval t) {
|
void BTimer::setTimeout(timeval t) {
|
||||||
_timeout.tv_sec = t.tv_sec;
|
m_timeout.tv_sec = t.tv_sec;
|
||||||
_timeout.tv_usec = t.tv_usec;
|
m_timeout.tv_usec = t.tv_usec;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void BTimer::start(void) {
|
void BTimer::start(void) {
|
||||||
gettimeofday(&_start, 0);
|
gettimeofday(&m_start, 0);
|
||||||
|
|
||||||
if (! timing) {
|
if (! m_timing) {
|
||||||
timing = True;
|
m_timing = true;
|
||||||
display->addTimer(this);
|
addTimer(this); //add us to the list
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void BTimer::stop(void) {
|
void BTimer::stop(void) {
|
||||||
timing = False;
|
m_timing = false;
|
||||||
|
removeTimer(this); //remove us from the list
|
||||||
display->removeTimer(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void BTimer::fireTimeout(void) {
|
void BTimer::fireTimeout(void) {
|
||||||
if (handler) handler->timeout();
|
if (m_handler) m_handler->timeout();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void BTimer::updateTimers(int fd) {
|
||||||
|
fd_set rfds;
|
||||||
|
timeval now, tm, *timeout = 0;
|
||||||
|
|
||||||
|
FD_ZERO(&rfds);
|
||||||
|
FD_SET(fd, &rfds);
|
||||||
|
|
||||||
|
if (m_timerlist.size() > 0) {
|
||||||
|
gettimeofday(&now, 0);
|
||||||
|
|
||||||
|
tm.tv_sec = tm.tv_usec = 0l;
|
||||||
|
|
||||||
|
BTimer *timer = m_timerlist.front();
|
||||||
|
|
||||||
|
tm.tv_sec = timer->getStartTime().tv_sec +
|
||||||
|
timer->getTimeout().tv_sec - now.tv_sec;
|
||||||
|
tm.tv_usec = timer->getStartTime().tv_usec +
|
||||||
|
timer->getTimeout().tv_usec - now.tv_usec;
|
||||||
|
|
||||||
|
while (tm.tv_usec >= 1000000) {
|
||||||
|
tm.tv_sec++;
|
||||||
|
tm.tv_usec -= 1000000;
|
||||||
|
}
|
||||||
|
|
||||||
|
while (tm.tv_usec < 0) {
|
||||||
|
if (tm.tv_sec > 0) {
|
||||||
|
tm.tv_sec--;
|
||||||
|
tm.tv_usec += 1000000;
|
||||||
|
} else {
|
||||||
|
tm.tv_usec = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
timeout = &tm;
|
||||||
|
}
|
||||||
|
|
||||||
|
select(fd + 1, &rfds, 0, 0, timeout);
|
||||||
|
|
||||||
|
// check for timer timeout
|
||||||
|
gettimeofday(&now, 0);
|
||||||
|
|
||||||
|
TimerList::iterator it = m_timerlist.begin();
|
||||||
|
//must check end ...the timer might remove
|
||||||
|
//it self from the list (should be fixed in the future)
|
||||||
|
for(; it != m_timerlist.end(); ++it) {
|
||||||
|
tm.tv_sec = (*it)->getStartTime().tv_sec +
|
||||||
|
(*it)->getTimeout().tv_sec;
|
||||||
|
tm.tv_usec = (*it)->getStartTime().tv_usec +
|
||||||
|
(*it)->getTimeout().tv_usec;
|
||||||
|
|
||||||
|
if ((now.tv_sec < tm.tv_sec) ||
|
||||||
|
(now.tv_sec == tm.tv_sec && now.tv_usec < tm.tv_usec))
|
||||||
|
break;
|
||||||
|
|
||||||
|
(*it)->fireTimeout();
|
||||||
|
|
||||||
|
// restart the current timer so that the start time is updated
|
||||||
|
if (! (*it)->doOnce())
|
||||||
|
(*it)->start();
|
||||||
|
else {
|
||||||
|
(*it)->stop();
|
||||||
|
it--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BTimer::addTimer(BTimer *timer) {
|
||||||
|
assert(timer);
|
||||||
|
|
||||||
|
TimerList::iterator it = m_timerlist.begin();
|
||||||
|
TimerList::iterator it_end = m_timerlist.end();
|
||||||
|
int index = 0;
|
||||||
|
for (; it != it_end; ++it, ++index) {
|
||||||
|
if (((*it)->getTimeout().tv_sec > timer->getTimeout().tv_sec) ||
|
||||||
|
(((*it)->getTimeout().tv_sec == timer->getTimeout().tv_sec) &&
|
||||||
|
((*it)->getTimeout().tv_usec >= timer->getTimeout().tv_usec))) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
m_timerlist.insert(it, timer);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void BTimer::removeTimer(BTimer *timer) {
|
||||||
|
assert(timer);
|
||||||
|
m_timerlist.remove(timer);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
84
src/Timer.hh
84
src/Timer.hh
|
@ -1,3 +1,6 @@
|
||||||
|
// Timer.hh for fluxbox
|
||||||
|
// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
|
||||||
|
//
|
||||||
// Timer.hh for Blackbox - An X11 Window Manager
|
// Timer.hh for Blackbox - An X11 Window Manager
|
||||||
// Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
|
// Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
|
||||||
//
|
//
|
||||||
|
@ -13,73 +16,72 @@
|
||||||
//
|
//
|
||||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
||||||
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||||
// DEALINGS IN THE SOFTWARE.
|
// DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
#ifndef TIMER_HH
|
#ifndef TIMER_HH
|
||||||
#define TIMER_HH
|
#define TIMER_HH
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
#ifdef HAVE_CONFIG_H
|
||||||
#include "../config.h"
|
#include "../config.h"
|
||||||
#endif //HAVE_CONFIG_H
|
#endif //HAVE_CONFIG_H
|
||||||
|
|
||||||
#ifdef TIME_WITH_SYS_TIME
|
#ifdef TIME_WITH_SYS_TIME
|
||||||
# include <sys/time.h>
|
# include <sys/time.h>
|
||||||
# include <time.h>
|
# include <time.h>
|
||||||
#else // !TIME_WITH_SYS_TIME
|
#else // !TIME_WITH_SYS_TIME
|
||||||
# ifdef HAVE_SYS_TIME_H
|
# ifdef HAVE_SYS_TIME_H
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
# else // !HAVE_SYS_TIME_H
|
# else // !HAVE_SYS_TIME_H
|
||||||
# include <time.h>
|
# include <time.h>
|
||||||
# endif // HAVE_SYS_TIME_H
|
# endif // HAVE_SYS_TIME_H
|
||||||
#endif // TIME_WITH_SYS_TIME
|
#endif // TIME_WITH_SYS_TIME
|
||||||
|
|
||||||
// forward declaration
|
#include <list>
|
||||||
class BTimer;
|
|
||||||
class TimeoutHandler;
|
|
||||||
|
|
||||||
#include "BaseDisplay.hh"
|
|
||||||
|
|
||||||
|
|
||||||
class TimeoutHandler {
|
class TimeoutHandler {
|
||||||
public:
|
public:
|
||||||
virtual void timeout(void) = 0;
|
virtual void timeout(void) = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class BTimer {
|
class BTimer {
|
||||||
friend class BaseDisplay;
|
|
||||||
private:
|
|
||||||
BaseDisplay *display;
|
|
||||||
TimeoutHandler *handler;
|
|
||||||
int timing, once;
|
|
||||||
|
|
||||||
timeval _start, _timeout;
|
|
||||||
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void fireTimeout(void);
|
|
||||||
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BTimer(BaseDisplay *, TimeoutHandler *);
|
explicit BTimer(TimeoutHandler *);
|
||||||
virtual ~BTimer(void);
|
virtual ~BTimer(void);
|
||||||
|
|
||||||
inline const int &isTiming(void) const { return timing; }
|
inline const int isTiming(void) const { return m_timing; }
|
||||||
inline const int &doOnce(void) const { return once; }
|
inline const int doOnce(void) const { return m_once; }
|
||||||
|
|
||||||
inline const timeval &getTimeout(void) const { return _timeout; }
|
inline const timeval &getTimeout(void) const { return m_timeout; }
|
||||||
inline const timeval &getStartTime(void) const { return _start; }
|
inline const timeval &getStartTime(void) const { return m_start; }
|
||||||
|
|
||||||
inline void fireOnce(int o) { once = o; }
|
inline void fireOnce(bool once) { m_once = once; }
|
||||||
|
|
||||||
|
void setTimeout(long);
|
||||||
|
void setTimeout(timeval);
|
||||||
|
void start(void);
|
||||||
|
void stop(void);
|
||||||
|
static void updateTimers(int fd);
|
||||||
|
protected:
|
||||||
|
void fireTimeout(void);
|
||||||
|
|
||||||
|
private:
|
||||||
|
static void addTimer(BTimer *timer);
|
||||||
|
static void removeTimer(BTimer *timer);
|
||||||
|
|
||||||
|
typedef std::list<BTimer *> TimerList;
|
||||||
|
static TimerList m_timerlist;
|
||||||
|
|
||||||
|
TimeoutHandler *m_handler;
|
||||||
|
|
||||||
|
bool m_timing, m_once;
|
||||||
|
|
||||||
|
timeval m_start, m_timeout;
|
||||||
|
|
||||||
void setTimeout(long);
|
|
||||||
void setTimeout(timeval);
|
|
||||||
void start(void);
|
|
||||||
void stop(void);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue