1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unix/private/timer.h
3 // Purpose: wxTimer for wxBase (unix)
4 // Author: Lukasz Michalski
7 // Copyright: (c) Lukasz Michalski
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_UNIX_PRIVATE_TIMER_H_
12 #define _WX_UNIX_PRIVATE_TIMER_H_
16 #include "wx/private/timer.h"
18 // the type used for milliseconds is large enough for microseconds too but
19 // introduce a synonym for it to avoid confusion
20 typedef wxMilliClock_t wxUsecClock_t
;
22 // ----------------------------------------------------------------------------
23 // wxTimer implementation class for Unix platforms
24 // ----------------------------------------------------------------------------
26 class wxUnixTimerImpl
: public wxTimerImpl
29 wxUnixTimerImpl(wxTimer
*timer
);
30 virtual ~wxUnixTimerImpl();
32 virtual bool IsRunning() const;
33 virtual bool Start(int milliseconds
= -1, bool oneShot
= false);
36 // for wxTimerScheduler only: resets the internal flag indicating that the
40 wxASSERT_MSG( m_isRunning
, _T("stopping non-running timer?") );
49 // ----------------------------------------------------------------------------
50 // wxTimerSchedule: information about a single timer, used by wxTimerScheduler
51 // ----------------------------------------------------------------------------
53 struct wxTimerSchedule
55 wxTimerSchedule(wxUnixTimerImpl
*timer
, wxUsecClock_t expiration
)
57 m_expiration(expiration
)
61 // the timer itself (we don't own this pointer)
62 wxUnixTimerImpl
*m_timer
;
64 // the time of its next expiration, in usec
65 wxUsecClock_t m_expiration
;
68 // the linked list of all active timers, we keep it sorted by expiration time
69 WX_DECLARE_LIST(wxTimerSchedule
, wxTimerList
);
71 // ----------------------------------------------------------------------------
72 // wxTimerScheduler: class responsible for updating all timers
73 // ----------------------------------------------------------------------------
75 class wxTimerScheduler
78 // get the unique timer scheduler instance
79 static wxTimerScheduler
& Get()
82 ms_instance
= new wxTimerScheduler
;
87 // must be called on shutdown to delete the global timer scheduler
88 static void Shutdown()
97 // adds timer which should expire at the given absolute time to the list
98 void AddTimer(wxUnixTimerImpl
*timer
, wxUsecClock_t expiration
);
100 // remove timer from the list, called automatically from timer dtor
101 void RemoveTimer(wxUnixTimerImpl
*timer
);
104 // the functions below are used by the event loop implementation to monitor
105 // and notify timers:
107 // if this function returns true, the time remaining until the next time
108 // expiration is returned in the provided parameter (always positive or 0)
110 // it returns false if there are no timers
111 bool GetNext(wxUsecClock_t
*remaining
) const;
113 // trigger the timer event for all timers which have expired
114 void NotifyExpired();
117 // ctor and dtor are private, this is a singleton class only created by
118 // Get() and destroyed by Shutdown()
119 wxTimerScheduler() { }
122 // add the given timer schedule to the list in the right place
124 // we take ownership of the pointer "s" which must be heap-allocated
125 void DoAddTimer(wxTimerSchedule
*s
);
128 // the list of all currently active timers sorted by expiration
129 wxTimerList m_timers
;
131 static wxTimerScheduler
*ms_instance
;
134 // this helper function currently only exists for Unix platforms but could be
135 // moved to wx/stopwatch.h if it turns out to be useful elsewhere
137 // returns the number of microseconds since the Epoch
138 extern wxUsecClock_t
wxGetLocalTimeUsec();
140 #endif // wxUSE_TIMER
142 #endif // _WX_UNIX_PRIVATE_TIMER_H_