]> git.saurik.com Git - wxWidgets.git/blame - src/osx/core/timer.cpp
new file added
[wxWidgets.git] / src / osx / core / timer.cpp
CommitLineData
56869c54 1/////////////////////////////////////////////////////////////////////////////
9b5eee6e 2// Name: src/osx/core/timer.cpp
56869c54
SC
3// Purpose: wxTimer implementation using CoreFoundation
4// Author: Stefan Csomor
5// Modified by:
6// Created: 2008-07-01
b5b208a1 7// RCS-ID: $Id$
56869c54
SC
8// Copyright: (c) Stefan Csomor
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#include "wx/wxprec.h"
13
14#if wxUSE_TIMER
15
16#ifndef WX_PRECOMP
17 #include "wx/dynarray.h"
18#endif
19
20#include "wx/osx/private.h"
21#include "wx/osx/private/timer.h"
22
23struct wxOSXTimerInfo
24{
25 wxOSXTimerImpl* m_timer;
26 CFRunLoopTimerRef m_timerRef;
27};
28
29void wxProcessTimer(CFRunLoopTimerRef WXUNUSED(theTimer), void *data)
30{
31 if ( data == NULL )
32 return;
33
34 wxOSXTimerImpl* timer = (wxOSXTimerImpl*)data;
35
36 if ( timer->IsOneShot() )
37 timer->Stop();
38
39 timer->Notify();
40}
41
42wxOSXTimerImpl::wxOSXTimerImpl(wxTimer *timer)
43 : wxTimerImpl(timer)
44{
45 m_info = new wxOSXTimerInfo();
46 m_info->m_timer = this;
47 m_info->m_timerRef = kInvalidID;
48}
49
50bool wxOSXTimerImpl::IsRunning() const
51{
52 return ( m_info->m_timerRef != kInvalidID && CFRunLoopTimerIsValid(m_info->m_timerRef));
53}
54
55wxOSXTimerImpl::~wxOSXTimerImpl()
56{
57 if (m_info->m_timerRef)
58 {
59 if ( CFRunLoopTimerIsValid(m_info->m_timerRef) )
60 CFRunLoopTimerInvalidate( m_info->m_timerRef );
61 CFRelease( m_info->m_timerRef );
62 }
63 delete m_info;
64}
65
66bool wxOSXTimerImpl::Start( int milliseconds, bool mode )
67{
68 (void)wxTimerImpl::Start(milliseconds, mode);
69
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") );
03647350 72
56869c54
SC
73 CFGregorianUnits gumilli ;
74 memset(&gumilli,0,sizeof(gumilli) );
75 gumilli.seconds = m_milli / 1000.0;
76
77 CFRunLoopTimerContext ctx ;
78 memset( &ctx, 0 , sizeof(ctx) );
79 ctx.version = 0;
80 ctx.info = this;
81
82 m_info->m_timer = this;
83 m_info->m_timerRef = CFRunLoopTimerCreate(
03647350 84 kCFAllocatorDefault,
56869c54
SC
85 CFAbsoluteTimeAddGregorianUnits( CFAbsoluteTimeGetCurrent() , NULL, gumilli ),
86 IsOneShot() ? 0 : CFTimeInterval( m_milli / 1000.0 ) ,
87 0, 0, wxProcessTimer, &ctx);
03647350 88
56869c54 89 wxASSERT_MSG( m_info->m_timerRef != NULL, wxT("unable to create timer"));
03647350 90
9b5eee6e
SC
91 CFRunLoopRef runLoop = 0;
92#if wxOSX_USE_IPHONE
93 runLoop = CFRunLoopGetMain();
94#else
95 runLoop = CFRunLoopGetCurrent();
96#endif
97 CFRunLoopAddTimer( runLoop, m_info->m_timerRef, kCFRunLoopCommonModes) ;
56869c54
SC
98
99
100 return true;
101}
102
103void wxOSXTimerImpl::Stop()
104{
105 if (m_info->m_timerRef)
106 {
107 CFRunLoopTimerInvalidate( m_info->m_timerRef );
108 CFRelease( m_info->m_timerRef );
109 }
110 m_info->m_timerRef = kInvalidID;
111}
112
113#endif // wxUSE_TIMER
114