]> git.saurik.com Git - wxWidgets.git/blame - src/generic/timer.cpp
reSWIGged
[wxWidgets.git] / src / generic / 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$
6d89ddef 6// Copyright: (c) Vaclav Slavik
32b8ec41
VZ
7// Licence: wxWindows licence
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
VS
21// ----------------------------------------------------------------------------
22// NB: when using generic wxTimer implementation in your port, you *must* call
23// wxTimer::NotifyTimers() often enough. The ideal place for this
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;
a0cb0ba5 47#else
9750481f
VZ
48 #define GetMillisecondsTime wxGetLocalTimeMillis
49
50 typedef wxLongLong wxTimerTick_t;
a0cb0ba5 51#endif
1acd70f9
VS
52
53// ----------------------------------------------------------------------------
54// helper structures and wxTimerScheduler
55// ----------------------------------------------------------------------------
56
57class wxTimerDesc
58{
59public:
0e4b9976
VS
60 wxTimerDesc(wxTimer *t) :
61 timer(t), running(FALSE), next(NULL), prev(NULL),
62 shotTime(0), deleteFlag(NULL) {}
1acd70f9
VS
63
64 wxTimer *timer;
65 bool running;
66 wxTimerDesc *next, *prev;
e38b6ea8 67 wxTimerTick_t shotTime;
0e4b9976 68 volatile bool *deleteFlag; // see comment in ~wxTimer
1acd70f9
VS
69};
70
71class wxTimerScheduler
72{
73public:
74 wxTimerScheduler() : m_timers(NULL) {}
75
9750481f 76 void QueueTimer(wxTimerDesc *desc, wxTimerTick_t when = 0);
1acd70f9
VS
77 void RemoveTimer(wxTimerDesc *desc);
78 void NotifyTimers();
79
80private:
81 wxTimerDesc *m_timers;
82};
83
9750481f 84void wxTimerScheduler::QueueTimer(wxTimerDesc *desc, wxTimerTick_t when)
1acd70f9 85{
0e4b9976
VS
86 if ( desc->running )
87 return; // already scheduled
88
1acd70f9 89 if ( when == 0 )
a0cb0ba5 90 when = GetMillisecondsTime() + desc->timer->GetInterval();
1acd70f9
VS
91 desc->shotTime = when;
92 desc->running = TRUE;
93
53fef662 94#ifndef __WXMGL__
ee9768c9
VZ
95 wxLogTrace( wxT("timer"),
96 wxT("queued timer %p at tick %") wxLongLongFmtSpec _T("d"),
97 desc->timer, when.GetValue());
53fef662 98#endif
88f2a771 99
1acd70f9
VS
100 if ( m_timers )
101 {
102 wxTimerDesc *d = m_timers;
103 while ( d->next && d->next->shotTime < when ) d = d->next;
104 desc->next = d->next;
105 desc->prev = d;
106 if ( d->next )
107 d->next->prev = desc;
108 d->next = desc;
109 }
110 else
111 {
112 m_timers = desc;
113 desc->prev = desc->next = NULL;
114 }
115}
116
117void wxTimerScheduler::RemoveTimer(wxTimerDesc *desc)
118{
119 desc->running = FALSE;
120 if ( desc == m_timers )
121 m_timers = desc->next;
122 if ( desc->prev )
123 desc->prev->next = desc->next;
124 if ( desc->next )
125 desc->next->prev = desc->prev;
126 desc->prev = desc->next = NULL;
127}
128
129void wxTimerScheduler::NotifyTimers()
130{
131 if ( m_timers )
132 {
0e4b9976
VS
133 bool oneShot;
134 volatile bool timerDeleted;
9750481f 135 wxTimerTick_t now = GetMillisecondsTime();
1acd70f9 136 wxTimerDesc *desc;
88f2a771 137
1acd70f9
VS
138 while ( m_timers && m_timers->shotTime <= now )
139 {
140 desc = m_timers;
0e4b9976 141 oneShot = desc->timer->IsOneShot();
1acd70f9 142 RemoveTimer(desc);
88f2a771 143
0e4b9976
VS
144 timerDeleted = FALSE;
145 desc->deleteFlag = &timerDeleted;
88f2a771 146 desc->timer->Notify();
0e4b9976
VS
147
148 if ( !timerDeleted )
1acd70f9 149 {
53fef662 150#ifndef __WXMGL__
e38b6ea8 151 wxLogTrace( wxT("timer"),
ee9768c9
VZ
152 wxT("notified timer %p sheduled for %") wxLongLongFmtSpec _T("d"),
153 desc->timer, desc->shotTime.GetValue() );
53fef662 154#endif
0e4b9976
VS
155
156 desc->deleteFlag = NULL;
157 if ( !oneShot )
158 QueueTimer(desc, now + desc->timer->GetInterval());
1acd70f9
VS
159 }
160 }
161 }
162}
163
164
32b8ec41
VZ
165// ----------------------------------------------------------------------------
166// wxTimer
167// ----------------------------------------------------------------------------
168
169IMPLEMENT_ABSTRACT_CLASS(wxTimer,wxObject)
170
0e4b9976 171wxTimerScheduler *gs_scheduler = NULL;
1acd70f9
VS
172
173void wxTimer::Init()
174{
0e4b9976
VS
175 if ( !gs_scheduler )
176 gs_scheduler = new wxTimerScheduler;
1acd70f9
VS
177 m_desc = new wxTimerDesc(this);
178}
179
180wxTimer::~wxTimer()
181{
2b5f62a0 182 wxLogTrace( wxT("timer"), wxT("destroying timer %p..."), this);
1acd70f9
VS
183 if ( IsRunning() )
184 Stop();
185
0e4b9976
VS
186 // NB: this is a hack: wxTimerScheduler must have some way of knowing
187 // that wxTimer object was deleted under its hands -- this may
188 // happen if somebody is really nasty and deletes the timer
189 // from wxTimer::Notify()
190 if ( m_desc->deleteFlag != NULL )
191 *m_desc->deleteFlag = TRUE;
192
1acd70f9 193 delete m_desc;
2b5f62a0 194 wxLogTrace( wxT("timer"), wxT(" ...done destroying timer %p..."), this);
1acd70f9
VS
195}
196
197bool wxTimer::IsRunning() const
198{
199 return m_desc->running;
200}
201
202bool wxTimer::Start(int millisecs, bool oneShot)
203{
2b5f62a0 204 wxLogTrace( wxT("timer"), wxT("started timer %p: %i ms, oneshot=%i"),
88f2a771
VS
205 this, millisecs, oneShot);
206
1acd70f9
VS
207 if ( !wxTimerBase::Start(millisecs, oneShot) )
208 return FALSE;
209
0e4b9976 210 gs_scheduler->QueueTimer(m_desc);
1acd70f9
VS
211 return TRUE;
212}
213
214void wxTimer::Stop()
215{
216 if ( !m_desc->running ) return;
217
0e4b9976 218 gs_scheduler->RemoveTimer(m_desc);
1acd70f9
VS
219}
220
221/*static*/ void wxTimer::NotifyTimers()
222{
0e4b9976
VS
223 if ( gs_scheduler )
224 gs_scheduler->NotifyTimers();
1acd70f9
VS
225}
226
0e4b9976
VS
227
228
229// A module to deallocate memory properly:
230class wxTimerModule: public wxModule
231{
232DECLARE_DYNAMIC_CLASS(wxTimerModule)
233public:
234 wxTimerModule() {}
235 bool OnInit() { return TRUE; }
236 void OnExit() { delete gs_scheduler; gs_scheduler = NULL; }
237};
238
239IMPLEMENT_DYNAMIC_CLASS(wxTimerModule, wxModule)
240
241
1acd70f9 242#endif //wxUSE_TIMER