From 1acd70f921609754caa7f1a6cd9ade044b0b3c19 Mon Sep 17 00:00:00 2001 From: =?utf8?q?V=C3=A1clav=20Slav=C3=ADk?= Date: Sun, 11 Nov 2001 18:59:30 +0000 Subject: [PATCH] wxTimer for wxMGL git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@12377 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/mgl/timer.h | 28 +++++--- src/mgl/app.cpp | 1 + src/mgl/evtloop.cpp | 20 +++++- src/mgl/timer.cpp | 148 +++++++++++++++++++++++++++++++++++++++-- 4 files changed, 181 insertions(+), 16 deletions(-) diff --git a/include/wx/mgl/timer.h b/include/wx/mgl/timer.h index b9b2841651..a97030f2be 100644 --- a/include/wx/mgl/timer.h +++ b/include/wx/mgl/timer.h @@ -1,6 +1,6 @@ ///////////////////////////////////////////////////////////////////////////// // Name: timer.h -// Purpose: +// Purpose: wxTimer class // Author: Vaclav Slavik // Id: $Id$ // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com) @@ -19,27 +19,35 @@ // wxTimer //----------------------------------------------------------------------------- -//FIXME_MGL +class wxTimerDesc; +class wxTimerScheduler; + class WXDLLEXPORT wxTimer : public wxTimerBase { public: wxTimer() { Init(); } wxTimer(wxEvtHandler *owner, int id = -1) : wxTimerBase(owner, id) { Init(); } - ~wxTimer() {} + ~wxTimer(); - virtual bool Start( int millisecs = -1, bool oneShot = FALSE ) { return TRUE; } - virtual void Stop() {} + virtual bool Start(int millisecs = -1, bool oneShot = FALSE); + virtual void Stop(); - virtual bool IsRunning() const { return m_tag != -1; } + virtual bool IsRunning() const; -protected: - void Init() {} + // implementation + static void NotifyTimers(); - int m_tag; +protected: + void Init(); private: + wxTimerDesc *m_desc; + + static wxTimerScheduler *ms_scheduler; + static size_t ms_timersCnt; + DECLARE_ABSTRACT_CLASS(wxTimer) }; -#endif // __GTKTIMERH__ +#endif // __WX_TIMER_H__ diff --git a/src/mgl/app.cpp b/src/mgl/app.cpp index d4903a2af2..77a30f0220 100644 --- a/src/mgl/app.cpp +++ b/src/mgl/app.cpp @@ -27,6 +27,7 @@ #include "wx/dialog.h" #include "wx/log.h" #include "wx/intl.h" + #include "wx/resource.h" #endif #include "wx/app.h" diff --git a/src/mgl/evtloop.cpp b/src/mgl/evtloop.cpp index d19bd8639c..e4b4f26373 100644 --- a/src/mgl/evtloop.cpp +++ b/src/mgl/evtloop.cpp @@ -29,8 +29,9 @@ #endif //WX_PRECOMP #include "wx/evtloop.h" - +#include "wx/timer.h" #include "wx/mgl/private.h" +#include "pmapi.h" // ---------------------------------------------------------------------------- // wxEventLoopImpl @@ -77,7 +78,22 @@ void wxEventLoopImpl::Dispatch() MGL_wmUpdateDC(g_winMng); - EVT_halt(&evt, EVT_EVERYEVT); + // VS: The code bellow is equal to MGL's EVT_halt implementation, with + // two things added: sleeping (busy waiting is stupid, lets make CPU's + // life a bit easier) and timers updating + + // EVT_halt(&evt, EVT_EVERYEVT); + do + { + EVT_pollJoystick(); + EVT_getNext(&evt, EVT_EVERYEVT); +#if wxUSE_TIMER + wxTimer::NotifyTimers(); +#endif + PM_sleep(10); + } while (!(evt.what & EVT_EVERYEVT)); + // end of EVT_halt + MGL_wmProcessEvent(g_winMng, &evt); } diff --git a/src/mgl/timer.cpp b/src/mgl/timer.cpp index dbef60e5dd..f56c8459c8 100644 --- a/src/mgl/timer.cpp +++ b/src/mgl/timer.cpp @@ -1,9 +1,9 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: gtk/timer.cpp +// Name: mgl/timer.cpp // Purpose: wxTimer implementation -// Author: Robert Roebling +// Author: Vaclav Slavik // Id: $Id$ -// Copyright: (c) 1998 Robert Roebling +// Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com) // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -14,6 +14,97 @@ #include "wx/timer.h" +#if wxUSE_TIMER + +#include "wx/mgl/private.h" + +extern "C" ulong _EVT_getTicks(); + +// ---------------------------------------------------------------------------- +// helper structures and wxTimerScheduler +// ---------------------------------------------------------------------------- + +class wxTimerDesc +{ +public: + wxTimerDesc(wxTimer *t) : timer(t), running(FALSE), next(NULL), prev(NULL) {} + + wxTimer *timer; + bool running; + wxTimerDesc *next, *prev; + unsigned long shotTime; +}; + +class wxTimerScheduler +{ +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 ( when == 0 ) + when = _EVT_getTicks() + desc->timer->GetInterval(); + desc->shotTime = when; + desc->running = TRUE; + + 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 ) + { + unsigned long now = _EVT_getTicks(); + wxTimerDesc *desc; + + while ( m_timers && m_timers->shotTime <= now ) + { + desc = m_timers; + desc->timer->Notify(); + RemoveTimer(desc); + if ( !desc->timer->IsOneShot() ) + { + QueueTimer(desc, now + desc->timer->GetInterval()); + } + } + } +} + + // ---------------------------------------------------------------------------- // wxTimer @@ -21,4 +112,53 @@ IMPLEMENT_ABSTRACT_CLASS(wxTimer,wxObject) -// FIXME_MGL +wxTimerScheduler *wxTimer::ms_scheduler = NULL; +size_t wxTimer::ms_timersCnt = 0; + +void wxTimer::Init() +{ + if ( ms_timersCnt++ == 0 ) + ms_scheduler = new wxTimerScheduler; + m_desc = new wxTimerDesc(this); +} + +wxTimer::~wxTimer() +{ + if ( IsRunning() ) + Stop(); + + if ( --ms_timersCnt == 0 ) + { + delete ms_scheduler; + ms_scheduler = NULL; + } + delete m_desc; +} + +bool wxTimer::IsRunning() const +{ + return m_desc->running; +} + +bool wxTimer::Start(int millisecs, bool oneShot) +{ + if ( !wxTimerBase::Start(millisecs, oneShot) ) + return FALSE; + + ms_scheduler->QueueTimer(m_desc); + return TRUE; +} + +void wxTimer::Stop() +{ + if ( !m_desc->running ) return; + + ms_scheduler->RemoveTimer(m_desc); +} + +/*static*/ void wxTimer::NotifyTimers() +{ + ms_scheduler->NotifyTimers(); +} + +#endif //wxUSE_TIMER -- 2.45.2