diff options
author | fluxgen <fluxgen> | 2003-01-09 21:52:09 (GMT) |
---|---|---|
committer | fluxgen <fluxgen> | 2003-01-09 21:52:09 (GMT) |
commit | c9299fff8f5492df6aafce4455c1ee7cff1ab1f8 (patch) | |
tree | 4de76e3b6e3100bb9869ea0eaef2c0e38866e808 /src/Timer.cc | |
parent | 7dd4823340ae5f710a08ff3a8fbe4276defc6b85 (diff) | |
download | fluxbox_pavel-c9299fff8f5492df6aafce4455c1ee7cff1ab1f8.zip fluxbox_pavel-c9299fff8f5492df6aafce4455c1ee7cff1ab1f8.tar.bz2 |
moved to FbTk
Diffstat (limited to 'src/Timer.cc')
-rw-r--r-- | src/Timer.cc | 177 |
1 files changed, 0 insertions, 177 deletions
diff --git a/src/Timer.cc b/src/Timer.cc deleted file mode 100644 index ebc1cd8..0000000 --- a/src/Timer.cc +++ /dev/null | |||
@@ -1,177 +0,0 @@ | |||
1 | // Timer.cc for Blackbox - An X11 Window Manager | ||
2 | // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // 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 | ||
18 | // 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 | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | #include "Timer.hh" | ||
23 | |||
24 | //use GNU extensions | ||
25 | #ifndef _GNU_SOURCE | ||
26 | #define _GNU_SOURCE | ||
27 | #endif // _GNU_SOURCE | ||
28 | |||
29 | #ifdef HAVE_CONFIG_H | ||
30 | #include "config.h" | ||
31 | #endif // HAVE_CONFIG_H | ||
32 | |||
33 | #include <sys/time.h> | ||
34 | #include <sys/types.h> | ||
35 | #include <unistd.h> | ||
36 | #include <cassert> | ||
37 | |||
38 | //static var | ||
39 | BTimer::TimerList BTimer::m_timerlist; | ||
40 | |||
41 | BTimer::BTimer(TimeoutHandler *h): | ||
42 | m_handler(h), | ||
43 | m_timing(false), | ||
44 | m_once(false) { | ||
45 | } | ||
46 | |||
47 | |||
48 | BTimer::~BTimer() { | ||
49 | if (isTiming()) stop(); | ||
50 | } | ||
51 | |||
52 | |||
53 | void BTimer::setTimeout(long t) { | ||
54 | m_timeout.tv_sec = t / 1000; | ||
55 | m_timeout.tv_usec = t; | ||
56 | m_timeout.tv_usec -= (m_timeout.tv_sec * 1000); | ||
57 | m_timeout.tv_usec *= 1000; | ||
58 | } | ||
59 | |||
60 | |||
61 | void BTimer::setTimeout(timeval t) { | ||
62 | m_timeout.tv_sec = t.tv_sec; | ||
63 | m_timeout.tv_usec = t.tv_usec; | ||
64 | } | ||
65 | |||
66 | |||
67 | void BTimer::start() { | ||
68 | gettimeofday(&m_start, 0); | ||
69 | |||
70 | if (! m_timing) { | ||
71 | m_timing = true; | ||
72 | addTimer(this); //add us to the list | ||
73 | } | ||
74 | } | ||
75 | |||
76 | |||
77 | void BTimer::stop() { | ||
78 | m_timing = false; | ||
79 | removeTimer(this); //remove us from the list | ||
80 | } | ||
81 | |||
82 | |||
83 | void BTimer::fireTimeout() { | ||
84 | if (m_handler) m_handler->timeout(); | ||
85 | } | ||
86 | |||
87 | void BTimer::updateTimers(int fd) { | ||
88 | fd_set rfds; | ||
89 | timeval now, tm, *timeout = 0; | ||
90 | |||
91 | FD_ZERO(&rfds); | ||
92 | FD_SET(fd, &rfds); | ||
93 | |||
94 | if (m_timerlist.size() > 0) { | ||
95 | gettimeofday(&now, 0); | ||
96 | |||
97 | tm.tv_sec = tm.tv_usec = 0l; | ||
98 | |||
99 | BTimer *timer = m_timerlist.front(); | ||
100 | |||
101 | tm.tv_sec = timer->getStartTime().tv_sec + | ||
102 | timer->getTimeout().tv_sec - now.tv_sec; | ||
103 | tm.tv_usec = timer->getStartTime().tv_usec + | ||
104 | timer->getTimeout().tv_usec - now.tv_usec; | ||
105 | |||
106 | while (tm.tv_usec >= 1000000) { | ||
107 | tm.tv_sec++; | ||
108 | tm.tv_usec -= 1000000; | ||
109 | } | ||
110 | |||
111 | while (tm.tv_usec < 0) { | ||
112 | if (tm.tv_sec > 0) { | ||
113 | tm.tv_sec--; | ||
114 | tm.tv_usec += 1000000; | ||
115 | } else { | ||
116 | tm.tv_usec = 0; | ||
117 | break; | ||
118 | } | ||
119 | } | ||
120 | |||
121 | timeout = &tm; | ||
122 | } | ||
123 | |||
124 | select(fd + 1, &rfds, 0, 0, timeout); | ||
125 | |||
126 | // check for timer timeout | ||
127 | gettimeofday(&now, 0); | ||
128 | |||
129 | TimerList::iterator it = m_timerlist.begin(); | ||
130 | //must check end ...the timer might remove | ||
131 | //it self from the list (should be fixed in the future) | ||
132 | for(; it != m_timerlist.end(); ++it) { | ||
133 | //This is to make sure we don't get an invalid iterator | ||
134 | //when we do fireTimeout | ||
135 | BTimer &t = *(*it); | ||
136 | tm.tv_sec = t.getStartTime().tv_sec + | ||
137 | t.getTimeout().tv_sec; | ||
138 | tm.tv_usec = t.getStartTime().tv_usec + | ||
139 | t.getTimeout().tv_usec; | ||
140 | |||
141 | if ((now.tv_sec < tm.tv_sec) || | ||
142 | (now.tv_sec == tm.tv_sec && now.tv_usec < tm.tv_usec)) | ||
143 | break; | ||
144 | |||
145 | t.fireTimeout(); | ||
146 | // restart the current timer so that the start time is updated | ||
147 | if (! t.doOnce()) | ||
148 | t.start(); | ||
149 | else { | ||
150 | t.stop(); | ||
151 | it--; | ||
152 | } | ||
153 | } | ||
154 | } | ||
155 | |||
156 | void BTimer::addTimer(BTimer *timer) { | ||
157 | assert(timer); | ||
158 | |||
159 | TimerList::iterator it = m_timerlist.begin(); | ||
160 | TimerList::iterator it_end = m_timerlist.end(); | ||
161 | int index = 0; | ||
162 | for (; it != it_end; ++it, ++index) { | ||
163 | if (((*it)->getTimeout().tv_sec > timer->getTimeout().tv_sec) || | ||
164 | (((*it)->getTimeout().tv_sec == timer->getTimeout().tv_sec) && | ||
165 | ((*it)->getTimeout().tv_usec >= timer->getTimeout().tv_usec))) { | ||
166 | break; | ||
167 | } | ||
168 | } | ||
169 | m_timerlist.insert(it, timer); | ||
170 | |||
171 | } | ||
172 | |||
173 | void BTimer::removeTimer(BTimer *timer) { | ||
174 | assert(timer); | ||
175 | m_timerlist.remove(timer); | ||
176 | } | ||
177 | |||