1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sec/mac/carbon/timer.cpp
3 // Purpose: wxTimer implementation
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
17 #include "wx/mac/private.h"
20 #include "wx/dynarray.h"
26 IMPLEMENT_ABSTRACT_CLASS(wxTimer
, wxEvtHandler
)
28 #define wxMAC_USE_CARBON_TIMER 1
30 #if wxMAC_USE_CARBON_TIMER
35 EventLoopTimerUPP m_proc
;
36 EventLoopTimerRef m_timerRef
;
39 static pascal void wxProcessTimer( EventLoopTimerRef theTimer
, void *data
);
40 static pascal void wxProcessTimer( EventLoopTimerRef theTimer
, void *data
)
45 wxTimer
* timer
= (wxTimer
*)data
;
47 if ( timer
->IsOneShot() )
55 m_info
= new MacTimerInfo();
56 m_info
->m_timer
= this;
57 m_info
->m_proc
= NULL
;
58 m_info
->m_timerRef
= kInvalidID
;
61 bool wxTimer::IsRunning() const
63 return ( m_info
->m_timerRef
!= kInvalidID
);
76 bool wxTimer::Start( int milliseconds
, bool mode
)
78 (void)wxTimerBase::Start(milliseconds
, mode
);
80 wxCHECK_MSG( m_milli
> 0, false, wxT("invalid value for timer timeout") );
81 wxCHECK_MSG( m_info
->m_timerRef
== NULL
, false, wxT("attempting to restart a timer") );
83 m_info
->m_timer
= this;
84 m_info
->m_proc
= NewEventLoopTimerUPP( &wxProcessTimer
);
86 OSStatus err
= InstallEventLoopTimer(
88 m_milli
*kEventDurationMillisecond
,
89 IsOneShot() ? 0 : m_milli
* kEventDurationMillisecond
,
92 &m_info
->m_timerRef
);
100 if (m_info
->m_timerRef
)
101 RemoveEventLoopTimer( m_info
->m_timerRef
);
103 DisposeEventLoopTimerUPP( m_info
->m_proc
);
105 m_info
->m_proc
= NULL
;
106 m_info
->m_timerRef
= kInvalidID
;
111 typedef struct MacTimerInfo
114 wxMacNotifierTableRef m_table
;
118 static void wxProcessTimer( unsigned long event
, void *data
);
120 static pascal void MacTimerProc( TMTask
* t
)
122 MacTimerInfo
* tm
= (MacTimerInfo
*) t
;
123 wxMacAddEvent( tm
->m_table
, wxProcessTimer
, 0, (void*) tm
->m_timer
, true );
126 // we need this array to track timers that are being deleted within the Notify procedure
127 // adding the timer before the Notify call and checking after whether it still is in there
128 // as the destructor would have removed it from the array
130 wxArrayPtrVoid gTimersInProcess
;
132 static void wxProcessTimer( unsigned long event
, void *data
)
137 wxTimer
* timer
= (wxTimer
*) data
;
138 if ( timer
->IsOneShot() )
141 gTimersInProcess
.Add( timer
);
144 int index
= gTimersInProcess
.Index( timer
);
145 if ( index
!= wxNOT_FOUND
)
147 gTimersInProcess
.RemoveAt( index
);
149 if ( !timer
->IsOneShot() && timer
->m_info
->m_task
.tmAddr
)
150 PrimeTime( (QElemPtr
) &timer
->m_info
->m_task
, timer
->GetInterval() );
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;
165 bool wxTimer::IsRunning() const
167 // as the qType may already indicate it is elapsed, but it
168 // was not handled internally yet
169 return ( m_info
->m_task
.tmAddr
!= NULL
);
181 int index
= gTimersInProcess
.Index( this );
182 if ( index
!= wxNOT_FOUND
)
183 gTimersInProcess
.RemoveAt( index
);
186 bool wxTimer::Start( int milliseconds
, bool mode
)
188 (void)wxTimerBase::Start( milliseconds
, mode
);
190 wxCHECK_MSG( m_milli
> 0, false, wxT("invalid value for timer timeout") );
191 wxCHECK_MSG( m_info
->m_task
.tmAddr
== NULL
, false, wxT("attempting to restart a timer") );
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
);
206 if ( m_info
->m_task
.tmAddr
)
208 RmvTime( (QElemPtr
) &m_info
->m_task
);
209 DisposeTimerUPP( m_info
->m_task
.tmAddr
);
210 m_info
->m_task
.tmAddr
= NULL
;
213 wxMacRemoveAllNotifiersForData( wxMacGetNotifierTable(), this );