]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/timer.cpp
3 // Purpose: wxTimer implementation
4 // Author: Vaclav Slavik
6 // Copyright: (c) Vaclav Slavik
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
17 // ----------------------------------------------------------------------------
18 // NB: when using generic wxTimer implementation in your port, you *must* call
19 // wxTimer::NotifyTimers() often enough. The ideal place for this
20 // is in wxEventLoop::Dispatch().
21 // ----------------------------------------------------------------------------
27 #include "wx/module.h"
30 #include "wx/apptrait.h"
31 #include "wx/generic/private/timer.h"
33 // ----------------------------------------------------------------------------
34 // Time input function
35 // ----------------------------------------------------------------------------
37 #define GetMillisecondsTime wxGetLocalTimeMillis
39 typedef wxLongLong wxTimerTick_t
;
42 #define wxTimerTickFmtSpec wxLongLongFmtSpec "d"
43 #define wxTimerTickPrintfArg(tt) (tt.GetValue())
44 #else // using native wxLongLong
45 #define wxTimerTickFmtSpec wxT("s")
46 #define wxTimerTickPrintfArg(tt) (tt.ToString().c_str())
47 #endif // wx/native long long
49 inline bool wxTickGreaterEqual(wxTimerTick_t x
, wxTimerTick_t y
)
54 // ----------------------------------------------------------------------------
55 // helper structures and wxTimerScheduler
56 // ----------------------------------------------------------------------------
61 wxTimerDesc(wxGenericTimerImpl
*t
) :
62 timer(t
), running(false), next(NULL
), prev(NULL
),
63 shotTime(0), deleteFlag(NULL
) {}
65 wxGenericTimerImpl
*timer
;
67 wxTimerDesc
*next
, *prev
;
68 wxTimerTick_t shotTime
;
69 volatile bool *deleteFlag
; // see comment in ~wxTimer
72 class wxTimerScheduler
75 wxTimerScheduler() : m_timers(NULL
) {}
77 void QueueTimer(wxTimerDesc
*desc
, wxTimerTick_t when
= 0);
78 void RemoveTimer(wxTimerDesc
*desc
);
82 wxTimerDesc
*m_timers
;
85 void wxTimerScheduler::QueueTimer(wxTimerDesc
*desc
, wxTimerTick_t when
)
88 return; // already scheduled
91 when
= GetMillisecondsTime() + desc
->timer
->GetInterval();
92 desc
->shotTime
= when
;
95 wxLogTrace( wxT("timer"),
96 wxT("queued timer %p at tick %") wxTimerTickFmtSpec
,
97 desc
->timer
, wxTimerTickPrintfArg(when
));
101 wxTimerDesc
*d
= m_timers
;
102 while ( d
->next
&& d
->next
->shotTime
< when
) d
= d
->next
;
103 desc
->next
= d
->next
;
106 d
->next
->prev
= desc
;
112 desc
->prev
= desc
->next
= NULL
;
116 void wxTimerScheduler::RemoveTimer(wxTimerDesc
*desc
)
118 desc
->running
= false;
119 if ( desc
== m_timers
)
120 m_timers
= desc
->next
;
122 desc
->prev
->next
= desc
->next
;
124 desc
->next
->prev
= desc
->prev
;
125 desc
->prev
= desc
->next
= NULL
;
128 void wxTimerScheduler::NotifyTimers()
133 volatile bool timerDeleted
;
134 wxTimerTick_t now
= GetMillisecondsTime();
136 for ( wxTimerDesc
*desc
= m_timers
; desc
; desc
= desc
->next
)
138 if ( desc
->running
&& wxTickGreaterEqual(now
, desc
->shotTime
) )
140 oneShot
= desc
->timer
->IsOneShot();
143 timerDeleted
= false;
144 desc
->deleteFlag
= &timerDeleted
;
145 desc
->timer
->Notify();
149 wxLogTrace( wxT("timer"),
150 wxT("notified timer %p sheduled for %")
153 wxTimerTickPrintfArg(desc
->shotTime
) );
155 desc
->deleteFlag
= NULL
;
157 QueueTimer(desc
, now
+ desc
->timer
->GetInterval());
171 // ----------------------------------------------------------------------------
173 // ----------------------------------------------------------------------------
175 wxTimerScheduler
*gs_scheduler
= NULL
;
177 void wxGenericTimerImpl::Init()
180 gs_scheduler
= new wxTimerScheduler
;
181 m_desc
= new wxTimerDesc(this);
184 wxGenericTimerImpl::~wxGenericTimerImpl()
186 wxLogTrace( wxT("timer"), wxT("destroying timer %p..."), this);
190 // NB: this is a hack: wxTimerScheduler must have some way of knowing
191 // that wxTimer object was deleted under its hands -- this may
192 // happen if somebody is really nasty and deletes the timer
193 // from wxTimer::Notify()
194 if ( m_desc
->deleteFlag
!= NULL
)
195 *m_desc
->deleteFlag
= true;
198 wxLogTrace( wxT("timer"), wxT(" ...done destroying timer %p..."), this);
201 bool wxGenericTimerImpl::IsRunning() const
203 return m_desc
->running
;
206 bool wxGenericTimerImpl::Start(int millisecs
, bool oneShot
)
208 wxLogTrace( wxT("timer"), wxT("started timer %p: %i ms, oneshot=%i"),
209 this, millisecs
, oneShot
);
211 if ( !wxTimerImpl::Start(millisecs
, oneShot
) )
214 gs_scheduler
->QueueTimer(m_desc
);
218 void wxGenericTimerImpl::Stop()
220 if ( !m_desc
->running
) return;
222 gs_scheduler
->RemoveTimer(m_desc
);
225 /*static*/ void wxGenericTimerImpl::NotifyTimers()
228 gs_scheduler
->NotifyTimers();
233 // A module to deallocate memory properly:
234 class wxTimerModule
: public wxModule
236 DECLARE_DYNAMIC_CLASS(wxTimerModule
)
239 bool OnInit() { return true; }
240 void OnExit() { wxDELETE(gs_scheduler
); }
243 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule
, wxModule
)
245 // ----------------------------------------------------------------------------
247 // ----------------------------------------------------------------------------
249 wxTimerImpl
*wxGUIAppTraits::CreateTimerImpl(wxTimer
*timer
)
251 return new wxGenericTimerImpl(timer
);