X-Git-Url: https://git.brokenzipper.com/gitweb?a=blobdiff_plain;f=src%2FTimer.cc;h=99f05ba9360f2166e10baee3afe4877ed34ddde6;hb=6e07a64d3306ac26c0cb6bd029356fe1fcee4756;hp=95985a0159c780e46430db03d749ee3ed4516ab1;hpb=dfc5f034581f5a26cba5c4811500438f89f0634a;p=chaz%2Fopenbox diff --git a/src/Timer.cc b/src/Timer.cc index 95985a01..99f05ba9 100644 --- a/src/Timer.cc +++ b/src/Timer.cc @@ -1,5 +1,6 @@ -// Timer.cc for Openbox -// Copyright (c) 2001 Sean 'Shaleh' Perry +// -*- mode: C++; indent-tabs-mode: nil; -*- +// Timer.cc for Blackbox - An X11 Window Manager +// Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net) // // Permission is hereby granted, free of charge, to any person obtaining a @@ -20,57 +21,91 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// stupid macros needed to access some functions in version 2 of the GNU C -// library -#ifndef _GNU_SOURCE -# define _GNU_SOURCE -#endif // _GNU_SOURCE - #ifdef HAVE_CONFIG_H # include "../config.h" #endif // HAVE_CONFIG_H -#include "BaseDisplay.h" -#include "Timer.h" +#include "BaseDisplay.hh" +#include "Timer.hh" +#include "Util.hh" -BTimer::BTimer(BaseDisplay *d, TimeoutHandler *h) { - display = d; +BTimer::BTimer(TimerQueueManager *m, TimeoutHandler *h) { + manager = m; handler = h; - once = timing = False; + recur = timing = False; } + BTimer::~BTimer(void) { if (timing) 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 = t % 1000; _timeout.tv_usec *= 1000; } -void BTimer::setTimeout(timeval t) { + +void BTimer::setTimeout(const timeval &t) { _timeout.tv_sec = t.tv_sec; _timeout.tv_usec = t.tv_usec; } + void BTimer::start(void) { gettimeofday(&_start, 0); if (! timing) { timing = True; - display->addTimer(this); + manager->addTimer(this); } } + void BTimer::stop(void) { timing = False; - display->removeTimer(this); + manager->removeTimer(this); } + +void BTimer::halt(void) { + timing = False; +} + + void BTimer::fireTimeout(void) { - if (handler) handler->timeout(); + if (handler) + handler->timeout(); +} + + +timeval BTimer::timeRemaining(const timeval &tm) const { + timeval ret = endpoint(); + + ret.tv_sec -= tm.tv_sec; + ret.tv_usec -= tm.tv_usec; + + return normalizeTimeval(ret); +} + + +timeval BTimer::endpoint(void) const { + timeval ret; + + ret.tv_sec = _start.tv_sec + _timeout.tv_sec; + ret.tv_usec = _start.tv_usec + _timeout.tv_usec; + + return normalizeTimeval(ret); +} + + +bool BTimer::shouldFire(const timeval &tm) const { + timeval end = endpoint(); + + return ! ((tm.tv_sec < end.tv_sec) || + (tm.tv_sec == end.tv_sec && tm.tv_usec < end.tv_usec)); }