X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2FTimer.cc;h=99f05ba9360f2166e10baee3afe4877ed34ddde6;hb=eb1f1b359f821061e1fe6a2953c8d984474c9ef4;hp=8cd4714eb00b14bfaeec07ebd06e1d0fe2a673b1;hpb=ae093dba2fb97124bb4af3eaf4070b46f07dfd74;p=chaz%2Fopenbox diff --git a/src/Timer.cc b/src/Timer.cc index 8cd4714e..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,60 +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(TimerQueueManager *m, TimeoutHandler *h) { + manager = m; + handler = h; -BTimer::BTimer(BaseDisplay &d, TimeoutHandler &h) : display(d), 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; - if (timing) { - display.removeTimer(this); - display.addTimer(this); // reorder the display - } } -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) { - if (timing) { - timing = False; + timing = False; - display.removeTimer(this); - } + manager->removeTimer(this); } + +void BTimer::halt(void) { + timing = False; +} + + void BTimer::fireTimeout(void) { - 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)); }