1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // Timer.hh for Blackbox - An X11 Window Manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
24 #ifndef _BLACKBOX_Timer_hh
25 #define _BLACKBOX_Timer_hh
28 #ifdef TIME_WITH_SYS_TIME
29 # include <sys/time.h>
31 #else // !TIME_WITH_SYS_TIME
32 # ifdef HAVE_SYS_TIME_H
33 # include <sys/time.h>
34 # else // !HAVE_SYS_TIME_H
36 # endif // HAVE_SYS_TIME_H
37 #endif // TIME_WITH_SYS_TIME
40 // forward declaration
41 class TimerQueueManager;
43 class TimeoutHandler {
45 virtual void timeout(void) = 0;
50 TimerQueueManager *manager;
51 TimeoutHandler *handler;
54 timeval _start, _timeout;
56 BTimer(const BTimer&);
57 BTimer& operator=(const BTimer&);
60 BTimer(TimerQueueManager *m, TimeoutHandler *h);
61 virtual ~BTimer(void);
63 void fireTimeout(void);
65 inline bool isTiming(void) const { return timing; }
66 inline bool isRecurring(void) const { return recur; }
68 inline const timeval &getTimeout(void) const { return _timeout; }
69 inline const timeval &getStartTime(void) const { return _start; }
71 timeval timeRemaining(const timeval &tm) const;
72 bool shouldFire(const timeval &tm) const;
73 timeval endpoint(void) const;
75 inline void recurring(bool b) { recur = b; }
77 void setTimeout(long t);
78 void setTimeout(const timeval &t);
80 void start(void); // manager acquires timer
81 void stop(void); // manager releases timer
82 void halt(void); // halts the timer
84 bool operator<(const BTimer& other) const
85 { return shouldFire(other.endpoint()); }
92 template <class _Tp, class _Sequence, class _Compare>
93 class _timer_queue: protected std::priority_queue<_Tp, _Sequence, _Compare> {
95 typedef std::priority_queue<_Tp, _Sequence, _Compare> _Base;
97 _timer_queue(void): _Base() {}
98 ~_timer_queue(void) {}
100 void release(const _Tp& value) {
101 c.erase(std::remove(c.begin(), c.end(), value), c.end());
102 // after removing the item we need to make the heap again
103 std::make_heap(c.begin(), c.end(), comp);
105 bool empty(void) const { return _Base::empty(); }
106 size_t size(void) const { return _Base::size(); }
107 void push(const _Tp& value) { _Base::push(value); }
108 void pop(void) { _Base::pop(); }
109 const _Tp& top(void) const { return _Base::top(); }
112 _timer_queue(const _timer_queue&) {}
113 _timer_queue& operator=(const _timer_queue&) {}
116 struct TimerLessThan {
117 bool operator()(const BTimer* const l, const BTimer* const r) const {
123 typedef _timer_queue<BTimer*, std::vector<BTimer*>, TimerLessThan> TimerQueue;
125 class TimerQueueManager {
127 virtual void addTimer(BTimer* timer) = 0;
128 virtual void removeTimer(BTimer* timer) = 0;
131 #endif // _BLACKBOX_Timer_hh