2 * Copyright (c) 2019 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/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 mAdaptorStarted( false ),
128 SceneHolder::~SceneHolder()
132 mAdaptor->RemoveObserver( *mLifeCycleObserver.get() );
133 mAdaptor->RemoveWindow( this );
135 mAdaptor->DeleteSurface( *mSurface.get() );
146 void SceneHolder::Add( Dali::Actor actor )
154 void SceneHolder::Remove( Dali::Actor actor )
158 mScene.Remove( actor );
162 Dali::Layer SceneHolder::GetRootLayer() const
164 return mScene ? mScene.GetRootLayer() : Dali::Layer();
167 uint32_t SceneHolder::GetId() const
172 std::string SceneHolder::GetName() const
177 bool SceneHolder::IsVisible() const
182 Dali::Integration::Scene SceneHolder::GetScene()
187 void SceneHolder::SetSurface(Dali::RenderSurfaceInterface* surface)
189 mSurface.reset( surface );
191 mScene.SetSurface( *mSurface.get() );
193 unsigned int dpiHorizontal, dpiVertical;
194 dpiHorizontal = dpiVertical = 0;
196 mSurface->GetDpi( dpiHorizontal, dpiVertical );
197 mScene.SetDpi( Vector2( static_cast<float>( dpiHorizontal ), static_cast<float>( dpiVertical ) ) );
199 mSurface->SetAdaptor( *mAdaptor );
201 OnSurfaceSet( surface );
204 void SceneHolder::SurfaceResized()
206 mScene.SurfaceResized();
209 Dali::RenderSurfaceInterface* SceneHolder::GetSurface() const
211 return mSurface.get();
214 void SceneHolder::SetBackgroundColor( const Vector4& color )
218 mScene.SetBackgroundColor( color );
222 Vector4 SceneHolder::GetBackgroundColor() const
224 return mScene ? mScene.GetBackgroundColor() : Color::BLACK;
227 void SceneHolder::SetAdaptor(Dali::Adaptor& adaptor)
229 // Avoid doing this more than once
230 if( mAdaptorStarted )
235 mAdaptorStarted = true;
238 PositionSize positionSize = mSurface->GetPositionSize();
239 mScene = Dali::Integration::Scene::New( Vector2( positionSize.width, positionSize.height ) );
240 mScene.SetSurface( *mSurface.get() );
242 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation( adaptor );
243 mAdaptor = &adaptorImpl;
245 // Create an observer for the adaptor lifecycle
246 mAdaptor->AddObserver( *mLifeCycleObserver );
250 unsigned int dpiHorizontal, dpiVertical;
251 dpiHorizontal = dpiVertical = 0;
253 mSurface->GetDpi( dpiHorizontal, dpiVertical );
254 mScene.SetDpi( Vector2( static_cast<float>( dpiHorizontal ), static_cast<float>( dpiVertical ) ) );
256 mSurface->SetAdaptor( *mAdaptor );
259 OnAdaptorSet( adaptor );
262 void SceneHolder::Pause()
269 void SceneHolder::Resume()
276 void SceneHolder::FeedTouchPoint( Dali::Integration::Point& point, int timeStamp )
280 timeStamp = GetCurrentMilliSeconds();
283 RecalculateTouchPosition( point );
285 Integration::TouchEvent touchEvent;
286 Integration::HoverEvent hoverEvent;
287 Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
288 if( type != Integration::TouchEventCombiner::DispatchNone )
290 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 );
292 // First the touch and/or hover event & related gesture events are queued
293 if( type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth )
295 mScene.QueueEvent( touchEvent );
298 if( type == Integration::TouchEventCombiner::DispatchHover || type == Integration::TouchEventCombiner::DispatchBoth )
300 mScene.QueueEvent( hoverEvent );
303 // Next the events are processed with a single call into Core
304 mAdaptor->ProcessCoreEvents();
308 void SceneHolder::FeedWheelEvent( Dali::Integration::WheelEvent& wheelEvent )
310 mScene.QueueEvent( wheelEvent );
311 mAdaptor->ProcessCoreEvents();
314 void SceneHolder::FeedKeyEvent( Dali::Integration::KeyEvent& keyEvent )
316 Dali::PhysicalKeyboard physicalKeyboard = PhysicalKeyboard::Get();
317 if( physicalKeyboard )
319 if( ! KeyLookup::IsDeviceButton( keyEvent.keyName.c_str() ) )
321 GetImplementation( physicalKeyboard ).KeyReceived( keyEvent.time > 1 );
325 // Create send KeyEvent to Core.
326 mScene.QueueEvent( keyEvent );
327 mAdaptor->ProcessCoreEvents();
330 void SceneHolder::Reset()
334 // Any touch listeners should be told of the interruption.
335 Integration::TouchEvent event;
336 Integration::Point point;
337 point.SetState( PointState::INTERRUPTED );
338 event.AddPoint( point );
340 // First the touch event & related gesture events are queued
341 mScene.QueueEvent( event );
343 // Next the events are processed with a single call into Core
344 mAdaptor->ProcessCoreEvents();