2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/system/common/timer-impl.h>
22 #include <dali/integration-api/debug.h>
33 extern uv_loop_t* GetUVMainLoop();
37 void TimerSourceFunc(uv_timer_t* handle)
39 Timer* timer = static_cast<Timer*>(handle->data);
41 bool keepRunning = timer->Tick();
47 void FreeHandleCallback(uv_handle_t* handle)
52 } // unnamed namespace
55 * Struct to hide away libuv implementation details
59 Impl(unsigned int milliSec)
68 // the handle will still be alive for a short period after calling uv_close
69 // set the data to NULL to avoid a dangling pointer
70 mTimerHandle->data = NULL;
72 uv_close(reinterpret_cast<uv_handle_t*>(mTimerHandle), FreeHandleCallback);
80 void Start(void* internalTimerPtr)
82 Stop(); // make sure we stop first if its currently running
86 // heap allocate the handle as its lifetime will be longer than TimerImpl
87 mTimerHandle = new uv_timer_t;
89 // initialize the handle
90 uv_timer_init(GetUVMainLoop(), mTimerHandle);
95 mTimerHandle->data = internalTimerPtr;
97 uv_timer_start(mTimerHandle, TimerSourceFunc, mInterval, mInterval);
104 mTimerHandle->data = NULL;
105 uv_timer_stop(mTimerHandle);
112 uv_timer_stop(mTimerHandle);
117 uv_timer_start(mTimerHandle, TimerSourceFunc, mInterval, mInterval);
120 uv_timer_t* mTimerHandle;
121 unsigned int mInterval;
125 TimerPtr Timer::New(unsigned int milliSec)
127 DALI_LOG_ERROR(" new timer\n");
128 TimerPtr timer(new Timer(milliSec));
132 Timer::Timer(unsigned int milliSec)
133 : mImpl(new Impl(milliSec))
165 void Timer::SetInterval(unsigned int interval, bool restart)
167 // stop existing timer
170 mImpl->mInterval = interval;
179 unsigned int Timer::GetInterval() const
181 return mImpl->mInterval;
186 // Guard against destruction during signal emission
187 Dali::Timer handle(this);
191 // Override with new signal if used
192 if(!mTickSignal.Empty())
194 retVal = mTickSignal.Emit();
196 // Timer stops if return value is false
203 retVal = true; // continue emission
206 else // no callbacks registered
208 // periodic timer is started but nobody listens, continue
215 Dali::Timer::TimerSignalType& Timer::TickSignal()
220 bool Timer::IsRunning() const
222 return mImpl->mRunning;
225 } // namespace Adaptor
227 } // namespace Internal