Dali 3D User Interface Engine
uv-timer-impl.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 // CLASS HEADER
19 #include "timer-impl.h"
21 
22 // EXTERNAL INCLUDES
23 #include <uv.h>
24 
25 namespace Dali
26 {
27 
28 namespace Internal
29 {
30 
31 namespace Adaptor
32 {
33 
34 namespace
35 {
36 void TimerSourceFunc (uv_timer_t* handle)
37 {
38  Timer* timer = static_cast<Timer*>(handle->data);
39 
40  bool keepRunning = timer->Tick();
41  if( !keepRunning )
42  {
43  timer->Stop();
44  }
45 }
46 void FreeHandleCallback(uv_handle_t* handle )
47 {
48  delete handle;
49 }
50 
51 } // unnamed namespace
52 
56 struct Timer::Impl
57 {
58  Impl( unsigned int milliSec )
59  : mTimerHandle( NULL ),
60  mInterval( milliSec ),
61  mRunning( false )
62  {
63  }
64 
66  {
67  // the handle will still be alive for a short period after calling uv_close
68  // set the data to NULL to avoid a dangling pointer
69  mTimerHandle->data = NULL;
70 
71  uv_close( reinterpret_cast< uv_handle_t* >( mTimerHandle ), FreeHandleCallback );
72  }
73 
74  bool Running()
75  {
76  return mRunning;
77  }
78 
79  void Start( void* internalTimerPtr )
80  {
81  Stop(); // make sure we stop first if its currently running
82 
83  if( !mTimerHandle )
84  {
85  // heap allocate the handle as its lifetime will be longer than TimerImpl
86  mTimerHandle = new uv_timer_t;
87 
88  // initialize the handle
89  uv_timer_init( uv_default_loop(), mTimerHandle);
90  }
91 
92  mRunning = true;
93 
94  mTimerHandle->data = internalTimerPtr;
95 
96  uv_timer_start( mTimerHandle, TimerSourceFunc, mInterval, mInterval);
97  }
98 
99  void Stop()
100  {
101  if( mRunning )
102  {
103  mTimerHandle->data = NULL;
104  uv_timer_stop( mTimerHandle );
105  mRunning = false;
106  }
107  }
108 
109  uv_timer_t* mTimerHandle;
110  unsigned int mInterval;
111  bool mRunning;
112 };
113 
114 TimerPtr Timer::New( unsigned int milliSec )
115 {
116  DALI_LOG_ERROR(" new timer");
117  TimerPtr timer( new Timer( milliSec ) );
118  return timer;
119 }
120 
121 Timer::Timer( unsigned int milliSec )
122 : mImpl(new Impl(milliSec))
123 {
124 }
125 
127 {
128  // stop timers
129  Stop();
130 
131  delete mImpl;
132 }
133 
134 void Timer::Start()
135 {
136  mImpl->Start( this );
137 }
138 
139 void Timer::Stop()
140 {
141  mImpl->Stop();
142 }
143 
144 void Timer::SetInterval( unsigned int interval )
145 {
146  // stop existing timer
147  Stop();
148 
149  mImpl->mInterval = interval;
150 
151  // start new tick
152  Start();
153 }
154 
155 unsigned int Timer::GetInterval() const
156 {
157  return mImpl->mInterval;
158 }
159 
160 bool Timer::Tick()
161 {
162  // Guard against destruction during signal emission
163  Dali::Timer handle( this );
164 
165  bool retVal( false );
166 
167  // Override with new signal if used
168  if( !mTickSignal.Empty() )
169  {
170  retVal = mTickSignal.Emit();
171 
172  // Timer stops if return value is false
173  if (retVal == false)
174  {
175  Stop();
176  }
177  else
178  {
179  retVal = true; // continue emission
180  }
181  }
182  else // no callbacks registered
183  {
184  // periodic timer is started but nobody listens, continue
185  retVal = true;
186  }
187 
188  return retVal;
189 }
190 
192 {
193  return mTickSignal;
194 }
195 
196 bool Timer::IsRunning() const
197 {
198  return mImpl->mRunning;
199 }
200 
201 } // namespace Adaptor
202 
203 } // namespace Internal
204 
205 } // namespace Dali
Dali Docs Home
Read more about Dali