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