]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: sec/mac/carbon/timer.cpp | |
3 | // Purpose: wxTimer implementation | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 1998-01-01 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Stefan Csomor | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | #include "wx/wxprec.h" | |
13 | ||
14 | #ifndef WX_PRECOMP | |
15 | #include "wx/dynarray.h" | |
16 | #endif | |
17 | ||
18 | #include "wx/timer.h" | |
19 | ||
20 | #ifdef __WXMAC__ | |
21 | #include "wx/mac/private.h" | |
22 | #endif | |
23 | ||
24 | #ifndef __DARWIN__ | |
25 | #include <Timer.h> | |
26 | #endif | |
27 | ||
28 | IMPLEMENT_ABSTRACT_CLASS(wxTimer, wxEvtHandler) | |
29 | ||
30 | #define wxMAC_USE_CARBON_TIMER 1 | |
31 | ||
32 | #if wxMAC_USE_CARBON_TIMER | |
33 | ||
34 | struct MacTimerInfo | |
35 | { | |
36 | wxTimer* m_timer; | |
37 | EventLoopTimerUPP m_proc; | |
38 | EventLoopTimerRef m_timerRef; | |
39 | }; | |
40 | ||
41 | static pascal void wxProcessTimer( EventLoopTimerRef theTimer, void *data ); | |
42 | static pascal void wxProcessTimer( EventLoopTimerRef theTimer, void *data ) | |
43 | { | |
44 | if ( data == NULL ) | |
45 | return; | |
46 | ||
47 | wxTimer* timer = (wxTimer*)data; | |
48 | ||
49 | if ( timer->IsOneShot() ) | |
50 | timer->Stop(); | |
51 | ||
52 | timer->Notify(); | |
53 | } | |
54 | ||
55 | void wxTimer::Init() | |
56 | { | |
57 | m_info = new MacTimerInfo(); | |
58 | m_info->m_timer = this; | |
59 | m_info->m_proc = NULL; | |
60 | m_info->m_timerRef = kInvalidID; | |
61 | } | |
62 | ||
63 | bool wxTimer::IsRunning() const | |
64 | { | |
65 | return ( m_info->m_timerRef != kInvalidID ); | |
66 | } | |
67 | ||
68 | wxTimer::~wxTimer() | |
69 | { | |
70 | Stop(); | |
71 | if (m_info != NULL) | |
72 | { | |
73 | delete m_info; | |
74 | m_info = NULL; | |
75 | } | |
76 | } | |
77 | ||
78 | bool wxTimer::Start( int milliseconds, bool mode ) | |
79 | { | |
80 | (void)wxTimerBase::Start(milliseconds, mode); | |
81 | ||
82 | wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") ); | |
83 | wxCHECK_MSG( m_info->m_timerRef == NULL, false, wxT("attempting to restart a timer") ); | |
84 | ||
85 | m_info->m_timer = this; | |
86 | m_info->m_proc = NewEventLoopTimerUPP( &wxProcessTimer ); | |
87 | ||
88 | OSStatus err = InstallEventLoopTimer( | |
89 | GetMainEventLoop(), | |
90 | m_milli*kEventDurationMillisecond, | |
91 | IsOneShot() ? 0 : m_milli * kEventDurationMillisecond, | |
92 | m_info->m_proc, | |
93 | this, | |
94 | &m_info->m_timerRef ); | |
95 | verify_noerr( err ); | |
96 | ||
97 | return true; | |
98 | } | |
99 | ||
100 | void wxTimer::Stop() | |
101 | { | |
102 | if (m_info->m_timerRef) | |
103 | RemoveEventLoopTimer( m_info->m_timerRef ); | |
104 | if (m_info->m_proc) | |
105 | DisposeEventLoopTimerUPP( m_info->m_proc ); | |
106 | ||
107 | m_info->m_proc = NULL; | |
108 | m_info->m_timerRef = kInvalidID; | |
109 | } | |
110 | ||
111 | #else | |
112 | ||
113 | typedef struct MacTimerInfo | |
114 | { | |
115 | TMTask m_task; | |
116 | wxMacNotifierTableRef m_table; | |
117 | wxTimer* m_timer; | |
118 | }; | |
119 | ||
120 | static void wxProcessTimer( unsigned long event, void *data ); | |
121 | ||
122 | static pascal void MacTimerProc( TMTask * t ) | |
123 | { | |
124 | MacTimerInfo * tm = (MacTimerInfo*) t; | |
125 | wxMacAddEvent( tm->m_table, wxProcessTimer, 0, (void*) tm->m_timer, true ); | |
126 | } | |
127 | ||
128 | // we need this array to track timers that are being deleted within the Notify procedure | |
129 | // adding the timer before the Notify call and checking after whether it still is in there | |
130 | // as the destructor would have removed it from the array | |
131 | ||
132 | wxArrayPtrVoid gTimersInProcess; | |
133 | ||
134 | static void wxProcessTimer( unsigned long event, void *data ) | |
135 | { | |
136 | if ( data == NULL ) | |
137 | return; | |
138 | ||
139 | wxTimer* timer = (wxTimer*) data; | |
140 | if ( timer->IsOneShot() ) | |
141 | timer->Stop(); | |
142 | ||
143 | gTimersInProcess.Add( timer ); | |
144 | timer->Notify(); | |
145 | ||
146 | int index = gTimersInProcess.Index( timer ); | |
147 | if ( index != wxNOT_FOUND ) | |
148 | { | |
149 | gTimersInProcess.RemoveAt( index ); | |
150 | ||
151 | if ( !timer->IsOneShot() && timer->m_info->m_task.tmAddr ) | |
152 | PrimeTime( (QElemPtr) &timer->m_info->m_task, timer->GetInterval() ); | |
153 | } | |
154 | } | |
155 | ||
156 | void wxTimer::Init() | |
157 | { | |
158 | m_info = new MacTimerInfo(); | |
159 | m_info->m_task.tmAddr = NULL; | |
160 | m_info->m_task.tmWakeUp = 0; | |
161 | m_info->m_task.tmReserved = 0; | |
162 | m_info->m_task.qType = 0; | |
163 | m_info->m_table = wxMacGetNotifierTable(); | |
164 | m_info->m_timer = this; | |
165 | } | |
166 | ||
167 | bool wxTimer::IsRunning() const | |
168 | { | |
169 | // as the qType may already indicate it is elapsed, but it | |
170 | // was not handled internally yet | |
171 | return ( m_info->m_task.tmAddr != NULL ); | |
172 | } | |
173 | ||
174 | wxTimer::~wxTimer() | |
175 | { | |
176 | Stop(); | |
177 | if (m_info != NULL) | |
178 | { | |
179 | delete m_info; | |
180 | m_info = NULL; | |
181 | } | |
182 | ||
183 | int index = gTimersInProcess.Index( this ); | |
184 | if ( index != wxNOT_FOUND ) | |
185 | gTimersInProcess.RemoveAt( index ); | |
186 | } | |
187 | ||
188 | bool wxTimer::Start( int milliseconds, bool mode ) | |
189 | { | |
190 | (void)wxTimerBase::Start( milliseconds, mode ); | |
191 | ||
192 | wxCHECK_MSG( m_milli > 0, false, wxT("invalid value for timer timeout") ); | |
193 | wxCHECK_MSG( m_info->m_task.tmAddr == NULL, false, wxT("attempting to restart a timer") ); | |
194 | ||
195 | m_info->m_task.tmAddr = NewTimerUPP( MacTimerProc ); | |
196 | m_info->m_task.tmWakeUp = 0; | |
197 | m_info->m_task.tmReserved = 0; | |
198 | m_info->m_task.qType = 0; | |
199 | m_info->m_timer = this; | |
200 | InsXTime( (QElemPtr) &m_info->m_task ); | |
201 | PrimeTime( (QElemPtr) &m_info->m_task, m_milli ); | |
202 | ||
203 | return true; | |
204 | } | |
205 | ||
206 | void wxTimer::Stop() | |
207 | { | |
208 | if ( m_info->m_task.tmAddr ) | |
209 | { | |
210 | RmvTime( (QElemPtr) &m_info->m_task ); | |
211 | DisposeTimerUPP( m_info->m_task.tmAddr ); | |
212 | m_info->m_task.tmAddr = NULL; | |
213 | } | |
214 | ||
215 | wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable(), this ); | |
216 | } | |
217 | ||
218 | #endif |