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