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