2 * Copyright (c) 2014 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>
39 const float INDICATOR_ANIMATION_DURATION( 0.18f ); // 180 milli seconds
40 const float INDICATOR_SHOW_Y_POSITION( 0.0f );
41 const float INDICATOR_HIDE_Y_POSITION( -52.0f );
50 #if defined(DEBUG_ENABLED)
51 Debug::Filter* gWindowLogFilter = Debug::Filter::New(Debug::Concise, false, "LOG_WINDOW");
55 * TODO: Abstract Window class out and move this into a window implementation for Ecore
57 struct Window::EventHandler
61 * @param[in] window A pointer to the window class.
63 EventHandler( Window* window )
65 mWindowPropertyHandler( NULL ),
66 mWindowIconifyStateHandler( NULL ),
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 if( mWindow->mEcoreEventHander )
79 mWindowIconifyStateHandler = ecore_event_handler_add( ECORE_WL_EVENT_WINDOW_ICONIFY_STATE_CHANGE, EcoreEventWindowIconifyStateChanged, this );
89 if ( mWindowPropertyHandler )
91 ecore_event_handler_del( mWindowPropertyHandler );
93 if ( mWindowIconifyStateHandler )
95 ecore_event_handler_del( mWindowIconifyStateHandler );
101 /// Called when the window properties are changed.
102 static Eina_Bool EcoreEventWindowPropertyChanged( void* data, int type, void* event )
107 /// Called when the window iconify state is changed.
108 static Eina_Bool EcoreEventWindowIconifyStateChanged( void* data, int type, void* event )
110 Ecore_Wl_Event_Window_Iconify_State_Change* iconifyChangedEvent( (Ecore_Wl_Event_Window_Iconify_State_Change*)event );
111 EventHandler* handler( (EventHandler*)data );
112 Eina_Bool handled( ECORE_CALLBACK_PASS_ON );
114 if ( handler && handler->mWindow )
116 WindowVisibilityObserver* observer( handler->mWindow->mAdaptor );
117 if ( observer && ( iconifyChangedEvent->win == (unsigned int) ecore_wl_window_id_get( handler->mEcoreWindow ) ) )
119 if( iconifyChangedEvent->iconified == EINA_TRUE )
121 observer->OnWindowHidden();
122 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Iconfied\n", handler->mEcoreWindow );
126 observer->OnWindowShown();
127 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Shown\n", handler->mEcoreWindow );
129 handled = ECORE_CALLBACK_DONE;
138 Ecore_Event_Handler* mWindowPropertyHandler;
139 Ecore_Event_Handler* mWindowIconifyStateHandler;
140 Ecore_Wl_Window* mEcoreWindow;
144 Window* Window::New(const PositionSize& posSize, const std::string& name, const std::string& className, bool isTransparent)
146 Window* window = new Window();
147 window->mIsTransparent = isTransparent;
148 window->Initialize(posSize, name, className);
152 void Window::SetAdaptor(Dali::Adaptor& adaptor)
154 DALI_ASSERT_ALWAYS( !mStarted && "Adaptor already started" );
157 // Only create one overlay per window
158 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
159 Integration::Core& core = adaptorImpl.GetCore();
160 mOverlay = &core.GetSystemOverlay();
162 Dali::RenderTaskList taskList = mOverlay->GetOverlayRenderTasks();
163 taskList.CreateTask();
165 mAdaptor = &adaptorImpl;
166 mAdaptor->AddObserver( *this );
168 // Can only create the detector when we know the Core has been instantiated.
169 mDragAndDropDetector = DragAndDropDetector::New();
170 mAdaptor->SetDragAndDropDetector( &GetImplementation( mDragAndDropDetector ) );
174 mOrientation->SetAdaptor(adaptor);
177 if( mIndicator != NULL )
179 mIndicator->SetAdaptor(mAdaptor);
183 RenderSurface* Window::GetSurface()
188 void Window::ShowIndicator( Dali::Window::IndicatorVisibleMode visibleMode )
190 DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "visible : %d\n", visibleMode );
191 DALI_ASSERT_DEBUG(mOverlay);
193 ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
194 DALI_ASSERT_DEBUG(wlSurface);
195 Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
197 mIndicatorVisible = visibleMode;
199 if ( mIndicatorVisible == Dali::Window::VISIBLE )
201 // when the indicator is visible, set proper mode for indicator server according to bg mode
202 if ( mIndicatorOpacityMode == Dali::Window::OPAQUE )
204 ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_OPAQUE);
206 else if ( mIndicatorOpacityMode == Dali::Window::TRANSLUCENT )
208 ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_TRANSLUCENT);
210 else if ( mIndicatorOpacityMode == Dali::Window::TRANSPARENT )
212 ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_OPAQUE);
217 // when the indicator is not visible, set TRANSPARENT mode for indicator server
218 ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_TRANSPARENT); // it means hidden indicator
221 DoShowIndicator( mIndicatorOrientation );
224 void Window::RotateIndicator(Dali::Window::WindowOrientation orientation)
226 DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "Orientation: %d\n", orientation );
228 DoRotateIndicator( orientation );
231 void Window::SetIndicatorBgOpacity( Dali::Window::IndicatorBgOpacity opacityMode )
233 mIndicatorOpacityMode = opacityMode;
235 if( mIndicator != NULL )
237 mIndicator->SetOpacityMode( opacityMode );
241 void Window::SetClass(std::string name, std::string klass)
243 ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
247 Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
248 ecore_wl_window_title_set( wlWindow, name.c_str() );
249 ecore_wl_window_class_name_set( wlWindow, klass.c_str() );
253 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window has no surface\n" );
259 mIndicatorVisible(Dali::Window::VISIBLE),
260 mIndicatorIsShown(false),
261 mShowRotatedIndicatorOnClose(false),
263 mIsTransparent(false),
264 mWMRotationAppSet(false),
265 mEcoreEventHander(true),
267 mIndicatorOrientation(Dali::Window::PORTRAIT),
268 mNextIndicatorOrientation(Dali::Window::PORTRAIT),
269 mIndicatorOpacityMode(Dali::Window::OPAQUE),
273 mPreferredOrientation(Dali::Window::PORTRAIT)
279 delete mEventHandler;
289 mAdaptor->RemoveObserver( *this );
290 mAdaptor->SetDragAndDropDetector( NULL );
297 void Window::Initialize(const PositionSize& windowPosition, const std::string& name, const std::string& className)
299 // create an Wayland window by default
301 ECore::WindowRenderSurface* windowSurface = new ECore::WindowRenderSurface( windowPosition, surface, name, mIsTransparent );
303 mSurface = windowSurface;
304 SetClass( name, className );
305 windowSurface->Map();
307 mOrientation = Orientation::New(this);
309 // create event handler for Wayland window
310 mEventHandler = new EventHandler( this );
313 void Window::DoShowIndicator( Dali::Window::WindowOrientation lastOrientation )
315 if( mIndicator == NULL )
317 if( mIndicatorVisible != Dali::Window::INVISIBLE )
319 mIndicator = new Indicator( mAdaptor, mIndicatorOrientation, this );
320 mIndicator->SetOpacityMode( mIndicatorOpacityMode );
321 Dali::Actor actor = mIndicator->GetActor();
322 SetIndicatorActorRotation();
323 mOverlay->Add(actor);
325 // else don't create a hidden indicator
327 else // Already have indicator
329 if( mIndicatorVisible == Dali::Window::VISIBLE )
331 // If we are resuming, and rotation has changed,
332 if( mIndicatorIsShown == false && mIndicatorOrientation != mNextIndicatorOrientation )
334 // then close current indicator and open new one
335 mShowRotatedIndicatorOnClose = true;
336 mIndicator->Close(); // May synchronously call IndicatorClosed() callback & 1 level of recursion
337 // Don't show actor - will contain indicator for old orientation.
342 // set indicator visible mode
343 if( mIndicator != NULL )
345 mIndicator->SetVisible( mIndicatorVisible );
348 bool show = (mIndicatorVisible != Dali::Window::INVISIBLE );
349 SetIndicatorProperties( show, lastOrientation );
350 mIndicatorIsShown = show;
353 void Window::DoRotateIndicator( Dali::Window::WindowOrientation orientation )
355 if( mIndicatorIsShown )
357 mShowRotatedIndicatorOnClose = true;
358 mNextIndicatorOrientation = orientation;
359 mIndicator->Close(); // May synchronously call IndicatorClosed() callback
363 // Save orientation for when the indicator is next shown
364 mShowRotatedIndicatorOnClose = false;
365 mNextIndicatorOrientation = orientation;
369 void Window::SetIndicatorProperties( bool isShow, Dali::Window::WindowOrientation lastOrientation )
371 ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
375 Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
378 ecore_wl_window_indicator_state_set(wlWindow, ECORE_WL_INDICATOR_STATE_ON);
382 ecore_wl_window_indicator_state_set(wlWindow, ECORE_WL_INDICATOR_STATE_OFF);
387 void Window::IndicatorTypeChanged(Indicator::Type type)
389 #if defined(DALI_PROFILE_MOBILE)
390 ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
394 Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
397 case Indicator::INDICATOR_TYPE_1:
398 ecore_wl_indicator_visible_type_set(wlWindow, ECORE_WL_INDICATOR_VISIBLE_TYPE_SHOWN);
401 case Indicator::INDICATOR_TYPE_2:
402 ecore_wl_indicator_visible_type_set(wlWindow, ECORE_WL_INDICATOR_VISIBLE_TYPE_HIDDEN);
405 case Indicator::INDICATOR_TYPE_UNKNOWN:
413 void Window::IndicatorClosed( IndicatorInterface* indicator )
415 DALI_LOG_TRACE_METHOD( gWindowLogFilter );
417 if( mShowRotatedIndicatorOnClose )
419 Dali::Window::WindowOrientation currentOrientation = mIndicatorOrientation;
420 mIndicator->Open(mNextIndicatorOrientation);
421 mIndicatorOrientation = mNextIndicatorOrientation;
422 SetIndicatorActorRotation();
423 DoShowIndicator(currentOrientation);
427 void Window::IndicatorVisibilityChanged(bool isVisible)
429 mIndicatorVisibilityChangedSignal.Emit(isVisible);
432 void Window::SetIndicatorActorRotation()
434 DALI_LOG_TRACE_METHOD( gWindowLogFilter );
435 DALI_ASSERT_DEBUG( mIndicator != NULL );
437 Dali::Actor actor = mIndicator->GetActor();
438 switch( mIndicatorOrientation )
440 case Dali::Window::PORTRAIT:
441 actor.SetParentOrigin( ParentOrigin::TOP_CENTER );
442 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
443 actor.SetOrientation( Degree(0), Vector3::ZAXIS );
445 case Dali::Window::PORTRAIT_INVERSE:
446 actor.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
447 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
448 actor.SetOrientation( Degree(180), Vector3::ZAXIS );
450 case Dali::Window::LANDSCAPE:
451 actor.SetParentOrigin( ParentOrigin::CENTER_LEFT );
452 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
453 actor.SetOrientation( Degree(270), Vector3::ZAXIS );
455 case Dali::Window::LANDSCAPE_INVERSE:
456 actor.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
457 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
458 actor.SetOrientation( Degree(90), Vector3::ZAXIS );
465 ecore_wl_window_raise( mEventHandler->mEcoreWindow );
470 ecore_wl_window_lower( mEventHandler->mEcoreWindow );
473 void Window::Activate()
475 ecore_wl_window_activate( mEventHandler->mEcoreWindow );
478 Dali::DragAndDropDetector Window::GetDragAndDropDetector() const
480 return mDragAndDropDetector;
483 Dali::Any Window::GetNativeHandle() const
487 return mEventHandler->mEcoreWindow;
495 void Window::OnStart()
497 DoShowIndicator( mIndicatorOrientation );
500 void Window::OnPause()
504 void Window::OnResume()
506 // resume indicator status
507 if( mIndicator != NULL )
509 // Restore own indicator opacity
510 // Send opacity mode to indicator service when app resumed
511 mIndicator->SetOpacityMode( mIndicatorOpacityMode );
515 void Window::OnStop()
526 void Window::OnDestroy()
531 void Window::AddAvailableOrientation(Dali::Window::WindowOrientation orientation)
535 for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
537 if(mAvailableOrientations[i] == orientation)
546 mAvailableOrientations.push_back(orientation);
547 SetAvailableOrientations( mAvailableOrientations );
551 void Window::RemoveAvailableOrientation(Dali::Window::WindowOrientation orientation)
553 for( std::vector<Dali::Window::WindowOrientation>::iterator iter = mAvailableOrientations.begin();
554 iter != mAvailableOrientations.end(); ++iter )
556 if( *iter == orientation )
558 mAvailableOrientations.erase( iter );
562 SetAvailableOrientations( mAvailableOrientations );
565 void Window::SetAvailableOrientations(const std::vector<Dali::Window::WindowOrientation>& orientations)
568 for( std::size_t i = 0; i < mAvailableOrientations.size(); ++i )
570 rotations[i] = static_cast< int >( mAvailableOrientations[i] );
572 ecore_wl_window_rotation_available_rotations_set( mEventHandler->mEcoreWindow, rotations, mAvailableOrientations.size() );
575 const std::vector<Dali::Window::WindowOrientation>& Window::GetAvailableOrientations()
577 return mAvailableOrientations;
580 void Window::SetPreferredOrientation(Dali::Window::WindowOrientation orientation)
582 mPreferredOrientation = orientation;
584 ecore_wl_window_rotation_preferred_rotation_set( mEventHandler->mEcoreWindow, orientation );
587 Dali::Window::WindowOrientation Window::GetPreferredOrientation()
589 return mPreferredOrientation;
592 void Window::RotationDone( int orientation, int width, int height )
594 ecore_wl_window_rotation_change_done_send( mEventHandler->mEcoreWindow );