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>
29 #include <orientation.h>
32 #include <window-render-surface.h>
33 #include <drag-and-drop-detector-impl.h>
34 #include <indicator-impl.h>
35 #include <window-visibility-observer.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
92 if ( mWindowPropertyHandler )
94 ecore_event_handler_del( mWindowPropertyHandler );
96 if ( mClientMessagehandler )
98 ecore_event_handler_del( mClientMessagehandler );
104 /// Called when the window properties are changed.
105 static Eina_Bool EcoreEventWindowPropertyChanged( void* data, int type, void* event )
107 Ecore_X_Event_Window_Property* propertyChangedEvent( (Ecore_X_Event_Window_Property*)event );
108 EventHandler* handler( (EventHandler*)data );
109 Eina_Bool handled( ECORE_CALLBACK_PASS_ON );
111 if ( handler && handler->mWindow )
113 WindowVisibilityObserver* observer( handler->mWindow->mAdaptor );
114 if ( observer && ( propertyChangedEvent->win == handler->mEcoreWindow ) )
116 Ecore_X_Window_State_Hint state( ecore_x_icccm_state_get( propertyChangedEvent->win ) );
120 case ECORE_X_WINDOW_STATE_HINT_WITHDRAWN:
122 // Window was hidden.
123 observer->OnWindowHidden();
124 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Withdrawn\n", handler->mEcoreWindow );
125 handled = ECORE_CALLBACK_DONE;
129 case ECORE_X_WINDOW_STATE_HINT_ICONIC:
131 // Window was iconified (minimised).
132 observer->OnWindowHidden();
133 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Iconfied\n", handler->mEcoreWindow );
134 handled = ECORE_CALLBACK_DONE;
138 case ECORE_X_WINDOW_STATE_HINT_NORMAL:
141 observer->OnWindowShown();
142 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Shown\n", handler->mEcoreWindow );
143 handled = ECORE_CALLBACK_DONE;
157 /// Called when the window properties are changed.
158 static Eina_Bool EcoreEventClientMessage( void* data, int type, void* event )
160 Eina_Bool handled( ECORE_CALLBACK_PASS_ON );
161 #ifndef DALI_PROFILE_UBUNTU
162 Ecore_X_Event_Client_Message* clientMessageEvent( (Ecore_X_Event_Client_Message*)event );
163 EventHandler* handler( (EventHandler*)data );
165 if (clientMessageEvent->message_type == ECORE_X_ATOM_E_DEICONIFY_APPROVE)
167 ECore::WindowRenderSurface* x11Window( dynamic_cast< ECore::WindowRenderSurface * >( handler->mWindow->mSurface ) );
168 WindowVisibilityObserver* observer( handler->mWindow->mAdaptor );
170 if ( observer && ( (unsigned int)clientMessageEvent->data.l[0] == handler->mEcoreWindow ) )
172 if (clientMessageEvent->data.l[1] == 0) //wm sends request message using value 0
174 observer->OnWindowShown();
176 // request to approve the deiconify. render-surface should send proper event after real rendering
179 x11Window->RequestToApproveDeiconify();
182 handled = ECORE_CALLBACK_DONE;
186 #endif // DALI_PROFILE_UBUNTU
193 Ecore_Event_Handler* mWindowPropertyHandler;
194 Ecore_Event_Handler* mClientMessagehandler;
195 Ecore_X_Window mEcoreWindow;
199 Window* Window::New(const PositionSize& posSize, const std::string& name, bool isTransparent)
201 Window* window = new Window();
202 window->mIsTransparent = isTransparent;
203 window->Initialize(posSize, name);
207 void Window::SetAdaptor(Dali::Adaptor& adaptor)
209 DALI_ASSERT_ALWAYS( !mStarted && "Adaptor already started" );
212 // Only create one overlay per window
213 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
214 Integration::Core& core = adaptorImpl.GetCore();
215 mOverlay = &core.GetSystemOverlay();
217 Dali::RenderTaskList taskList = mOverlay->GetOverlayRenderTasks();
218 taskList.CreateTask();
220 mAdaptor = &adaptorImpl;
221 mAdaptor->AddObserver( *this );
223 // Can only create the detector when we know the Core has been instantiated.
224 mDragAndDropDetector = DragAndDropDetector::New();
225 mAdaptor->SetDragAndDropDetector( &GetImplementation( mDragAndDropDetector ) );
229 mOrientation->SetAdaptor(adaptor);
232 if( mIndicator != NULL )
234 mIndicator->SetAdaptor(mAdaptor);
238 RenderSurface* Window::GetSurface()
243 void Window::SetIndicatorStyle( Dali::Window::IndicatorStyle style )
245 mIndicatorStyle = style;
248 void Window::ShowIndicator( Dali::Window::IndicatorVisibleMode visibleMode )
250 DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "visible : %d\n", visibleMode );
251 DALI_ASSERT_DEBUG(mOverlay);
253 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
254 DALI_ASSERT_DEBUG(x11Window);
255 Ecore_X_Window xWinId = x11Window->GetXWindow();
257 mIndicatorVisible = visibleMode;
259 if ( mIndicatorVisible == Dali::Window::VISIBLE )
261 // when the indicator is visible, set proper mode for indicator server according to bg mode
262 if ( mIndicatorOpacityMode == Dali::Window::OPAQUE )
264 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_OPAQUE);
266 else if ( mIndicatorOpacityMode == Dali::Window::TRANSLUCENT )
268 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_TRANSLUCENT);
270 #if defined(DALI_PROFILE_MOBILE)
271 else if ( mIndicatorOpacityMode == Dali::Window::TRANSPARENT )
273 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_BG_TRANSPARENT);
279 // when the indicator is not visible, set TRANSPARENT mode for indicator server
280 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_TRANSPARENT); // it means hidden indicator
283 DoShowIndicator( mIndicatorOrientation );
286 void Window::RotateIndicator(Dali::Window::WindowOrientation orientation)
288 DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "Orientation: %d\n", orientation );
290 DoRotateIndicator( orientation );
293 void Window::SetIndicatorBgOpacity( Dali::Window::IndicatorBgOpacity opacityMode )
295 mIndicatorOpacityMode = opacityMode;
297 if( mIndicator != NULL )
299 mIndicator->SetOpacityMode( opacityMode );
303 void Window::SetClass(std::string name, std::string klass)
305 // Get render surface's x11 window
308 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
311 ecore_x_icccm_name_class_set( x11Window->GetXWindow(), name.c_str(), klass.c_str() );
318 mIndicatorStyle(Dali::Window::CHANGEABLE_COLOR),
319 mIndicatorVisible(Dali::Window::VISIBLE),
320 mIndicatorIsShown(false),
321 mShowRotatedIndicatorOnClose(false),
323 mIsTransparent(false),
324 mWMRotationAppSet(false),
326 mIndicatorOrientation(Dali::Window::PORTRAIT),
327 mNextIndicatorOrientation(Dali::Window::PORTRAIT),
328 mIndicatorOpacityMode(Dali::Window::OPAQUE),
332 mPreferredOrientation(Dali::Window::PORTRAIT)
338 delete mEventHandler;
342 mOverlay->Remove( mIndicator->GetActor() );
343 Dali::RenderTaskList taskList = mOverlay->GetOverlayRenderTasks();
344 Dali::RenderTask indicatorTask = taskList.GetTask(0);
345 mOverlay->GetOverlayRenderTasks().RemoveTask(indicatorTask);
352 mAdaptor->RemoveObserver( *this );
353 mAdaptor->SetDragAndDropDetector( NULL );
360 void Window::Initialize(const PositionSize& windowPosition, const std::string& name)
362 // create an X11 window by default
365 mSurface = new ECore::WindowRenderSurface( windowPosition, surface, display, name, mIsTransparent );
366 mOrientation = Orientation::New(this);
368 // create event handler for X11 window
369 mEventHandler = new EventHandler( this );
372 void Window::DoShowIndicator( Dali::Window::WindowOrientation lastOrientation )
374 if( mIndicator == NULL )
376 if( mIndicatorVisible != Dali::Window::INVISIBLE )
378 mIndicator = new Indicator( mAdaptor, mIndicatorOrientation, mIndicatorStyle, this );
379 mIndicator->SetOpacityMode( mIndicatorOpacityMode );
380 Dali::Actor actor = mIndicator->GetActor();
381 SetIndicatorActorRotation();
382 mOverlay->Add(actor);
384 // else don't create a hidden indicator
386 else // Already have indicator
388 if( mIndicatorVisible == Dali::Window::VISIBLE )
390 // If we are resuming, and rotation has changed,
391 if( mIndicatorIsShown == false && mIndicatorOrientation != mNextIndicatorOrientation )
393 // then close current indicator and open new one
394 mShowRotatedIndicatorOnClose = true;
395 mIndicator->Close(); // May synchronously call IndicatorClosed() callback & 1 level of recursion
396 // Don't show actor - will contain indicator for old orientation.
401 // set indicator visible mode
402 if( mIndicator != NULL )
404 mIndicator->SetVisible( mIndicatorVisible );
407 bool show = (mIndicatorVisible != Dali::Window::INVISIBLE );
408 SetIndicatorProperties( show, lastOrientation );
409 mIndicatorIsShown = show;
412 void Window::DoRotateIndicator( Dali::Window::WindowOrientation orientation )
414 if( mIndicatorIsShown )
416 mShowRotatedIndicatorOnClose = true;
417 mNextIndicatorOrientation = orientation;
418 mIndicator->Close(); // May synchronously call IndicatorClosed() callback
422 // Save orientation for when the indicator is next shown
423 mShowRotatedIndicatorOnClose = false;
424 mNextIndicatorOrientation = orientation;
428 void Window::SetIndicatorProperties( bool isShow, Dali::Window::WindowOrientation lastOrientation )
430 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
433 Ecore_X_Window win = x11Window->GetXWindow();
435 int show_state = static_cast<int>( isShow );
436 ecore_x_window_prop_property_set( win,
437 ECORE_X_ATOM_E_ILLUME_INDICATOR_STATE,
438 ECORE_X_ATOM_CARDINAL, 32, &show_state, 1);
442 ecore_x_e_illume_indicator_state_set(win, ECORE_X_ILLUME_INDICATOR_STATE_ON);
446 ecore_x_e_illume_indicator_state_set(win, ECORE_X_ILLUME_INDICATOR_STATE_OFF);
451 void Window::IndicatorTypeChanged(Indicator::Type type)
453 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
456 #ifndef DALI_PROFILE_UBUNTU
457 Ecore_X_Window win = x11Window->GetXWindow();
460 case Indicator::INDICATOR_TYPE_1:
461 ecore_x_e_illume_indicator_type_set( win, ECORE_X_ILLUME_INDICATOR_TYPE_1 );
464 case Indicator::INDICATOR_TYPE_2:
465 ecore_x_e_illume_indicator_type_set( win, ECORE_X_ILLUME_INDICATOR_TYPE_2 );
468 case Indicator::INDICATOR_TYPE_UNKNOWN:
472 #endif // DALI_PROFILE_UBUNTU
476 void Window::IndicatorClosed( Indicator* indicator )
478 DALI_LOG_TRACE_METHOD( gWindowLogFilter );
480 if( mShowRotatedIndicatorOnClose )
482 Dali::Window::WindowOrientation currentOrientation = mIndicatorOrientation;
483 mIndicator->Open(mNextIndicatorOrientation);
484 mIndicatorOrientation = mNextIndicatorOrientation;
485 SetIndicatorActorRotation();
486 DoShowIndicator(currentOrientation);
490 void Window::SetIndicatorActorRotation()
492 DALI_LOG_TRACE_METHOD( gWindowLogFilter );
493 DALI_ASSERT_DEBUG( mIndicator != NULL );
495 Dali::Actor actor = mIndicator->GetActor();
496 switch( mIndicatorOrientation )
498 case Dali::Window::PORTRAIT:
499 actor.SetParentOrigin( ParentOrigin::TOP_CENTER );
500 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
501 actor.SetRotation( Degree(0), Vector3::ZAXIS );
503 case Dali::Window::PORTRAIT_INVERSE:
504 actor.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
505 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
506 actor.SetRotation( Degree(180), Vector3::ZAXIS );
508 case Dali::Window::LANDSCAPE:
509 actor.SetParentOrigin( ParentOrigin::CENTER_LEFT );
510 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
511 actor.SetRotation( Degree(270), Vector3::ZAXIS );
513 case Dali::Window::LANDSCAPE_INVERSE:
514 actor.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
515 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
516 actor.SetRotation( Degree(90), Vector3::ZAXIS );
523 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
526 Ecore_X_Window win = x11Window->GetXWindow();
527 ecore_x_window_raise(win);
533 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
536 Ecore_X_Window win = x11Window->GetXWindow();
537 ecore_x_window_lower(win);
541 void Window::Activate()
543 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
546 Ecore_X_Window win = x11Window->GetXWindow();
547 ecore_x_netwm_client_active_request(ecore_x_window_root_get(win), win, 1 /* request type, 1:application, 2:pager */, 0);
551 Dali::DragAndDropDetector Window::GetDragAndDropDetector() const
553 return mDragAndDropDetector;
556 void Window::OnStart()
558 DoShowIndicator( mIndicatorOrientation );
561 void Window::OnPause()
565 void Window::OnResume()
567 // resume indicator status
568 if( mIndicator != NULL )
570 // Restore own indicator opacity
571 // Send opacity mode to indicator service when app resumed
572 mIndicator->SetOpacityMode( mIndicatorOpacityMode );
576 void Window::OnStop()
587 void Window::OnDestroy()
592 OrientationPtr Window::GetOrientation()
597 void Window::AddAvailableOrientation(Dali::Window::WindowOrientation orientation)
601 for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
603 if(mAvailableOrientations[i] == orientation)
612 mAvailableOrientations.push_back(orientation);
613 SetAvailableOrientations( mAvailableOrientations );
617 void Window::RemoveAvailableOrientation(Dali::Window::WindowOrientation orientation)
619 for( std::vector<Dali::Window::WindowOrientation>::iterator iter = mAvailableOrientations.begin();
620 iter != mAvailableOrientations.end(); ++iter )
622 if( *iter == orientation )
624 mAvailableOrientations.erase( iter );
628 SetAvailableOrientations( mAvailableOrientations );
631 void Window::SetAvailableOrientations(const std::vector<Dali::Window::WindowOrientation>& orientations)
633 DALI_ASSERT_ALWAYS( mAvailableOrientations.size() <= 4 && "Incorrect number of available orientations" );
635 mAvailableOrientations = orientations;
636 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
639 #ifndef DALI_PROFILE_UBUNTU
640 Ecore_X_Window ecoreWindow = x11Window->GetXWindow();
641 if( ! mWMRotationAppSet )
643 mWMRotationAppSet = true;
644 ecore_x_e_window_rotation_app_set(ecoreWindow, EINA_TRUE);
648 for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
650 rotations[i] = static_cast<int>(mAvailableOrientations[i]);
652 ecore_x_e_window_rotation_available_rotations_set(ecoreWindow, rotations, mAvailableOrientations.size() );
653 #endif // DALI_PROFILE_UBUNTU
657 const std::vector<Dali::Window::WindowOrientation>& Window::GetAvailableOrientations()
659 return mAvailableOrientations;
662 void Window::SetPreferredOrientation(Dali::Window::WindowOrientation orientation)
664 mPreferredOrientation = orientation;
666 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
669 #ifndef DALI_PROFILE_UBUNTU
670 Ecore_X_Window ecoreWindow = x11Window->GetXWindow();
672 if( ! mWMRotationAppSet )
674 mWMRotationAppSet = true;
675 ecore_x_e_window_rotation_app_set(ecoreWindow, EINA_TRUE);
678 ecore_x_e_window_rotation_preferred_rotation_set(ecoreWindow, orientation);
679 #endif // DALI_PROFILE_UBUNTU
683 Dali::Window::WindowOrientation Window::GetPreferredOrientation()
685 return mPreferredOrientation;
688 void Window::RotationDone( int orientation, int width, int height )
690 // Tell window manager we're done
691 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
694 #ifndef DALI_PROFILE_UBUNTU
695 Ecore_X_Window ecoreWindow = x11Window->GetXWindow();
696 Ecore_X_Window root = ecore_x_window_root_get(ecoreWindow);
699 * send rotation done message to wm, even if window is already rotated.
700 * that's why wm must be wait for comming rotation done message
701 * after sending rotation request.
703 ecore_x_e_window_rotation_change_done_send(root, ecoreWindow, orientation, width, height);
706 * set rotate window property
708 int angles[2] = { orientation, orientation };
709 ecore_x_window_prop_property_set( ecoreWindow,
710 ECORE_X_ATOM_E_ILLUME_ROTATE_WINDOW_ANGLE,
711 ECORE_X_ATOM_CARDINAL, 32, &angles, 2 );
712 #endif // DALI_PROFILE_UBUNTU