[Tizen] Add codes for Dali Windows Backend
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / windows / timer-impl-win.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 // Ecore is littered with C style cast
22 #pragma GCC diagnostic push
23 #pragma GCC diagnostic ignored "-Wold-style-cast"
24
25 // INTERNAL INCLUDES
26 #include <dali/internal/window-system/windows/platform-implement-win.h>
27
28 namespace Dali
29 {
30
31 namespace Internal
32 {
33
34 namespace Adaptor
35 {
36
37 // LOCAL STUFF
38 namespace
39 {
40 void TimerSourceFunc (void *data)
41 {
42   Timer* timer = static_cast<Timer*>(data);
43
44   bool keepRunning = timer->Tick();
45 }
46 }
47
48 /**
49  * Struct to hide away Ecore implementation details
50  */
51 struct Timer::Impl
52 {
53   Impl( unsigned int milliSec ) :
54     mId(-1),
55     mInterval(milliSec)
56   {
57   }
58
59   int mId;
60
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   mImpl = NULL;
82 }
83
84 void Timer::Start()
85 {
86   if( 0 > mImpl->mId )
87   {
88     mImpl->mId = WindowsPlatformImplement::SetTimer( mImpl->mInterval, TimerSourceFunc, this );
89   }
90 }
91
92 void Timer::Stop()
93 {
94   if( 0 <= mImpl->mId )
95   {
96     WindowsPlatformImplement::KillTimer( mImpl->mId );
97     mImpl->mId = -1;
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 0 <= mImpl->mId;
154 }
155
156 } // namespace Adaptor
157
158 } // namespace Internal
159
160 } // namespace Dali
161
162 #pragma GCC diagnostic pop