]> git.saurik.com Git - wxWidgets.git/blob - src/generic/timer.cpp
wxOS2 with Open Watcom: correct PCH usage, missing headers, warning fixes, source...
[wxWidgets.git] / src / generic / timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mgl/timer.cpp
3 // Purpose: wxTimer implementation
4 // Author: Vaclav Slavik
5 // Id: $Id$
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
11 #pragma implementation "timer.h"
12 #endif
13
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
16
17 #ifdef __BORLANDC__
18 #pragma hdrstop
19 #endif
20
21 // ----------------------------------------------------------------------------
22 // NB: when using generic wxTimer implementation in your port, you *must* call
23 // wxTimer::NotifyTimers() often enough. The ideal place for this
24 // is in wxEventLoop::Dispatch().
25 // ----------------------------------------------------------------------------
26
27 #include "wx/timer.h"
28
29 #if wxUSE_TIMER
30
31 #include "wx/log.h"
32 #include "wx/module.h"
33
34 // ----------------------------------------------------------------------------
35 // Time input function
36 // ----------------------------------------------------------------------------
37
38 #ifdef __WXMGL__
39 // We take advantage of wxMGL's _EVT_getTicks because it is faster
40 // (especially under MS-DOS!) and more precise than wxGetLocalTimeMillis
41 // if we are unlucky and the latter combines information from two sources.
42 #include "wx/mgl/private.h"
43 extern "C" ulong _EVT_getTicks();
44 #define GetMillisecondsTime _EVT_getTicks
45
46 typedef ulong wxTimerTick_t;
47
48 #define wxTimerTickFmtSpec _T("lu")
49 #define wxTimerTickPrintfArg(tt) (tt)
50
51 #ifdef __DOS__
52 // Under DOS the MGL timer has a 24hr period, so consider the 12 hours
53 // before y to be 'less' and the the 12 hours after 'greater' modulo
54 // 24 hours.
55 inline bool wxTickGreaterEqual(wxTimerTick_t x, wxTimerTick_t y)
56 {
57 // _EVT_getTicks wraps at 1573040 * 55
58 const wxTimerTick_t modulus = 1573040 * 55;
59 return (2 * modulus + x - y) % modulus < modulus / 2;
60 }
61 #else
62 // If wxTimerTick_t is 32-bits then it'll wrap in around 50 days. So
63 // let the 25 days before y be 'less' and 25 days after be 'greater'.
64 inline bool wxTickGreaterEqual(wxTimerTick_t x, wxTimerTick_t y)
65 {
66 // This code assumes wxTimerTick_t is an unsigned type.
67 // Set half_modulus with top bit set and the rest zeros.
68 const wxTimerTick_t half_modulus = ~((~(wxTimerTick_t)0) >> 1);
69 return x - y < half_modulus;
70 }
71 #endif
72 #else // !__WXMGL__
73 #define GetMillisecondsTime wxGetLocalTimeMillis
74
75 typedef wxLongLong wxTimerTick_t;
76
77 #if wxUSE_LONGLONG_WX
78 #define wxTimerTickFmtSpec wxLongLongFmtSpec _T("d")
79 #define wxTimerTickPrintfArg(tt) (tt.GetValue())
80 #else // using native wxLongLong
81 #define wxTimerTickFmtSpec _T("s")
82 #define wxTimerTickPrintfArg(tt) (tt.ToString().c_str())
83 #endif // wx/native long long
84
85 inline bool wxTickGreaterEqual(wxTimerTick_t x, wxTimerTick_t y)
86 {
87 return x >= y;
88 }
89 #endif // __WXMGL__/!__WXMGL__
90
91 // ----------------------------------------------------------------------------
92 // helper structures and wxTimerScheduler
93 // ----------------------------------------------------------------------------
94
95 class wxTimerDesc
96 {
97 public:
98 wxTimerDesc(wxTimer *t) :
99 timer(t), running(false), next(NULL), prev(NULL),
100 shotTime(0), deleteFlag(NULL) {}
101
102 wxTimer *timer;
103 bool running;
104 wxTimerDesc *next, *prev;
105 wxTimerTick_t shotTime;
106 volatile bool *deleteFlag; // see comment in ~wxTimer
107 };
108
109 class wxTimerScheduler
110 {
111 public:
112 wxTimerScheduler() : m_timers(NULL) {}
113
114 void QueueTimer(wxTimerDesc *desc, wxTimerTick_t when = 0);
115 void RemoveTimer(wxTimerDesc *desc);
116 void NotifyTimers();
117
118 private:
119 wxTimerDesc *m_timers;
120 };
121
122 void wxTimerScheduler::QueueTimer(wxTimerDesc *desc, wxTimerTick_t when)
123 {
124 if ( desc->running )
125 return; // already scheduled
126
127 if ( when == 0 )
128 when = GetMillisecondsTime() + desc->timer->GetInterval();
129 desc->shotTime = when;
130 desc->running = true;
131
132 wxLogTrace( wxT("timer"),
133 wxT("queued timer %p at tick %") wxTimerTickFmtSpec,
134 desc->timer, wxTimerTickPrintfArg(when));
135
136 if ( m_timers )
137 {
138 wxTimerDesc *d = m_timers;
139 while ( d->next && d->next->shotTime < when ) d = d->next;
140 desc->next = d->next;
141 desc->prev = d;
142 if ( d->next )
143 d->next->prev = desc;
144 d->next = desc;
145 }
146 else
147 {
148 m_timers = desc;
149 desc->prev = desc->next = NULL;
150 }
151 }
152
153 void wxTimerScheduler::RemoveTimer(wxTimerDesc *desc)
154 {
155 desc->running = false;
156 if ( desc == m_timers )
157 m_timers = desc->next;
158 if ( desc->prev )
159 desc->prev->next = desc->next;
160 if ( desc->next )
161 desc->next->prev = desc->prev;
162 desc->prev = desc->next = NULL;
163 }
164
165 void wxTimerScheduler::NotifyTimers()
166 {
167 if ( m_timers )
168 {
169 bool oneShot;
170 volatile bool timerDeleted;
171 wxTimerTick_t now = GetMillisecondsTime();
172
173 for ( wxTimerDesc *desc = m_timers; desc; desc = desc->next )
174 {
175 if ( desc->running && wxTickGreaterEqual(now, desc->shotTime) )
176 {
177 oneShot = desc->timer->IsOneShot();
178 RemoveTimer(desc);
179
180 timerDeleted = false;
181 desc->deleteFlag = &timerDeleted;
182 desc->timer->Notify();
183
184 if ( !timerDeleted )
185 {
186 wxLogTrace( wxT("timer"),
187 wxT("notified timer %p sheduled for %")
188 wxTimerTickFmtSpec,
189 desc->timer,
190 wxTimerTickPrintfArg(desc->shotTime) );
191
192 desc->deleteFlag = NULL;
193 if ( !oneShot )
194 QueueTimer(desc, now + desc->timer->GetInterval());
195 }
196 else
197 desc = m_timers;
198 }
199 }
200 }
201 }
202
203
204 // ----------------------------------------------------------------------------
205 // wxTimer
206 // ----------------------------------------------------------------------------
207
208 IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler)
209
210 wxTimerScheduler *gs_scheduler = NULL;
211
212 void wxTimer::Init()
213 {
214 if ( !gs_scheduler )
215 gs_scheduler = new wxTimerScheduler;
216 m_desc = new wxTimerDesc(this);
217 }
218
219 wxTimer::~wxTimer()
220 {
221 wxLogTrace( wxT("timer"), wxT("destroying timer %p..."), this);
222 if ( IsRunning() )
223 Stop();
224
225 // NB: this is a hack: wxTimerScheduler must have some way of knowing
226 // that wxTimer object was deleted under its hands -- this may
227 // happen if somebody is really nasty and deletes the timer
228 // from wxTimer::Notify()
229 if ( m_desc->deleteFlag != NULL )
230 *m_desc->deleteFlag = true;
231
232 delete m_desc;
233 wxLogTrace( wxT("timer"), wxT(" ...done destroying timer %p..."), this);
234 }
235
236 bool wxTimer::IsRunning() const
237 {
238 return m_desc->running;
239 }
240
241 bool wxTimer::Start(int millisecs, bool oneShot)
242 {
243 wxLogTrace( wxT("timer"), wxT("started timer %p: %i ms, oneshot=%i"),
244 this, millisecs, oneShot);
245
246 if ( !wxTimerBase::Start(millisecs, oneShot) )
247 return false;
248
249 gs_scheduler->QueueTimer(m_desc);
250 return true;
251 }
252
253 void wxTimer::Stop()
254 {
255 if ( !m_desc->running ) return;
256
257 gs_scheduler->RemoveTimer(m_desc);
258 }
259
260 /*static*/ void wxTimer::NotifyTimers()
261 {
262 if ( gs_scheduler )
263 gs_scheduler->NotifyTimers();
264 }
265
266
267
268 // A module to deallocate memory properly:
269 class wxTimerModule: public wxModule
270 {
271 DECLARE_DYNAMIC_CLASS(wxTimerModule)
272 public:
273 wxTimerModule() {}
274 bool OnInit() { return true; }
275 void OnExit() { delete gs_scheduler; gs_scheduler = NULL; }
276 };
277
278 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule, wxModule)
279
280
281 #endif //wxUSE_TIMER