]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTimer implementation
4 // Author: Vaclav Slavik
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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 typedef ulong wxTimerTick_t
;
48 #define wxTimerTickFmtSpec _T("lu")
49 #define wxTimerTickPrintfArg(tt) (tt)
51 #define GetMillisecondsTime wxGetLocalTimeMillis
53 typedef wxLongLong wxTimerTick_t
;
56 #define wxTimerTickFmtSpec wxLongLongFmtSpec _T("d")
57 #define wxTimerTickPrintfArg(tt) (tt.GetValue())
58 #else // using native wxLongLong
59 #define wxTimerTickFmtSpec _T("s")
60 #define wxTimerTickPrintfArg(tt) (tt.ToString().c_str())
61 #endif // wx/native long long
62 #endif // __WXMGL__/!__WXMGL__
64 // ----------------------------------------------------------------------------
65 // helper structures and wxTimerScheduler
66 // ----------------------------------------------------------------------------
71 wxTimerDesc(wxTimer
*t
) :
72 timer(t
), running(false), next(NULL
), prev(NULL
),
73 shotTime(0), deleteFlag(NULL
) {}
77 wxTimerDesc
*next
, *prev
;
78 wxTimerTick_t shotTime
;
79 volatile bool *deleteFlag
; // see comment in ~wxTimer
82 class wxTimerScheduler
85 wxTimerScheduler() : m_timers(NULL
) {}
87 void QueueTimer(wxTimerDesc
*desc
, wxTimerTick_t when
= 0);
88 void RemoveTimer(wxTimerDesc
*desc
);
92 wxTimerDesc
*m_timers
;
95 void wxTimerScheduler::QueueTimer(wxTimerDesc
*desc
, wxTimerTick_t when
)
98 return; // already scheduled
101 when
= GetMillisecondsTime() + desc
->timer
->GetInterval();
102 desc
->shotTime
= when
;
103 desc
->running
= true;
105 wxLogTrace( wxT("timer"),
106 wxT("queued timer %p at tick %") wxTimerTickFmtSpec
,
107 desc
->timer
, wxTimerTickPrintfArg(when
));
111 wxTimerDesc
*d
= m_timers
;
112 while ( d
->next
&& d
->next
->shotTime
< when
) d
= d
->next
;
113 desc
->next
= d
->next
;
116 d
->next
->prev
= desc
;
122 desc
->prev
= desc
->next
= NULL
;
126 void wxTimerScheduler::RemoveTimer(wxTimerDesc
*desc
)
128 desc
->running
= false;
129 if ( desc
== m_timers
)
130 m_timers
= desc
->next
;
132 desc
->prev
->next
= desc
->next
;
134 desc
->next
->prev
= desc
->prev
;
135 desc
->prev
= desc
->next
= NULL
;
138 void wxTimerScheduler::NotifyTimers()
143 volatile bool timerDeleted
;
144 wxTimerTick_t now
= GetMillisecondsTime();
146 for ( wxTimerDesc
*desc
= m_timers
; desc
; desc
= desc
->next
)
148 if ( desc
->running
&& desc
->shotTime
<= now
)
151 oneShot
= desc
->timer
->IsOneShot();
154 timerDeleted
= false;
155 desc
->deleteFlag
= &timerDeleted
;
156 desc
->timer
->Notify();
160 wxLogTrace( wxT("timer"),
161 wxT("notified timer %p sheduled for %")
164 wxTimerTickPrintfArg(desc
->shotTime
) );
166 desc
->deleteFlag
= NULL
;
168 QueueTimer(desc
, now
+ desc
->timer
->GetInterval());
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 IMPLEMENT_ABSTRACT_CLASS(wxTimer
, wxEvtHandler
)
182 wxTimerScheduler
*gs_scheduler
= NULL
;
187 gs_scheduler
= new wxTimerScheduler
;
188 m_desc
= new wxTimerDesc(this);
193 wxLogTrace( wxT("timer"), wxT("destroying timer %p..."), this);
197 // NB: this is a hack: wxTimerScheduler must have some way of knowing
198 // that wxTimer object was deleted under its hands -- this may
199 // happen if somebody is really nasty and deletes the timer
200 // from wxTimer::Notify()
201 if ( m_desc
->deleteFlag
!= NULL
)
202 *m_desc
->deleteFlag
= true;
205 wxLogTrace( wxT("timer"), wxT(" ...done destroying timer %p..."), this);
208 bool wxTimer::IsRunning() const
210 return m_desc
->running
;
213 bool wxTimer::Start(int millisecs
, bool oneShot
)
215 wxLogTrace( wxT("timer"), wxT("started timer %p: %i ms, oneshot=%i"),
216 this, millisecs
, oneShot
);
218 if ( !wxTimerBase::Start(millisecs
, oneShot
) )
221 gs_scheduler
->QueueTimer(m_desc
);
227 if ( !m_desc
->running
) return;
229 gs_scheduler
->RemoveTimer(m_desc
);
232 /*static*/ void wxTimer::NotifyTimers()
235 gs_scheduler
->NotifyTimers();
240 // A module to deallocate memory properly:
241 class wxTimerModule
: public wxModule
243 DECLARE_DYNAMIC_CLASS(wxTimerModule
)
246 bool OnInit() { return true; }
247 void OnExit() { delete gs_scheduler
; gs_scheduler
= NULL
; }
250 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule
, wxModule
)