Merge "Add support for new accessibility actions" into devel/master
[platform/core/uifw/dali-adaptor.git] / adaptors / common / timer-impl.cpp
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"
20
21 // EXTERNAL INCLUDES
22 #include <Ecore.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 namespace Adaptor
31 {
32
33 // LOCAL STUFF
34 namespace
35 {
36 Eina_Bool TimerSourceFunc (void *data)
37 {
38   Timer* timer = static_cast<Timer*>(data);
39
40   bool keepRunning = timer->Tick();
41
42   return keepRunning ? EINA_TRUE : EINA_FALSE;
43 }
44 } // unnamed namespace
45
46 /**
47  * Struct to hide away Ecore implementation details
48  */
49 struct Timer::Impl
50 {
51   Impl( unsigned int milliSec )
52   : mId(NULL),
53     mInterval(milliSec)
54   {
55   }
56
57   Ecore_Timer * mId;
58   unsigned int mInterval;
59 };
60
61 TimerPtr Timer::New( unsigned int milliSec )
62 {
63   TimerPtr timer( new Timer( milliSec ) );
64   return timer;
65 }
66
67 Timer::Timer( unsigned int milliSec )
68 : mImpl(new Impl(milliSec))
69 {
70 }
71
72 Timer::~Timer()
73 {
74   // stop timers
75   Stop();
76
77   delete mImpl;
78 }
79
80 void Timer::Start()
81 {
82   if(mImpl->mId != NULL)
83   {
84     Stop();
85   }
86   mImpl->mId = ecore_timer_add( (double)mImpl->mInterval/1000.0f, (Ecore_Task_Cb)TimerSourceFunc, this );
87 }
88
89 void Timer::Stop()
90 {
91   if (mImpl->mId != NULL)
92   {
93     ecore_timer_del(mImpl->mId);
94     mImpl->mId = NULL;
95   }
96 }
97
98 void Timer::SetInterval( unsigned int interval )
99 {
100   // stop existing timer
101   Stop();
102   mImpl->mInterval = interval;
103   // start new tick
104   Start();
105 }
106
107 unsigned int Timer::GetInterval() const
108 {
109   return mImpl->mInterval;
110 }
111
112 bool Timer::Tick()
113 {
114   // Guard against destruction during signal emission
115   Dali::Timer handle( this );
116
117   bool retVal( false );
118
119   // Override with new signal if used
120   if( !mTickSignal.Empty() )
121   {
122     retVal = mTickSignal.Emit();
123
124     // Timer stops if return value is false
125     if (retVal == false)
126     {
127       Stop();
128     }
129     else
130     {
131       retVal = true;   // continue emission
132     }
133   }
134   else // no callbacks registered
135   {
136     // periodic timer is started but nobody listens, continue
137     retVal = true;
138   }
139
140   return retVal;
141 }
142
143 Dali::Timer::TimerSignalType& Timer::TickSignal()
144 {
145   return mTickSignal;
146 }
147
148 bool Timer::IsRunning() const
149 {
150   return mImpl->mId != NULL;
151 }
152
153 } // namespace Adaptor
154
155 } // namespace Internal
156
157 } // namespace Dali