Seperate the API macros
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / 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::SetInterval( unsigned int interval )
105 {
106   // stop existing timer
107   Stop();
108   mImpl->mInterval = interval;
109   // start new tick
110   Start();
111 }
112
113 unsigned int Timer::GetInterval() const
114 {
115   return mImpl->mInterval;
116 }
117
118 bool Timer::Tick()
119 {
120   // Guard against destruction during signal emission
121   Dali::Timer handle( this );
122
123   bool retVal( false );
124
125   // Override with new signal if used
126   if( !mTickSignal.Empty() )
127   {
128     retVal = mTickSignal.Emit();
129
130     // Timer stops if return value is false
131     if (retVal == false)
132     {
133       Stop();
134     }
135     else
136     {
137       retVal = true;   // continue emission
138     }
139   }
140   else // no callbacks registered
141   {
142     // periodic timer is started but nobody listens, continue
143     retVal = true;
144   }
145
146   return retVal;
147 }
148
149 Dali::Timer::TimerSignalType& Timer::TickSignal()
150 {
151   return mTickSignal;
152 }
153
154 void Timer::ResetTimerData()
155 {
156   if (mImpl->mId != NULL)
157   {
158     ecore_timer_del(mImpl->mId);
159     mImpl->mId = NULL;
160   }
161 }
162
163 bool Timer::IsRunning() const
164 {
165   return mImpl->mId != NULL;
166 }
167
168 } // namespace Adaptor
169
170 } // namespace Internal
171
172 } // namespace Dali
173
174 #pragma GCC diagnostic pop