1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Timer.hh for Blackbox - An X11 Window Manager
3 // Copyright (c) 2001 - 2003 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000, 2002 - 2003
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.
34 // forward declare to avoid the header
39 // use a wrapper class to avoid the header as well
45 { tv_sec
= tv_usec
= 0l; }
47 // POSIX<->bt conversion
48 timeval(const ::timeval
&);
49 timeval
&operator=(const ::timeval
&);
50 operator ::timeval() const;
53 timeval
normalizeTimeval(const timeval
&tm
);
55 // forward declaration
56 class TimerQueueManager
;
59 class TimeoutHandler
{
61 virtual void timeout(Timer
*t
) = 0;
64 class Timer
: public NoCopy
{
66 TimerQueueManager
*manager
;
67 TimeoutHandler
*handler
;
70 timeval _start
, _timeout
;
73 Timer(TimerQueueManager
*m
, TimeoutHandler
*h
);
76 void fireTimeout(void);
78 inline bool isTiming(void) const
80 inline bool isRecurring(void) const
83 inline const timeval
&timeout(void) const
85 inline const timeval
&startTime(void) const
88 timeval
timeRemaining(const timeval
&tm
) const;
89 bool shouldFire(const timeval
&tm
) const;
90 timeval
endpoint(void) const;
92 inline void recurring(bool b
)
95 void setTimeout(long t
);
96 void setTimeout(const timeval
&t
);
98 void start(void); // manager acquires timer
99 void stop(void); // manager releases timer
100 void halt(void); // halts the timer
102 inline bool operator<(const Timer
& other
) const
103 { return shouldFire(other
.endpoint()); }
106 template <class _Tp
, class _Sequence
, class _Compare
>
107 class _timer_queue
: protected std::priority_queue
<_Tp
, _Sequence
, _Compare
> {
109 typedef std::priority_queue
<_Tp
, _Sequence
, _Compare
> _Base
;
111 inline _timer_queue(void)
114 inline ~_timer_queue(void)
117 inline void release(const _Tp
& value
) {
118 _Base::c
.erase(std::remove(_Base::c
.begin(), _Base::c
.end(), value
),
120 // after removing the item we need to make the heap again
121 std::make_heap(_Base::c
.begin(), _Base::c
.end(), _Base::comp
);
123 inline bool empty(void) const
124 { return _Base::empty(); }
125 inline size_t size(void) const
126 { return _Base::size(); }
127 inline void push(const _Tp
& value
)
128 { _Base::push(value
); }
129 inline void pop(void)
131 inline const _Tp
& top(void) const
132 { return _Base::top(); }
135 _timer_queue(const _timer_queue
&);
136 _timer_queue
& operator=(const _timer_queue
&);
139 struct TimerLessThan
{
140 inline bool operator()(const Timer
* const l
, const Timer
* const r
) const
144 typedef _timer_queue
<Timer
*, std::vector
<Timer
*>, TimerLessThan
> TimerQueue
;
146 class TimerQueueManager
{
148 virtual void addTimer(Timer
* timer
) = 0;
149 virtual void removeTimer(Timer
* timer
) = 0;