6f78e7d4706c8395febf86bdcba38f47fc45a10c
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / internal / common / timer-impl.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include "timer-impl.h"
19
20 // EXTERNAL INCLUDES
21 #include <Ecore.h>
22
23 namespace Dali
24 {
25
26 namespace Internal
27 {
28
29 namespace Adaptor
30 {
31
32 // LOCAL STUFF
33 namespace
34 {
35 Eina_Bool TimerSourceFunc (void *data)
36 {
37   Timer* timer = static_cast<Timer*>(data);
38
39   bool keepRunning = timer->Tick();
40
41   return keepRunning ? EINA_TRUE : EINA_FALSE;
42 }
43 } // unnamed namespace
44
45 /**
46  * Struct to hide away Ecore implementation details
47  */
48 struct Timer::Impl
49 {
50   Impl( unsigned int milliSec )
51   : mId(NULL),
52     mInterval(milliSec)
53   {
54   }
55
56   Ecore_Timer * mId;
57   unsigned int mInterval;
58 };
59
60 TimerPtr Timer::New( unsigned int milliSec )
61 {
62   TimerPtr timer( new Timer( milliSec ) );
63   return timer;
64 }
65
66 Timer::Timer( unsigned int milliSec )
67 : mImpl(new Impl(milliSec))
68 {
69 }
70
71 Timer::~Timer()
72 {
73   // stop timers
74   Stop();
75
76   delete mImpl;
77 }
78
79 void Timer::Start()
80 {
81   if(mImpl->mId > 0)
82   {
83     Stop();
84   }
85   mImpl->mId = ecore_timer_add( (double)mImpl->mInterval/1000.0f, (Ecore_Task_Cb)TimerSourceFunc, this );
86 }
87
88 void Timer::Stop()
89 {
90   if (mImpl->mId != NULL)
91   {
92     ecore_timer_del(mImpl->mId);
93     mImpl->mId = NULL;
94   }
95 }
96
97 void Timer::SetInterval( unsigned int interval )
98 {
99   // stop existing timer
100   Stop();
101   mImpl->mInterval = interval;
102   // start new tick
103   Start();
104 }
105
106 unsigned int Timer::GetInterval() const
107 {
108   return mImpl->mInterval;
109 }
110
111 bool Timer::Tick()
112 {
113   // Guard against destruction during signal emission
114   Dali::Timer handle( this );
115
116   bool retVal( false );
117
118   // Override with new signal if used
119   if( !mTickSignal.Empty() )
120   {
121     retVal = mTickSignal.Emit();
122
123     // Timer stops if return value is false
124     if (retVal == false)
125     {
126       Stop();
127     }
128     else
129     {
130       retVal = true;   // continue emission
131     }
132   }
133   else // no callbacks registered
134   {
135     // periodic timer is started but nobody listens, continue
136     retVal = true;
137   }
138
139   return retVal;
140 }
141
142 Dali::Timer::TimerSignalV2& Timer::TickSignal()
143 {
144   return mTickSignal;
145 }
146
147 bool Timer::IsRunning() const
148 {
149   return mImpl->mId != NULL;
150 }
151
152 } // namespace Adaptor
153
154 } // namespace Internal
155
156 } // namespace Dali