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 #ifndef DALI_PROFILE_UBUNTU
78 if( mWindow->mEcoreEventHander )
80 mWindowIconifyStateHandler = ecore_event_handler_add( ECORE_WL_EVENT_WINDOW_ICONIFY_STATE_CHANGE, EcoreEventWindowIconifyStateChanged, this );
91 if ( mWindowPropertyHandler )
93 ecore_event_handler_del( mWindowPropertyHandler );
95 if ( mWindowIconifyStateHandler )
97 ecore_event_handler_del( mWindowIconifyStateHandler );
103 /// Called when the window properties are changed.
104 static Eina_Bool EcoreEventWindowPropertyChanged( void* data, int type, void* event )
109 #ifndef DALI_PROFILE_UBUNTU
110 /// Called when the window iconify state is changed.
111 static Eina_Bool EcoreEventWindowIconifyStateChanged( void* data, int type, void* event )
113 Ecore_Wl_Event_Window_Iconify_State_Change* iconifyChangedEvent( (Ecore_Wl_Event_Window_Iconify_State_Change*)event );
114 EventHandler* handler( (EventHandler*)data );
115 Eina_Bool handled( ECORE_CALLBACK_PASS_ON );
117 if ( handler && handler->mWindow )
119 WindowVisibilityObserver* observer( handler->mWindow->mAdaptor );
120 if ( observer && ( iconifyChangedEvent->win == (unsigned int) ecore_wl_window_id_get( handler->mEcoreWindow ) ) )
122 if( iconifyChangedEvent->iconified == EINA_TRUE )
124 observer->OnWindowHidden();
125 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Iconfied\n", handler->mEcoreWindow );
129 observer->OnWindowShown();
130 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Shown\n", handler->mEcoreWindow );
132 handled = ECORE_CALLBACK_DONE;
142 Ecore_Event_Handler* mWindowPropertyHandler;
143 Ecore_Event_Handler* mWindowIconifyStateHandler;
144 Ecore_Wl_Window* mEcoreWindow;
148 Window* Window::New(const PositionSize& posSize, const std::string& name, const std::string& className, bool isTransparent)
150 Window* window = new Window();
151 window->mIsTransparent = isTransparent;
152 window->Initialize(posSize, name, className);
156 void Window::SetAdaptor(Dali::Adaptor& adaptor)
158 DALI_ASSERT_ALWAYS( !mStarted && "Adaptor already started" );
161 // Only create one overlay per window
162 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
163 Integration::Core& core = adaptorImpl.GetCore();
164 mOverlay = &core.GetSystemOverlay();
166 Dali::RenderTaskList taskList = mOverlay->GetOverlayRenderTasks();
167 taskList.CreateTask();
169 mAdaptor = &adaptorImpl;
170 mAdaptor->AddObserver( *this );
172 // Can only create the detector when we know the Core has been instantiated.
173 mDragAndDropDetector = DragAndDropDetector::New();
174 mAdaptor->SetDragAndDropDetector( &GetImplementation( mDragAndDropDetector ) );
178 mOrientation->SetAdaptor(adaptor);
181 if( mIndicator != NULL )
183 mIndicator->SetAdaptor(mAdaptor);
187 RenderSurface* Window::GetSurface()
192 void Window::ShowIndicator( Dali::Window::IndicatorVisibleMode visibleMode )
194 DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "visible : %d\n", visibleMode );
195 DALI_ASSERT_DEBUG(mOverlay);
197 ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
198 DALI_ASSERT_DEBUG(wlSurface);
199 Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
201 mIndicatorVisible = visibleMode;
203 if ( mIndicatorVisible == Dali::Window::VISIBLE )
205 // when the indicator is visible, set proper mode for indicator server according to bg mode
206 if ( mIndicatorOpacityMode == Dali::Window::OPAQUE )
208 ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_OPAQUE);
210 else if ( mIndicatorOpacityMode == Dali::Window::TRANSLUCENT )
212 ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_TRANSLUCENT);
214 else if ( mIndicatorOpacityMode == Dali::Window::TRANSPARENT )
216 ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_OPAQUE);
221 // when the indicator is not visible, set TRANSPARENT mode for indicator server
222 ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_TRANSPARENT); // it means hidden indicator
225 DoShowIndicator( mIndicatorOrientation );
228 void Window::RotateIndicator(Dali::Window::WindowOrientation orientation)
230 DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "Orientation: %d\n", orientation );
232 DoRotateIndicator( orientation );
235 void Window::SetIndicatorBgOpacity( Dali::Window::IndicatorBgOpacity opacityMode )
237 mIndicatorOpacityMode = opacityMode;
239 if( mIndicator != NULL )
241 mIndicator->SetOpacityMode( opacityMode );
245 void Window::SetClass(std::string name, std::string klass)
251 mIndicatorVisible(Dali::Window::VISIBLE),
252 mIndicatorIsShown(false),
253 mShowRotatedIndicatorOnClose(false),
255 mIsTransparent(false),
256 mWMRotationAppSet(false),
257 mEcoreEventHander(true),
259 mIndicatorOrientation(Dali::Window::PORTRAIT),
260 mNextIndicatorOrientation(Dali::Window::PORTRAIT),
261 mIndicatorOpacityMode(Dali::Window::OPAQUE),
265 mPreferredOrientation(Dali::Window::PORTRAIT)
271 delete mEventHandler;
281 mAdaptor->RemoveObserver( *this );
282 mAdaptor->SetDragAndDropDetector( NULL );
289 void Window::Initialize(const PositionSize& windowPosition, const std::string& name, const std::string& className)
291 // create an Wayland window by default
293 ECore::WindowRenderSurface* windowSurface = new ECore::WindowRenderSurface( windowPosition, surface, name, mIsTransparent );
294 SetClass( name, className );
295 windowSurface->Map();
297 mSurface = windowSurface;
300 mAdaptor->GetAppId( appId );
301 Ecore_Wl_Window* wlWindow = windowSurface ->GetWlWindow();
302 ecore_wl_window_class_name_set(wlWindow, appId.c_str());
304 mOrientation = Orientation::New(this);
306 // create event handler for Wayland window
307 mEventHandler = new EventHandler( this );
310 void Window::DoShowIndicator( Dali::Window::WindowOrientation lastOrientation )
312 if( mIndicator == NULL )
314 if( mIndicatorVisible != Dali::Window::INVISIBLE )
316 mIndicator = new Indicator( mAdaptor, mIndicatorOrientation, this );
317 mIndicator->SetOpacityMode( mIndicatorOpacityMode );
318 Dali::Actor actor = mIndicator->GetActor();
319 SetIndicatorActorRotation();
320 mOverlay->Add(actor);
322 // else don't create a hidden indicator
324 else // Already have indicator
326 if( mIndicatorVisible == Dali::Window::VISIBLE )
328 // If we are resuming, and rotation has changed,
329 if( mIndicatorIsShown == false && mIndicatorOrientation != mNextIndicatorOrientation )
331 // then close current indicator and open new one
332 mShowRotatedIndicatorOnClose = true;
333 mIndicator->Close(); // May synchronously call IndicatorClosed() callback & 1 level of recursion
334 // Don't show actor - will contain indicator for old orientation.
339 // set indicator visible mode
340 if( mIndicator != NULL )
342 mIndicator->SetVisible( mIndicatorVisible );
345 bool show = (mIndicatorVisible != Dali::Window::INVISIBLE );
346 SetIndicatorProperties( show, lastOrientation );
347 mIndicatorIsShown = show;
350 void Window::DoRotateIndicator( Dali::Window::WindowOrientation orientation )
352 if( mIndicatorIsShown )
354 mShowRotatedIndicatorOnClose = true;
355 mNextIndicatorOrientation = orientation;
356 mIndicator->Close(); // May synchronously call IndicatorClosed() callback
360 // Save orientation for when the indicator is next shown
361 mShowRotatedIndicatorOnClose = false;
362 mNextIndicatorOrientation = orientation;
366 void Window::SetIndicatorProperties( bool isShow, Dali::Window::WindowOrientation lastOrientation )
368 ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
372 Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
375 ecore_wl_window_indicator_state_set(wlWindow, ECORE_WL_INDICATOR_STATE_ON);
379 ecore_wl_window_indicator_state_set(wlWindow, ECORE_WL_INDICATOR_STATE_OFF);
384 void Window::IndicatorTypeChanged(Indicator::Type type)
386 #if defined(DALI_PROFILE_MOBILE)
387 ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
391 Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
394 case Indicator::INDICATOR_TYPE_1:
395 ecore_wl_indicator_visible_type_set(wlWindow, ECORE_WL_INDICATOR_VISIBLE_TYPE_SHOWN);
398 case Indicator::INDICATOR_TYPE_2:
399 ecore_wl_indicator_visible_type_set(wlWindow, ECORE_WL_INDICATOR_VISIBLE_TYPE_HIDDEN);
402 case Indicator::INDICATOR_TYPE_UNKNOWN:
410 void Window::IndicatorClosed( IndicatorInterface* indicator )
412 DALI_LOG_TRACE_METHOD( gWindowLogFilter );
414 if( mShowRotatedIndicatorOnClose )
416 Dali::Window::WindowOrientation currentOrientation = mIndicatorOrientation;
417 mIndicator->Open(mNextIndicatorOrientation);
418 mIndicatorOrientation = mNextIndicatorOrientation;
419 SetIndicatorActorRotation();
420 DoShowIndicator(currentOrientation);
424 void Window::IndicatorVisibilityChanged(bool isVisible)
426 mIndicatorVisibilityChangedSignal.Emit(isVisible);
429 void Window::SetIndicatorActorRotation()
431 DALI_LOG_TRACE_METHOD( gWindowLogFilter );
432 DALI_ASSERT_DEBUG( mIndicator != NULL );
434 Dali::Actor actor = mIndicator->GetActor();
435 switch( mIndicatorOrientation )
437 case Dali::Window::PORTRAIT:
438 actor.SetParentOrigin( ParentOrigin::TOP_CENTER );
439 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
440 actor.SetOrientation( Degree(0), Vector3::ZAXIS );
442 case Dali::Window::PORTRAIT_INVERSE:
443 actor.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
444 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
445 actor.SetOrientation( Degree(180), Vector3::ZAXIS );
447 case Dali::Window::LANDSCAPE:
448 actor.SetParentOrigin( ParentOrigin::CENTER_LEFT );
449 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
450 actor.SetOrientation( Degree(270), Vector3::ZAXIS );
452 case Dali::Window::LANDSCAPE_INVERSE:
453 actor.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
454 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
455 actor.SetOrientation( Degree(90), Vector3::ZAXIS );
468 void Window::Activate()
472 Dali::DragAndDropDetector Window::GetDragAndDropDetector() const
474 return mDragAndDropDetector;
477 Dali::Any Window::GetNativeHandle() const
481 return mEventHandler->mEcoreWindow;
489 void Window::OnStart()
491 DoShowIndicator( mIndicatorOrientation );
494 void Window::OnPause()
498 void Window::OnResume()
500 // resume indicator status
501 if( mIndicator != NULL )
503 // Restore own indicator opacity
504 // Send opacity mode to indicator service when app resumed
505 mIndicator->SetOpacityMode( mIndicatorOpacityMode );
509 void Window::OnStop()
520 void Window::OnDestroy()
525 void Window::AddAvailableOrientation(Dali::Window::WindowOrientation orientation)
529 for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
531 if(mAvailableOrientations[i] == orientation)
540 mAvailableOrientations.push_back(orientation);
541 SetAvailableOrientations( mAvailableOrientations );
545 void Window::RemoveAvailableOrientation(Dali::Window::WindowOrientation orientation)
547 for( std::vector<Dali::Window::WindowOrientation>::iterator iter = mAvailableOrientations.begin();
548 iter != mAvailableOrientations.end(); ++iter )
550 if( *iter == orientation )
552 mAvailableOrientations.erase( iter );
556 SetAvailableOrientations( mAvailableOrientations );
559 void Window::SetAvailableOrientations(const std::vector<Dali::Window::WindowOrientation>& orientations)
561 DALI_ASSERT_ALWAYS( mAvailableOrientations.size() <= 4 && "Incorrect number of available orientations" );
564 const std::vector<Dali::Window::WindowOrientation>& Window::GetAvailableOrientations()
566 return mAvailableOrientations;
569 void Window::SetPreferredOrientation(Dali::Window::WindowOrientation orientation)
571 mPreferredOrientation = orientation;
574 Dali::Window::WindowOrientation Window::GetPreferredOrientation()
576 return mPreferredOrientation;
579 void Window::RotationDone( int orientation, int width, int height )