491c5b4aaaacee6d995e33abcdf83bfac1d11c6e
[platform/core/uifw/dali-adaptor.git] / dali / integration-api / scene-holder-impl.cpp
1 /*
2  * Copyright (c) 2019 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/integration-api/scene-holder-impl.h>
20
21 // EXTERNAL HEADERS
22 #include <sys/time.h>
23 #include <dali/public-api/actors/actor.h>
24 #include <dali/public-api/actors/layer.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/events/key-event-integ.h>
27 #include <dali/integration-api/events/touch-event-integ.h>
28 #include <dali/integration-api/events/hover-event-integ.h>
29 #include <dali/integration-api/events/wheel-event-integ.h>
30
31 // INTERNAL HEADERS
32 #include <dali/internal/adaptor/common/adaptor-impl.h>
33 #include <dali/internal/adaptor/common/lifecycle-observer.h>
34 #include <dali/internal/input/common/key-impl.h>
35 #include <dali/internal/input/common/physical-keyboard-impl.h>
36
37 namespace Dali
38 {
39
40 namespace Internal
41 {
42
43 namespace Adaptor
44 {
45
46 namespace
47 {
48
49 #if defined(DEBUG_ENABLED)
50 Integration::Log::Filter* gTouchEventLogFilter  = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_TOUCH");
51 #endif
52
53 // Copied from x server
54 static uint32_t GetCurrentMilliSeconds(void)
55 {
56   struct timeval tv;
57
58   struct timespec tp;
59   static clockid_t clockid;
60
61   if (!clockid)
62   {
63 #ifdef CLOCK_MONOTONIC_COARSE
64     if (clock_getres(CLOCK_MONOTONIC_COARSE, &tp) == 0 &&
65       (tp.tv_nsec / 1000) <= 1000 && clock_gettime(CLOCK_MONOTONIC_COARSE, &tp) == 0)
66     {
67       clockid = CLOCK_MONOTONIC_COARSE;
68     }
69     else
70 #endif
71     if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
72     {
73       clockid = CLOCK_MONOTONIC;
74     }
75     else
76     {
77       clockid = ~0L;
78     }
79   }
80   if (clockid != ~0L && clock_gettime(clockid, &tp) == 0)
81   {
82     return static_cast<uint32_t>( (tp.tv_sec * 1000 ) + (tp.tv_nsec / 1000000L) );
83   }
84
85   gettimeofday(&tv, NULL);
86   return static_cast<uint32_t>( (tv.tv_sec * 1000 ) + (tv.tv_usec / 1000) );
87 }
88
89 } // unnamed namespace
90
91 uint32_t SceneHolder::mSceneHolderCounter = 0;
92
93 class SceneHolder::SceneHolderLifeCycleObserver : public LifeCycleObserver
94 {
95 public:
96
97   SceneHolderLifeCycleObserver(Adaptor*& adaptor)
98   : mAdaptor( adaptor )
99   {
100   };
101
102 private: // Adaptor::LifeCycleObserver interface
103
104   void OnStart() override {};
105   void OnPause() override {};
106   void OnResume() override {};
107   void OnStop() override {};
108   void OnDestroy() override
109   {
110     mAdaptor = nullptr;
111   };
112
113 private:
114   Adaptor*& mAdaptor;
115 };
116
117
118 SceneHolder::SceneHolder()
119 : mLifeCycleObserver( new SceneHolderLifeCycleObserver( mAdaptor ) ),
120   mId( mSceneHolderCounter++ ),
121   mSurface( nullptr ),
122   mAdaptor( nullptr ),
123   mAdaptorStarted( false ),
124   mVisible( true )
125 {
126 }
127
128 SceneHolder::~SceneHolder()
129 {
130   if ( mAdaptor )
131   {
132     mAdaptor->RemoveObserver( *mLifeCycleObserver.get() );
133     mAdaptor->RemoveWindow( this );
134
135     mAdaptor->DeleteSurface( *mSurface.get() );
136
137     mAdaptor = nullptr;
138   }
139
140   if ( mScene )
141   {
142     mScene.Discard();
143   }
144 }
145
146 void SceneHolder::Add( Dali::Actor actor )
147 {
148   if ( mScene )
149   {
150     mScene.Add( actor );
151   }
152 }
153
154 void SceneHolder::Remove( Dali::Actor actor )
155 {
156   if ( mScene )
157   {
158     mScene.Remove( actor );
159   }
160 }
161
162 Dali::Layer SceneHolder::GetRootLayer() const
163 {
164   return mScene ? mScene.GetRootLayer() : Dali::Layer();
165 }
166
167 uint32_t SceneHolder::GetId() const
168 {
169   return mId;
170 }
171
172 std::string SceneHolder::GetName() const
173 {
174   return mName;
175 }
176
177 bool SceneHolder::IsVisible() const
178 {
179   return mVisible;
180 }
181
182 Dali::Integration::Scene SceneHolder::GetScene()
183 {
184   return mScene;
185 }
186
187 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
188 {
189   mSurface.reset( surface );
190
191   mScene.SetSurface( *mSurface.get() );
192
193   unsigned int dpiHorizontal, dpiVertical;
194   dpiHorizontal = dpiVertical = 0;
195
196   mSurface->GetDpi( dpiHorizontal, dpiVertical );
197   mScene.SetDpi( Vector2( static_cast<float>( dpiHorizontal ), static_cast<float>( dpiVertical ) ) );
198
199   mSurface->SetAdaptor( *mAdaptor );
200
201   OnSurfaceSet( surface );
202 }
203
204 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
205 {
206   return mSurface.get();
207 }
208
209 void SceneHolder::SetBackgroundColor( const Vector4& color )
210 {
211   if( mScene )
212   {
213     mScene.SetBackgroundColor( color );
214   }
215 }
216
217 Vector4 SceneHolder::GetBackgroundColor() const
218 {
219   return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
220 }
221
222 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
223 {
224   // Avoid doing this more than once
225   if( mAdaptorStarted )
226   {
227     return;
228   }
229
230   mAdaptorStarted = true;
231
232   // Create the scene
233   PositionSize positionSize = mSurface->GetPositionSize();
234   mScene = Dali::Integration::Scene::New( Vector2( positionSize.width, positionSize.height ) );
235   mScene.SetSurface( *mSurface.get() );
236
237   Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation( adaptor );
238   mAdaptor = &adaptorImpl;
239
240   // Create an observer for the adaptor lifecycle
241   mAdaptor->AddObserver( *mLifeCycleObserver );
242
243   if ( mSurface )
244   {
245     unsigned int dpiHorizontal, dpiVertical;
246     dpiHorizontal = dpiVertical = 0;
247
248     mSurface->GetDpi( dpiHorizontal, dpiVertical );
249     mScene.SetDpi( Vector2( static_cast<float>( dpiHorizontal ), static_cast<float>( dpiVertical ) ) );
250
251     mSurface->SetAdaptor( *mAdaptor );
252   }
253
254   OnAdaptorSet( adaptor );
255 }
256
257 void SceneHolder::Pause()
258 {
259   Reset();
260
261   OnPause();
262 }
263
264 void SceneHolder::Resume()
265 {
266   Reset();
267
268   OnResume();
269 }
270
271 void SceneHolder::FeedTouchPoint( Dali::Integration::Point& point, int timeStamp )
272 {
273   if( timeStamp < 1 )
274   {
275     timeStamp = GetCurrentMilliSeconds();
276   }
277
278   RecalculateTouchPosition( point );
279
280   Integration::TouchEvent touchEvent;
281   Integration::HoverEvent hoverEvent;
282   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
283   if( type != Integration::TouchEventCombiner::DispatchNone )
284   {
285     DALI_LOG_INFO( gTouchEventLogFilter, Debug::General, "%d: Device %d: Button state %d (%.2f, %.2f)\n", timeStamp, point.GetDeviceId(), point.GetState(), point.GetScreenPosition().x, point.GetScreenPosition().y );
286
287     // First the touch and/or hover event & related gesture events are queued
288     if( type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth )
289     {
290       mScene.QueueEvent( touchEvent );
291     }
292
293     if( type == Integration::TouchEventCombiner::DispatchHover || type == Integration::TouchEventCombiner::DispatchBoth )
294     {
295       mScene.QueueEvent( hoverEvent );
296     }
297
298     // Next the events are processed with a single call into Core
299     mAdaptor->ProcessCoreEvents();
300   }
301 }
302
303 void SceneHolder::FeedWheelEvent( Dali::Integration::WheelEvent& wheelEvent )
304 {
305   mScene.QueueEvent( wheelEvent );
306   mAdaptor->ProcessCoreEvents();
307 }
308
309 void SceneHolder::FeedKeyEvent( Dali::Integration::KeyEvent& keyEvent )
310 {
311   Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
312   if( physicalKeyboard )
313   {
314     if( ! KeyLookup::IsDeviceButton( keyEvent.keyName.c_str() ) )
315     {
316       GetImplementation( physicalKeyboard ).KeyReceived( keyEvent.time > 1 );
317     }
318   }
319
320   // Create send KeyEvent to Core.
321   mScene.QueueEvent( keyEvent );
322   mAdaptor->ProcessCoreEvents();
323 }
324
325 void SceneHolder::Reset()
326 {
327   mCombiner.Reset();
328
329   // Any touch listeners should be told of the interruption.
330   Integration::TouchEvent event;
331   Integration::Point point;
332   point.SetState( PointState::INTERRUPTED );
333   event.AddPoint( point );
334
335   // First the touch event & related gesture events are queued
336   mScene.QueueEvent( event );
337
338   // Next the events are processed with a single call into Core
339   mAdaptor->ProcessCoreEvents();
340 }
341
342
343 }// Adaptor
344
345 }// Internal
346
347 } // Dali