769119912b365213385ae10b94c88efba1c83fb0
[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 // INTERNAL INCLUDES
22 #include <dali/internal/window-system/windows/platform-implement-win.h>
23
24 namespace Dali
25 {
26
27 namespace Internal
28 {
29
30 namespace Adaptor
31 {
32
33 // LOCAL STUFF
34 namespace
35 {
36 bool TimerSourceFunc (void *data)
37 {
38   Timer* timer = static_cast<Timer*>(data);
39   return timer->Tick();
40 }
41 }
42
43 /**
44  * Struct to hide away Windows implementation details
45  */
46 struct Timer::Impl
47 {
48   Impl( unsigned int milliSec ) :
49     mId(-1),
50     mInterval(milliSec)
51   {
52   }
53
54   int mId;
55
56   unsigned int mInterval;
57 };
58
59 TimerPtr Timer::New( unsigned int milliSec )
60 {
61   TimerPtr timer( new Timer( milliSec ) );
62   return timer;
63 }
64
65 Timer::Timer( unsigned int milliSec )
66 : mImpl(new Impl(milliSec))
67 {
68 }
69
70 Timer::~Timer()
71 {
72   // stop timers
73   Stop();
74
75   delete mImpl;
76   mImpl = NULL;
77 }
78
79 void Timer::Start()
80 {
81   if( 0 > mImpl->mId )
82   {
83     mImpl->mId = WindowsPlatformImplementation::SetTimer( mImpl->mInterval, TimerSourceFunc, this );
84   }
85 }
86
87 void Timer::Stop()
88 {
89   if( 0 <= mImpl->mId )
90   {
91     WindowsPlatformImplementation::KillTimer( mImpl->mId );
92     mImpl->mId = -1;
93   }
94 }
95
96 void Timer::Pause()
97 {
98
99 }
100
101 void Timer::Resume()
102 {
103
104 }
105
106 void Timer::SetInterval( unsigned int interval, bool restart )
107 {
108   if( true == restart )
109   {
110     // stop existing timer
111     Stop();
112     mImpl->mInterval = interval;
113     // start new tick
114     Start();
115   }
116   else
117   {
118     mImpl->mInterval = interval;
119   }
120 }
121
122 unsigned int Timer::GetInterval() const
123 {
124   return mImpl->mInterval;
125 }
126
127 bool Timer::Tick()
128 {
129   // Guard against destruction during signal emission
130   Dali::Timer handle( this );
131
132   bool retVal( false );
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 bool Timer::IsRunning() const
164 {
165   return 0 <= mImpl->mId;
166 }
167
168 } // namespace Adaptor
169
170 } // namespace Internal
171
172 } // namespace Dali
173