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