fixed bad overflow bug in wxX11 timer
[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 #ifdef __GNUG__
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 #else
48 #define GetMillisecondsTime wxGetLocalTimeMillis
49
50 typedef wxLongLong wxTimerTick_t;
51 #endif
52
53 // ----------------------------------------------------------------------------
54 // helper structures and wxTimerScheduler
55 // ----------------------------------------------------------------------------
56
57 class wxTimerDesc
58 {
59 public:
60 wxTimerDesc(wxTimer *t) :
61 timer(t), running(FALSE), next(NULL), prev(NULL),
62 shotTime(0), deleteFlag(NULL) {}
63
64 wxTimer *timer;
65 bool running;
66 wxTimerDesc *next, *prev;
67 unsigned long shotTime;
68 volatile bool *deleteFlag; // see comment in ~wxTimer
69 };
70
71 class wxTimerScheduler
72 {
73 public:
74 wxTimerScheduler() : m_timers(NULL) {}
75
76 void QueueTimer(wxTimerDesc *desc, wxTimerTick_t when = 0);
77 void RemoveTimer(wxTimerDesc *desc);
78 void NotifyTimers();
79
80 private:
81 wxTimerDesc *m_timers;
82 };
83
84 void wxTimerScheduler::QueueTimer(wxTimerDesc *desc, wxTimerTick_t when)
85 {
86 if ( desc->running )
87 return; // already scheduled
88
89 if ( when == 0 )
90 when = GetMillisecondsTime() + desc->timer->GetInterval();
91 desc->shotTime = when;
92 desc->running = TRUE;
93
94 wxLogTrace( wxT("timer"), wxT("queued timer %p at tick %ld"),
95 desc->timer, (long) when);
96
97 if ( m_timers )
98 {
99 wxTimerDesc *d = m_timers;
100 while ( d->next && d->next->shotTime < when ) d = d->next;
101 desc->next = d->next;
102 desc->prev = d;
103 if ( d->next )
104 d->next->prev = desc;
105 d->next = desc;
106 }
107 else
108 {
109 m_timers = desc;
110 desc->prev = desc->next = NULL;
111 }
112 }
113
114 void wxTimerScheduler::RemoveTimer(wxTimerDesc *desc)
115 {
116 desc->running = FALSE;
117 if ( desc == m_timers )
118 m_timers = desc->next;
119 if ( desc->prev )
120 desc->prev->next = desc->next;
121 if ( desc->next )
122 desc->next->prev = desc->prev;
123 desc->prev = desc->next = NULL;
124 }
125
126 void wxTimerScheduler::NotifyTimers()
127 {
128 if ( m_timers )
129 {
130 bool oneShot;
131 volatile bool timerDeleted;
132 wxTimerTick_t now = GetMillisecondsTime();
133 wxTimerDesc *desc;
134
135 while ( m_timers && m_timers->shotTime <= now )
136 {
137 desc = m_timers;
138 oneShot = desc->timer->IsOneShot();
139 RemoveTimer(desc);
140
141 timerDeleted = FALSE;
142 desc->deleteFlag = &timerDeleted;
143 desc->timer->Notify();
144
145 if ( !timerDeleted )
146 {
147 wxLogTrace( wxT("timer"), wxT("notified timer %p sheduled for %ld"),
148 desc->timer, (long) desc->shotTime);
149
150 desc->deleteFlag = NULL;
151 if ( !oneShot )
152 QueueTimer(desc, now + desc->timer->GetInterval());
153 }
154 }
155 }
156 }
157
158
159 // ----------------------------------------------------------------------------
160 // wxTimer
161 // ----------------------------------------------------------------------------
162
163 IMPLEMENT_ABSTRACT_CLASS(wxTimer,wxObject)
164
165 wxTimerScheduler *gs_scheduler = NULL;
166
167 void wxTimer::Init()
168 {
169 if ( !gs_scheduler )
170 gs_scheduler = new wxTimerScheduler;
171 m_desc = new wxTimerDesc(this);
172 }
173
174 wxTimer::~wxTimer()
175 {
176 wxLogTrace( wxT("timer"), wxT("destroying timer %p..."), this);
177 if ( IsRunning() )
178 Stop();
179
180 // NB: this is a hack: wxTimerScheduler must have some way of knowing
181 // that wxTimer object was deleted under its hands -- this may
182 // happen if somebody is really nasty and deletes the timer
183 // from wxTimer::Notify()
184 if ( m_desc->deleteFlag != NULL )
185 *m_desc->deleteFlag = TRUE;
186
187 delete m_desc;
188 wxLogTrace( wxT("timer"), wxT(" ...done destroying timer %p..."), this);
189 }
190
191 bool wxTimer::IsRunning() const
192 {
193 return m_desc->running;
194 }
195
196 bool wxTimer::Start(int millisecs, bool oneShot)
197 {
198 wxLogTrace( wxT("timer"), wxT("started timer %p: %i ms, oneshot=%i"),
199 this, millisecs, oneShot);
200
201 if ( !wxTimerBase::Start(millisecs, oneShot) )
202 return FALSE;
203
204 gs_scheduler->QueueTimer(m_desc);
205 return TRUE;
206 }
207
208 void wxTimer::Stop()
209 {
210 if ( !m_desc->running ) return;
211
212 gs_scheduler->RemoveTimer(m_desc);
213 }
214
215 /*static*/ void wxTimer::NotifyTimers()
216 {
217 if ( gs_scheduler )
218 gs_scheduler->NotifyTimers();
219 }
220
221
222
223 // A module to deallocate memory properly:
224 class wxTimerModule: public wxModule
225 {
226 DECLARE_DYNAMIC_CLASS(wxTimerModule)
227 public:
228 wxTimerModule() {}
229 bool OnInit() { return TRUE; }
230 void OnExit() { delete gs_scheduler; gs_scheduler = NULL; }
231 };
232
233 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule, wxModule)
234
235
236 #endif //wxUSE_TIMER