]> icculus.org git repositories - dana/openbox.git/blob - src/Timer.hh
bindable/disableable root/workspace menus
[dana/openbox.git] / src / Timer.hh
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)
5 //
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:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
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.
23
24 #ifndef   _BLACKBOX_Timer_hh
25 #define   _BLACKBOX_Timer_hh
26
27 extern "C" {
28 #ifdef    TIME_WITH_SYS_TIME
29 #  include <sys/time.h>
30 #  include <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
35 #    include <time.h>
36 #  endif // HAVE_SYS_TIME_H
37 #endif // TIME_WITH_SYS_TIME
38 }
39
40 // forward declaration
41 class TimerQueueManager;
42
43 class TimeoutHandler {
44 public:
45   virtual void timeout(void) = 0;
46 };
47
48 class BTimer {
49 private:
50   TimerQueueManager *manager;
51   TimeoutHandler *handler;
52   bool timing, recur;
53
54   timeval _start, _timeout;
55
56   BTimer(const BTimer&);
57   BTimer& operator=(const BTimer&);
58
59 public:
60   BTimer(TimerQueueManager *m, TimeoutHandler *h);
61   virtual ~BTimer(void);
62
63   void fireTimeout(void);
64
65   inline bool isTiming(void) const { return timing; }
66   inline bool isRecurring(void) const { return recur; }
67
68   inline const timeval &getTimeout(void) const { return _timeout; }
69   inline const timeval &getStartTime(void) const { return _start; }
70
71   timeval timeRemaining(const timeval &tm) const;
72   bool shouldFire(const timeval &tm) const;
73   timeval endpoint(void) const;
74
75   inline void recurring(bool b) { recur = b; }
76
77   void setTimeout(long t);
78   void setTimeout(const timeval &t);
79
80   void start(void);  // manager acquires timer
81   void stop(void);   // manager releases timer
82   void halt(void);   // halts the timer
83
84   bool operator<(const BTimer& other) const
85   { return shouldFire(other.endpoint()); }
86 };
87
88
89 #include <queue>
90 #include <algorithm>
91
92 template <class _Tp, class _Sequence, class _Compare>
93 class _timer_queue: protected std::priority_queue<_Tp, _Sequence, _Compare> {
94 public:
95   typedef std::priority_queue<_Tp, _Sequence, _Compare> _Base;
96
97   _timer_queue(void): _Base() {}
98   ~_timer_queue(void) {}
99
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);
104   }
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(); }
110 private:
111   // no copying!
112   _timer_queue(const _timer_queue&) {}
113   _timer_queue& operator=(const _timer_queue&) {}
114 };
115
116 struct TimerLessThan {
117   bool operator()(const BTimer* const l, const BTimer* const r) const {
118     return *r < *l;
119   }
120 };
121
122 #include <vector>
123 typedef _timer_queue<BTimer*, std::vector<BTimer*>, TimerLessThan> TimerQueue;
124
125 class TimerQueueManager {
126 public:
127   virtual void addTimer(BTimer* timer) = 0;
128   virtual void removeTimer(BTimer* timer) = 0;
129 };
130
131 #endif // _BLACKBOX_Timer_hh