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