]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: mgl/timer.cpp | |
3 | // Purpose: wxTimer implementation | |
4 | // Author: Vaclav Slavik | |
5 | // Id: $Id$ | |
6 | // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com) | |
7 | // Licence: wxWindows licence | |
8 | ///////////////////////////////////////////////////////////////////////////// | |
9 | ||
10 | ||
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "timer.h" | |
13 | #endif | |
14 | ||
15 | #include "wx/timer.h" | |
16 | ||
17 | #if wxUSE_TIMER | |
18 | ||
19 | #include "wx/log.h" | |
20 | #include "wx/mgl/private.h" | |
21 | ||
22 | extern "C" ulong _EVT_getTicks(); | |
23 | ||
24 | // ---------------------------------------------------------------------------- | |
25 | // helper structures and wxTimerScheduler | |
26 | // ---------------------------------------------------------------------------- | |
27 | ||
28 | class wxTimerDesc | |
29 | { | |
30 | public: | |
31 | wxTimerDesc(wxTimer *t) : timer(t), running(FALSE), next(NULL), prev(NULL) {} | |
32 | ||
33 | wxTimer *timer; | |
34 | bool running; | |
35 | wxTimerDesc *next, *prev; | |
36 | unsigned long shotTime; | |
37 | }; | |
38 | ||
39 | class wxTimerScheduler | |
40 | { | |
41 | public: | |
42 | wxTimerScheduler() : m_timers(NULL) {} | |
43 | ||
44 | void QueueTimer(wxTimerDesc *desc, unsigned long when = 0); | |
45 | void RemoveTimer(wxTimerDesc *desc); | |
46 | void NotifyTimers(); | |
47 | ||
48 | private: | |
49 | wxTimerDesc *m_timers; | |
50 | }; | |
51 | ||
52 | void wxTimerScheduler::QueueTimer(wxTimerDesc *desc, unsigned long when) | |
53 | { | |
54 | if ( when == 0 ) | |
55 | when = _EVT_getTicks() + desc->timer->GetInterval(); | |
56 | desc->shotTime = when; | |
57 | desc->running = TRUE; | |
58 | ||
59 | wxLogTrace("mgl_timer", "queued timer %p at tick %i", | |
60 | desc->timer, when); | |
61 | ||
62 | if ( m_timers ) | |
63 | { | |
64 | wxTimerDesc *d = m_timers; | |
65 | while ( d->next && d->next->shotTime < when ) d = d->next; | |
66 | desc->next = d->next; | |
67 | desc->prev = d; | |
68 | if ( d->next ) | |
69 | d->next->prev = desc; | |
70 | d->next = desc; | |
71 | } | |
72 | else | |
73 | { | |
74 | m_timers = desc; | |
75 | desc->prev = desc->next = NULL; | |
76 | } | |
77 | } | |
78 | ||
79 | void wxTimerScheduler::RemoveTimer(wxTimerDesc *desc) | |
80 | { | |
81 | desc->running = FALSE; | |
82 | if ( desc == m_timers ) | |
83 | m_timers = desc->next; | |
84 | if ( desc->prev ) | |
85 | desc->prev->next = desc->next; | |
86 | if ( desc->next ) | |
87 | desc->next->prev = desc->prev; | |
88 | desc->prev = desc->next = NULL; | |
89 | } | |
90 | ||
91 | void wxTimerScheduler::NotifyTimers() | |
92 | { | |
93 | if ( m_timers ) | |
94 | { | |
95 | unsigned long now = _EVT_getTicks(); | |
96 | wxTimerDesc *desc; | |
97 | ||
98 | wxLogTrace("mgl_timer", "notifying timers, time is %i", now); | |
99 | ||
100 | while ( m_timers && m_timers->shotTime <= now ) | |
101 | { | |
102 | desc = m_timers; | |
103 | bool oneShot = desc->timer->IsOneShot(); | |
104 | RemoveTimer(desc); | |
105 | ||
106 | desc->timer->Notify(); | |
107 | wxLogTrace("mgl_timer", "notified timer %p sheduled for %i", | |
108 | desc->timer, desc->shotTime); | |
109 | ||
110 | if ( !oneShot ) | |
111 | { | |
112 | QueueTimer(desc, now + desc->timer->GetInterval()); | |
113 | } | |
114 | } | |
115 | } | |
116 | } | |
117 | ||
118 | ||
119 | ||
120 | // ---------------------------------------------------------------------------- | |
121 | // wxTimer | |
122 | // ---------------------------------------------------------------------------- | |
123 | ||
124 | IMPLEMENT_ABSTRACT_CLASS(wxTimer,wxObject) | |
125 | ||
126 | wxTimerScheduler *wxTimer::ms_scheduler = NULL; | |
127 | size_t wxTimer::ms_timersCnt = 0; | |
128 | ||
129 | void wxTimer::Init() | |
130 | { | |
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); | |
135 | } | |
136 | ||
137 | wxTimer::~wxTimer() | |
138 | { | |
139 | if ( IsRunning() ) | |
140 | Stop(); | |
141 | ||
142 | if ( --ms_timersCnt == 0 ) | |
143 | { | |
144 | delete ms_scheduler; | |
145 | ms_scheduler = NULL; | |
146 | } | |
147 | delete m_desc; | |
148 | wxLogTrace("mgl_timer", "--removed timer (count=%i)", ms_timersCnt); | |
149 | } | |
150 | ||
151 | bool wxTimer::IsRunning() const | |
152 | { | |
153 | return m_desc->running; | |
154 | } | |
155 | ||
156 | bool wxTimer::Start(int millisecs, bool oneShot) | |
157 | { | |
158 | wxLogTrace("mgl_timer", "started timer %p: %i ms, oneshot=%i", | |
159 | this, millisecs, oneShot); | |
160 | ||
161 | if ( !wxTimerBase::Start(millisecs, oneShot) ) | |
162 | return FALSE; | |
163 | ||
164 | ms_scheduler->QueueTimer(m_desc); | |
165 | return TRUE; | |
166 | } | |
167 | ||
168 | void wxTimer::Stop() | |
169 | { | |
170 | if ( !m_desc->running ) return; | |
171 | ||
172 | ms_scheduler->RemoveTimer(m_desc); | |
173 | } | |
174 | ||
175 | /*static*/ void wxTimer::NotifyTimers() | |
176 | { | |
177 | ms_scheduler->NotifyTimers(); | |
178 | } | |
179 | ||
180 | #endif //wxUSE_TIMER |