fde050d5abae08c5525374e6dc9c97d1203c6aec
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / linux / timer-impl-ecore.cpp
1 /*
2  * Copyright (c) 2019 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 <dali/internal/system/common/timer-impl.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/adaptor/common/adaptor-impl.h>
23 #include <dali/public-api/dali-adaptor-common.h>
24
25 #include <dali/internal/system/linux/dali-ecore.h>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace Adaptor
34 {
35
36 // LOCAL STUFF
37 namespace
38 {
39 Eina_Bool TimerSourceFunc (void *data)
40 {
41   Timer* timer = static_cast<Timer*>(data);
42
43   bool keepRunning = timer->Tick();
44
45   return keepRunning ? EINA_TRUE : EINA_FALSE;
46 }
47 } // unnamed namespace
48
49 /**
50  * Struct to hide away Ecore implementation details
51  */
52 struct Timer::Impl
53 {
54   Impl( unsigned int milliSec )
55   : mId(NULL),
56     mInterval(milliSec)
57   {
58   }
59
60   Ecore_Timer * mId;
61   unsigned int mInterval;
62 };
63
64 TimerPtr Timer::New( unsigned int milliSec )
65 {
66   TimerPtr timer( new Timer( milliSec ) );
67   return timer;
68 }
69
70 Timer::Timer( unsigned int milliSec )
71 : mImpl(new Impl(milliSec))
72 {
73 }
74
75 Timer::~Timer()
76 {
77   ResetTimerData();
78   delete mImpl;
79 }
80
81 void Timer::Start()
82 {
83   // Timer should be used in the event thread
84   DALI_ASSERT_DEBUG( Adaptor::IsAvailable() );
85
86   if(mImpl->mId != NULL)
87   {
88     Stop();
89   }
90   double interval = static_cast<double> ( mImpl->mInterval ) / 1000.0f;
91   mImpl->mId = ecore_timer_add( interval, reinterpret_cast<Ecore_Task_Cb>( TimerSourceFunc ), this );
92 }
93
94 void Timer::Stop()
95 {
96   // Timer should be used in the event thread
97   DALI_ASSERT_DEBUG( Adaptor::IsAvailable() );
98
99   ResetTimerData();
100 }
101
102 void Timer::Pause()
103 {
104   // Timer should be used in the event thread
105   DALI_ASSERT_DEBUG( Adaptor::IsAvailable() );
106
107   if( mImpl->mId != NULL )
108   {
109     ecore_timer_freeze( mImpl->mId );
110   }
111 }
112
113 void Timer::Resume()
114 {
115   // Timer should be used in the event thread
116   DALI_ASSERT_DEBUG( Adaptor::IsAvailable() );
117
118   if( mImpl->mId != NULL )
119   {
120     ecore_timer_thaw( mImpl->mId );
121   }
122 }
123
124 void Timer::SetInterval( unsigned int interval, bool restart )
125 {
126   // stop existing timer
127   Stop();
128   mImpl->mInterval = interval;
129
130   if( restart )
131   {
132     // start new tick
133     Start();
134   }
135 }
136
137 unsigned int Timer::GetInterval() const
138 {
139   return mImpl->mInterval;
140 }
141
142 bool Timer::Tick()
143 {
144   // Guard against destruction during signal emission
145   Dali::Timer handle( this );
146
147   bool retVal( false );
148
149   // Override with new signal if used
150   if( !mTickSignal.Empty() )
151   {
152     retVal = mTickSignal.Emit();
153
154     // Timer stops if return value is false
155     if (retVal == false)
156     {
157       Stop();
158     }
159     else
160     {
161       retVal = true;   // continue emission
162     }
163   }
164   else // no callbacks registered
165   {
166     // periodic timer is started but nobody listens, continue
167     retVal = true;
168   }
169
170   return retVal;
171 }
172
173 Dali::Timer::TimerSignalType& Timer::TickSignal()
174 {
175   return mTickSignal;
176 }
177
178 void Timer::ResetTimerData()
179 {
180   if (mImpl->mId != NULL)
181   {
182     ecore_timer_del(mImpl->mId);
183     mImpl->mId = NULL;
184   }
185 }
186
187 bool Timer::IsRunning() const
188 {
189   return mImpl->mId != NULL;
190 }
191
192 } // namespace Adaptor
193
194 } // namespace Internal
195
196 } // namespace Dali