Merge branch 'devel/master' into tizen
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / windows / timer-impl-win.cpp
1 /*
2  * Copyright (c) 2023 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/windows/timer-impl-win.h>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/window-system/windows/platform-implement-win.h>
23
24 namespace Dali
25 {
26 namespace Internal
27 {
28 namespace Adaptor
29 {
30 // LOCAL STUFF
31 namespace
32 {
33 bool TimerSourceFunc(void* data)
34 {
35   TimerWin* timer = static_cast<TimerWin*>(data);
36   return timer->Tick();
37 }
38 } // namespace
39
40 /**
41  * Struct to hide away Windows implementation details
42  */
43 struct TimerWin::Impl
44 {
45   Impl(uint32_t milliSec)
46   : mId(-1),
47     mInterval(milliSec)
48   {
49   }
50
51   intptr_t mId;
52
53   uint32_t mInterval;
54 };
55
56 TimerWinPtr TimerWin::New(uint32_t milliSec)
57 {
58   TimerWinPtr timer(new TimerWin(milliSec));
59   return timer;
60 }
61
62 TimerWin::TimerWin(uint32_t milliSec)
63 : mImpl(new Impl(milliSec))
64 {
65 }
66
67 TimerWin::~TimerWin()
68 {
69   // stop timers
70   Stop();
71
72   delete mImpl;
73   mImpl = NULL;
74 }
75
76 void TimerWin::Start()
77 {
78   if(0 > mImpl->mId)
79   {
80     mImpl->mId = WindowsPlatform::SetTimer(mImpl->mInterval, TimerSourceFunc, this);
81   }
82 }
83
84 void TimerWin::Stop()
85 {
86   if(0 <= mImpl->mId)
87   {
88     WindowsPlatform::KillTimer(mImpl->mId);
89     mImpl->mId = -1;
90   }
91 }
92
93 void TimerWin::Pause()
94 {
95 }
96
97 void TimerWin::Resume()
98 {
99 }
100
101 void TimerWin::SetInterval(uint32_t interval, bool restart)
102 {
103   if(true == restart)
104   {
105     // stop existing timer
106     Stop();
107     mImpl->mInterval = interval;
108     // start new tick
109     Start();
110   }
111   else
112   {
113     mImpl->mInterval = interval;
114   }
115 }
116
117 uint32_t TimerWin::GetInterval() const
118 {
119   return mImpl->mInterval;
120 }
121
122 bool TimerWin::Tick()
123 {
124   // Guard against destruction during signal emission
125   Dali::Timer handle(this);
126
127   bool retVal(false);
128
129   // Override with new signal if used
130   if(!mTickSignal.Empty())
131   {
132     retVal = mTickSignal.Emit();
133
134     // Timer stops if return value is false
135     if(retVal == false)
136     {
137       Stop();
138     }
139     else
140     {
141       retVal = true; // continue emission
142     }
143   }
144   else // no callbacks registered
145   {
146     // periodic timer is started but nobody listens, continue
147     retVal = true;
148   }
149
150   return retVal;
151 }
152
153 bool TimerWin::IsRunning() const
154 {
155   return 0 <= mImpl->mId;
156 }
157
158 } // namespace Adaptor
159
160 } // namespace Internal
161
162 } // namespace Dali