moved the update timer routines from BaseDisplay to BTimer and fixed indentation

This commit is contained in:
fluxgen 2002-03-19 00:09:59 +00:00
parent 6c1f079fbd
commit 9291eb0911
2 changed files with 159 additions and 67 deletions

View file

@ -21,62 +21,152 @@
// stupid macros needed to access some functions in version 2 of the GNU C
// library
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif // _GNU_SOURCE
#ifdef HAVE_CONFIG_H
# include "../config.h"
#ifdef HAVE_CONFIG_H
# include "../config.h"
#endif // HAVE_CONFIG_H
#include "BaseDisplay.hh"
#include "Timer.hh"
//static var
BTimer::TimerList BTimer::m_timerlist;
BTimer::BTimer(BaseDisplay *d, TimeoutHandler *h) {
display = d;
handler = h;
once = timing = False;
BTimer::BTimer(TimeoutHandler *h):
m_handler(h),
m_timing(false) ,
m_once(false) {
}
BTimer::~BTimer(void) {
if (isTiming()) stop();
if (isTiming()) stop();
}
void BTimer::setTimeout(long t) {
_timeout.tv_sec = t / 1000;
_timeout.tv_usec = t;
_timeout.tv_usec -= (_timeout.tv_sec * 1000);
_timeout.tv_usec *= 1000;
m_timeout.tv_sec = t / 1000;
m_timeout.tv_usec = t;
m_timeout.tv_usec -= (m_timeout.tv_sec * 1000);
m_timeout.tv_usec *= 1000;
}
void BTimer::setTimeout(timeval t) {
_timeout.tv_sec = t.tv_sec;
_timeout.tv_usec = t.tv_usec;
m_timeout.tv_sec = t.tv_sec;
m_timeout.tv_usec = t.tv_usec;
}
void BTimer::start(void) {
gettimeofday(&_start, 0);
gettimeofday(&m_start, 0);
if (! timing) {
timing = True;
display->addTimer(this);
}
if (! m_timing) {
m_timing = true;
addTimer(this); //add us to the list
}
}
void BTimer::stop(void) {
timing = False;
display->removeTimer(this);
m_timing = false;
removeTimer(this); //remove us from the list
}
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);
}

View file

@ -1,3 +1,6 @@
// Timer.hh for fluxbox
// Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org)
//
// Timer.hh for Blackbox - An X11 Window Manager
// 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
// 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
// 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
// DEALINGS IN THE SOFTWARE.
#ifndef TIMER_HH
#define TIMER_HH
#ifndef TIMER_HH
#define TIMER_HH
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif //HAVE_CONFIG_H
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#ifdef TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else // !TIME_WITH_SYS_TIME
# ifdef HAVE_SYS_TIME_H
# ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
# else // !HAVE_SYS_TIME_H
# include <time.h>
# endif // HAVE_SYS_TIME_H
# else // !HAVE_SYS_TIME_H
# include <time.h>
# endif // HAVE_SYS_TIME_H
#endif // TIME_WITH_SYS_TIME
// forward declaration
class BTimer;
class TimeoutHandler;
#include "BaseDisplay.hh"
#include <list>
class TimeoutHandler {
public:
virtual void timeout(void) = 0;
virtual void timeout(void) = 0;
};
class BTimer {
friend class BaseDisplay;
private:
BaseDisplay *display;
TimeoutHandler *handler;
int timing, once;
timeval _start, _timeout;
protected:
void fireTimeout(void);
public:
BTimer(BaseDisplay *, TimeoutHandler *);
virtual ~BTimer(void);
explicit BTimer(TimeoutHandler *);
virtual ~BTimer(void);
inline const int &isTiming(void) const { return timing; }
inline const int &doOnce(void) const { return once; }
inline const int isTiming(void) const { return m_timing; }
inline const int doOnce(void) const { return m_once; }
inline const timeval &getTimeout(void) const { return _timeout; }
inline const timeval &getStartTime(void) const { return _start; }
inline const timeval &getTimeout(void) const { return m_timeout; }
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);
};