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, mIsTransparent );
379 SetClass( name, className );
380 windowSurface->Map();
382 mSurface = windowSurface;
384 mOrientation = Orientation::New(this);
386 // create event handler for X11 window
387 mEventHandler = new EventHandler( 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* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
451 Ecore_X_Window win = x11Window->GetXWindow();
453 int show_state = static_cast<int>( isShow );
454 ecore_x_window_prop_property_set( win,
455 ECORE_X_ATOM_E_ILLUME_INDICATOR_STATE,
456 ECORE_X_ATOM_CARDINAL, 32, &show_state, 1);
460 ecore_x_e_illume_indicator_state_set(win, ECORE_X_ILLUME_INDICATOR_STATE_ON);
464 ecore_x_e_illume_indicator_state_set(win, ECORE_X_ILLUME_INDICATOR_STATE_OFF);
469 void Window::IndicatorTypeChanged(Indicator::Type type)
471 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
474 #ifndef DALI_PROFILE_UBUNTU
475 Ecore_X_Window win = x11Window->GetXWindow();
478 case Indicator::INDICATOR_TYPE_1:
479 ecore_x_e_illume_indicator_type_set( win, ECORE_X_ILLUME_INDICATOR_TYPE_1 );
482 case Indicator::INDICATOR_TYPE_2:
483 ecore_x_e_illume_indicator_type_set( win, ECORE_X_ILLUME_INDICATOR_TYPE_2 );
486 case Indicator::INDICATOR_TYPE_UNKNOWN:
490 #endif // DALI_PROFILE_UBUNTU
494 void Window::IndicatorClosed( Indicator* indicator )
496 DALI_LOG_TRACE_METHOD( gWindowLogFilter );
498 if( mShowRotatedIndicatorOnClose )
500 Dali::Window::WindowOrientation currentOrientation = mIndicatorOrientation;
501 mIndicator->Open(mNextIndicatorOrientation);
502 mIndicatorOrientation = mNextIndicatorOrientation;
503 SetIndicatorActorRotation();
504 DoShowIndicator(currentOrientation);
508 void Window::IndicatorVisibilityChanged(bool isVisible)
510 mIndicatorVisibilityChangedSignal.Emit(isVisible);
513 void Window::SetIndicatorActorRotation()
515 DALI_LOG_TRACE_METHOD( gWindowLogFilter );
516 DALI_ASSERT_DEBUG( mIndicator != NULL );
518 Dali::Actor actor = mIndicator->GetActor();
519 switch( mIndicatorOrientation )
521 case Dali::Window::PORTRAIT:
522 actor.SetParentOrigin( ParentOrigin::TOP_CENTER );
523 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
524 actor.SetOrientation( Degree(0), Vector3::ZAXIS );
526 case Dali::Window::PORTRAIT_INVERSE:
527 actor.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
528 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
529 actor.SetOrientation( Degree(180), Vector3::ZAXIS );
531 case Dali::Window::LANDSCAPE:
532 actor.SetParentOrigin( ParentOrigin::CENTER_LEFT );
533 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
534 actor.SetOrientation( Degree(270), Vector3::ZAXIS );
536 case Dali::Window::LANDSCAPE_INVERSE:
537 actor.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
538 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
539 actor.SetOrientation( Degree(90), Vector3::ZAXIS );
546 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
549 Ecore_X_Window win = x11Window->GetXWindow();
550 ecore_x_window_raise(win);
556 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
559 Ecore_X_Window win = x11Window->GetXWindow();
560 ecore_x_window_lower(win);
564 void Window::Activate()
566 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
569 Ecore_X_Window win = x11Window->GetXWindow();
570 ecore_x_netwm_client_active_request(ecore_x_window_root_get(win), win, 1 /* request type, 1:application, 2:pager */, 0);
574 Dali::DragAndDropDetector Window::GetDragAndDropDetector() const
576 return mDragAndDropDetector;
579 Dali::Any Window::GetNativeHandle() const
583 return mEventHandler->mEcoreWindow;
591 void Window::OnStart()
593 ShowIndicator( mIndicatorVisible );
596 void Window::OnPause()
600 void Window::OnResume()
602 // resume indicator status
603 if( mIndicator != NULL )
605 // Restore own indicator opacity
606 // Send opacity mode to indicator service when app resumed
607 mIndicator->SetOpacityMode( mIndicatorOpacityMode );
611 void Window::OnStop()
622 void Window::OnDestroy()
627 void Window::AddAvailableOrientation(Dali::Window::WindowOrientation orientation)
631 for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
633 if(mAvailableOrientations[i] == orientation)
642 mAvailableOrientations.push_back(orientation);
643 SetAvailableOrientations( mAvailableOrientations );
647 void Window::RemoveAvailableOrientation(Dali::Window::WindowOrientation orientation)
649 for( std::vector<Dali::Window::WindowOrientation>::iterator iter = mAvailableOrientations.begin();
650 iter != mAvailableOrientations.end(); ++iter )
652 if( *iter == orientation )
654 mAvailableOrientations.erase( iter );
658 SetAvailableOrientations( mAvailableOrientations );
661 void Window::SetAvailableOrientations(const std::vector<Dali::Window::WindowOrientation>& orientations)
663 DALI_ASSERT_ALWAYS( mAvailableOrientations.size() <= 4 && "Incorrect number of available orientations" );
665 mAvailableOrientations = orientations;
666 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
669 #ifndef DALI_PROFILE_UBUNTU
670 Ecore_X_Window ecoreWindow = x11Window->GetXWindow();
671 if( ! mWMRotationAppSet )
673 mWMRotationAppSet = true;
674 ecore_x_e_window_rotation_app_set(ecoreWindow, EINA_TRUE);
678 for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
680 rotations[i] = static_cast<int>(mAvailableOrientations[i]);
682 ecore_x_e_window_rotation_available_rotations_set(ecoreWindow, rotations, mAvailableOrientations.size() );
683 #endif // DALI_PROFILE_UBUNTU
687 const std::vector<Dali::Window::WindowOrientation>& Window::GetAvailableOrientations()
689 return mAvailableOrientations;
692 void Window::SetPreferredOrientation(Dali::Window::WindowOrientation orientation)
694 mPreferredOrientation = orientation;
696 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
699 #ifndef DALI_PROFILE_UBUNTU
700 Ecore_X_Window ecoreWindow = x11Window->GetXWindow();
702 if( ! mWMRotationAppSet )
704 mWMRotationAppSet = true;
705 ecore_x_e_window_rotation_app_set(ecoreWindow, EINA_TRUE);
708 ecore_x_e_window_rotation_preferred_rotation_set(ecoreWindow, orientation);
709 #endif // DALI_PROFILE_UBUNTU
713 Dali::Window::WindowOrientation Window::GetPreferredOrientation()
715 return mPreferredOrientation;
718 void Window::RotationDone( int orientation, int width, int height )
720 // Tell window manager we're done
721 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
724 #ifndef DALI_PROFILE_UBUNTU
725 Ecore_X_Window ecoreWindow = x11Window->GetXWindow();
726 Ecore_X_Window root = ecore_x_window_root_get(ecoreWindow);
729 * send rotation done message to wm, even if window is already rotated.
730 * that's why wm must be wait for comming rotation done message
731 * after sending rotation request.
733 ecore_x_e_window_rotation_change_done_send(root, ecoreWindow, orientation, width, height);
736 * set rotate window property
738 int angles[2] = { orientation, orientation };
739 ecore_x_window_prop_property_set( ecoreWindow,
740 ECORE_X_ATOM_E_ILLUME_ROTATE_WINDOW_ANGLE,
741 ECORE_X_ATOM_CARDINAL, 32, &angles, 2 );
742 #endif // DALI_PROFILE_UBUNTU