diff options
author | fluxgen <fluxgen> | 2002-03-19 00:09:59 (GMT) |
---|---|---|
committer | fluxgen <fluxgen> | 2002-03-19 00:09:59 (GMT) |
commit | 9291eb0911bcb029cb0d3b527f45ffbb95b5d402 (patch) | |
tree | 292914f1259f09627f630214e2438df54983e101 /src | |
parent | 6c1f079fbd223946863ad445f8ffffbddf65f169 (diff) | |
download | fluxbox_lack-9291eb0911bcb029cb0d3b527f45ffbb95b5d402.zip fluxbox_lack-9291eb0911bcb029cb0d3b527f45ffbb95b5d402.tar.bz2 |
moved the update timer routines from BaseDisplay to BTimer and fixed indentation
Diffstat (limited to 'src')
-rw-r--r-- | src/Timer.cc | 140 | ||||
-rw-r--r-- | src/Timer.hh | 84 |
2 files changed, 158 insertions, 66 deletions
diff --git a/src/Timer.cc b/src/Timer.cc index b031816..ba695e1 100644 --- a/src/Timer.cc +++ b/src/Timer.cc | |||
@@ -21,62 +21,152 @@ | |||
21 | 21 | ||
22 | // stupid macros needed to access some functions in version 2 of the GNU C | 22 | // stupid macros needed to access some functions in version 2 of the GNU C |
23 | // library | 23 | // library |
24 | #ifndef _GNU_SOURCE | 24 | #ifndef _GNU_SOURCE |
25 | # define _GNU_SOURCE | 25 | # define _GNU_SOURCE |
26 | #endif // _GNU_SOURCE | 26 | #endif // _GNU_SOURCE |
27 | 27 | ||
28 | #ifdef HAVE_CONFIG_H | 28 | #ifdef HAVE_CONFIG_H |
29 | # include "../config.h" | 29 | # include "../config.h" |
30 | #endif // HAVE_CONFIG_H | 30 | #endif // HAVE_CONFIG_H |
31 | 31 | ||
32 | #include "BaseDisplay.hh" | 32 | #include "BaseDisplay.hh" |
33 | #include "Timer.hh" | 33 | #include "Timer.hh" |
34 | 34 | ||
35 | //static var | ||
36 | BTimer::TimerList BTimer::m_timerlist; | ||
35 | 37 | ||
36 | BTimer::BTimer(BaseDisplay *d, TimeoutHandler *h) { | 38 | BTimer::BTimer(TimeoutHandler *h): |
37 | display = d; | 39 | m_handler(h), |
38 | handler = h; | 40 | m_timing(false) , |
39 | 41 | m_once(false) { | |
40 | once = timing = False; | ||
41 | } | 42 | } |
42 | 43 | ||
43 | 44 | ||
44 | BTimer::~BTimer(void) { | 45 | BTimer::~BTimer(void) { |
45 | if (isTiming()) stop(); | 46 | if (isTiming()) stop(); |
46 | } | 47 | } |
47 | 48 | ||
48 | 49 | ||
49 | void BTimer::setTimeout(long t) { | 50 | void BTimer::setTimeout(long t) { |
50 | _timeout.tv_sec = t / 1000; | 51 | m_timeout.tv_sec = t / 1000; |
51 | _timeout.tv_usec = t; | 52 | m_timeout.tv_usec = t; |
52 | _timeout.tv_usec -= (_timeout.tv_sec * 1000); | 53 | m_timeout.tv_usec -= (m_timeout.tv_sec * 1000); |
53 | _timeout.tv_usec *= 1000; | 54 | m_timeout.tv_usec *= 1000; |
54 | } | 55 | } |
55 | 56 | ||
56 | 57 | ||
57 | void BTimer::setTimeout(timeval t) { | 58 | void BTimer::setTimeout(timeval t) { |
58 | _timeout.tv_sec = t.tv_sec; | 59 | m_timeout.tv_sec = t.tv_sec; |
59 | _timeout.tv_usec = t.tv_usec; | 60 | m_timeout.tv_usec = t.tv_usec; |
60 | } | 61 | } |
61 | 62 | ||
62 | 63 | ||
63 | void BTimer::start(void) { | 64 | void BTimer::start(void) { |
64 | gettimeofday(&_start, 0); | 65 | gettimeofday(&m_start, 0); |
65 | 66 | ||
66 | if (! timing) { | 67 | if (! m_timing) { |
67 | timing = True; | 68 | m_timing = true; |
68 | display->addTimer(this); | 69 | addTimer(this); //add us to the list |
69 | } | 70 | } |
70 | } | 71 | } |
71 | 72 | ||
72 | 73 | ||
73 | void BTimer::stop(void) { | 74 | void BTimer::stop(void) { |
74 | timing = False; | 75 | m_timing = false; |
75 | 76 | removeTimer(this); //remove us from the list | |
76 | display->removeTimer(this); | ||
77 | } | 77 | } |
78 | 78 | ||
79 | 79 | ||
80 | void BTimer::fireTimeout(void) { | 80 | void BTimer::fireTimeout(void) { |
81 | if (handler) handler->timeout(); | 81 | if (m_handler) m_handler->timeout(); |
82 | } | ||
83 | |||
84 | void BTimer::updateTimers(int fd) { | ||
85 | fd_set rfds; | ||
86 | timeval now, tm, *timeout = 0; | ||
87 | |||
88 | FD_ZERO(&rfds); | ||
89 | FD_SET(fd, &rfds); | ||
90 | |||
91 | if (m_timerlist.size() > 0) { | ||
92 | gettimeofday(&now, 0); | ||
93 | |||
94 | tm.tv_sec = tm.tv_usec = 0l; | ||
95 | |||
96 | BTimer *timer = m_timerlist.front(); | ||
97 | |||
98 | tm.tv_sec = timer->getStartTime().tv_sec + | ||
99 | timer->getTimeout().tv_sec - now.tv_sec; | ||
100 | tm.tv_usec = timer->getStartTime().tv_usec + | ||
101 | timer->getTimeout().tv_usec - now.tv_usec; | ||
102 | |||
103 | while (tm.tv_usec >= 1000000) { | ||
104 | tm.tv_sec++; | ||
105 | tm.tv_usec -= 1000000; | ||
106 | } | ||
107 | |||
108 | while (tm.tv_usec < 0) { | ||
109 | if (tm.tv_sec > 0) { | ||
110 | tm.tv_sec--; | ||
111 | tm.tv_usec += 1000000; | ||
112 | } else { | ||
113 | tm.tv_usec = 0; | ||
114 | break; | ||
115 | } | ||
116 | } | ||
117 | |||
118 | timeout = &tm; | ||
119 | } | ||
120 | |||
121 | select(fd + 1, &rfds, 0, 0, timeout); | ||
122 | |||
123 | // check for timer timeout | ||
124 | gettimeofday(&now, 0); | ||
125 | |||
126 | TimerList::iterator it = m_timerlist.begin(); | ||
127 | //must check end ...the timer might remove | ||
128 | //it self from the list (should be fixed in the future) | ||
129 | for(; it != m_timerlist.end(); ++it) { | ||
130 | tm.tv_sec = (*it)->getStartTime().tv_sec + | ||
131 | (*it)->getTimeout().tv_sec; | ||
132 | tm.tv_usec = (*it)->getStartTime().tv_usec + | ||
133 | (*it)->getTimeout().tv_usec; | ||
134 | |||
135 | if ((now.tv_sec < tm.tv_sec) || | ||
136 | (now.tv_sec == tm.tv_sec && now.tv_usec < tm.tv_usec)) | ||
137 | break; | ||
138 | |||
139 | (*it)->fireTimeout(); | ||
140 | |||
141 | // restart the current timer so that the start time is updated | ||
142 | if (! (*it)->doOnce()) | ||
143 | (*it)->start(); | ||
144 | else { | ||
145 | (*it)->stop(); | ||
146 | it--; | ||
147 | } | ||
148 | } | ||
149 | } | ||
150 | |||
151 | void BTimer::addTimer(BTimer *timer) { | ||
152 | assert(timer); | ||
153 | |||
154 | TimerList::iterator it = m_timerlist.begin(); | ||
155 | TimerList::iterator it_end = m_timerlist.end(); | ||
156 | int index = 0; | ||
157 | for (; it != it_end; ++it, ++index) { | ||
158 | if (((*it)->getTimeout().tv_sec > timer->getTimeout().tv_sec) || | ||
159 | (((*it)->getTimeout().tv_sec == timer->getTimeout().tv_sec) && | ||
160 | ((*it)->getTimeout().tv_usec >= timer->getTimeout().tv_usec))) { | ||
161 | break; | ||
162 | } | ||
163 | } | ||
164 | m_timerlist.insert(it, timer); | ||
165 | |||
166 | } | ||
167 | |||
168 | void BTimer::removeTimer(BTimer *timer) { | ||
169 | assert(timer); | ||
170 | m_timerlist.remove(timer); | ||
82 | } | 171 | } |
172 | |||
diff --git a/src/Timer.hh b/src/Timer.hh index bf40c3c..91d03b7 100644 --- a/src/Timer.hh +++ b/src/Timer.hh | |||
@@ -1,3 +1,6 @@ | |||
1 | // Timer.hh for fluxbox | ||
2 | // Copyright (c) 2002 Henrik Kinnunen (fluxgen@linuxmail.org) | ||
3 | // | ||
1 | // Timer.hh for Blackbox - An X11 Window Manager | 4 | // Timer.hh for Blackbox - An X11 Window Manager |
2 | // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net) | 5 | // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net) |
3 | // | 6 | // |
@@ -13,73 +16,72 @@ | |||
13 | // | 16 | // |
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 17 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | 18 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | 19 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 20 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | 21 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | 22 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
20 | // DEALINGS IN THE SOFTWARE. | 23 | // DEALINGS IN THE SOFTWARE. |
21 | 24 | ||
22 | #ifndef TIMER_HH | 25 | #ifndef TIMER_HH |
23 | #define TIMER_HH | 26 | #define TIMER_HH |
24 | 27 | ||
25 | #ifdef HAVE_CONFIG_H | 28 | #ifdef HAVE_CONFIG_H |
26 | #include "../config.h" | 29 | #include "../config.h" |
27 | #endif //HAVE_CONFIG_H | 30 | #endif //HAVE_CONFIG_H |
28 | 31 | ||
29 | #ifdef TIME_WITH_SYS_TIME | 32 | #ifdef TIME_WITH_SYS_TIME |
30 | # include <sys/time.h> | 33 | # include <sys/time.h> |
31 | # include <time.h> | 34 | # include <time.h> |
32 | #else // !TIME_WITH_SYS_TIME | 35 | #else // !TIME_WITH_SYS_TIME |
33 | # ifdef HAVE_SYS_TIME_H | 36 | # ifdef HAVE_SYS_TIME_H |
34 | #include <sys/time.h> | 37 | #include <sys/time.h> |
35 | # else // !HAVE_SYS_TIME_H | 38 | # else // !HAVE_SYS_TIME_H |
36 | # include <time.h> | 39 | # include <time.h> |
37 | # endif // HAVE_SYS_TIME_H | 40 | # endif // HAVE_SYS_TIME_H |
38 | #endif // TIME_WITH_SYS_TIME | 41 | #endif // TIME_WITH_SYS_TIME |
39 | 42 | ||
40 | // forward declaration | 43 | #include <list> |
41 | class BTimer; | ||
42 | class TimeoutHandler; | ||
43 | |||
44 | #include "BaseDisplay.hh" | ||
45 | |||
46 | 44 | ||
47 | class TimeoutHandler { | 45 | class TimeoutHandler { |
48 | public: | 46 | public: |
49 | virtual void timeout(void) = 0; | 47 | virtual void timeout(void) = 0; |
50 | }; | 48 | }; |
51 | 49 | ||
52 | 50 | ||
53 | class BTimer { | 51 | class BTimer { |
54 | friend class BaseDisplay; | 52 | public: |
55 | private: | 53 | explicit BTimer(TimeoutHandler *); |
56 | BaseDisplay *display; | 54 | virtual ~BTimer(void); |
57 | TimeoutHandler *handler; | ||
58 | int timing, once; | ||
59 | |||
60 | timeval _start, _timeout; | ||
61 | |||
62 | |||
63 | protected: | ||
64 | void fireTimeout(void); | ||
65 | 55 | ||
56 | inline const int isTiming(void) const { return m_timing; } | ||
57 | inline const int doOnce(void) const { return m_once; } | ||
66 | 58 | ||
67 | public: | 59 | inline const timeval &getTimeout(void) const { return m_timeout; } |
68 | BTimer(BaseDisplay *, TimeoutHandler *); | 60 | inline const timeval &getStartTime(void) const { return m_start; } |
69 | virtual ~BTimer(void); | ||
70 | 61 | ||
71 | inline const int &isTiming(void) const { return timing; } | 62 | inline void fireOnce(bool once) { m_once = once; } |
72 | inline const int &doOnce(void) const { return once; } | ||
73 | 63 | ||
74 | inline const timeval &getTimeout(void) const { return _timeout; } | 64 | void setTimeout(long); |
75 | inline const timeval &getStartTime(void) const { return _start; } | 65 | void setTimeout(timeval); |
66 | void start(void); | ||
67 | void stop(void); | ||
68 | static void updateTimers(int fd); | ||
69 | protected: | ||
70 | void fireTimeout(void); | ||
76 | 71 | ||
77 | inline void fireOnce(int o) { once = o; } | 72 | private: |
73 | static void addTimer(BTimer *timer); | ||
74 | static void removeTimer(BTimer *timer); | ||
75 | |||
76 | typedef std::list<BTimer *> TimerList; | ||
77 | static TimerList m_timerlist; | ||
78 | |||
79 | TimeoutHandler *m_handler; | ||
80 | |||
81 | bool m_timing, m_once; | ||
82 | |||
83 | timeval m_start, m_timeout; | ||
78 | 84 | ||
79 | void setTimeout(long); | ||
80 | void setTimeout(timeval); | ||
81 | void start(void); | ||
82 | void stop(void); | ||
83 | }; | 85 | }; |
84 | 86 | ||
85 | 87 | ||