]>
Commit | Line | Data |
---|---|---|
56869c54 SC |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: sec/osx/core/timer.cpp | |
3 | // Purpose: wxTimer implementation using CoreFoundation | |
4 | // Author: Stefan Csomor | |
5 | // Modified by: | |
6 | // Created: 2008-07-01 | |
7 | // RCS-ID: $Id: timer.cpp 54129 2008-06-11 19:30:52Z 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 | ||
23 | struct wxOSXTimerInfo | |
24 | { | |
25 | wxOSXTimerImpl* m_timer; | |
26 | CFRunLoopTimerRef m_timerRef; | |
27 | }; | |
28 | ||
29 | void 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 | ||
42 | wxOSXTimerImpl::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 | ||
50 | bool wxOSXTimerImpl::IsRunning() const | |
51 | { | |
52 | return ( m_info->m_timerRef != kInvalidID && CFRunLoopTimerIsValid(m_info->m_timerRef)); | |
53 | } | |
54 | ||
55 | wxOSXTimerImpl::~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 | ||
66 | bool 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") ); | |
72 | ||
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( | |
84 | kCFAllocatorDefault, | |
85 | CFAbsoluteTimeAddGregorianUnits( CFAbsoluteTimeGetCurrent() , NULL, gumilli ), | |
86 | IsOneShot() ? 0 : CFTimeInterval( m_milli / 1000.0 ) , | |
87 | 0, 0, wxProcessTimer, &ctx); | |
88 | ||
89 | wxASSERT_MSG( m_info->m_timerRef != NULL, wxT("unable to create timer")); | |
90 | ||
91 | CFRunLoopAddTimer( CFRunLoopGetMain() // or CFRunLoopGetCurrent() ? | |
92 | , m_info->m_timerRef, kCFRunLoopCommonModes) ; | |
93 | ||
94 | ||
95 | return true; | |
96 | } | |
97 | ||
98 | void wxOSXTimerImpl::Stop() | |
99 | { | |
100 | if (m_info->m_timerRef) | |
101 | { | |
102 | CFRunLoopTimerInvalidate( m_info->m_timerRef ); | |
103 | CFRelease( m_info->m_timerRef ); | |
104 | } | |
105 | m_info->m_timerRef = kInvalidID; | |
106 | } | |
107 | ||
108 | #endif // wxUSE_TIMER | |
109 |