]>
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()
47 // Suppresses the debug warning in ToLong. FIXME: check
48 // that we don't drastically lose precision
49 #define GetMillisecondsTime() (unsigned long) wxGetLocalTimeMillis().GetValue()
52 // ----------------------------------------------------------------------------
53 // helper structures and wxTimerScheduler
54 // ----------------------------------------------------------------------------
59 wxTimerDesc(wxTimer
*t
) :
60 timer(t
), running(FALSE
), next(NULL
), prev(NULL
),
61 shotTime(0), deleteFlag(NULL
) {}
65 wxTimerDesc
*next
, *prev
;
66 unsigned long shotTime
;
67 volatile bool *deleteFlag
; // see comment in ~wxTimer
70 class wxTimerScheduler
73 wxTimerScheduler() : m_timers(NULL
) {}
75 void QueueTimer(wxTimerDesc
*desc
, unsigned long when
= 0);
76 void RemoveTimer(wxTimerDesc
*desc
);
80 wxTimerDesc
*m_timers
;
83 void wxTimerScheduler::QueueTimer(wxTimerDesc
*desc
, unsigned long when
)
86 return; // already scheduled
89 when
= GetMillisecondsTime() + desc
->timer
->GetInterval();
90 desc
->shotTime
= when
;
93 wxLogTrace("timer", "queued timer %p at tick %ld",
94 desc
->timer
, (long) when
);
98 wxTimerDesc
*d
= m_timers
;
99 while ( d
->next
&& d
->next
->shotTime
< when
) d
= d
->next
;
100 desc
->next
= d
->next
;
103 d
->next
->prev
= desc
;
109 desc
->prev
= desc
->next
= NULL
;
113 void wxTimerScheduler::RemoveTimer(wxTimerDesc
*desc
)
115 desc
->running
= FALSE
;
116 if ( desc
== m_timers
)
117 m_timers
= desc
->next
;
119 desc
->prev
->next
= desc
->next
;
121 desc
->next
->prev
= desc
->prev
;
122 desc
->prev
= desc
->next
= NULL
;
125 void wxTimerScheduler::NotifyTimers()
130 volatile bool timerDeleted
;
131 unsigned long now
= GetMillisecondsTime();
134 while ( m_timers
&& m_timers
->shotTime
<= now
)
137 oneShot
= desc
->timer
->IsOneShot();
140 timerDeleted
= FALSE
;
141 desc
->deleteFlag
= &timerDeleted
;
142 desc
->timer
->Notify();
146 wxLogTrace("timer", "notified timer %p sheduled for %ld",
147 desc
->timer
, (long) desc
->shotTime
);
149 desc
->deleteFlag
= NULL
;
151 QueueTimer(desc
, now
+ desc
->timer
->GetInterval());
158 // ----------------------------------------------------------------------------
160 // ----------------------------------------------------------------------------
162 IMPLEMENT_ABSTRACT_CLASS(wxTimer
,wxObject
)
164 wxTimerScheduler
*gs_scheduler
= NULL
;
169 gs_scheduler
= new wxTimerScheduler
;
170 m_desc
= new wxTimerDesc(this);
175 wxLogTrace("timer", "destroying timer %p...", this);
179 // NB: this is a hack: wxTimerScheduler must have some way of knowing
180 // that wxTimer object was deleted under its hands -- this may
181 // happen if somebody is really nasty and deletes the timer
182 // from wxTimer::Notify()
183 if ( m_desc
->deleteFlag
!= NULL
)
184 *m_desc
->deleteFlag
= TRUE
;
187 wxLogTrace("timer", " ...done destroying timer %p...", this);
190 bool wxTimer::IsRunning() const
192 return m_desc
->running
;
195 bool wxTimer::Start(int millisecs
, bool oneShot
)
197 wxLogTrace("timer", "started timer %p: %i ms, oneshot=%i",
198 this, millisecs
, oneShot
);
200 if ( !wxTimerBase::Start(millisecs
, oneShot
) )
203 gs_scheduler
->QueueTimer(m_desc
);
209 if ( !m_desc
->running
) return;
211 gs_scheduler
->RemoveTimer(m_desc
);
214 /*static*/ void wxTimer::NotifyTimers()
217 gs_scheduler
->NotifyTimers();
222 // A module to deallocate memory properly:
223 class wxTimerModule
: public wxModule
225 DECLARE_DYNAMIC_CLASS(wxTimerModule
)
228 bool OnInit() { return TRUE
; }
229 void OnExit() { delete gs_scheduler
; gs_scheduler
= NULL
; }
232 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule
, wxModule
)