allow the user to override the rootCommand from their ~/.blackboxrc
[blackbox.git] / lib / Timer.cc
blob9c7f019824ed1e78d1ba27881775374b6f62d399
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Timer.cc for Blackbox - An X11 Window Manager
3 // Copyright (c) 2001 - 2005 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000, 2002 - 2005
5 // Bradley T Hughes <bhughes at trolltech.com>
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a
8 // copy of this software and associated documentation files (the "Software"),
9 // to deal in the Software without restriction, including without limitation
10 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 // and/or sell copies of the Software, and to permit persons to whom the
12 // Software is furnished to do so, subject to the following conditions:
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 // DEALINGS IN THE SOFTWARE.
25 #include "Timer.hh"
27 #include <sys/time.h>
30 bt::timeval::timeval(const ::timeval &t)
31 : tv_sec(t.tv_sec), tv_usec(t.tv_usec)
32 { }
35 bt::timeval &bt::timeval::operator=(const ::timeval &t)
36 { return (*this = timeval(t)); }
39 bt::timeval::operator ::timeval() const {
40 ::timeval ret = { tv_sec, tv_usec };
41 return ret;
45 bt::timeval bt::normalizeTimeval(const timeval &tm) {
46 timeval ret = tm;
48 while (ret.tv_usec < 0) {
49 if (ret.tv_sec > 0) {
50 --ret.tv_sec;
51 ret.tv_usec += 1000000;
52 } else {
53 ret.tv_usec = 0;
57 if (ret.tv_usec >= 1000000) {
58 ret.tv_sec += ret.tv_usec / 1000000;
59 ret.tv_usec %= 1000000;
62 if (ret.tv_sec < 0) ret.tv_sec = 0;
64 return ret;
68 bt::Timer::Timer(TimerQueueManager *m, TimeoutHandler *h) {
69 manager = m;
70 handler = h;
72 recur = timing = false;
76 bt::Timer::~Timer(void) {
77 if (timing)
78 stop();
82 void bt::Timer::setTimeout(long t) {
83 _timeout.tv_sec = t / 1000;
84 _timeout.tv_usec = t % 1000;
85 _timeout.tv_usec *= 1000;
89 void bt::Timer::setTimeout(const timeval &t) {
90 _timeout.tv_sec = t.tv_sec;
91 _timeout.tv_usec = t.tv_usec;
95 void bt::Timer::start(void) {
96 ::timeval s;
97 gettimeofday(&s, 0);
98 _start = s;
100 if (!timing) {
101 timing = true;
102 manager->addTimer(this);
107 void bt::Timer::stop(void) {
108 timing = false;
110 manager->removeTimer(this);
114 void bt::Timer::halt(void)
115 { timing = false; }
118 void bt::Timer::fireTimeout(void)
120 if (handler)
121 handler->timeout(this);
125 bt::timeval bt::Timer::timeRemaining(const timeval &tm) const {
126 timeval ret = endpoint();
128 ret.tv_sec -= tm.tv_sec;
129 ret.tv_usec -= tm.tv_usec;
131 return bt::normalizeTimeval(ret);
135 bt::timeval bt::Timer::endpoint(void) const {
136 timeval ret;
138 ret.tv_sec = _start.tv_sec + _timeout.tv_sec;
139 ret.tv_usec = _start.tv_usec + _timeout.tv_usec;
141 return bt::normalizeTimeval(ret);
145 bool bt::Timer::shouldFire(const timeval &tm) const {
146 timeval end = endpoint();
148 return !((tm.tv_sec < end.tv_sec) ||
149 (tm.tv_sec == end.tv_sec && tm.tv_usec < end.tv_usec));