2 * Copyright (c) 2017 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 <window-impl.h>
23 #include <Ecore_Wayland.h>
25 #include <dali/integration-api/core.h>
26 #include <dali/integration-api/system-overlay.h>
27 #include <dali/public-api/render-tasks/render-task.h>
28 #include <dali/public-api/render-tasks/render-task-list.h>
29 #include <orientation.h>
32 #include <window-render-surface.h>
33 #include <drag-and-drop-detector-impl.h>
34 #include <ecore-indicator-impl.h>
35 #include <window-visibility-observer.h>
36 #include <orientation-impl.h>
40 const float INDICATOR_ANIMATION_DURATION( 0.18f ); // 180 milli seconds
41 const float INDICATOR_SHOW_Y_POSITION( 0.0f );
42 const float INDICATOR_HIDE_Y_POSITION( -52.0f );
51 #if defined(DEBUG_ENABLED)
52 Debug::Filter* gWindowLogFilter = Debug::Filter::New(Debug::Concise, false, "LOG_WINDOW");
56 * TODO: Abstract Window class out and move this into a window implementation for Ecore
58 struct Window::EventHandler
62 * @param[in] window A pointer to the window class.
64 EventHandler( Window* window )
69 // store ecore window handle
70 ECore::WindowRenderSurface* wlWindow( dynamic_cast< ECore::WindowRenderSurface * >( mWindow->mSurface ) );
73 mEcoreWindow = wlWindow->GetWlWindow();
75 DALI_ASSERT_ALWAYS( mEcoreWindow != 0 && "There is no ecore Wl window");
77 #ifndef DALI_PROFILE_UBUNTU
78 if( mWindow->mEcoreEventHander )
80 mEcoreEventHandler.PushBack( ecore_event_handler_add( ECORE_WL_EVENT_WINDOW_ICONIFY_STATE_CHANGE, EcoreEventWindowIconifyStateChanged, this ) );
81 mEcoreEventHandler.PushBack( ecore_event_handler_add( ECORE_WL_EVENT_OUTPUT_TRANSFORM, EcoreEventOutputTransform, this) );
82 mEcoreEventHandler.PushBack( ecore_event_handler_add( ECORE_WL_EVENT_IGNORE_OUTPUT_TRANSFORM, EcoreEventIgnoreOutputTransform, this) );
93 for( Dali::Vector< Ecore_Event_Handler* >::Iterator iter = mEcoreEventHandler.Begin(), endIter = mEcoreEventHandler.End(); iter != endIter; ++iter )
95 ecore_event_handler_del( *iter );
97 mEcoreEventHandler.Clear();
102 #ifndef DALI_PROFILE_UBUNTU
103 /// Called when the window iconify state is changed.
104 static Eina_Bool EcoreEventWindowIconifyStateChanged( void* data, int type, void* event )
106 Ecore_Wl_Event_Window_Iconify_State_Change* iconifyChangedEvent( static_cast< Ecore_Wl_Event_Window_Iconify_State_Change* >( event ) );
107 EventHandler* handler( static_cast< EventHandler* >( data ) );
108 Eina_Bool handled( ECORE_CALLBACK_PASS_ON );
110 if ( handler && handler->mWindow )
112 WindowVisibilityObserver* observer( handler->mWindow->mAdaptor );
113 if ( observer && ( iconifyChangedEvent->win == static_cast< unsigned int> ( ecore_wl_window_id_get( handler->mEcoreWindow ) ) ) )
115 if( iconifyChangedEvent->iconified == EINA_TRUE )
117 observer->OnWindowHidden();
118 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Iconfied\n", handler->mEcoreWindow );
122 observer->OnWindowShown();
123 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Shown\n", handler->mEcoreWindow );
125 handled = ECORE_CALLBACK_DONE;
132 /// Called when the output is transformed
133 static Eina_Bool EcoreEventOutputTransform( void* data, int type, void* event )
135 Ecore_Wl_Event_Output_Transform* transformEvent( static_cast< Ecore_Wl_Event_Output_Transform* >( event ) );
136 EventHandler* handler( static_cast< EventHandler* >( data ) );
138 if ( handler && handler->mWindow && transformEvent->output == ecore_wl_window_output_find( handler->mEcoreWindow ) )
140 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) EcoreEventOutputTransform\n", handler->mEcoreWindow );
142 ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( handler->mWindow->mSurface ) );
145 wlSurface->OutputTransformed();
147 PositionSize positionSize = wlSurface->GetPositionSize();
148 handler->mWindow->mAdaptor->SurfaceResizePrepare( Adaptor::SurfaceSize( positionSize.width, positionSize.height ) );
149 handler->mWindow->mAdaptor->SurfaceResizeComplete( Adaptor::SurfaceSize( positionSize.width, positionSize.height ) );
153 return ECORE_CALLBACK_PASS_ON;
156 /// Called when the output transform should be ignored
157 static Eina_Bool EcoreEventIgnoreOutputTransform( void* data, int type, void* event )
159 Ecore_Wl_Event_Ignore_Output_Transform* ignoreTransformEvent( static_cast< Ecore_Wl_Event_Ignore_Output_Transform* >( event ) );
160 EventHandler* handler( static_cast< EventHandler* >( data ) );
162 if ( handler && handler->mWindow && ignoreTransformEvent->win == handler->mEcoreWindow )
164 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) EcoreEventIgnoreOutputTransform\n", handler->mEcoreWindow );
166 ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( handler->mWindow->mSurface ) );
169 wlSurface->OutputTransformed();
171 PositionSize positionSize = wlSurface->GetPositionSize();
172 handler->mWindow->mAdaptor->SurfaceResizePrepare( Adaptor::SurfaceSize( positionSize.width, positionSize.height ) );
173 handler->mWindow->mAdaptor->SurfaceResizeComplete( Adaptor::SurfaceSize( positionSize.width, positionSize.height ) );
177 return ECORE_CALLBACK_PASS_ON;
183 Dali::Vector< Ecore_Event_Handler* > mEcoreEventHandler;
184 Ecore_Wl_Window* mEcoreWindow;
188 Window* Window::New( const PositionSize& positionSize, const std::string& name, const std::string& className, bool isTransparent )
190 Window* window = new Window();
191 window->mIsTransparent = isTransparent;
192 window->Initialize( positionSize, name, className );
196 void Window::SetAdaptor(Dali::Adaptor& adaptor)
198 DALI_ASSERT_ALWAYS( !mStarted && "Adaptor already started" );
201 // Only create one overlay per window
202 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
203 Integration::Core& core = adaptorImpl.GetCore();
204 mOverlay = &core.GetSystemOverlay();
206 Dali::RenderTaskList taskList = mOverlay->GetOverlayRenderTasks();
207 taskList.CreateTask();
209 mAdaptor = &adaptorImpl;
210 mAdaptor->AddObserver( *this );
212 // Can only create the detector when we know the Core has been instantiated.
213 mDragAndDropDetector = DragAndDropDetector::New();
214 mAdaptor->SetDragAndDropDetector( &GetImplementation( mDragAndDropDetector ) );
218 mOrientation->SetAdaptor(adaptor);
221 if( mIndicator != NULL )
223 mIndicator->SetAdaptor(mAdaptor);
227 RenderSurface* Window::GetSurface()
232 void Window::ShowIndicator( Dali::Window::IndicatorVisibleMode visibleMode )
234 DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "visible : %d\n", visibleMode );
235 DALI_ASSERT_DEBUG(mOverlay);
237 ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
238 DALI_ASSERT_DEBUG(wlSurface);
240 Ecore_Wl_Window* wlWindow = NULL;
243 wlWindow = wlSurface->GetWlWindow();
246 mIndicatorVisible = visibleMode;
248 if ( mIndicatorVisible == Dali::Window::VISIBLE )
250 // when the indicator is visible, set proper mode for indicator server according to bg mode
251 if ( mIndicatorOpacityMode == Dali::Window::OPAQUE )
253 ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_OPAQUE);
255 else if ( mIndicatorOpacityMode == Dali::Window::TRANSLUCENT )
257 ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_TRANSLUCENT);
259 else if ( mIndicatorOpacityMode == Dali::Window::TRANSPARENT )
261 ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_OPAQUE);
266 // when the indicator is not visible, set TRANSPARENT mode for indicator server
267 ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_TRANSPARENT); // it means hidden indicator
270 DoShowIndicator( mIndicatorOrientation );
273 void Window::RotateIndicator(Dali::Window::WindowOrientation orientation)
275 DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "Orientation: %d\n", orientation );
277 DoRotateIndicator( orientation );
280 void Window::SetIndicatorBgOpacity( Dali::Window::IndicatorBgOpacity opacityMode )
282 mIndicatorOpacityMode = opacityMode;
284 if( mIndicator != NULL )
286 mIndicator->SetOpacityMode( opacityMode );
290 void Window::SetClass(std::string name, std::string klass)
292 ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
296 Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
297 ecore_wl_window_title_set( wlWindow, name.c_str() );
298 ecore_wl_window_class_name_set( wlWindow, klass.c_str() );
302 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window has no surface\n" );
308 mIndicatorVisible( Dali::Window::VISIBLE ),
309 mIndicatorIsShown( false ),
310 mShowRotatedIndicatorOnClose( false ),
312 mIsTransparent( false ),
313 mWMRotationAppSet( false ),
314 mEcoreEventHander( true ),
315 mResizeEnabled( false ),
317 mIndicatorOrientation( Dali::Window::PORTRAIT ),
318 mNextIndicatorOrientation( Dali::Window::PORTRAIT ),
319 mIndicatorOpacityMode( Dali::Window::OPAQUE ),
322 mEventHandler( NULL ),
323 mPreferredOrientation( Dali::Window::PORTRAIT ),
324 mSupportedAuxiliaryHints(),
331 delete mEventHandler;
341 mAdaptor->RemoveObserver( *this );
342 mAdaptor->SetDragAndDropDetector( NULL );
348 mSupportedAuxiliaryHints.clear();
349 mAuxiliaryHints.clear();
352 void Window::Initialize(const PositionSize& positionSize, const std::string& name, const std::string& className)
354 // create an Wayland window by default
356 ECore::WindowRenderSurface* windowSurface = new ECore::WindowRenderSurface( positionSize, surface, name, mIsTransparent );
358 mSurface = windowSurface;
360 // create event handler for Wayland window
361 mEventHandler = new EventHandler( this );
363 // get auxiliary hint
364 Eina_List* hints = ecore_wl_window_aux_hints_supported_get( mEventHandler->mEcoreWindow );
370 for( l = hints, ( hint = static_cast< char* >( eina_list_data_get(l) ) ); l; l = eina_list_next(l), ( hint = static_cast< char* >( eina_list_data_get(l) ) ) )
372 mSupportedAuxiliaryHints.push_back( hint );
374 DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::Initialize: %s\n", hint );
378 if( !positionSize.IsEmpty() )
380 AddAuxiliaryHint( "wm.policy.win.user.geometry", "1" );
381 mResizeEnabled = true;
384 SetClass( name, className );
385 windowSurface->Map();
387 mOrientation = Orientation::New(this);
390 void Window::DoShowIndicator( Dali::Window::WindowOrientation lastOrientation )
392 if( mIndicator == NULL )
394 if( mIndicatorVisible != Dali::Window::INVISIBLE )
396 mIndicator = new Indicator( mAdaptor, mIndicatorOrientation, this );
397 mIndicator->SetOpacityMode( mIndicatorOpacityMode );
398 Dali::Actor actor = mIndicator->GetActor();
399 SetIndicatorActorRotation();
400 mOverlay->Add(actor);
402 // else don't create a hidden indicator
404 else // Already have indicator
406 if( mIndicatorVisible == Dali::Window::VISIBLE )
408 // If we are resuming, and rotation has changed,
409 if( mIndicatorIsShown == false && mIndicatorOrientation != mNextIndicatorOrientation )
411 // then close current indicator and open new one
412 mShowRotatedIndicatorOnClose = true;
413 mIndicator->Close(); // May synchronously call IndicatorClosed() callback & 1 level of recursion
414 // Don't show actor - will contain indicator for old orientation.
419 // set indicator visible mode
420 if( mIndicator != NULL )
422 mIndicator->SetVisible( mIndicatorVisible );
425 bool show = (mIndicatorVisible != Dali::Window::INVISIBLE );
426 SetIndicatorProperties( show, lastOrientation );
427 mIndicatorIsShown = show;
430 void Window::DoRotateIndicator( Dali::Window::WindowOrientation orientation )
432 if( mIndicatorIsShown )
434 mShowRotatedIndicatorOnClose = true;
435 mNextIndicatorOrientation = orientation;
436 mIndicator->Close(); // May synchronously call IndicatorClosed() callback
440 // Save orientation for when the indicator is next shown
441 mShowRotatedIndicatorOnClose = false;
442 mNextIndicatorOrientation = orientation;
446 void Window::SetIndicatorProperties( bool isShow, Dali::Window::WindowOrientation lastOrientation )
448 ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
452 Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
455 ecore_wl_window_indicator_state_set(wlWindow, ECORE_WL_INDICATOR_STATE_ON);
459 ecore_wl_window_indicator_state_set(wlWindow, ECORE_WL_INDICATOR_STATE_OFF);
464 void Window::IndicatorTypeChanged(Indicator::Type type)
466 #if defined(DALI_PROFILE_MOBILE)
467 ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
471 Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
474 case Indicator::INDICATOR_TYPE_1:
475 ecore_wl_indicator_visible_type_set(wlWindow, ECORE_WL_INDICATOR_VISIBLE_TYPE_SHOWN);
478 case Indicator::INDICATOR_TYPE_2:
479 ecore_wl_indicator_visible_type_set(wlWindow, ECORE_WL_INDICATOR_VISIBLE_TYPE_HIDDEN);
482 case Indicator::INDICATOR_TYPE_UNKNOWN:
490 void Window::IndicatorClosed( IndicatorInterface* indicator )
492 DALI_LOG_TRACE_METHOD( gWindowLogFilter );
494 if( mShowRotatedIndicatorOnClose )
496 Dali::Window::WindowOrientation currentOrientation = mIndicatorOrientation;
497 mIndicator->Open(mNextIndicatorOrientation);
498 mIndicatorOrientation = mNextIndicatorOrientation;
499 SetIndicatorActorRotation();
500 DoShowIndicator(currentOrientation);
504 void Window::IndicatorVisibilityChanged(bool isVisible)
506 mIndicatorVisibilityChangedSignal.Emit(isVisible);
509 void Window::SetIndicatorActorRotation()
511 DALI_LOG_TRACE_METHOD( gWindowLogFilter );
512 DALI_ASSERT_DEBUG( mIndicator != NULL );
514 Dali::Actor actor = mIndicator->GetActor();
515 switch( mIndicatorOrientation )
517 case Dali::Window::PORTRAIT:
518 actor.SetParentOrigin( ParentOrigin::TOP_CENTER );
519 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
520 actor.SetOrientation( Degree(0), Vector3::ZAXIS );
522 case Dali::Window::PORTRAIT_INVERSE:
523 actor.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
524 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
525 actor.SetOrientation( Degree(180), Vector3::ZAXIS );
527 case Dali::Window::LANDSCAPE:
528 actor.SetParentOrigin( ParentOrigin::CENTER_LEFT );
529 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
530 actor.SetOrientation( Degree(270), Vector3::ZAXIS );
532 case Dali::Window::LANDSCAPE_INVERSE:
533 actor.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
534 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
535 actor.SetOrientation( Degree(90), Vector3::ZAXIS );
542 ecore_wl_window_raise( mEventHandler->mEcoreWindow );
547 ecore_wl_window_lower( mEventHandler->mEcoreWindow );
550 void Window::Activate()
552 ecore_wl_window_activate( mEventHandler->mEcoreWindow );
555 Dali::DragAndDropDetector Window::GetDragAndDropDetector() const
557 return mDragAndDropDetector;
560 Dali::Any Window::GetNativeHandle() const
564 return mEventHandler->mEcoreWindow;
572 void Window::OnStart()
574 DoShowIndicator( mIndicatorOrientation );
577 void Window::OnPause()
581 void Window::OnResume()
583 // resume indicator status
584 if( mIndicator != NULL )
586 // Restore own indicator opacity
587 // Send opacity mode to indicator service when app resumed
588 mIndicator->SetOpacityMode( mIndicatorOpacityMode );
592 void Window::OnStop()
603 void Window::OnDestroy()
608 void Window::AddAvailableOrientation(Dali::Window::WindowOrientation orientation)
612 for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
614 if(mAvailableOrientations[i] == orientation)
623 mAvailableOrientations.push_back(orientation);
624 SetAvailableOrientations( mAvailableOrientations );
628 void Window::RemoveAvailableOrientation(Dali::Window::WindowOrientation orientation)
630 for( std::vector<Dali::Window::WindowOrientation>::iterator iter = mAvailableOrientations.begin();
631 iter != mAvailableOrientations.end(); ++iter )
633 if( *iter == orientation )
635 mAvailableOrientations.erase( iter );
639 SetAvailableOrientations( mAvailableOrientations );
642 void Window::SetAvailableOrientations(const std::vector<Dali::Window::WindowOrientation>& orientations)
645 for( std::size_t i = 0; i < mAvailableOrientations.size(); ++i )
647 rotations[i] = static_cast< int >( mAvailableOrientations[i] );
649 ecore_wl_window_rotation_available_rotations_set( mEventHandler->mEcoreWindow, rotations, mAvailableOrientations.size() );
652 const std::vector<Dali::Window::WindowOrientation>& Window::GetAvailableOrientations()
654 return mAvailableOrientations;
657 void Window::SetPreferredOrientation(Dali::Window::WindowOrientation orientation)
659 mPreferredOrientation = orientation;
661 ecore_wl_window_rotation_preferred_rotation_set( mEventHandler->mEcoreWindow, orientation );
664 Dali::Window::WindowOrientation Window::GetPreferredOrientation()
666 return mPreferredOrientation;
669 void Window::RotationDone( int orientation, int width, int height )
671 ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
674 wlSurface->RequestRotation( orientation, width, height );
677 mAdaptor->SurfaceResizePrepare( Adaptor::SurfaceSize( width, height ) );
679 mAdaptor->SurfaceResizeComplete( Adaptor::SurfaceSize( width, height ) );
682 unsigned int Window::AddAuxiliaryHint( const std::string& hint, const std::string& value )
684 bool supported = false;
686 // Check if the hint is suppported
687 for( std::vector< std::string >::iterator iter = mSupportedAuxiliaryHints.begin(); iter != mSupportedAuxiliaryHints.end(); ++iter )
698 DALI_LOG_INFO( gWindowLogFilter, Debug::Concise, "Window::AddAuxiliaryHint: Not supported auxiliary hint [%s]\n", hint.c_str() );
702 // Check if the hint is already added
703 for( unsigned int i = 0; i < mAuxiliaryHints.size(); i++ )
705 if( mAuxiliaryHints[i].first == hint )
707 // Just change the value
708 mAuxiliaryHints[i].second = value;
710 DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::AddAuxiliaryHint: Change! hint = %s, value = %s, id = %d\n", hint.c_str(), value.c_str(), i + 1 );
712 return i + 1; // id is index + 1
717 mAuxiliaryHints.push_back( std::pair< std::string, std::string >( hint, value ) );
719 unsigned int id = mAuxiliaryHints.size();
721 ecore_wl_window_aux_hint_add( mEventHandler->mEcoreWindow, static_cast< int >( id ), hint.c_str(), value.c_str() );
723 DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::AddAuxiliaryHint: hint = %s, value = %s, id = %d\n", hint.c_str(), value.c_str(), id );