1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/core/timer.cpp
3 // Purpose: wxTimer implementation using CoreFoundation
4 // Author: Stefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
17 #include "wx/dynarray.h"
20 #include "wx/osx/private.h"
21 #include "wx/osx/private/timer.h"
25 wxOSXTimerImpl
* m_timer
;
26 CFRunLoopTimerRef m_timerRef
;
29 void wxProcessTimer(CFRunLoopTimerRef
WXUNUSED(theTimer
), void *data
)
34 wxOSXTimerImpl
* timer
= (wxOSXTimerImpl
*)data
;
36 if ( timer
->IsOneShot() )
42 wxOSXTimerImpl::wxOSXTimerImpl(wxTimer
*timer
)
45 m_info
= new wxOSXTimerInfo();
46 m_info
->m_timer
= this;
47 m_info
->m_timerRef
= kInvalidID
;
50 bool wxOSXTimerImpl::IsRunning() const
52 return ( m_info
->m_timerRef
!= kInvalidID
&& CFRunLoopTimerIsValid(m_info
->m_timerRef
));
55 wxOSXTimerImpl::~wxOSXTimerImpl()
57 if (m_info
->m_timerRef
)
59 if ( CFRunLoopTimerIsValid(m_info
->m_timerRef
) )
60 CFRunLoopTimerInvalidate( m_info
->m_timerRef
);
61 CFRelease( m_info
->m_timerRef
);
66 bool wxOSXTimerImpl::Start( int milliseconds
, bool mode
)
68 (void)wxTimerImpl::Start(milliseconds
, mode
);
70 wxCHECK_MSG( m_milli
> 0, false, wxT("invalid value for timer timeout") );
71 wxCHECK_MSG( m_info
->m_timerRef
== NULL
, false, wxT("attempting to restart a timer") );
73 CFGregorianUnits gumilli
;
74 memset(&gumilli
,0,sizeof(gumilli
) );
75 gumilli
.seconds
= m_milli
/ 1000.0;
77 CFRunLoopTimerContext ctx
;
78 memset( &ctx
, 0 , sizeof(ctx
) );
82 m_info
->m_timer
= this;
83 m_info
->m_timerRef
= CFRunLoopTimerCreate(
85 CFAbsoluteTimeAddGregorianUnits( CFAbsoluteTimeGetCurrent() , NULL
, gumilli
),
86 IsOneShot() ? 0 : CFTimeInterval( m_milli
/ 1000.0 ) ,
87 0, 0, wxProcessTimer
, &ctx
);
89 wxASSERT_MSG( m_info
->m_timerRef
!= NULL
, wxT("unable to create timer"));
91 CFRunLoopRef runLoop
= 0;
93 runLoop
= CFRunLoopGetMain();
95 runLoop
= CFRunLoopGetCurrent();
97 CFRunLoopAddTimer( runLoop
, m_info
->m_timerRef
, kCFRunLoopCommonModes
) ;
103 void wxOSXTimerImpl::Stop()
105 if (m_info
->m_timerRef
)
107 CFRunLoopTimerInvalidate( m_info
->m_timerRef
);
108 CFRelease( m_info
->m_timerRef
);
110 m_info
->m_timerRef
= kInvalidID
;
113 #endif // wxUSE_TIMER