]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unix/private/timer.h
cdcf53b3d22ae3f24af896c7d6228f4a49b5a65d
[wxWidgets.git] / include / wx / unix / private / timer.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/unix/private/timer.h
3 // Purpose: wxTimer for wxBase (unix)
4 // Author: Lukasz Michalski
5 // Created: 15/01/2005
6 // RCS-ID: $Id$
7 // Copyright: (c) Lukasz Michalski
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_UNIX_PRIVATE_TIMER_H_
12 #define _WX_UNIX_PRIVATE_TIMER_H_
13
14 #include "wx/private/timer.h"
15
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;
19
20 // ----------------------------------------------------------------------------
21 // wxTimer implementation class for Unix platforms
22 // ----------------------------------------------------------------------------
23
24 class wxUnixTimerImpl : public wxTimerImpl
25 {
26 public:
27 wxUnixTimerImpl(wxTimer *timer);
28 virtual ~wxUnixTimerImpl();
29
30 virtual bool IsRunning() const;
31 virtual bool Start(int milliseconds = -1, bool oneShot = false);
32 virtual void Stop();
33
34 private:
35 bool m_isRunning;
36
37 friend class wxTimerScheduler;
38 };
39
40 // ----------------------------------------------------------------------------
41 // wxTimerSchedule: information about a single timer, used by wxTimerScheduler
42 // ----------------------------------------------------------------------------
43
44 struct wxTimerSchedule
45 {
46 wxTimerSchedule(wxUnixTimerImpl *timer, wxUsecClock_t expiration)
47 : m_timer(timer),
48 m_expiration(expiration)
49 {
50 }
51
52 // the timer itself (we don't own this pointer)
53 wxUnixTimerImpl *m_timer;
54
55 // the time of its next expiration, in usec
56 wxUsecClock_t m_expiration;
57 };
58
59 // the linked list of all active timers, we keep it sorted by expiration time
60 WX_DECLARE_LIST(wxTimerSchedule, wxTimerList);
61
62 // ----------------------------------------------------------------------------
63 // wxTimerScheduler: class responsible for updating all timers
64 // ----------------------------------------------------------------------------
65
66 class wxTimerScheduler
67 {
68 public:
69 // get the unique timer scheduler instance
70 static wxTimerScheduler& Get()
71 {
72 if ( !ms_instance )
73 ms_instance = new wxTimerScheduler;
74
75 return *ms_instance;
76 }
77
78 // must be called on shutdown to delete the global timer scheduler
79 static void Shutdown()
80 {
81 if ( ms_instance )
82 {
83 delete ms_instance;
84 ms_instance = NULL;
85 }
86 }
87
88 // adds timer which should expire at the given absolute time to the list
89 void AddTimer(wxUnixTimerImpl *timer, wxUsecClock_t expiration);
90
91 // remove timer from the list, called automatically from timer dtor
92 void RemoveTimer(wxUnixTimerImpl *timer);
93
94
95 // the functions below are used by the event loop implementation to monitor
96 // and notify timers:
97
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)
100 //
101 // it returns false if there are no timers
102 bool GetNext(wxUsecClock_t *remaining) const;
103
104 // trigger the timer event for all timers which have expired
105 void NotifyExpired();
106
107 private:
108 // ctor and dtor are private, this is a singleton class only created by
109 // Get() and destroyed by Shutdown()
110 wxTimerScheduler() { }
111 ~wxTimerScheduler();
112
113 // add the given timer schedule to the list in the right place
114 //
115 // we take ownership of the pointer "s" which must be heap-allocated
116 void DoAddTimer(wxTimerSchedule *s);
117
118
119 // the list of all currently active timers sorted by expiration
120 wxTimerList m_timers;
121
122 static wxTimerScheduler *ms_instance;
123 };
124
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
127 //
128 // returns the number of microseconds since the Epoch
129 extern wxUsecClock_t wxGetLocalTimeUsec();
130
131 #endif // _WX_UNIX_PRIVATE_TIMER_H_