Merge "Fixed string constant spacing for fw/compat with gcc 6.2" into devel/master
[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 // EXTERNAL INCLUDES
22 // Ecore is littered with C style cast
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-Wold-style-cast"
25 #include <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   // stop timers
78   Stop();
79
80   delete mImpl;
81 }
82
83 void Timer::Start()
84 {
85   if(mImpl->mId != NULL)
86   {
87     Stop();
88   }
89   mImpl->mId = ecore_timer_add( (double)mImpl->mInterval/1000.0f, (Ecore_Task_Cb)TimerSourceFunc, this );
90 }
91
92 void Timer::Stop()
93 {
94   if (mImpl->mId != NULL)
95   {
96     ecore_timer_del(mImpl->mId);
97     mImpl->mId = NULL;
98   }
99 }
100
101 void Timer::SetInterval( unsigned int interval )
102 {
103   // stop existing timer
104   Stop();
105   mImpl->mInterval = interval;
106   // start new tick
107   Start();
108 }
109
110 unsigned int Timer::GetInterval() const
111 {
112   return mImpl->mInterval;
113 }
114
115 bool Timer::Tick()
116 {
117   // Guard against destruction during signal emission
118   Dali::Timer handle( this );
119
120   bool retVal( false );
121
122   // Override with new signal if used
123   if( !mTickSignal.Empty() )
124   {
125     retVal = mTickSignal.Emit();
126
127     // Timer stops if return value is false
128     if (retVal == false)
129     {
130       Stop();
131     }
132     else
133     {
134       retVal = true;   // continue emission
135     }
136   }
137   else // no callbacks registered
138   {
139     // periodic timer is started but nobody listens, continue
140     retVal = true;
141   }
142
143   return retVal;
144 }
145
146 Dali::Timer::TimerSignalType& Timer::TickSignal()
147 {
148   return mTickSignal;
149 }
150
151 bool Timer::IsRunning() const
152 {
153   return mImpl->mId != NULL;
154 }
155
156 } // namespace Adaptor
157
158 } // namespace Internal
159
160 } // namespace Dali
161
162 #pragma GCC diagnostic pop