]>
Commit | Line | Data |
---|---|---|
32b8ec41 | 1 | ///////////////////////////////////////////////////////////////////////////// |
1acd70f9 | 2 | // Name: mgl/timer.cpp |
32b8ec41 | 3 | // Purpose: wxTimer implementation |
1acd70f9 | 4 | // Author: Vaclav Slavik |
32b8ec41 | 5 | // Id: $Id$ |
6d89ddef | 6 | // Copyright: (c) Vaclav Slavik |
65571936 | 7 | // Licence: wxWindows licence |
32b8ec41 VZ |
8 | ///////////////////////////////////////////////////////////////////////////// |
9 | ||
14f355c2 | 10 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
32b8ec41 VZ |
11 | #pragma implementation "timer.h" |
12 | #endif | |
13 | ||
a246f95e VS |
14 | // For compilers that support precompilation, includes "wx.h". |
15 | #include "wx/wxprec.h" | |
16 | ||
17 | #ifdef __BORLANDC__ | |
18 | #pragma hdrstop | |
19 | #endif | |
20 | ||
a0cb0ba5 | 21 | // ---------------------------------------------------------------------------- |
ca65c044 WS |
22 | // NB: when using generic wxTimer implementation in your port, you *must* call |
23 | // wxTimer::NotifyTimers() often enough. The ideal place for this | |
a0cb0ba5 VS |
24 | // is in wxEventLoop::Dispatch(). |
25 | // ---------------------------------------------------------------------------- | |
26 | ||
32b8ec41 VZ |
27 | #include "wx/timer.h" |
28 | ||
1acd70f9 VS |
29 | #if wxUSE_TIMER |
30 | ||
88f2a771 | 31 | #include "wx/log.h" |
0e4b9976 | 32 | #include "wx/module.h" |
1acd70f9 | 33 | |
a0cb0ba5 VS |
34 | // ---------------------------------------------------------------------------- |
35 | // Time input function | |
36 | // ---------------------------------------------------------------------------- | |
37 | ||
38 | #ifdef __WXMGL__ | |
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(); | |
9750481f VZ |
44 | #define GetMillisecondsTime _EVT_getTicks |
45 | ||
46 | typedef ulong wxTimerTick_t; | |
5ed08e5b VZ |
47 | |
48 | #define wxTimerTickFmtSpec _T("lu") | |
49 | #define wxTimerTickPrintfArg(tt) (tt) | |
50 | #else // !__WXMGL__ | |
9750481f VZ |
51 | #define GetMillisecondsTime wxGetLocalTimeMillis |
52 | ||
53 | typedef wxLongLong wxTimerTick_t; | |
5ed08e5b VZ |
54 | |
55 | #if wxUSE_LONGLONG_WX | |
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__ | |
1acd70f9 VS |
63 | |
64 | // ---------------------------------------------------------------------------- | |
65 | // helper structures and wxTimerScheduler | |
66 | // ---------------------------------------------------------------------------- | |
67 | ||
68 | class wxTimerDesc | |
69 | { | |
70 | public: | |
ca65c044 WS |
71 | wxTimerDesc(wxTimer *t) : |
72 | timer(t), running(false), next(NULL), prev(NULL), | |
0e4b9976 | 73 | shotTime(0), deleteFlag(NULL) {} |
1acd70f9 VS |
74 | |
75 | wxTimer *timer; | |
76 | bool running; | |
77 | wxTimerDesc *next, *prev; | |
ca65c044 | 78 | wxTimerTick_t shotTime; |
0e4b9976 | 79 | volatile bool *deleteFlag; // see comment in ~wxTimer |
1acd70f9 VS |
80 | }; |
81 | ||
82 | class wxTimerScheduler | |
83 | { | |
84 | public: | |
85 | wxTimerScheduler() : m_timers(NULL) {} | |
86 | ||
9750481f | 87 | void QueueTimer(wxTimerDesc *desc, wxTimerTick_t when = 0); |
1acd70f9 VS |
88 | void RemoveTimer(wxTimerDesc *desc); |
89 | void NotifyTimers(); | |
ca65c044 | 90 | |
1acd70f9 VS |
91 | private: |
92 | wxTimerDesc *m_timers; | |
93 | }; | |
94 | ||
9750481f | 95 | void wxTimerScheduler::QueueTimer(wxTimerDesc *desc, wxTimerTick_t when) |
1acd70f9 | 96 | { |
0e4b9976 VS |
97 | if ( desc->running ) |
98 | return; // already scheduled | |
ca65c044 | 99 | |
1acd70f9 | 100 | if ( when == 0 ) |
a0cb0ba5 | 101 | when = GetMillisecondsTime() + desc->timer->GetInterval(); |
1acd70f9 | 102 | desc->shotTime = when; |
ca65c044 | 103 | desc->running = true; |
1acd70f9 | 104 | |
ee9768c9 | 105 | wxLogTrace( wxT("timer"), |
ca65c044 | 106 | wxT("queued timer %p at tick %") wxTimerTickFmtSpec, |
5ed08e5b | 107 | desc->timer, wxTimerTickPrintfArg(when)); |
88f2a771 | 108 | |
1acd70f9 VS |
109 | if ( m_timers ) |
110 | { | |
111 | wxTimerDesc *d = m_timers; | |
112 | while ( d->next && d->next->shotTime < when ) d = d->next; | |
113 | desc->next = d->next; | |
114 | desc->prev = d; | |
115 | if ( d->next ) | |
116 | d->next->prev = desc; | |
117 | d->next = desc; | |
118 | } | |
119 | else | |
120 | { | |
121 | m_timers = desc; | |
122 | desc->prev = desc->next = NULL; | |
123 | } | |
124 | } | |
125 | ||
126 | void wxTimerScheduler::RemoveTimer(wxTimerDesc *desc) | |
127 | { | |
ca65c044 | 128 | desc->running = false; |
1acd70f9 VS |
129 | if ( desc == m_timers ) |
130 | m_timers = desc->next; | |
131 | if ( desc->prev ) | |
132 | desc->prev->next = desc->next; | |
133 | if ( desc->next ) | |
134 | desc->next->prev = desc->prev; | |
135 | desc->prev = desc->next = NULL; | |
136 | } | |
137 | ||
138 | void wxTimerScheduler::NotifyTimers() | |
139 | { | |
140 | if ( m_timers ) | |
141 | { | |
0e4b9976 VS |
142 | bool oneShot; |
143 | volatile bool timerDeleted; | |
9750481f | 144 | wxTimerTick_t now = GetMillisecondsTime(); |
1acd70f9 | 145 | wxTimerDesc *desc; |
88f2a771 | 146 | |
1acd70f9 VS |
147 | while ( m_timers && m_timers->shotTime <= now ) |
148 | { | |
149 | desc = m_timers; | |
0e4b9976 | 150 | oneShot = desc->timer->IsOneShot(); |
1acd70f9 | 151 | RemoveTimer(desc); |
88f2a771 | 152 | |
ca65c044 | 153 | timerDeleted = false; |
0e4b9976 | 154 | desc->deleteFlag = &timerDeleted; |
88f2a771 | 155 | desc->timer->Notify(); |
ca65c044 | 156 | |
0e4b9976 | 157 | if ( !timerDeleted ) |
1acd70f9 | 158 | { |
e38b6ea8 | 159 | wxLogTrace( wxT("timer"), |
5ed08e5b | 160 | wxT("notified timer %p sheduled for %") |
ca65c044 | 161 | wxTimerTickFmtSpec, |
5ed08e5b VZ |
162 | desc->timer, |
163 | wxTimerTickPrintfArg(desc->shotTime) ); | |
0e4b9976 VS |
164 | |
165 | desc->deleteFlag = NULL; | |
166 | if ( !oneShot ) | |
167 | QueueTimer(desc, now + desc->timer->GetInterval()); | |
1acd70f9 VS |
168 | } |
169 | } | |
170 | } | |
171 | } | |
172 | ||
173 | ||
32b8ec41 VZ |
174 | // ---------------------------------------------------------------------------- |
175 | // wxTimer | |
176 | // ---------------------------------------------------------------------------- | |
177 | ||
313feadc | 178 | IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler) |
32b8ec41 | 179 | |
0e4b9976 | 180 | wxTimerScheduler *gs_scheduler = NULL; |
1acd70f9 VS |
181 | |
182 | void wxTimer::Init() | |
183 | { | |
0e4b9976 VS |
184 | if ( !gs_scheduler ) |
185 | gs_scheduler = new wxTimerScheduler; | |
1acd70f9 VS |
186 | m_desc = new wxTimerDesc(this); |
187 | } | |
188 | ||
189 | wxTimer::~wxTimer() | |
190 | { | |
2b5f62a0 | 191 | wxLogTrace( wxT("timer"), wxT("destroying timer %p..."), this); |
1acd70f9 VS |
192 | if ( IsRunning() ) |
193 | Stop(); | |
194 | ||
0e4b9976 | 195 | // NB: this is a hack: wxTimerScheduler must have some way of knowing |
ca65c044 | 196 | // that wxTimer object was deleted under its hands -- this may |
0e4b9976 VS |
197 | // happen if somebody is really nasty and deletes the timer |
198 | // from wxTimer::Notify() | |
199 | if ( m_desc->deleteFlag != NULL ) | |
ca65c044 | 200 | *m_desc->deleteFlag = true; |
0e4b9976 | 201 | |
1acd70f9 | 202 | delete m_desc; |
2b5f62a0 | 203 | wxLogTrace( wxT("timer"), wxT(" ...done destroying timer %p..."), this); |
1acd70f9 VS |
204 | } |
205 | ||
206 | bool wxTimer::IsRunning() const | |
207 | { | |
208 | return m_desc->running; | |
209 | } | |
210 | ||
211 | bool wxTimer::Start(int millisecs, bool oneShot) | |
212 | { | |
ca65c044 | 213 | wxLogTrace( wxT("timer"), wxT("started timer %p: %i ms, oneshot=%i"), |
88f2a771 VS |
214 | this, millisecs, oneShot); |
215 | ||
1acd70f9 | 216 | if ( !wxTimerBase::Start(millisecs, oneShot) ) |
ca65c044 WS |
217 | return false; |
218 | ||
0e4b9976 | 219 | gs_scheduler->QueueTimer(m_desc); |
ca65c044 | 220 | return true; |
1acd70f9 VS |
221 | } |
222 | ||
223 | void wxTimer::Stop() | |
224 | { | |
225 | if ( !m_desc->running ) return; | |
ca65c044 | 226 | |
0e4b9976 | 227 | gs_scheduler->RemoveTimer(m_desc); |
1acd70f9 VS |
228 | } |
229 | ||
230 | /*static*/ void wxTimer::NotifyTimers() | |
231 | { | |
0e4b9976 VS |
232 | if ( gs_scheduler ) |
233 | gs_scheduler->NotifyTimers(); | |
1acd70f9 VS |
234 | } |
235 | ||
0e4b9976 VS |
236 | |
237 | ||
238 | // A module to deallocate memory properly: | |
239 | class wxTimerModule: public wxModule | |
240 | { | |
241 | DECLARE_DYNAMIC_CLASS(wxTimerModule) | |
242 | public: | |
243 | wxTimerModule() {} | |
ca65c044 | 244 | bool OnInit() { return true; } |
0e4b9976 VS |
245 | void OnExit() { delete gs_scheduler; gs_scheduler = NULL; } |
246 | }; | |
247 | ||
248 | IMPLEMENT_DYNAMIC_CLASS(wxTimerModule, wxModule) | |
249 | ||
250 | ||
1acd70f9 | 251 | #endif //wxUSE_TIMER |