1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/timer.cpp
3 // Purpose: wxTimer implementation using CoreFoundation
4 // Author: Stefan Csomor
7 // Copyright: (c) Stefan Csomor
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/wxprec.h"
16 #include "wx/dynarray.h"
19 #include "wx/osx/private.h"
20 #include "wx/osx/private/timer.h"
24 wxOSXTimerImpl
* m_timer
;
25 CFRunLoopTimerRef m_timerRef
;
28 void wxProcessTimer(CFRunLoopTimerRef
WXUNUSED(theTimer
), void *data
)
33 wxOSXTimerImpl
* timer
= (wxOSXTimerImpl
*)data
;
35 if ( timer
->IsOneShot() )
41 wxOSXTimerImpl::wxOSXTimerImpl(wxTimer
*timer
)
44 m_info
= new wxOSXTimerInfo();
45 m_info
->m_timer
= this;
46 m_info
->m_timerRef
= kInvalidID
;
49 bool wxOSXTimerImpl::IsRunning() const
51 return ( m_info
->m_timerRef
!= kInvalidID
&& CFRunLoopTimerIsValid(m_info
->m_timerRef
));
54 wxOSXTimerImpl::~wxOSXTimerImpl()
56 if (m_info
->m_timerRef
)
58 if ( CFRunLoopTimerIsValid(m_info
->m_timerRef
) )
59 CFRunLoopTimerInvalidate( m_info
->m_timerRef
);
60 CFRelease( m_info
->m_timerRef
);
65 bool wxOSXTimerImpl::Start( int milliseconds
, bool mode
)
67 (void)wxTimerImpl::Start(milliseconds
, mode
);
69 wxCHECK_MSG( m_milli
> 0, false, wxT("invalid value for timer timeout") );
70 wxCHECK_MSG( m_info
->m_timerRef
== NULL
, false, wxT("attempting to restart a timer") );
72 CFGregorianUnits gumilli
;
73 memset(&gumilli
,0,sizeof(gumilli
) );
74 gumilli
.seconds
= m_milli
/ 1000.0;
76 CFRunLoopTimerContext ctx
;
77 memset( &ctx
, 0 , sizeof(ctx
) );
81 m_info
->m_timer
= this;
82 m_info
->m_timerRef
= CFRunLoopTimerCreate(
84 CFAbsoluteTimeAddGregorianUnits( CFAbsoluteTimeGetCurrent() , NULL
, gumilli
),
85 IsOneShot() ? 0 : CFTimeInterval( m_milli
/ 1000.0 ) ,
86 0, 0, wxProcessTimer
, &ctx
);
88 wxASSERT_MSG( m_info
->m_timerRef
!= NULL
, wxT("unable to create timer"));
90 CFRunLoopRef runLoop
= 0;
92 runLoop
= CFRunLoopGetMain();
94 runLoop
= CFRunLoopGetCurrent();
96 CFRunLoopAddTimer( runLoop
, m_info
->m_timerRef
, kCFRunLoopCommonModes
) ;
102 void wxOSXTimerImpl::Stop()
104 if (m_info
->m_timerRef
)
106 CFRunLoopTimerInvalidate( m_info
->m_timerRef
);
107 CFRelease( m_info
->m_timerRef
);
109 m_info
->m_timerRef
= kInvalidID
;
112 #endif // wxUSE_TIMER