]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTimer implementation
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2001-2002 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
11 #pragma implementation "timer.h"
14 // For compilers that support precompilation, includes "wx.h".
15 #include "wx/wxprec.h"
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 // ----------------------------------------------------------------------------
32 #include "wx/module.h"
34 // ----------------------------------------------------------------------------
35 // Time input function
36 // ----------------------------------------------------------------------------
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()
46 #define GetMillisecondsTime() wxGetLocalTimeMillis().ToLong()
49 // ----------------------------------------------------------------------------
50 // helper structures and wxTimerScheduler
51 // ----------------------------------------------------------------------------
56 wxTimerDesc(wxTimer
*t
) :
57 timer(t
), running(FALSE
), next(NULL
), prev(NULL
),
58 shotTime(0), deleteFlag(NULL
) {}
62 wxTimerDesc
*next
, *prev
;
63 unsigned long shotTime
;
64 volatile bool *deleteFlag
; // see comment in ~wxTimer
67 class wxTimerScheduler
70 wxTimerScheduler() : m_timers(NULL
) {}
72 void QueueTimer(wxTimerDesc
*desc
, unsigned long when
= 0);
73 void RemoveTimer(wxTimerDesc
*desc
);
77 wxTimerDesc
*m_timers
;
80 void wxTimerScheduler::QueueTimer(wxTimerDesc
*desc
, unsigned long when
)
83 return; // already scheduled
86 when
= GetMillisecondsTime() + desc
->timer
->GetInterval();
87 desc
->shotTime
= when
;
90 wxLogTrace("timer", "queued timer %p at tick %i",
95 wxTimerDesc
*d
= m_timers
;
96 while ( d
->next
&& d
->next
->shotTime
< when
) d
= d
->next
;
100 d
->next
->prev
= desc
;
106 desc
->prev
= desc
->next
= NULL
;
110 void wxTimerScheduler::RemoveTimer(wxTimerDesc
*desc
)
112 desc
->running
= FALSE
;
113 if ( desc
== m_timers
)
114 m_timers
= desc
->next
;
116 desc
->prev
->next
= desc
->next
;
118 desc
->next
->prev
= desc
->prev
;
119 desc
->prev
= desc
->next
= NULL
;
122 void wxTimerScheduler::NotifyTimers()
127 volatile bool timerDeleted
;
128 unsigned long now
= GetMillisecondsTime();
131 while ( m_timers
&& m_timers
->shotTime
<= now
)
134 oneShot
= desc
->timer
->IsOneShot();
137 timerDeleted
= FALSE
;
138 desc
->deleteFlag
= &timerDeleted
;
139 desc
->timer
->Notify();
143 wxLogTrace("timer", "notified timer %p sheduled for %i",
144 desc
->timer
, desc
->shotTime
);
146 desc
->deleteFlag
= NULL
;
148 QueueTimer(desc
, now
+ desc
->timer
->GetInterval());
155 // ----------------------------------------------------------------------------
157 // ----------------------------------------------------------------------------
159 IMPLEMENT_ABSTRACT_CLASS(wxTimer
,wxObject
)
161 wxTimerScheduler
*gs_scheduler
= NULL
;
166 gs_scheduler
= new wxTimerScheduler
;
167 m_desc
= new wxTimerDesc(this);
172 wxLogTrace("timer", "destroying timer %p...", this);
176 // NB: this is a hack: wxTimerScheduler must have some way of knowing
177 // that wxTimer object was deleted under its hands -- this may
178 // happen if somebody is really nasty and deletes the timer
179 // from wxTimer::Notify()
180 if ( m_desc
->deleteFlag
!= NULL
)
181 *m_desc
->deleteFlag
= TRUE
;
184 wxLogTrace("timer", " ...done destroying timer %p...", this);
187 bool wxTimer::IsRunning() const
189 return m_desc
->running
;
192 bool wxTimer::Start(int millisecs
, bool oneShot
)
194 wxLogTrace("timer", "started timer %p: %i ms, oneshot=%i",
195 this, millisecs
, oneShot
);
197 if ( !wxTimerBase::Start(millisecs
, oneShot
) )
200 gs_scheduler
->QueueTimer(m_desc
);
206 if ( !m_desc
->running
) return;
208 gs_scheduler
->RemoveTimer(m_desc
);
211 /*static*/ void wxTimer::NotifyTimers()
214 gs_scheduler
->NotifyTimers();
219 // A module to deallocate memory properly:
220 class wxTimerModule
: public wxModule
222 DECLARE_DYNAMIC_CLASS(wxTimerModule
)
225 bool OnInit() { return TRUE
; }
226 void OnExit() { delete gs_scheduler
; gs_scheduler
= NULL
; }
229 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule
, wxModule
)