]> git.saurik.com Git - wxWidgets.git/blob - src/x11/timer.cpp
Fixed typo in my last commit (it did break socket detection code :-( ).
[wxWidgets.git] / src / x11 / timer.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: x11/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 // For compilers that support precompilation, includes "wx.h".
16 #include "wx/wxprec.h"
17
18 #ifdef __BORLANDC__
19 #pragma hdrstop
20 #endif
21
22 #include "wx/timer.h"
23
24 #if wxUSE_TIMER
25
26 #include "wx/log.h"
27 #include "wx/module.h"
28 #include "wx/x11/private.h"
29
30 // ----------------------------------------------------------------------------
31 // helper structures and wxTimerScheduler
32 // ----------------------------------------------------------------------------
33
34 class wxTimerDesc
35 {
36 public:
37 wxTimerDesc(wxTimer *t) :
38 timer(t), running(FALSE), next(NULL), prev(NULL),
39 shotTime(0), deleteFlag(NULL) {}
40
41 wxTimer *timer;
42 bool running;
43 wxTimerDesc *next, *prev;
44 unsigned long shotTime;
45 volatile bool *deleteFlag; // see comment in ~wxTimer
46 };
47
48 class wxTimerScheduler
49 {
50 public:
51 wxTimerScheduler() : m_timers(NULL) {}
52
53 void QueueTimer(wxTimerDesc *desc, unsigned long when = 0);
54 void RemoveTimer(wxTimerDesc *desc);
55 void NotifyTimers();
56
57 private:
58 wxTimerDesc *m_timers;
59 };
60
61 void wxTimerScheduler::QueueTimer(wxTimerDesc *desc, unsigned long when)
62 {
63 if ( desc->running )
64 return; // already scheduled
65
66 if ( when == 0 )
67 {
68 unsigned long local = wxGetLocalTimeMillis().ToLong();
69 when = local + desc->timer->GetInterval();
70 }
71 desc->shotTime = when;
72 desc->running = TRUE;
73
74 wxLogTrace("mgl_timer", "queued timer %p at tick %i",
75 desc->timer, when);
76
77 if ( m_timers )
78 {
79 wxTimerDesc *d = m_timers;
80 while ( d->next && d->next->shotTime < when ) d = d->next;
81 desc->next = d->next;
82 desc->prev = d;
83 if ( d->next )
84 d->next->prev = desc;
85 d->next = desc;
86 }
87 else
88 {
89 m_timers = desc;
90 desc->prev = desc->next = NULL;
91 }
92 }
93
94 void wxTimerScheduler::RemoveTimer(wxTimerDesc *desc)
95 {
96 desc->running = FALSE;
97 if ( desc == m_timers )
98 m_timers = desc->next;
99 if ( desc->prev )
100 desc->prev->next = desc->next;
101 if ( desc->next )
102 desc->next->prev = desc->prev;
103 desc->prev = desc->next = NULL;
104 }
105
106 void wxTimerScheduler::NotifyTimers()
107 {
108 if ( m_timers )
109 {
110 bool oneShot;
111 volatile bool timerDeleted;
112 unsigned long now = wxGetLocalTimeMillis().ToLong();
113 wxTimerDesc *desc;
114
115 while ( m_timers && m_timers->shotTime <= now )
116 {
117 desc = m_timers;
118 oneShot = desc->timer->IsOneShot();
119 RemoveTimer(desc);
120
121 timerDeleted = FALSE;
122 desc->deleteFlag = &timerDeleted;
123 desc->timer->Notify();
124
125 if ( !timerDeleted )
126 {
127 wxLogTrace("mgl_timer", "notified timer %p sheduled for %i",
128 desc->timer, desc->shotTime);
129
130 desc->deleteFlag = NULL;
131 if ( !oneShot )
132 QueueTimer(desc, now + desc->timer->GetInterval());
133 }
134 }
135 }
136 }
137
138
139
140 // ----------------------------------------------------------------------------
141 // wxTimer
142 // ----------------------------------------------------------------------------
143
144 IMPLEMENT_ABSTRACT_CLASS(wxTimer,wxObject)
145
146 wxTimerScheduler *gs_scheduler = NULL;
147
148 void wxTimer::Init()
149 {
150 if ( !gs_scheduler )
151 gs_scheduler = new wxTimerScheduler;
152 m_desc = new wxTimerDesc(this);
153 }
154
155 wxTimer::~wxTimer()
156 {
157 wxLogTrace("mgl_timer", "destroying timer %p...", this);
158 if ( IsRunning() )
159 Stop();
160
161 // NB: this is a hack: wxTimerScheduler must have some way of knowing
162 // that wxTimer object was deleted under its hands -- this may
163 // happen if somebody is really nasty and deletes the timer
164 // from wxTimer::Notify()
165 if ( m_desc->deleteFlag != NULL )
166 *m_desc->deleteFlag = TRUE;
167
168 delete m_desc;
169 wxLogTrace("mgl_timer", " ...done destroying timer %p...", this);
170 }
171
172 bool wxTimer::IsRunning() const
173 {
174 return m_desc->running;
175 }
176
177 bool wxTimer::Start(int millisecs, bool oneShot)
178 {
179 wxLogTrace("mgl_timer", "started timer %p: %i ms, oneshot=%i",
180 this, millisecs, oneShot);
181
182 if ( !wxTimerBase::Start(millisecs, oneShot) )
183 return FALSE;
184
185 gs_scheduler->QueueTimer(m_desc);
186 return TRUE;
187 }
188
189 void wxTimer::Stop()
190 {
191 if ( !m_desc->running ) return;
192
193 gs_scheduler->RemoveTimer(m_desc);
194 }
195
196 /*static*/ void wxTimer::NotifyTimers()
197 {
198 if ( gs_scheduler )
199 gs_scheduler->NotifyTimers();
200 }
201
202
203
204 // A module to deallocate memory properly:
205 class wxTimerModule: public wxModule
206 {
207 DECLARE_DYNAMIC_CLASS(wxTimerModule)
208 public:
209 wxTimerModule() {}
210 bool OnInit() { return TRUE; }
211 void OnExit() { delete gs_scheduler; gs_scheduler = NULL; }
212 };
213
214 IMPLEMENT_DYNAMIC_CLASS(wxTimerModule, wxModule)
215
216
217 #endif //wxUSE_TIMER