[4.0] add Timer debugging log temporarily, will be removed later
[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   DALI_LOG_ERROR("Timer::New: called [%p]\n", timer.Get());
73   return timer;
74 }
75
76 Timer::Timer( unsigned int milliSec )
77 : mImpl(new Impl(milliSec))
78 {
79 }
80
81 Timer::~Timer()
82 {
83   ResetTimerData();
84   delete mImpl;
85 }
86
87 void Timer::Start()
88 {
89   // Timer should be used in the event thread
90   DALI_ASSERT_DEBUG( Adaptor::IsAvailable() );
91
92   DALI_LOG_ERROR("Timer::Start: called [%p]\n", this);
93
94   if(mImpl->mId != NULL)
95   {
96     Stop();
97   }
98   mImpl->mId = ecore_timer_add( (double)mImpl->mInterval/1000.0f, (Ecore_Task_Cb)TimerSourceFunc, this );
99 }
100
101 void Timer::Stop()
102 {
103   // Timer should be used in the event thread
104   DALI_ASSERT_DEBUG( Adaptor::IsAvailable() );
105
106   DALI_LOG_ERROR("Timer::Stop: called [%p]\n", this);
107
108   ResetTimerData();
109 }
110
111 void Timer::SetInterval( unsigned int interval )
112 {
113   // stop existing timer
114   Stop();
115   mImpl->mInterval = interval;
116   // start new tick
117   Start();
118 }
119
120 unsigned int Timer::GetInterval() const
121 {
122   return mImpl->mInterval;
123 }
124
125 bool Timer::Tick()
126 {
127   // Guard against destruction during signal emission
128   Dali::Timer handle( this );
129
130   bool retVal( false );
131
132   DALI_LOG_ERROR("Timer::Tick: called [%p]\n", this);
133
134   // Override with new signal if used
135   if( !mTickSignal.Empty() )
136   {
137     retVal = mTickSignal.Emit();
138
139     // Timer stops if return value is false
140     if (retVal == false)
141     {
142       Stop();
143     }
144     else
145     {
146       retVal = true;   // continue emission
147     }
148   }
149   else // no callbacks registered
150   {
151     // periodic timer is started but nobody listens, continue
152     retVal = true;
153   }
154
155   return retVal;
156 }
157
158 Dali::Timer::TimerSignalType& Timer::TickSignal()
159 {
160   return mTickSignal;
161 }
162
163 void Timer::ResetTimerData()
164 {
165   if (mImpl->mId != NULL)
166   {
167     ecore_timer_del(mImpl->mId);
168     mImpl->mId = NULL;
169   }
170 }
171
172 bool Timer::IsRunning() const
173 {
174   return mImpl->mId != NULL;
175 }
176
177 } // namespace Adaptor
178
179 } // namespace Internal
180
181 } // namespace Dali
182
183 #pragma GCC diagnostic pop