]> git.saurik.com Git - wxWidgets.git/blame - src/x11/timer.cpp
Fixed toolbar crash for MinGW/Cygwin
[wxWidgets.git] / src / x11 / timer.cpp
CommitLineData
83df96d6 1/////////////////////////////////////////////////////////////////////////////
b513212d 2// Name: x11/timer.cpp
83df96d6 3// Purpose: wxTimer implementation
b513212d
JS
4// Author: Vaclav Slavik
5// Id: $Id$
6// Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
7// Licence: wxWindows licence
83df96d6
JS
8/////////////////////////////////////////////////////////////////////////////
9
b513212d 10
83df96d6
JS
11#ifdef __GNUG__
12#pragma implementation "timer.h"
13#endif
14
b513212d
JS
15// For compilers that support precompilation, includes "wx.h".
16#include "wx/wxprec.h"
83df96d6 17
b513212d
JS
18#ifdef __BORLANDC__
19 #pragma hdrstop
83df96d6 20#endif
bc797f4c 21
b513212d
JS
22#include "wx/timer.h"
23
24#if wxUSE_TIMER
83df96d6 25
b513212d
JS
26#include "wx/log.h"
27#include "wx/module.h"
bc797f4c 28#include "wx/x11/private.h"
83df96d6 29
b513212d
JS
30// ----------------------------------------------------------------------------
31// helper structures and wxTimerScheduler
32// ----------------------------------------------------------------------------
83df96d6 33
b513212d
JS
34class wxTimerDesc
35{
36public:
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
48class wxTimerScheduler
49{
50public:
51 wxTimerScheduler() : m_timers(NULL) {}
52
53 void QueueTimer(wxTimerDesc *desc, unsigned long when = 0);
54 void RemoveTimer(wxTimerDesc *desc);
55 void NotifyTimers();
56
57private:
58 wxTimerDesc *m_timers;
59};
60
61void wxTimerScheduler::QueueTimer(wxTimerDesc *desc, unsigned long when)
62{
63 if ( desc->running )
64 return; // already scheduled
178572bb 65
b513212d 66 if ( when == 0 )
8ac7c509
RR
67 {
68 unsigned long local = wxGetLocalTimeMillis().ToLong();
69 when = local + desc->timer->GetInterval();
70 }
b513212d
JS
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}
83df96d6 93
b513212d 94void wxTimerScheduler::RemoveTimer(wxTimerDesc *desc)
83df96d6 95{
b513212d
JS
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}
83df96d6 105
b513212d
JS
106void wxTimerScheduler::NotifyTimers()
107{
108 if ( m_timers )
109 {
110 bool oneShot;
111 volatile bool timerDeleted;
8ac7c509 112 unsigned long now = wxGetLocalTimeMillis().ToLong();
b513212d
JS
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 }
83df96d6
JS
136}
137
b513212d
JS
138
139
140// ----------------------------------------------------------------------------
141// wxTimer
142// ----------------------------------------------------------------------------
143
144IMPLEMENT_ABSTRACT_CLASS(wxTimer,wxObject)
145
146wxTimerScheduler *gs_scheduler = NULL;
147
83df96d6
JS
148void wxTimer::Init()
149{
b513212d
JS
150 if ( !gs_scheduler )
151 gs_scheduler = new wxTimerScheduler;
152 m_desc = new wxTimerDesc(this);
83df96d6
JS
153}
154
155wxTimer::~wxTimer()
156{
b513212d
JS
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);
83df96d6
JS
170}
171
b513212d 172bool wxTimer::IsRunning() const
83df96d6 173{
b513212d
JS
174 return m_desc->running;
175}
176
177bool 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);
83df96d6
JS
186 return TRUE;
187}
188
189void wxTimer::Stop()
190{
b513212d
JS
191 if ( !m_desc->running ) return;
192
193 gs_scheduler->RemoveTimer(m_desc);
83df96d6
JS
194}
195
b513212d
JS
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:
205class wxTimerModule: public wxModule
206{
207DECLARE_DYNAMIC_CLASS(wxTimerModule)
208public:
209 wxTimerModule() {}
210 bool OnInit() { return TRUE; }
211 void OnExit() { delete gs_scheduler; gs_scheduler = NULL; }
212};
213
214IMPLEMENT_DYNAMIC_CLASS(wxTimerModule, wxModule)
215
83df96d6 216
b513212d 217#endif //wxUSE_TIMER