]>
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 GetMillisecondsTime wxGetLocalTimeMillis
50 typedef wxLongLong wxTimerTick_t
;
53 // ----------------------------------------------------------------------------
54 // helper structures and wxTimerScheduler
55 // ----------------------------------------------------------------------------
60 wxTimerDesc(wxTimer
*t
) :
61 timer(t
), running(FALSE
), next(NULL
), prev(NULL
),
62 shotTime(0), deleteFlag(NULL
) {}
66 wxTimerDesc
*next
, *prev
;
67 wxTimerTick_t shotTime
;
68 volatile bool *deleteFlag
; // see comment in ~wxTimer
71 class wxTimerScheduler
74 wxTimerScheduler() : m_timers(NULL
) {}
76 void QueueTimer(wxTimerDesc
*desc
, wxTimerTick_t when
= 0);
77 void RemoveTimer(wxTimerDesc
*desc
);
81 wxTimerDesc
*m_timers
;
84 void wxTimerScheduler::QueueTimer(wxTimerDesc
*desc
, wxTimerTick_t when
)
87 return; // already scheduled
90 when
= GetMillisecondsTime() + desc
->timer
->GetInterval();
91 desc
->shotTime
= when
;
95 wxLogTrace( wxT("timer"),
96 wxT("queued timer %p at tick %") wxLongLongFmtSpec
_T("d"),
97 desc
->timer
, when
.GetValue());
102 wxTimerDesc
*d
= m_timers
;
103 while ( d
->next
&& d
->next
->shotTime
< when
) d
= d
->next
;
104 desc
->next
= d
->next
;
107 d
->next
->prev
= desc
;
113 desc
->prev
= desc
->next
= NULL
;
117 void wxTimerScheduler::RemoveTimer(wxTimerDesc
*desc
)
119 desc
->running
= FALSE
;
120 if ( desc
== m_timers
)
121 m_timers
= desc
->next
;
123 desc
->prev
->next
= desc
->next
;
125 desc
->next
->prev
= desc
->prev
;
126 desc
->prev
= desc
->next
= NULL
;
129 void wxTimerScheduler::NotifyTimers()
134 volatile bool timerDeleted
;
135 wxTimerTick_t now
= GetMillisecondsTime();
138 while ( m_timers
&& m_timers
->shotTime
<= now
)
141 oneShot
= desc
->timer
->IsOneShot();
144 timerDeleted
= FALSE
;
145 desc
->deleteFlag
= &timerDeleted
;
146 desc
->timer
->Notify();
151 wxLogTrace( wxT("timer"),
152 wxT("notified timer %p sheduled for %") wxLongLongFmtSpec
_T("d"),
153 desc
->timer
, desc
->shotTime
.GetValue() );
156 desc
->deleteFlag
= NULL
;
158 QueueTimer(desc
, now
+ desc
->timer
->GetInterval());
165 // ----------------------------------------------------------------------------
167 // ----------------------------------------------------------------------------
169 IMPLEMENT_ABSTRACT_CLASS(wxTimer
,wxObject
)
171 wxTimerScheduler
*gs_scheduler
= NULL
;
176 gs_scheduler
= new wxTimerScheduler
;
177 m_desc
= new wxTimerDesc(this);
182 wxLogTrace( wxT("timer"), wxT("destroying timer %p..."), this);
186 // NB: this is a hack: wxTimerScheduler must have some way of knowing
187 // that wxTimer object was deleted under its hands -- this may
188 // happen if somebody is really nasty and deletes the timer
189 // from wxTimer::Notify()
190 if ( m_desc
->deleteFlag
!= NULL
)
191 *m_desc
->deleteFlag
= TRUE
;
194 wxLogTrace( wxT("timer"), wxT(" ...done destroying timer %p..."), this);
197 bool wxTimer::IsRunning() const
199 return m_desc
->running
;
202 bool wxTimer::Start(int millisecs
, bool oneShot
)
204 wxLogTrace( wxT("timer"), wxT("started timer %p: %i ms, oneshot=%i"),
205 this, millisecs
, oneShot
);
207 if ( !wxTimerBase::Start(millisecs
, oneShot
) )
210 gs_scheduler
->QueueTimer(m_desc
);
216 if ( !m_desc
->running
) return;
218 gs_scheduler
->RemoveTimer(m_desc
);
221 /*static*/ void wxTimer::NotifyTimers()
224 gs_scheduler
->NotifyTimers();
229 // A module to deallocate memory properly:
230 class wxTimerModule
: public wxModule
232 DECLARE_DYNAMIC_CLASS(wxTimerModule
)
235 bool OnInit() { return TRUE
; }
236 void OnExit() { delete gs_scheduler
; gs_scheduler
= NULL
; }
239 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule
, wxModule
)