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