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"
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>
31 #include <window-render-surface.h>
32 #include <drag-and-drop-detector-impl.h>
33 #include <indicator-impl.h>
34 #include <window-visibility-observer.h>
35 #include <orientation.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 )
66 mWindowPropertyHandler( NULL ),
67 mClientMessagehandler( NULL ),
70 // store ecore window handle
71 ECore::WindowRenderSurface* x11Window( dynamic_cast< ECore::WindowRenderSurface * >( mWindow->mSurface ) );
74 mEcoreWindow = x11Window->GetXWindow();
76 DALI_ASSERT_ALWAYS( mEcoreWindow != 0 && "There is no ecore x window");
78 #ifndef DALI_PROFILE_UBUNTU
79 // set property on window to get deiconify approve client message
81 ecore_x_window_prop_card32_set(mEcoreWindow,
82 ECORE_X_ATOM_E_DEICONIFY_APPROVE,
84 #endif // DALI_PROFILE_UBUNTU
86 if( mWindow->mEcoreEventHander )
88 ecore_x_input_multi_select( mEcoreWindow );
89 mWindowPropertyHandler= ecore_event_handler_add( ECORE_X_EVENT_WINDOW_PROPERTY, EcoreEventWindowPropertyChanged, this );
90 mClientMessagehandler = ecore_event_handler_add( ECORE_X_EVENT_CLIENT_MESSAGE, EcoreEventClientMessage, this );
100 if ( mWindowPropertyHandler )
102 ecore_event_handler_del( mWindowPropertyHandler );
104 if ( mClientMessagehandler )
106 ecore_event_handler_del( mClientMessagehandler );
112 /// Called when the window properties are changed.
113 static Eina_Bool EcoreEventWindowPropertyChanged( void* data, int type, void* event )
115 Ecore_X_Event_Window_Property* propertyChangedEvent( (Ecore_X_Event_Window_Property*)event );
116 EventHandler* handler( (EventHandler*)data );
117 Eina_Bool handled( ECORE_CALLBACK_PASS_ON );
119 if ( handler && handler->mWindow )
121 WindowVisibilityObserver* observer( handler->mWindow->mAdaptor );
122 if ( observer && ( propertyChangedEvent->win == handler->mEcoreWindow ) )
124 Ecore_X_Window_State_Hint state( ecore_x_icccm_state_get( propertyChangedEvent->win ) );
128 case ECORE_X_WINDOW_STATE_HINT_WITHDRAWN:
130 // Window was hidden.
131 observer->OnWindowHidden();
132 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Withdrawn\n", handler->mEcoreWindow );
133 handled = ECORE_CALLBACK_DONE;
137 case ECORE_X_WINDOW_STATE_HINT_ICONIC:
139 // Window was iconified (minimised).
140 observer->OnWindowHidden();
141 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Iconfied\n", handler->mEcoreWindow );
142 handled = ECORE_CALLBACK_DONE;
146 case ECORE_X_WINDOW_STATE_HINT_NORMAL:
149 observer->OnWindowShown();
150 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Shown\n", handler->mEcoreWindow );
151 handled = ECORE_CALLBACK_DONE;
165 /// Called when the window properties are changed.
166 static Eina_Bool EcoreEventClientMessage( void* data, int type, void* event )
168 Eina_Bool handled( ECORE_CALLBACK_PASS_ON );
169 #ifndef DALI_PROFILE_UBUNTU
170 Ecore_X_Event_Client_Message* clientMessageEvent( (Ecore_X_Event_Client_Message*)event );
171 EventHandler* handler( (EventHandler*)data );
173 if (clientMessageEvent->message_type == ECORE_X_ATOM_E_DEICONIFY_APPROVE)
175 ECore::WindowRenderSurface* x11Window( dynamic_cast< ECore::WindowRenderSurface * >( handler->mWindow->mSurface ) );
176 WindowVisibilityObserver* observer( handler->mWindow->mAdaptor );
178 if ( observer && ( (unsigned int)clientMessageEvent->data.l[0] == handler->mEcoreWindow ) )
180 if (clientMessageEvent->data.l[1] == 0) //wm sends request message using value 0
182 observer->OnWindowShown();
184 // request to approve the deiconify. render-surface should send proper event after real rendering
187 x11Window->RequestToApproveDeiconify();
190 handled = ECORE_CALLBACK_DONE;
194 #endif // DALI_PROFILE_UBUNTU
201 Ecore_Event_Handler* mWindowPropertyHandler;
202 Ecore_Event_Handler* mClientMessagehandler;
203 Ecore_X_Window mEcoreWindow;
207 Window* Window::New(const PositionSize& posSize, const std::string& name, const std::string& className, bool isTransparent)
209 Window* window = new Window();
210 window->mIsTransparent = isTransparent;
211 window->Initialize(posSize, name, className);
215 void Window::SetAdaptor(Dali::Adaptor& adaptor)
217 DALI_ASSERT_ALWAYS( !mStarted && "Adaptor already started" );
220 // Only create one overlay per window
221 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
222 Integration::Core& core = adaptorImpl.GetCore();
223 mOverlay = &core.GetSystemOverlay();
225 Dali::RenderTaskList taskList = mOverlay->GetOverlayRenderTasks();
226 taskList.CreateTask();
228 mAdaptor = &adaptorImpl;
229 mAdaptor->AddObserver( *this );
231 // Can only create the detector when we know the Core has been instantiated.
232 mDragAndDropDetector = DragAndDropDetector::New();
233 mAdaptor->SetDragAndDropDetector( &GetImplementation( mDragAndDropDetector ) );
237 mOrientation->SetAdaptor(adaptor);
240 if( mIndicator != NULL )
242 mIndicator->SetAdaptor(mAdaptor);
246 RenderSurface* Window::GetSurface()
251 void Window::ShowIndicator( Dali::Window::IndicatorVisibleMode visibleMode )
253 DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "visible : %d\n", visibleMode );
254 DALI_ASSERT_DEBUG(mOverlay);
256 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
257 DALI_ASSERT_DEBUG(x11Window);
258 Ecore_X_Window xWinId = x11Window->GetXWindow();
260 mIndicatorVisible = visibleMode;
262 if ( mIndicatorVisible == Dali::Window::VISIBLE )
264 // when the indicator is visible, set proper mode for indicator server according to bg mode
265 if ( mIndicatorOpacityMode == Dali::Window::OPAQUE )
267 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_OPAQUE);
269 else if ( mIndicatorOpacityMode == Dali::Window::TRANSLUCENT )
271 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_TRANSLUCENT);
273 #if defined(DALI_PROFILE_MOBILE)
274 else if ( mIndicatorOpacityMode == Dali::Window::TRANSPARENT )
276 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_OPAQUE);
282 // when the indicator is not visible, set TRANSPARENT mode for indicator server
283 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_TRANSPARENT); // it means hidden indicator
286 DoShowIndicator( mIndicatorOrientation );
289 void Window::RotateIndicator(Dali::Window::WindowOrientation orientation)
291 DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "Orientation: %d\n", orientation );
293 DoRotateIndicator( orientation );
296 void Window::SetIndicatorBgOpacity( Dali::Window::IndicatorBgOpacity opacityMode )
298 mIndicatorOpacityMode = opacityMode;
300 if( mIndicator != NULL )
302 mIndicator->SetOpacityMode( opacityMode );
306 void Window::SetClass(std::string name, std::string klass)
308 // Get render surface's x11 window
311 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
314 ecore_x_icccm_name_class_set( x11Window->GetXWindow(), name.c_str(), klass.c_str() );
321 mIndicatorVisible(Dali::Window::INVISIBLE),
322 mIndicatorIsShown(false),
323 mShowRotatedIndicatorOnClose(false),
325 mIsTransparent(false),
326 mWMRotationAppSet(false),
327 mEcoreEventHander(true),
329 mIndicatorOrientation(Dali::Window::PORTRAIT),
330 mNextIndicatorOrientation(Dali::Window::PORTRAIT),
331 mIndicatorOpacityMode(Dali::Window::OPAQUE),
335 mPreferredOrientation(Dali::Window::PORTRAIT)
338 // Detect if we're not running in a ecore main loop (e.g. libuv).
339 // Typically ecore_x_init is called by app_efl_main->elm_init
340 // but if we're not using app_efl_main then we have to call it ourselves
341 // This is a hack until we create a pure X Window class
342 if( ecore_x_display_get() == NULL )
344 mEcoreEventHander = false;
345 ecore_x_init (NULL); // internally calls _ecore_x_input_init
352 delete mEventHandler;
356 mOverlay->Remove( mIndicator->GetActor() );
357 Dali::RenderTaskList taskList = mOverlay->GetOverlayRenderTasks();
358 Dali::RenderTask indicatorTask = taskList.GetTask(0);
359 mOverlay->GetOverlayRenderTasks().RemoveTask(indicatorTask);
366 mAdaptor->RemoveObserver( *this );
367 mAdaptor->SetDragAndDropDetector( NULL );
374 void Window::Initialize(const PositionSize& windowPosition, const std::string& name, const std::string& className)
376 // create an X11 window by default
378 ECore::WindowRenderSurface* windowSurface = new ECore::WindowRenderSurface( windowPosition, surface, name, className, mIsTransparent );
379 windowSurface->Map();
381 mSurface = windowSurface;
383 mOrientation = Orientation::New(this);
385 // create event handler for X11 window
386 mEventHandler = new EventHandler( this );
389 void Window::DoShowIndicator( Dali::Window::WindowOrientation lastOrientation )
391 if( mIndicator == NULL )
393 if( mIndicatorVisible != Dali::Window::INVISIBLE )
395 mIndicator = new Indicator( mAdaptor, mIndicatorOrientation, this );
396 mIndicator->SetOpacityMode( mIndicatorOpacityMode );
397 Dali::Actor actor = mIndicator->GetActor();
398 SetIndicatorActorRotation();
399 mOverlay->Add(actor);
401 // else don't create a hidden indicator
403 else // Already have indicator
405 if( mIndicatorVisible == Dali::Window::VISIBLE )
407 // If we are resuming, and rotation has changed,
408 if( mIndicatorIsShown == false && mIndicatorOrientation != mNextIndicatorOrientation )
410 // then close current indicator and open new one
411 mShowRotatedIndicatorOnClose = true;
412 mIndicator->Close(); // May synchronously call IndicatorClosed() callback & 1 level of recursion
413 // Don't show actor - will contain indicator for old orientation.
418 // set indicator visible mode
419 if( mIndicator != NULL )
421 mIndicator->SetVisible( mIndicatorVisible );
424 bool show = (mIndicatorVisible != Dali::Window::INVISIBLE );
425 SetIndicatorProperties( show, lastOrientation );
426 mIndicatorIsShown = show;
429 void Window::DoRotateIndicator( Dali::Window::WindowOrientation orientation )
431 if( mIndicatorIsShown )
433 mShowRotatedIndicatorOnClose = true;
434 mNextIndicatorOrientation = orientation;
435 mIndicator->Close(); // May synchronously call IndicatorClosed() callback
439 // Save orientation for when the indicator is next shown
440 mShowRotatedIndicatorOnClose = false;
441 mNextIndicatorOrientation = orientation;
445 void Window::SetIndicatorProperties( bool isShow, Dali::Window::WindowOrientation lastOrientation )
447 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
450 Ecore_X_Window win = x11Window->GetXWindow();
452 int show_state = static_cast<int>( isShow );
453 ecore_x_window_prop_property_set( win,
454 ECORE_X_ATOM_E_ILLUME_INDICATOR_STATE,
455 ECORE_X_ATOM_CARDINAL, 32, &show_state, 1);
459 ecore_x_e_illume_indicator_state_set(win, ECORE_X_ILLUME_INDICATOR_STATE_ON);
463 ecore_x_e_illume_indicator_state_set(win, ECORE_X_ILLUME_INDICATOR_STATE_OFF);
468 void Window::IndicatorTypeChanged(Indicator::Type type)
470 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
473 #ifndef DALI_PROFILE_UBUNTU
474 Ecore_X_Window win = x11Window->GetXWindow();
477 case Indicator::INDICATOR_TYPE_1:
478 ecore_x_e_illume_indicator_type_set( win, ECORE_X_ILLUME_INDICATOR_TYPE_1 );
481 case Indicator::INDICATOR_TYPE_2:
482 ecore_x_e_illume_indicator_type_set( win, ECORE_X_ILLUME_INDICATOR_TYPE_2 );
485 case Indicator::INDICATOR_TYPE_UNKNOWN:
489 #endif // DALI_PROFILE_UBUNTU
493 void Window::IndicatorClosed( Indicator* indicator )
495 DALI_LOG_TRACE_METHOD( gWindowLogFilter );
497 if( mShowRotatedIndicatorOnClose )
499 Dali::Window::WindowOrientation currentOrientation = mIndicatorOrientation;
500 mIndicator->Open(mNextIndicatorOrientation);
501 mIndicatorOrientation = mNextIndicatorOrientation;
502 SetIndicatorActorRotation();
503 DoShowIndicator(currentOrientation);
507 void Window::IndicatorVisibilityChanged(bool isVisible)
509 mIndicatorVisibilityChangedSignal.Emit(isVisible);
512 void Window::SetIndicatorActorRotation()
514 DALI_LOG_TRACE_METHOD( gWindowLogFilter );
515 DALI_ASSERT_DEBUG( mIndicator != NULL );
517 Dali::Actor actor = mIndicator->GetActor();
518 switch( mIndicatorOrientation )
520 case Dali::Window::PORTRAIT:
521 actor.SetParentOrigin( ParentOrigin::TOP_CENTER );
522 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
523 actor.SetOrientation( Degree(0), Vector3::ZAXIS );
525 case Dali::Window::PORTRAIT_INVERSE:
526 actor.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
527 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
528 actor.SetOrientation( Degree(180), Vector3::ZAXIS );
530 case Dali::Window::LANDSCAPE:
531 actor.SetParentOrigin( ParentOrigin::CENTER_LEFT );
532 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
533 actor.SetOrientation( Degree(270), Vector3::ZAXIS );
535 case Dali::Window::LANDSCAPE_INVERSE:
536 actor.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
537 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
538 actor.SetOrientation( Degree(90), Vector3::ZAXIS );
545 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
548 Ecore_X_Window win = x11Window->GetXWindow();
549 ecore_x_window_raise(win);
555 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
558 Ecore_X_Window win = x11Window->GetXWindow();
559 ecore_x_window_lower(win);
563 void Window::Activate()
565 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
568 Ecore_X_Window win = x11Window->GetXWindow();
569 ecore_x_netwm_client_active_request(ecore_x_window_root_get(win), win, 1 /* request type, 1:application, 2:pager */, 0);
573 Dali::DragAndDropDetector Window::GetDragAndDropDetector() const
575 return mDragAndDropDetector;
578 Dali::Any Window::GetNativeHandle() const
582 return mEventHandler->mEcoreWindow;
590 void Window::OnStart()
592 ShowIndicator( mIndicatorVisible );
595 void Window::OnPause()
599 void Window::OnResume()
601 // resume indicator status
602 if( mIndicator != NULL )
604 // Restore own indicator opacity
605 // Send opacity mode to indicator service when app resumed
606 mIndicator->SetOpacityMode( mIndicatorOpacityMode );
610 void Window::OnStop()
621 void Window::OnDestroy()
626 void Window::AddAvailableOrientation(Dali::Window::WindowOrientation orientation)
630 for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
632 if(mAvailableOrientations[i] == orientation)
641 mAvailableOrientations.push_back(orientation);
642 SetAvailableOrientations( mAvailableOrientations );
646 void Window::RemoveAvailableOrientation(Dali::Window::WindowOrientation orientation)
648 for( std::vector<Dali::Window::WindowOrientation>::iterator iter = mAvailableOrientations.begin();
649 iter != mAvailableOrientations.end(); ++iter )
651 if( *iter == orientation )
653 mAvailableOrientations.erase( iter );
657 SetAvailableOrientations( mAvailableOrientations );
660 void Window::SetAvailableOrientations(const std::vector<Dali::Window::WindowOrientation>& orientations)
662 DALI_ASSERT_ALWAYS( mAvailableOrientations.size() <= 4 && "Incorrect number of available orientations" );
664 mAvailableOrientations = orientations;
665 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
668 #ifndef DALI_PROFILE_UBUNTU
669 Ecore_X_Window ecoreWindow = x11Window->GetXWindow();
670 if( ! mWMRotationAppSet )
672 mWMRotationAppSet = true;
673 ecore_x_e_window_rotation_app_set(ecoreWindow, EINA_TRUE);
677 for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
679 rotations[i] = static_cast<int>(mAvailableOrientations[i]);
681 ecore_x_e_window_rotation_available_rotations_set(ecoreWindow, rotations, mAvailableOrientations.size() );
682 #endif // DALI_PROFILE_UBUNTU
686 const std::vector<Dali::Window::WindowOrientation>& Window::GetAvailableOrientations()
688 return mAvailableOrientations;
691 void Window::SetPreferredOrientation(Dali::Window::WindowOrientation orientation)
693 mPreferredOrientation = orientation;
695 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
698 #ifndef DALI_PROFILE_UBUNTU
699 Ecore_X_Window ecoreWindow = x11Window->GetXWindow();
701 if( ! mWMRotationAppSet )
703 mWMRotationAppSet = true;
704 ecore_x_e_window_rotation_app_set(ecoreWindow, EINA_TRUE);
707 ecore_x_e_window_rotation_preferred_rotation_set(ecoreWindow, orientation);
708 #endif // DALI_PROFILE_UBUNTU
712 Dali::Window::WindowOrientation Window::GetPreferredOrientation()
714 return mPreferredOrientation;
717 void Window::RotationDone( int orientation, int width, int height )
719 // Tell window manager we're done
720 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
723 #ifndef DALI_PROFILE_UBUNTU
724 Ecore_X_Window ecoreWindow = x11Window->GetXWindow();
725 Ecore_X_Window root = ecore_x_window_root_get(ecoreWindow);
728 * send rotation done message to wm, even if window is already rotated.
729 * that's why wm must be wait for comming rotation done message
730 * after sending rotation request.
732 ecore_x_e_window_rotation_change_done_send(root, ecoreWindow, orientation, width, height);
735 * set rotate window property
737 int angles[2] = { orientation, orientation };
738 ecore_x_window_prop_property_set( ecoreWindow,
739 ECORE_X_ATOM_E_ILLUME_ROTATE_WINDOW_ANGLE,
740 ECORE_X_ATOM_CARDINAL, 32, &angles, 2 );
741 #endif // DALI_PROFILE_UBUNTU