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/Timer.cc | |
parent | 6c1f079fbd223946863ad445f8ffffbddf65f169 (diff) | |
download | fluxbox-9291eb0911bcb029cb0d3b527f45ffbb95b5d402.zip fluxbox-9291eb0911bcb029cb0d3b527f45ffbb95b5d402.tar.bz2 |
moved the update timer routines from BaseDisplay to BTimer and fixed indentation
Diffstat (limited to 'src/Timer.cc')
-rw-r--r-- | src/Timer.cc | 140 |
1 files changed, 115 insertions, 25 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 | |||