| 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 <[email protected]> |
| 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: |
| 13 |
// |
| 14 |
// The above copyright notice and this permission notice shall be included in |
| 15 |
// all copies or substantial portions of the Software. |
| 16 |
// |
| 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. |
| 24 |
|
| 25 |
#include "Timer.hh" |
| 26 |
|
| 27 |
#include <sys/time.h> |
| 28 |
|
| 29 |
|
| 30 |
bt::timeval::timeval(const ::timeval &t) |
| 31 |
: tv_sec(t.tv_sec), tv_usec(t.tv_usec) |
| 32 |
{ } |
| 33 |
|
| 34 |
|
| 35 |
bool bt::timeval::operator<(const timeval &tv) |
| 36 |
{ return tv_sec < tv.tv_sec || (tv_sec == tv.tv_sec && tv_usec < tv.tv_usec); } |
| 37 |
|
| 38 |
|
| 39 |
bt::timeval bt::timeval::operator+(const timeval &tv) |
| 40 |
{ return timeval(tv_sec + tv.tv_sec, tv_usec + tv.tv_usec); } |
| 41 |
|
| 42 |
|
| 43 |
bt::timeval &bt::timeval::operator+=(const timeval &tv) |
| 44 |
{ return *this = normalizeTimeval(operator+(tv)); } |
| 45 |
|
| 46 |
|
| 47 |
bt::timeval bt::timeval::operator-(const timeval &tv) |
| 48 |
{ return timeval(tv_sec - tv.tv_sec, tv_usec - tv.tv_usec); } |
| 49 |
|
| 50 |
|
| 51 |
bt::timeval &bt::timeval::operator-=(const timeval &tv) |
| 52 |
{ return *this = normalizeTimeval(operator-(tv)); } |
| 53 |
|
| 54 |
|
| 55 |
bt::timeval &bt::timeval::operator=(const ::timeval &t) |
| 56 |
{ return (*this = timeval(t)); } |
| 57 |
|
| 58 |
|
| 59 |
bt::timeval::operator ::timeval() const { |
| 60 |
::timeval ret = { tv_sec, tv_usec }; |
| 61 |
return ret; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
bt::timeval bt::normalizeTimeval(const timeval &tm) { |
| 66 |
timeval ret = tm; |
| 67 |
|
| 68 |
while (ret.tv_usec < 0) { |
| 69 |
if (ret.tv_sec > 0) { |
| 70 |
--ret.tv_sec; |
| 71 |
ret.tv_usec += 1000000; |
| 72 |
} else { |
| 73 |
ret.tv_usec = 0; |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
if (ret.tv_usec >= 1000000) { |
| 78 |
ret.tv_sec += ret.tv_usec / 1000000; |
| 79 |
ret.tv_usec %= 1000000; |
| 80 |
} |
| 81 |
|
| 82 |
if (ret.tv_sec < 0) ret.tv_sec = 0; |
| 83 |
|
| 84 |
return ret; |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
bt::Timer::Timer(TimerQueueManager *m, TimeoutHandler *h) { |
| 89 |
manager = m; |
| 90 |
handler = h; |
| 91 |
|
| 92 |
recur = timing = false; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
bt::Timer::~Timer(void) { |
| 97 |
if (timing) |
| 98 |
stop(); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
void printTime(const char *message, const bt::timeval &tv); |
| 103 |
|
| 104 |
void bt::Timer::adjustStartTime(const timeval &offset) |
| 105 |
{ _start += offset; } |
| 106 |
|
| 107 |
|
| 108 |
void bt::Timer::setTimeout(long t) { |
| 109 |
_timeout.tv_sec = t / 1000; |
| 110 |
_timeout.tv_usec = t % 1000; |
| 111 |
_timeout.tv_usec *= 1000; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
void bt::Timer::setTimeout(const timeval &t) |
| 116 |
{ _timeout = t; } |
| 117 |
|
| 118 |
|
| 119 |
void bt::Timer::start(void) { |
| 120 |
::timeval s; |
| 121 |
gettimeofday(&s, 0); |
| 122 |
_start = s; |
| 123 |
|
| 124 |
if (!timing) { |
| 125 |
timing = true; |
| 126 |
manager->addTimer(this); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
void bt::Timer::stop(void) { |
| 132 |
timing = false; |
| 133 |
manager->removeTimer(this); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
void bt::Timer::halt(void) |
| 138 |
{ timing = false; } |
| 139 |
|
| 140 |
|
| 141 |
void bt::Timer::fireTimeout(void) |
| 142 |
{ |
| 143 |
if (handler) |
| 144 |
handler->timeout(this); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
bt::timeval bt::Timer::timeRemaining(const timeval &tm) const { |
| 149 |
timeval ret = endpoint(); |
| 150 |
|
| 151 |
ret.tv_sec -= tm.tv_sec; |
| 152 |
ret.tv_usec -= tm.tv_usec; |
| 153 |
|
| 154 |
return bt::normalizeTimeval(ret); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
bt::timeval bt::Timer::endpoint(void) const { |
| 159 |
timeval ret; |
| 160 |
|
| 161 |
ret.tv_sec = _start.tv_sec + _timeout.tv_sec; |
| 162 |
ret.tv_usec = _start.tv_usec + _timeout.tv_usec; |
| 163 |
|
| 164 |
return bt::normalizeTimeval(ret); |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
bool bt::Timer::shouldFire(const timeval &tm) const { |
| 169 |
timeval end = endpoint(); |
| 170 |
|
| 171 |
return !((tm.tv_sec < end.tv_sec) || |
| 172 |
(tm.tv_sec == end.tv_sec && tm.tv_usec < end.tv_usec)); |
| 173 |
} |