/////////////////////////////////////////////////////////////////////////////
-// Name: timer.cpp
+// Name: x11/timer.cpp
// Purpose: wxTimer implementation
-// Author: Julian Smart
-// Modified by:
-// Created: 17/09/98
-// RCS-ID: $Id$
-// Copyright: (c) Julian Smart
-// Licence: wxWindows licence
+// Author: Vaclav Slavik
+// Id: $Id$
+// Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com)
+// Licence: wxWindows licence
/////////////////////////////////////////////////////////////////////////////
+
#ifdef __GNUG__
#pragma implementation "timer.h"
#endif
-#include "wx/timer.h"
-#include "wx/app.h"
-#include "wx/list.h"
+// For compilers that support precompilation, includes "wx.h".
+#include "wx/wxprec.h"
-#ifdef __VMS__
-#pragma message disable nosimpint
-#endif
-#include <Xm/Xm.h>
-#ifdef __VMS__
-#pragma message enable nosimpint
+#ifdef __BORLANDC__
+ #pragma hdrstop
#endif
-#include "wx/motif/private.h"
+#include "wx/timer.h"
+
+#if wxUSE_TIMER
-IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxObject)
+#include "wx/log.h"
+#include "wx/module.h"
+#include "wx/x11/private.h"
-static wxList wxTimerList(wxKEY_INTEGER);
+// ----------------------------------------------------------------------------
+// helper structures and wxTimerScheduler
+// ----------------------------------------------------------------------------
-void wxTimerCallback (wxTimer * timer)
+class wxTimerDesc
+{
+public:
+ wxTimerDesc(wxTimer *t) :
+ timer(t), running(FALSE), next(NULL), prev(NULL),
+ shotTime(0), deleteFlag(NULL) {}
+
+ wxTimer *timer;
+ bool running;
+ wxTimerDesc *next, *prev;
+ unsigned long shotTime;
+ volatile bool *deleteFlag; // see comment in ~wxTimer
+};
+
+class wxTimerScheduler
{
- // Check to see if it's still on
- if (!wxTimerList.Find((long)timer))
- return;
-
- if (timer->m_id == 0)
- return; // Avoid to process spurious timer events
-
- if (!timer->m_oneShot)
- timer->m_id = XtAppAddTimeOut((XtAppContext) wxTheApp->GetAppContext(),
- timer->m_milli,
- (XtTimerCallbackProc) wxTimerCallback,
- (XtPointer) timer);
- else
- timer->m_id = 0;
-
- timer->Notify();
+public:
+ wxTimerScheduler() : m_timers(NULL) {}
+
+ void QueueTimer(wxTimerDesc *desc, unsigned long when = 0);
+ void RemoveTimer(wxTimerDesc *desc);
+ void NotifyTimers();
+
+private:
+ wxTimerDesc *m_timers;
+};
+
+void wxTimerScheduler::QueueTimer(wxTimerDesc *desc, unsigned long when)
+{
+ if ( desc->running )
+ return; // already scheduled
+
+ if ( when == 0 )
+ {
+ unsigned long local = wxGetLocalTimeMillis().ToLong();
+ when = local + desc->timer->GetInterval();
+ }
+ desc->shotTime = when;
+ desc->running = TRUE;
+
+ wxLogTrace("mgl_timer", "queued timer %p at tick %i",
+ desc->timer, when);
+
+ if ( m_timers )
+ {
+ wxTimerDesc *d = m_timers;
+ while ( d->next && d->next->shotTime < when ) d = d->next;
+ desc->next = d->next;
+ desc->prev = d;
+ if ( d->next )
+ d->next->prev = desc;
+ d->next = desc;
+ }
+ else
+ {
+ m_timers = desc;
+ desc->prev = desc->next = NULL;
+ }
+}
+
+void wxTimerScheduler::RemoveTimer(wxTimerDesc *desc)
+{
+ desc->running = FALSE;
+ if ( desc == m_timers )
+ m_timers = desc->next;
+ if ( desc->prev )
+ desc->prev->next = desc->next;
+ if ( desc->next )
+ desc->next->prev = desc->prev;
+ desc->prev = desc->next = NULL;
}
+void wxTimerScheduler::NotifyTimers()
+{
+ if ( m_timers )
+ {
+ bool oneShot;
+ volatile bool timerDeleted;
+ unsigned long now = wxGetLocalTimeMillis().ToLong();
+ wxTimerDesc *desc;
+
+ while ( m_timers && m_timers->shotTime <= now )
+ {
+ desc = m_timers;
+ oneShot = desc->timer->IsOneShot();
+ RemoveTimer(desc);
+
+ timerDeleted = FALSE;
+ desc->deleteFlag = &timerDeleted;
+ desc->timer->Notify();
+
+ if ( !timerDeleted )
+ {
+ wxLogTrace("mgl_timer", "notified timer %p sheduled for %i",
+ desc->timer, desc->shotTime);
+
+ desc->deleteFlag = NULL;
+ if ( !oneShot )
+ QueueTimer(desc, now + desc->timer->GetInterval());
+ }
+ }
+ }
+}
+
+
+
+// ----------------------------------------------------------------------------
+// wxTimer
+// ----------------------------------------------------------------------------
+
+IMPLEMENT_ABSTRACT_CLASS(wxTimer,wxObject)
+
+wxTimerScheduler *gs_scheduler = NULL;
+
void wxTimer::Init()
{
- m_id = 0;
- m_milli = 1000;
+ if ( !gs_scheduler )
+ gs_scheduler = new wxTimerScheduler;
+ m_desc = new wxTimerDesc(this);
}
wxTimer::~wxTimer()
{
- wxTimer::Stop();
- wxTimerList.DeleteObject(this);
+ wxLogTrace("mgl_timer", "destroying timer %p...", this);
+ if ( IsRunning() )
+ Stop();
+
+ // NB: this is a hack: wxTimerScheduler must have some way of knowing
+ // that wxTimer object was deleted under its hands -- this may
+ // happen if somebody is really nasty and deletes the timer
+ // from wxTimer::Notify()
+ if ( m_desc->deleteFlag != NULL )
+ *m_desc->deleteFlag = TRUE;
+
+ delete m_desc;
+ wxLogTrace("mgl_timer", " ...done destroying timer %p...", this);
}
-bool wxTimer::Start(int milliseconds, bool mode)
+bool wxTimer::IsRunning() const
{
- Stop();
-
- (void)wxTimerBase::Start(milliseconds, mode);
+ return m_desc->running;
+}
- if (!wxTimerList.Find((long)this))
- wxTimerList.Append((long)this, this);
+bool wxTimer::Start(int millisecs, bool oneShot)
+{
+ wxLogTrace("mgl_timer", "started timer %p: %i ms, oneshot=%i",
+ this, millisecs, oneShot);
- m_id = XtAppAddTimeOut((XtAppContext) wxTheApp->GetAppContext(),
- m_milli,
- (XtTimerCallbackProc) wxTimerCallback,
- (XtPointer) this);
+ if ( !wxTimerBase::Start(millisecs, oneShot) )
+ return FALSE;
+
+ gs_scheduler->QueueTimer(m_desc);
return TRUE;
}
void wxTimer::Stop()
{
- if (m_id > 0)
- {
- XtRemoveTimeOut (m_id);
- m_id = 0;
- }
- m_milli = 0 ;
+ if ( !m_desc->running ) return;
+
+ gs_scheduler->RemoveTimer(m_desc);
+}
+
+/*static*/ void wxTimer::NotifyTimers()
+{
+ if ( gs_scheduler )
+ gs_scheduler->NotifyTimers();
}
+
+// A module to deallocate memory properly:
+class wxTimerModule: public wxModule
+{
+DECLARE_DYNAMIC_CLASS(wxTimerModule)
+public:
+ wxTimerModule() {}
+ bool OnInit() { return TRUE; }
+ void OnExit() { delete gs_scheduler; gs_scheduler = NULL; }
+};
+
+IMPLEMENT_DYNAMIC_CLASS(wxTimerModule, wxModule)
+
+
+#endif //wxUSE_TIMER