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