]>
git.saurik.com Git - wxWidgets.git/blob - src/unix/timerunx.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/unix/timerunx.cpp
3 // Purpose: wxTimer implementation for non-GUI applications under Unix
4 // Author: Lukasz Michalski
7 // Copyright: (c) 2007 Lukasz Michalski
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
25 #include "wx/module.h"
28 #include "wx/hashmap.h"
32 #include "wx/apptrait.h"
33 #include "wx/longlong.h"
35 #include "wx/vector.h"
40 #include "wx/unix/private/timer.h"
42 #include "wx/listimpl.cpp"
43 WX_DEFINE_LIST(wxTimerList
)
45 // trace mask for the debugging messages used here
46 #define wxTrace_Timer wxT("timer")
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
52 // helper function to format wxUsecClock_t
53 static inline wxString
wxUsecClockAsString(wxUsecClock_t usec
)
56 return usec
.ToString();
57 #else // wxUsecClock_t == double
58 return wxString::Format(wxT("%.0f"), usec
);
62 // ============================================================================
63 // wxTimerScheduler implementation
64 // ============================================================================
66 wxTimerScheduler
*wxTimerScheduler::ms_instance
= NULL
;
68 wxTimerScheduler::~wxTimerScheduler()
70 for ( wxTimerList::iterator node
= m_timers
.begin();
71 node
!= m_timers
.end();
78 void wxTimerScheduler::AddTimer(wxUnixTimerImpl
*timer
, wxUsecClock_t expiration
)
80 DoAddTimer(new wxTimerSchedule(timer
, expiration
));
83 void wxTimerScheduler::DoAddTimer(wxTimerSchedule
*s
)
85 // do an insertion sort to keep the list sorted in expiration order
86 wxTimerList::iterator node
;
87 for ( node
= m_timers
.begin(); node
!= m_timers
.end(); ++node
)
89 wxASSERT_MSG( (*node
)->m_timer
!= s
->m_timer
,
90 wxT("adding the same timer twice?") );
92 if ( (*node
)->m_expiration
> s
->m_expiration
)
96 m_timers
.insert(node
, s
);
98 wxLogTrace(wxTrace_Timer
, wxT("Inserted timer %d expiring at %s"),
100 wxUsecClockAsString(s
->m_expiration
).c_str());
103 void wxTimerScheduler::RemoveTimer(wxUnixTimerImpl
*timer
)
105 wxLogTrace(wxTrace_Timer
, wxT("Removing timer %d"), timer
->GetId());
107 for ( wxTimerList::iterator node
= m_timers
.begin();
108 node
!= m_timers
.end();
111 if ( (*node
)->m_timer
== timer
)
114 m_timers
.erase(node
);
119 wxFAIL_MSG( wxT("removing inexistent timer?") );
122 bool wxTimerScheduler::GetNext(wxUsecClock_t
*remaining
) const
124 if ( m_timers
.empty() )
127 wxCHECK_MSG( remaining
, false, wxT("NULL pointer") );
129 *remaining
= (*m_timers
.begin())->m_expiration
- wxGetUTCTimeUSec();
130 if ( *remaining
< 0 )
132 // timer already expired, don't wait at all before notifying it
139 bool wxTimerScheduler::NotifyExpired()
141 if ( m_timers
.empty() )
144 const wxUsecClock_t now
= wxGetUTCTimeUSec();
146 typedef wxVector
<wxUnixTimerImpl
*> TimerImpls
;
148 for ( wxTimerList::iterator next
,
149 cur
= m_timers
.begin(); cur
!= m_timers
.end(); cur
= next
)
151 wxTimerSchedule
* const s
= *cur
;
152 if ( s
->m_expiration
> now
)
154 // as the list is sorted by expiration time, we can skip the rest
158 // remember next as we will delete the node pointed to by cur
164 // check whether we need to keep this timer
165 wxUnixTimerImpl
* const timer
= s
->m_timer
;
166 if ( timer
->IsOneShot() )
168 // the timer needs to be stopped but don't call its Stop() from
169 // here as it would attempt to remove the timer from our list and
170 // we had already done it, so we just need to reset its state
171 timer
->MarkStopped();
173 // don't need it any more
176 else // reschedule the next timer expiration
178 // always keep the expiration time in the future, i.e. base it on
179 // the current time instead of just offsetting it from the current
180 // expiration time because it could happen that we're late and the
181 // current expiration time is (far) in the past
182 s
->m_expiration
= now
+ timer
->GetInterval()*1000;
186 // we can't notify the timer from this loop as the timer event handler
187 // could modify m_timers (for example, but not only, by stopping this
188 // timer) which would render our iterators invalid, so do it after the
190 toNotify
.push_back(timer
);
193 if ( toNotify
.empty() )
196 for ( TimerImpls::const_iterator i
= toNotify
.begin(),
197 end
= toNotify
.end();
207 // ============================================================================
208 // wxUnixTimerImpl implementation
209 // ============================================================================
211 wxUnixTimerImpl::wxUnixTimerImpl(wxTimer
* timer
)
217 bool wxUnixTimerImpl::Start(int milliseconds
, bool oneShot
)
219 // notice that this will stop an already running timer
220 wxTimerImpl::Start(milliseconds
, oneShot
);
222 wxTimerScheduler::Get().AddTimer(this, wxGetUTCTimeUSec() + m_milli
*1000);
228 void wxUnixTimerImpl::Stop()
232 wxTimerScheduler::Get().RemoveTimer(this);
238 bool wxUnixTimerImpl::IsRunning() const
243 wxUnixTimerImpl::~wxUnixTimerImpl()
245 wxASSERT_MSG( !m_isRunning
, wxT("must have been stopped before") );
248 // ============================================================================
249 // wxTimerUnixModule: responsible for freeing the global timer scheduler
250 // ============================================================================
252 class wxTimerUnixModule
: public wxModule
255 wxTimerUnixModule() {}
256 virtual bool OnInit() { return true; }
257 virtual void OnExit() { wxTimerScheduler::Shutdown(); }
259 DECLARE_DYNAMIC_CLASS(wxTimerUnixModule
)
262 IMPLEMENT_DYNAMIC_CLASS(wxTimerUnixModule
, wxModule
)
264 // ============================================================================
266 // ============================================================================
268 wxTimerImpl
*wxConsoleAppTraits::CreateTimerImpl(wxTimer
*timer
)
270 return new wxUnixTimerImpl(timer
);
273 #endif // wxUSE_TIMER