2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/integration-api/adaptor-framework/scene-holder-impl.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>
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>
49 #if defined(DEBUG_ENABLED)
50 Integration::Log::Filter* gTouchEventLogFilter = Integration::Log::Filter::New(Debug::NoLogging, false, "LOG_ADAPTOR_EVENTS_TOUCH");
53 // Copied from x server
54 static uint32_t GetCurrentMilliSeconds(void)
59 static clockid_t clockid;
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)
67 clockid = CLOCK_MONOTONIC_COARSE;
71 if (clock_gettime(CLOCK_MONOTONIC, &tp) == 0)
73 clockid = CLOCK_MONOTONIC;
80 if (clockid != ~0L && clock_gettime(clockid, &tp) == 0)
82 return static_cast<uint32_t>( (tp.tv_sec * 1000 ) + (tp.tv_nsec / 1000000L) );
85 gettimeofday(&tv, NULL);
86 return static_cast<uint32_t>( (tv.tv_sec * 1000 ) + (tv.tv_usec / 1000) );
89 } // unnamed namespace
91 uint32_t SceneHolder::mSceneHolderCounter = 0;
93 class SceneHolder::SceneHolderLifeCycleObserver : public LifeCycleObserver
97 SceneHolderLifeCycleObserver(Adaptor*& adaptor)
102 private: // Adaptor::LifeCycleObserver interface
104 void OnStart() override {};
105 void OnPause() override {};
106 void OnResume() override {};
107 void OnStop() override {};
108 void OnDestroy() override
118 SceneHolder::SceneHolder()
119 : mLifeCycleObserver( new SceneHolderLifeCycleObserver( mAdaptor ) ),
120 mId( mSceneHolderCounter++ ),
123 mIsBeingDeleted( false ),
124 mAdaptorStarted( false ),
129 SceneHolder::~SceneHolder()
133 mAdaptor->RemoveObserver( *mLifeCycleObserver.get() );
134 mAdaptor->RemoveWindow( this );
136 mAdaptor->DeleteSurface( *mSurface.get() );
147 void SceneHolder::Add( Dali::Actor actor )
155 void SceneHolder::Remove( Dali::Actor actor )
159 mScene.Remove( actor );
163 Dali::Layer SceneHolder::GetRootLayer() const
165 return mScene ? mScene.GetRootLayer() : Dali::Layer();
168 uint32_t SceneHolder::GetId() const
173 std::string SceneHolder::GetName() const
178 bool SceneHolder::IsVisible() const
183 Dali::Integration::Scene SceneHolder::GetScene()
188 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
190 mSurface.reset( surface );
192 mScene.SurfaceReplaced();
196 unsigned int dpiHorizontal, dpiVertical;
197 dpiHorizontal = dpiVertical = 0;
199 mSurface->GetDpi( dpiHorizontal, dpiVertical );
200 mScene.SetDpi( Vector2( static_cast<float>( dpiHorizontal ), static_cast<float>( dpiVertical ) ) );
202 mSurface->SetAdaptor( *mAdaptor );
204 OnSurfaceSet( surface );
207 void SceneHolder::SurfaceResized()
209 PositionSize surfacePositionSize = mSurface->GetPositionSize();
210 mScene.SurfaceResized( static_cast<float>( surfacePositionSize.width ), static_cast<float>( surfacePositionSize.height ) );
213 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
215 return mSurface.get();
218 void SceneHolder::SetBackgroundColor( const Vector4& color )
222 mScene.SetBackgroundColor( color );
226 Vector4 SceneHolder::GetBackgroundColor() const
228 return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
231 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
233 // Avoid doing this more than once
234 if( mAdaptorStarted )
239 mAdaptorStarted = true;
242 PositionSize surfacePositionSize = mSurface->GetPositionSize();
243 mScene = Dali::Integration::Scene::New( Size(static_cast<float>( surfacePositionSize.width ), static_cast<float>( surfacePositionSize.height )) );
245 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation( adaptor );
246 mAdaptor = &adaptorImpl;
248 // Create an observer for the adaptor lifecycle
249 mAdaptor->AddObserver( *mLifeCycleObserver );
253 unsigned int dpiHorizontal, dpiVertical;
254 dpiHorizontal = dpiVertical = 0;
256 mSurface->GetDpi( dpiHorizontal, dpiVertical );
257 mScene.SetDpi( Vector2( static_cast<float>( dpiHorizontal ), static_cast<float>( dpiVertical ) ) );
259 mSurface->SetAdaptor( *mAdaptor );
262 OnAdaptorSet( adaptor );
265 void SceneHolder::Pause()
272 void SceneHolder::Resume()
279 void SceneHolder::FeedTouchPoint( Dali::Integration::Point& point, int timeStamp )
283 timeStamp = GetCurrentMilliSeconds();
286 RecalculateTouchPosition( point );
288 Integration::TouchEvent touchEvent;
289 Integration::HoverEvent hoverEvent;
290 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
291 if( type != Integration::TouchEventCombiner::DispatchNone )
293 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 );
295 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
296 // Keep the handle alive until the core events are processed.
297 Dali::BaseHandle sceneHolder( this );
299 // First the touch and/or hover event & related gesture events are queued
300 if( type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth )
302 mScene.QueueEvent( touchEvent );
305 if( type == Integration::TouchEventCombiner::DispatchHover || type == Integration::TouchEventCombiner::DispatchBoth )
307 mScene.QueueEvent( hoverEvent );
310 // Next the events are processed with a single call into Core
311 mAdaptor->ProcessCoreEvents();
315 void SceneHolder::FeedWheelEvent( Dali::Integration::WheelEvent& wheelEvent )
317 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
318 // Keep the handle alive until the core events are processed.
319 Dali::BaseHandle sceneHolder( this );
321 mScene.QueueEvent( wheelEvent );
322 mAdaptor->ProcessCoreEvents();
325 void SceneHolder::FeedKeyEvent( Dali::Integration::KeyEvent& keyEvent )
327 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
328 if( physicalKeyboard )
330 if( ! KeyLookup::IsDeviceButton( keyEvent.keyName.c_str() ) )
332 GetImplementation( physicalKeyboard ).KeyReceived( keyEvent.time > 1 );
336 // Signals can be emitted while processing core events, and the scene holder could be deleted in the signal callback.
337 // Keep the handle alive until the core events are processed.
338 Dali::BaseHandle sceneHolder( this );
340 // Create send KeyEvent to Core.
341 mScene.QueueEvent( keyEvent );
342 mAdaptor->ProcessCoreEvents();
345 Dali::Integration::SceneHolder SceneHolder::Get( Dali::Actor actor )
347 SceneHolder* sceneHolderImpl = nullptr;
349 if ( Internal::Adaptor::Adaptor::IsAvailable() )
351 Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation( Internal::Adaptor::Adaptor::Get() );
352 sceneHolderImpl = adaptor.GetWindow( actor );
355 return Dali::Integration::SceneHolder( sceneHolderImpl );
358 void SceneHolder::Reset()
362 // Any touch listeners should be told of the interruption.
363 Integration::TouchEvent event;
364 Integration::Point point;
365 point.SetState( PointState::INTERRUPTED );
366 event.AddPoint( point );
368 // First the touch event & related gesture events are queued
369 mScene.QueueEvent( event );
371 // Next the events are processed with a single call into Core
372 mAdaptor->ProcessCoreEvents();