]> git.saurik.com Git - wxWidgets.git/blob - include/wx/unix/private/timer.h
return true if we did anything in NotifyExpired()
[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 #if wxUSE_TIMER
15
16 #include "wx/private/timer.h"
17
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;
21
22 // ----------------------------------------------------------------------------
23 // wxTimer implementation class for Unix platforms
24 // ----------------------------------------------------------------------------
25
26 class wxUnixTimerImpl : public wxTimerImpl
27 {
28 public:
29 wxUnixTimerImpl(wxTimer *timer);
30 virtual ~wxUnixTimerImpl();
31
32 virtual bool IsRunning() const;
33 virtual bool Start(int milliseconds = -1, bool oneShot = false);
34 virtual void Stop();
35
36 // for wxTimerScheduler only: resets the internal flag indicating that the
37 // timer is running
38 void MarkStopped()
39 {
40 wxASSERT_MSG( m_isRunning, _T("stopping non-running timer?") );
41
42 m_isRunning = false;
43 }
44
45 private:
46 bool m_isRunning;
47 };
48
49 // ----------------------------------------------------------------------------
50 // wxTimerSchedule: information about a single timer, used by wxTimerScheduler
51 // ----------------------------------------------------------------------------
52
53 struct wxTimerSchedule
54 {
55 wxTimerSchedule(wxUnixTimerImpl *timer, wxUsecClock_t expiration)
56 : m_timer(timer),
57 m_expiration(expiration)
58 {
59 }
60
61 // the timer itself (we don't own this pointer)
62 wxUnixTimerImpl *m_timer;
63
64 // the time of its next expiration, in usec
65 wxUsecClock_t m_expiration;
66 };
67
68 // the linked list of all active timers, we keep it sorted by expiration time
69 WX_DECLARE_LIST(wxTimerSchedule, wxTimerList);
70
71 // ----------------------------------------------------------------------------
72 // wxTimerScheduler: class responsible for updating all timers
73 // ----------------------------------------------------------------------------
74
75 class wxTimerScheduler
76 {
77 public:
78 // get the unique timer scheduler instance
79 static wxTimerScheduler& Get()
80 {
81 if ( !ms_instance )
82 ms_instance = new wxTimerScheduler;
83
84 return *ms_instance;
85 }
86
87 // must be called on shutdown to delete the global timer scheduler
88 static void Shutdown()
89 {
90 if ( ms_instance )
91 {
92 delete ms_instance;
93 ms_instance = NULL;
94 }
95 }
96
97 // adds timer which should expire at the given absolute time to the list
98 void AddTimer(wxUnixTimerImpl *timer, wxUsecClock_t expiration);
99
100 // remove timer from the list, called automatically from timer dtor
101 void RemoveTimer(wxUnixTimerImpl *timer);
102
103
104 // the functions below are used by the event loop implementation to monitor
105 // and notify timers:
106
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)
109 //
110 // it returns false if there are no timers
111 bool GetNext(wxUsecClock_t *remaining) const;
112
113 // trigger the timer event for all timers which have expired, return true
114 // if any did
115 bool NotifyExpired();
116
117 private:
118 // ctor and dtor are private, this is a singleton class only created by
119 // Get() and destroyed by Shutdown()
120 wxTimerScheduler() { }
121 ~wxTimerScheduler();
122
123 // add the given timer schedule to the list in the right place
124 //
125 // we take ownership of the pointer "s" which must be heap-allocated
126 void DoAddTimer(wxTimerSchedule *s);
127
128
129 // the list of all currently active timers sorted by expiration
130 wxTimerList m_timers;
131
132 static wxTimerScheduler *ms_instance;
133 };
134
135 // this helper function currently only exists for Unix platforms but could be
136 // moved to wx/stopwatch.h if it turns out to be useful elsewhere
137 //
138 // returns the number of microseconds since the Epoch
139 extern wxUsecClock_t wxGetLocalTimeUsec();
140
141 #endif // wxUSE_TIMER
142
143 #endif // _WX_UNIX_PRIVATE_TIMER_H_