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_
14 #include "wx/private/timer.h"
16 // the type used for milliseconds is large enough for microseconds too but
17 // introduce a synonym for it to avoid confusion
18 typedef wxMilliClock_t wxUsecClock_t
;
20 // ----------------------------------------------------------------------------
21 // wxTimer implementation class for Unix platforms
22 // ----------------------------------------------------------------------------
24 class wxUnixTimerImpl
: public wxTimerImpl
27 wxUnixTimerImpl(wxTimer
*timer
);
28 virtual ~wxUnixTimerImpl();
30 virtual bool IsRunning() const;
31 virtual bool Start(int milliseconds
= -1, bool oneShot
= false);
37 friend class wxTimerScheduler
;
40 // ----------------------------------------------------------------------------
41 // wxTimerSchedule: information about a single timer, used by wxTimerScheduler
42 // ----------------------------------------------------------------------------
44 struct wxTimerSchedule
46 wxTimerSchedule(wxUnixTimerImpl
*timer
, wxUsecClock_t expiration
)
48 m_expiration(expiration
)
52 // the timer itself (we don't own this pointer)
53 wxUnixTimerImpl
*m_timer
;
55 // the time of its next expiration, in usec
56 wxUsecClock_t m_expiration
;
59 // the linked list of all active timers, we keep it sorted by expiration time
60 WX_DECLARE_LIST(wxTimerSchedule
, wxTimerList
);
62 // ----------------------------------------------------------------------------
63 // wxTimerScheduler: class responsible for updating all timers
64 // ----------------------------------------------------------------------------
66 class wxTimerScheduler
69 // get the unique timer scheduler instance
70 static wxTimerScheduler
& Get()
73 ms_instance
= new wxTimerScheduler
;
78 // must be called on shutdown to delete the global timer scheduler
79 static void Shutdown()
88 // adds timer which should expire at the given absolute time to the list
89 void AddTimer(wxUnixTimerImpl
*timer
, wxUsecClock_t expiration
);
91 // remove timer from the list, called automatically from timer dtor
92 void RemoveTimer(wxUnixTimerImpl
*timer
);
95 // the functions below are used by the event loop implementation to monitor
98 // if this function returns true, the time remaining until the next time
99 // expiration is returned in the provided parameter (always positive or 0)
101 // it returns false if there are no timers
102 bool GetNext(wxUsecClock_t
*remaining
) const;
104 // trigger the timer event for all timers which have expired
105 void NotifyExpired();
108 // ctor and dtor are private, this is a singleton class only created by
109 // Get() and destroyed by Shutdown()
110 wxTimerScheduler() { }
113 // add the given timer schedule to the list in the right place
115 // we take ownership of the pointer "s" which must be heap-allocated
116 void DoAddTimer(wxTimerSchedule
*s
);
119 // the list of all currently active timers sorted by expiration
120 wxTimerList m_timers
;
122 static wxTimerScheduler
*ms_instance
;
125 // this helper function currently only exists for Unix platforms but could be
126 // moved to wx/stopwatch.h if it turns out to be useful elsewhere
128 // returns the number of microseconds since the Epoch
129 extern wxUsecClock_t
wxGetLocalTimeUsec();
131 #endif // _WX_UNIX_PRIVATE_TIMER_H_