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>
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.
30 bt::timeval::timeval(const ::timeval
&t
)
31 : tv_sec(t
.tv_sec
), tv_usec(t
.tv_usec
)
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
); }
39 bt::timeval
bt::timeval::operator+(const timeval
&tv
)
40 { return timeval(tv_sec
+ tv
.tv_sec
, tv_usec
+ tv
.tv_usec
); }
43 bt::timeval
&bt::timeval::operator+=(const timeval
&tv
)
44 { return *this = normalizeTimeval(operator+(tv
)); }
47 bt::timeval
bt::timeval::operator-(const timeval
&tv
)
48 { return timeval(tv_sec
- tv
.tv_sec
, tv_usec
- tv
.tv_usec
); }
51 bt::timeval
&bt::timeval::operator-=(const timeval
&tv
)
52 { return *this = normalizeTimeval(operator-(tv
)); }
55 bt::timeval
&bt::timeval::operator=(const ::timeval
&t
)
56 { return (*this = timeval(t
)); }
59 bt::timeval::operator ::timeval() const {
60 ::timeval ret
= { tv_sec
, tv_usec
};
65 bt::timeval
bt::normalizeTimeval(const timeval
&tm
) {
68 while (ret
.tv_usec
< 0) {
71 ret
.tv_usec
+= 1000000;
77 if (ret
.tv_usec
>= 1000000) {
78 ret
.tv_sec
+= ret
.tv_usec
/ 1000000;
79 ret
.tv_usec
%= 1000000;
82 if (ret
.tv_sec
< 0) ret
.tv_sec
= 0;
88 bt::Timer::Timer(TimerQueueManager
*m
, TimeoutHandler
*h
) {
92 recur
= timing
= false;
96 bt::Timer::~Timer(void) {
102 void printTime(const char *message
, const bt::timeval
&tv
);
104 void bt::Timer::adjustStartTime(const timeval
&offset
)
105 { _start
+= offset
; }
108 void bt::Timer::setTimeout(long t
) {
109 _timeout
.tv_sec
= t
/ 1000;
110 _timeout
.tv_usec
= t
% 1000;
111 _timeout
.tv_usec
*= 1000;
115 void bt::Timer::setTimeout(const timeval
&t
)
119 void bt::Timer::start(void) {
126 manager
->addTimer(this);
131 void bt::Timer::stop(void) {
133 manager
->removeTimer(this);
137 void bt::Timer::halt(void)
141 void bt::Timer::fireTimeout(void)
144 handler
->timeout(this);
148 bt::timeval
bt::Timer::timeRemaining(const timeval
&tm
) const {
149 timeval ret
= endpoint();
151 ret
.tv_sec
-= tm
.tv_sec
;
152 ret
.tv_usec
-= tm
.tv_usec
;
154 return bt::normalizeTimeval(ret
);
158 bt::timeval
bt::Timer::endpoint(void) const {
161 ret
.tv_sec
= _start
.tv_sec
+ _timeout
.tv_sec
;
162 ret
.tv_usec
= _start
.tv_usec
+ _timeout
.tv_usec
;
164 return bt::normalizeTimeval(ret
);
168 bool bt::Timer::shouldFire(const timeval
&tm
) const {
169 timeval end
= endpoint();
171 return !((tm
.tv_sec
< end
.tv_sec
) ||
172 (tm
.tv_sec
== end
.tv_sec
&& tm
.tv_usec
< end
.tv_usec
));