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( ecore_event_handler_add( ECORE_X_EVENT_WINDOW_PROPERTY, EcoreEventWindowPropertyChanged, this ) ),
67 mClientMessagehandler( ecore_event_handler_add( ECORE_X_EVENT_CLIENT_MESSAGE, EcoreEventClientMessage, this ) ),
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 ecore_x_input_multi_select( mEcoreWindow );
94 if ( mWindowPropertyHandler )
96 ecore_event_handler_del( mWindowPropertyHandler );
98 if ( mClientMessagehandler )
100 ecore_event_handler_del( mClientMessagehandler );
106 /// Called when the window properties are changed.
107 static Eina_Bool EcoreEventWindowPropertyChanged( void* data, int type, void* event )
109 Ecore_X_Event_Window_Property* propertyChangedEvent( (Ecore_X_Event_Window_Property*)event );
110 EventHandler* handler( (EventHandler*)data );
111 Eina_Bool handled( ECORE_CALLBACK_PASS_ON );
113 if ( handler && handler->mWindow )
115 WindowVisibilityObserver* observer( handler->mWindow->mAdaptor );
116 if ( observer && ( propertyChangedEvent->win == handler->mEcoreWindow ) )
118 Ecore_X_Window_State_Hint state( ecore_x_icccm_state_get( propertyChangedEvent->win ) );
122 case ECORE_X_WINDOW_STATE_HINT_WITHDRAWN:
124 // Window was hidden.
125 observer->OnWindowHidden();
126 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Withdrawn\n", handler->mEcoreWindow );
127 handled = ECORE_CALLBACK_DONE;
131 case ECORE_X_WINDOW_STATE_HINT_ICONIC:
133 // Window was iconified (minimised).
134 observer->OnWindowHidden();
135 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Iconfied\n", handler->mEcoreWindow );
136 handled = ECORE_CALLBACK_DONE;
140 case ECORE_X_WINDOW_STATE_HINT_NORMAL:
143 observer->OnWindowShown();
144 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Shown\n", handler->mEcoreWindow );
145 handled = ECORE_CALLBACK_DONE;
159 /// Called when the window properties are changed.
160 static Eina_Bool EcoreEventClientMessage( void* data, int type, void* event )
162 Eina_Bool handled( ECORE_CALLBACK_PASS_ON );
163 #ifndef DALI_PROFILE_UBUNTU
164 Ecore_X_Event_Client_Message* clientMessageEvent( (Ecore_X_Event_Client_Message*)event );
165 EventHandler* handler( (EventHandler*)data );
167 if (clientMessageEvent->message_type == ECORE_X_ATOM_E_DEICONIFY_APPROVE)
169 ECore::WindowRenderSurface* x11Window( dynamic_cast< ECore::WindowRenderSurface * >( handler->mWindow->mSurface ) );
170 WindowVisibilityObserver* observer( handler->mWindow->mAdaptor );
172 if ( observer && ( (unsigned int)clientMessageEvent->data.l[0] == handler->mEcoreWindow ) )
174 if (clientMessageEvent->data.l[1] == 0) //wm sends request message using value 0
176 observer->OnWindowShown();
178 // request to approve the deiconify. render-surface should send proper event after real rendering
181 x11Window->RequestToApproveDeiconify();
184 handled = ECORE_CALLBACK_DONE;
188 #endif // DALI_PROFILE_UBUNTU
195 Ecore_Event_Handler* mWindowPropertyHandler;
196 Ecore_Event_Handler* mClientMessagehandler;
197 Ecore_X_Window mEcoreWindow;
201 Window* Window::New(const PositionSize& posSize, const std::string& name, bool isTransparent)
203 Window* window = new Window();
204 window->mIsTransparent = isTransparent;
205 window->Initialize(posSize, name);
209 void Window::SetAdaptor(Dali::Adaptor& adaptor)
211 DALI_ASSERT_ALWAYS( !mStarted && "Adaptor already started" );
214 // Only create one overlay per window
215 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
216 Integration::Core& core = adaptorImpl.GetCore();
217 mOverlay = &core.GetSystemOverlay();
219 Dali::RenderTaskList taskList = mOverlay->GetOverlayRenderTasks();
220 taskList.CreateTask();
222 mAdaptor = &adaptorImpl;
223 mAdaptor->AddObserver( *this );
225 // Can only create the detector when we know the Core has been instantiated.
226 mDragAndDropDetector = DragAndDropDetector::New();
227 mAdaptor->SetDragAndDropDetector( &GetImplementation( mDragAndDropDetector ) );
231 mOrientation->SetAdaptor(adaptor);
234 if( mIndicator != NULL )
236 mIndicator->SetAdaptor(mAdaptor);
240 RenderSurface* Window::GetSurface()
245 void Window::ShowIndicator( Dali::Window::IndicatorVisibleMode visibleMode )
247 DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "visible : %d\n", visibleMode );
248 DALI_ASSERT_DEBUG(mOverlay);
250 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
251 DALI_ASSERT_DEBUG(x11Window);
252 Ecore_X_Window xWinId = x11Window->GetXWindow();
254 mIndicatorVisible = visibleMode;
256 if ( mIndicatorVisible == Dali::Window::VISIBLE )
258 // when the indicator is visible, set proper mode for indicator server according to bg mode
259 if ( mIndicatorOpacityMode == Dali::Window::OPAQUE )
261 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_OPAQUE);
263 else if ( mIndicatorOpacityMode == Dali::Window::TRANSLUCENT )
265 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_TRANSLUCENT);
267 #if defined(DALI_PROFILE_MOBILE)
268 else if ( mIndicatorOpacityMode == Dali::Window::TRANSPARENT )
270 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_OPAQUE);
276 // when the indicator is not visible, set TRANSPARENT mode for indicator server
277 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_TRANSPARENT); // it means hidden indicator
280 DoShowIndicator( mIndicatorOrientation );
283 void Window::RotateIndicator(Dali::Window::WindowOrientation orientation)
285 DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "Orientation: %d\n", orientation );
287 DoRotateIndicator( orientation );
290 void Window::SetIndicatorBgOpacity( Dali::Window::IndicatorBgOpacity opacityMode )
292 mIndicatorOpacityMode = opacityMode;
294 if( mIndicator != NULL )
296 mIndicator->SetOpacityMode( opacityMode );
300 void Window::SetClass(std::string name, std::string klass)
302 // Get render surface's x11 window
305 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
308 ecore_x_icccm_name_class_set( x11Window->GetXWindow(), name.c_str(), klass.c_str() );
315 mIndicatorVisible(Dali::Window::INVISIBLE),
316 mIndicatorIsShown(false),
317 mShowRotatedIndicatorOnClose(false),
319 mIsTransparent(false),
320 mWMRotationAppSet(false),
322 mIndicatorOrientation(Dali::Window::PORTRAIT),
323 mNextIndicatorOrientation(Dali::Window::PORTRAIT),
324 mIndicatorOpacityMode(Dali::Window::OPAQUE),
328 mPreferredOrientation(Dali::Window::PORTRAIT)
334 delete mEventHandler;
338 mOverlay->Remove( mIndicator->GetActor() );
339 Dali::RenderTaskList taskList = mOverlay->GetOverlayRenderTasks();
340 Dali::RenderTask indicatorTask = taskList.GetTask(0);
341 mOverlay->GetOverlayRenderTasks().RemoveTask(indicatorTask);
348 mAdaptor->RemoveObserver( *this );
349 mAdaptor->SetDragAndDropDetector( NULL );
356 void Window::Initialize(const PositionSize& windowPosition, const std::string& name)
358 // create an X11 window by default
360 ECore::WindowRenderSurface* windowSurface = new ECore::WindowRenderSurface( windowPosition, surface, name, mIsTransparent );
361 windowSurface->Map();
363 mSurface = windowSurface;
365 mOrientation = Orientation::New(this);
367 // create event handler for X11 window
368 mEventHandler = new EventHandler( this );
371 void Window::DoShowIndicator( Dali::Window::WindowOrientation lastOrientation )
373 if( mIndicator == NULL )
375 if( mIndicatorVisible != Dali::Window::INVISIBLE )
377 mIndicator = new Indicator( mAdaptor, mIndicatorOrientation, this );
378 mIndicator->SetOpacityMode( mIndicatorOpacityMode );
379 Dali::Actor actor = mIndicator->GetActor();
380 SetIndicatorActorRotation();
381 mOverlay->Add(actor);
383 // else don't create a hidden indicator
385 else // Already have indicator
387 if( mIndicatorVisible == Dali::Window::VISIBLE )
389 // If we are resuming, and rotation has changed,
390 if( mIndicatorIsShown == false && mIndicatorOrientation != mNextIndicatorOrientation )
392 // then close current indicator and open new one
393 mShowRotatedIndicatorOnClose = true;
394 mIndicator->Close(); // May synchronously call IndicatorClosed() callback & 1 level of recursion
395 // Don't show actor - will contain indicator for old orientation.
400 // set indicator visible mode
401 if( mIndicator != NULL )
403 mIndicator->SetVisible( mIndicatorVisible );
406 bool show = (mIndicatorVisible != Dali::Window::INVISIBLE );
407 SetIndicatorProperties( show, lastOrientation );
408 mIndicatorIsShown = show;
411 void Window::DoRotateIndicator( Dali::Window::WindowOrientation orientation )
413 if( mIndicatorIsShown )
415 mShowRotatedIndicatorOnClose = true;
416 mNextIndicatorOrientation = orientation;
417 mIndicator->Close(); // May synchronously call IndicatorClosed() callback
421 // Save orientation for when the indicator is next shown
422 mShowRotatedIndicatorOnClose = false;
423 mNextIndicatorOrientation = orientation;
427 void Window::SetIndicatorProperties( bool isShow, Dali::Window::WindowOrientation lastOrientation )
429 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
432 Ecore_X_Window win = x11Window->GetXWindow();
434 int show_state = static_cast<int>( isShow );
435 ecore_x_window_prop_property_set( win,
436 ECORE_X_ATOM_E_ILLUME_INDICATOR_STATE,
437 ECORE_X_ATOM_CARDINAL, 32, &show_state, 1);
441 ecore_x_e_illume_indicator_state_set(win, ECORE_X_ILLUME_INDICATOR_STATE_ON);
445 ecore_x_e_illume_indicator_state_set(win, ECORE_X_ILLUME_INDICATOR_STATE_OFF);
450 void Window::IndicatorTypeChanged(Indicator::Type type)
452 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
455 #ifndef DALI_PROFILE_UBUNTU
456 Ecore_X_Window win = x11Window->GetXWindow();
459 case Indicator::INDICATOR_TYPE_1:
460 ecore_x_e_illume_indicator_type_set( win, ECORE_X_ILLUME_INDICATOR_TYPE_1 );
463 case Indicator::INDICATOR_TYPE_2:
464 ecore_x_e_illume_indicator_type_set( win, ECORE_X_ILLUME_INDICATOR_TYPE_2 );
467 case Indicator::INDICATOR_TYPE_UNKNOWN:
471 #endif // DALI_PROFILE_UBUNTU
475 void Window::IndicatorClosed( Indicator* indicator )
477 DALI_LOG_TRACE_METHOD( gWindowLogFilter );
479 if( mShowRotatedIndicatorOnClose )
481 Dali::Window::WindowOrientation currentOrientation = mIndicatorOrientation;
482 mIndicator->Open(mNextIndicatorOrientation);
483 mIndicatorOrientation = mNextIndicatorOrientation;
484 SetIndicatorActorRotation();
485 DoShowIndicator(currentOrientation);
489 void Window::IndicatorVisibilityChanged(bool isVisible)
491 mIndicatorVisibilityChangedSignal.Emit(isVisible);
494 void Window::SetIndicatorActorRotation()
496 DALI_LOG_TRACE_METHOD( gWindowLogFilter );
497 DALI_ASSERT_DEBUG( mIndicator != NULL );
499 Dali::Actor actor = mIndicator->GetActor();
500 switch( mIndicatorOrientation )
502 case Dali::Window::PORTRAIT:
503 actor.SetParentOrigin( ParentOrigin::TOP_CENTER );
504 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
505 actor.SetOrientation( Degree(0), Vector3::ZAXIS );
507 case Dali::Window::PORTRAIT_INVERSE:
508 actor.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
509 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
510 actor.SetOrientation( Degree(180), Vector3::ZAXIS );
512 case Dali::Window::LANDSCAPE:
513 actor.SetParentOrigin( ParentOrigin::CENTER_LEFT );
514 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
515 actor.SetOrientation( Degree(270), Vector3::ZAXIS );
517 case Dali::Window::LANDSCAPE_INVERSE:
518 actor.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
519 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
520 actor.SetOrientation( Degree(90), Vector3::ZAXIS );
527 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
530 Ecore_X_Window win = x11Window->GetXWindow();
531 ecore_x_window_raise(win);
537 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
540 Ecore_X_Window win = x11Window->GetXWindow();
541 ecore_x_window_lower(win);
545 void Window::Activate()
547 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
550 Ecore_X_Window win = x11Window->GetXWindow();
551 ecore_x_netwm_client_active_request(ecore_x_window_root_get(win), win, 1 /* request type, 1:application, 2:pager */, 0);
555 Dali::DragAndDropDetector Window::GetDragAndDropDetector() const
557 return mDragAndDropDetector;
560 Dali::Any Window::GetNativeHandle() const
564 return mEventHandler->mEcoreWindow;
572 void Window::OnStart()
574 ShowIndicator( mIndicatorVisible );
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)
644 DALI_ASSERT_ALWAYS( mAvailableOrientations.size() <= 4 && "Incorrect number of available orientations" );
646 mAvailableOrientations = orientations;
647 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
650 #ifndef DALI_PROFILE_UBUNTU
651 Ecore_X_Window ecoreWindow = x11Window->GetXWindow();
652 if( ! mWMRotationAppSet )
654 mWMRotationAppSet = true;
655 ecore_x_e_window_rotation_app_set(ecoreWindow, EINA_TRUE);
659 for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
661 rotations[i] = static_cast<int>(mAvailableOrientations[i]);
663 ecore_x_e_window_rotation_available_rotations_set(ecoreWindow, rotations, mAvailableOrientations.size() );
664 #endif // DALI_PROFILE_UBUNTU
668 const std::vector<Dali::Window::WindowOrientation>& Window::GetAvailableOrientations()
670 return mAvailableOrientations;
673 void Window::SetPreferredOrientation(Dali::Window::WindowOrientation orientation)
675 mPreferredOrientation = orientation;
677 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
680 #ifndef DALI_PROFILE_UBUNTU
681 Ecore_X_Window ecoreWindow = x11Window->GetXWindow();
683 if( ! mWMRotationAppSet )
685 mWMRotationAppSet = true;
686 ecore_x_e_window_rotation_app_set(ecoreWindow, EINA_TRUE);
689 ecore_x_e_window_rotation_preferred_rotation_set(ecoreWindow, orientation);
690 #endif // DALI_PROFILE_UBUNTU
694 Dali::Window::WindowOrientation Window::GetPreferredOrientation()
696 return mPreferredOrientation;
699 void Window::RotationDone( int orientation, int width, int height )
701 // Tell window manager we're done
702 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
705 #ifndef DALI_PROFILE_UBUNTU
706 Ecore_X_Window ecoreWindow = x11Window->GetXWindow();
707 Ecore_X_Window root = ecore_x_window_root_get(ecoreWindow);
710 * send rotation done message to wm, even if window is already rotated.
711 * that's why wm must be wait for comming rotation done message
712 * after sending rotation request.
714 ecore_x_e_window_rotation_change_done_send(root, ecoreWindow, orientation, width, height);
717 * set rotate window property
719 int angles[2] = { orientation, orientation };
720 ecore_x_window_prop_property_set( ecoreWindow,
721 ECORE_X_ATOM_E_ILLUME_ROTATE_WINDOW_ANGLE,
722 ECORE_X_ATOM_CARDINAL, 32, &angles, 2 );
723 #endif // DALI_PROFILE_UBUNTU