]>
git.saurik.com Git - wxWidgets.git/blob - src/mgl/timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxTimer implementation
4 // Author: Vaclav Slavik
6 // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
12 #pragma implementation "timer.h"
20 #include "wx/mgl/private.h"
22 extern "C" ulong
_EVT_getTicks();
24 // ----------------------------------------------------------------------------
25 // helper structures and wxTimerScheduler
26 // ----------------------------------------------------------------------------
31 wxTimerDesc(wxTimer
*t
) : timer(t
), running(FALSE
), next(NULL
), prev(NULL
) {}
35 wxTimerDesc
*next
, *prev
;
36 unsigned long shotTime
;
39 class wxTimerScheduler
42 wxTimerScheduler() : m_timers(NULL
) {}
44 void QueueTimer(wxTimerDesc
*desc
, unsigned long when
= 0);
45 void RemoveTimer(wxTimerDesc
*desc
);
49 wxTimerDesc
*m_timers
;
52 void wxTimerScheduler::QueueTimer(wxTimerDesc
*desc
, unsigned long when
)
55 when
= _EVT_getTicks() + desc
->timer
->GetInterval();
56 desc
->shotTime
= when
;
59 wxLogTrace("mgl_timer", "queued timer %p at tick %i",
64 wxTimerDesc
*d
= m_timers
;
65 while ( d
->next
&& d
->next
->shotTime
< when
) d
= d
->next
;
75 desc
->prev
= desc
->next
= NULL
;
79 void wxTimerScheduler::RemoveTimer(wxTimerDesc
*desc
)
81 desc
->running
= FALSE
;
82 if ( desc
== m_timers
)
83 m_timers
= desc
->next
;
85 desc
->prev
->next
= desc
->next
;
87 desc
->next
->prev
= desc
->prev
;
88 desc
->prev
= desc
->next
= NULL
;
91 void wxTimerScheduler::NotifyTimers()
95 unsigned long now
= _EVT_getTicks();
98 wxLogTrace("mgl_timer", "notifying timers, time is %i", now
);
100 while ( m_timers
&& m_timers
->shotTime
<= now
)
103 bool oneShot
= desc
->timer
->IsOneShot();
106 desc
->timer
->Notify();
107 wxLogTrace("mgl_timer", "notified timer %p sheduled for %i",
108 desc
->timer
, desc
->shotTime
);
112 QueueTimer(desc
, now
+ desc
->timer
->GetInterval());
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 IMPLEMENT_ABSTRACT_CLASS(wxTimer
,wxObject
)
126 wxTimerScheduler
*wxTimer::ms_scheduler
= NULL
;
127 size_t wxTimer::ms_timersCnt
= 0;
131 if ( ms_timersCnt
++ == 0 )
132 ms_scheduler
= new wxTimerScheduler
;
133 m_desc
= new wxTimerDesc(this);
134 wxLogTrace("mgl_timer", "--added timer (count=%i)", ms_timersCnt
);
142 if ( --ms_timersCnt
== 0 )
148 wxLogTrace("mgl_timer", "--removed timer (count=%i)", ms_timersCnt
);
151 bool wxTimer::IsRunning() const
153 return m_desc
->running
;
156 bool wxTimer::Start(int millisecs
, bool oneShot
)
158 wxLogTrace("mgl_timer", "started timer %p: %i ms, oneshot=%i",
159 this, millisecs
, oneShot
);
161 if ( !wxTimerBase::Start(millisecs
, oneShot
) )
164 ms_scheduler
->QueueTimer(m_desc
);
170 if ( !m_desc
->running
) return;
172 ms_scheduler
->RemoveTimer(m_desc
);
175 /*static*/ void wxTimer::NotifyTimers()
177 ms_scheduler
->NotifyTimers();