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