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 // set property on window to get deiconify approve client message
80 ecore_x_window_prop_card32_set(mEcoreWindow,
81 ECORE_X_ATOM_E_DEICONIFY_APPROVE,
90 if ( mWindowPropertyHandler )
92 ecore_event_handler_del( mWindowPropertyHandler );
94 if ( mClientMessagehandler )
96 ecore_event_handler_del( mClientMessagehandler );
102 /// Called when the window properties are changed.
103 static Eina_Bool EcoreEventWindowPropertyChanged( void* data, int type, void* event )
105 Ecore_X_Event_Window_Property* propertyChangedEvent( (Ecore_X_Event_Window_Property*)event );
106 EventHandler* handler( (EventHandler*)data );
107 Eina_Bool handled( ECORE_CALLBACK_PASS_ON );
109 if ( handler && handler->mWindow )
111 WindowVisibilityObserver* observer( handler->mWindow->mAdaptor );
112 if ( observer && ( propertyChangedEvent->win == handler->mEcoreWindow ) )
114 Ecore_X_Window_State_Hint state( ecore_x_icccm_state_get( propertyChangedEvent->win ) );
118 case ECORE_X_WINDOW_STATE_HINT_WITHDRAWN:
120 // Window was hidden.
121 observer->OnWindowHidden();
122 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Withdrawn\n", handler->mEcoreWindow );
123 handled = ECORE_CALLBACK_DONE;
127 case ECORE_X_WINDOW_STATE_HINT_ICONIC:
129 // Window was iconified (minimised).
130 observer->OnWindowHidden();
131 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Iconfied\n", handler->mEcoreWindow );
132 handled = ECORE_CALLBACK_DONE;
136 case ECORE_X_WINDOW_STATE_HINT_NORMAL:
139 observer->OnWindowShown();
140 DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Shown\n", handler->mEcoreWindow );
141 handled = ECORE_CALLBACK_DONE;
155 /// Called when the window properties are changed.
156 static Eina_Bool EcoreEventClientMessage( void* data, int type, void* event )
158 Eina_Bool handled( ECORE_CALLBACK_PASS_ON );
159 Ecore_X_Event_Client_Message* clientMessageEvent( (Ecore_X_Event_Client_Message*)event );
160 EventHandler* handler( (EventHandler*)data );
162 if (clientMessageEvent->message_type == ECORE_X_ATOM_E_DEICONIFY_APPROVE)
164 ECore::WindowRenderSurface* x11Window( dynamic_cast< ECore::WindowRenderSurface * >( handler->mWindow->mSurface ) );
165 WindowVisibilityObserver* observer( handler->mWindow->mAdaptor );
167 if ( observer && ( (unsigned int)clientMessageEvent->data.l[0] == handler->mEcoreWindow ) )
169 if (clientMessageEvent->data.l[1] == 0) //wm sends request message using value 0
171 observer->OnWindowShown();
173 // request to approve the deiconify. render-surface should send proper event after real rendering
176 x11Window->RequestToApproveDeiconify();
179 handled = ECORE_CALLBACK_DONE;
189 Ecore_Event_Handler* mWindowPropertyHandler;
190 Ecore_Event_Handler* mClientMessagehandler;
191 Ecore_X_Window mEcoreWindow;
195 Window* Window::New(const PositionSize& posSize, const std::string& name, bool isTransparent)
197 Window* window = new Window();
198 window->mIsTransparent = isTransparent;
199 window->Initialize(posSize, name);
203 void Window::SetAdaptor(Dali::Adaptor& adaptor)
205 DALI_ASSERT_ALWAYS( !mStarted && "Adaptor already started" );
208 // Only create one overlay per window
209 Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
210 Integration::Core& core = adaptorImpl.GetCore();
211 mOverlay = &core.GetSystemOverlay();
213 Dali::RenderTaskList taskList = mOverlay->GetOverlayRenderTasks();
214 taskList.CreateTask();
216 mAdaptor = &adaptorImpl;
217 mAdaptor->AddObserver( *this );
219 // Can only create the detector when we know the Core has been instantiated.
220 mDragAndDropDetector = DragAndDropDetector::New();
221 mAdaptor->SetDragAndDropDetector( &GetImplementation( mDragAndDropDetector ) );
225 mOrientation->SetAdaptor(adaptor);
228 if( mIndicator != NULL )
230 mIndicator->SetAdaptor(mAdaptor);
234 RenderSurface* Window::GetSurface()
239 void Window::SetIndicatorStyle( Dali::Window::IndicatorStyle style )
241 mIndicatorStyle = style;
244 void Window::ShowIndicator( bool show )
246 DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "%s\n", show?"SHOW":"HIDE" );
247 DALI_ASSERT_DEBUG(mOverlay);
251 mIndicatorVisible = Dali::Window::VISIBLE;
255 mIndicatorVisible = Dali::Window::INVISIBLE;
258 DoShowIndicator( mIndicatorOrientation );
261 void Window::ShowIndicator( Dali::Window::IndicatorVisibleMode visibleMode )
263 DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "visible : %d\n", visibleMode );
264 DALI_ASSERT_DEBUG(mOverlay);
266 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
267 DALI_ASSERT_DEBUG(x11Window);
268 Ecore_X_Window xWinId = x11Window->GetXWindow();
270 mIndicatorVisible = visibleMode;
272 if ( mIndicatorVisible == Dali::Window::VISIBLE )
274 // when the indicator is visible, set proper mode for indicator server according to bg mode
275 if ( mIndicatorOpacityMode == Dali::Window::OPAQUE )
277 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_OPAQUE);
279 else if ( mIndicatorOpacityMode == Dali::Window::TRANSLUCENT )
281 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_TRANSLUCENT);
283 #if defined(DALI_PROFILE_MOBILE)
284 else if ( mIndicatorOpacityMode == Dali::Window::TRANSPARENT )
286 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_BG_TRANSPARENT);
292 // when the indicator is not visible, set TRANSPARENT mode for indicator server
293 ecore_x_e_illume_indicator_opacity_set(xWinId, ECORE_X_ILLUME_INDICATOR_TRANSPARENT); // it means hidden indicator
296 DoShowIndicator( mIndicatorOrientation );
299 void Window::RotateIndicator(Dali::Window::WindowOrientation orientation)
301 DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "Orientation: %d\n", orientation );
303 DoRotateIndicator( orientation );
306 void Window::SetIndicatorBgOpacity( Dali::Window::IndicatorBgOpacity opacityMode )
308 mIndicatorOpacityMode = opacityMode;
310 if( mIndicator != NULL )
312 mIndicator->SetOpacityMode( opacityMode );
316 void Window::SetClass(std::string name, std::string klass)
318 // Get render surface's x11 window
321 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
324 ecore_x_icccm_name_class_set( x11Window->GetXWindow(), name.c_str(), klass.c_str() );
331 mIndicatorStyle(Dali::Window::CHANGEABLE_COLOR),
332 mIndicatorVisible(Dali::Window::VISIBLE),
333 mIndicatorIsShown(false),
334 mShowRotatedIndicatorOnClose(false),
336 mIsTransparent(false),
337 mWMRotationAppSet(false),
339 mIndicatorOrientation(Dali::Window::PORTRAIT),
340 mNextIndicatorOrientation(Dali::Window::PORTRAIT),
341 mIndicatorOpacityMode(Dali::Window::OPAQUE),
349 delete mEventHandler;
353 mAdaptor->RemoveObserver( *this );
354 mAdaptor->SetDragAndDropDetector( NULL );
361 void Window::Initialize(const PositionSize& windowPosition, const std::string& name)
363 // create an X11 window by default
366 mSurface = new ECore::WindowRenderSurface( windowPosition, surface, display, name, mIsTransparent );
367 mOrientation = Orientation::New(this);
369 // create event handler for X11 window
370 mEventHandler = new EventHandler( this );
373 void Window::DoShowIndicator( Dali::Window::WindowOrientation lastOrientation )
375 if( mIndicator == NULL )
377 if( mIndicatorVisible != Dali::Window::INVISIBLE )
379 mIndicator = new Indicator( mAdaptor, mIndicatorOrientation, mIndicatorStyle, this );
380 mIndicator->SetOpacityMode( mIndicatorOpacityMode );
381 Dali::Actor actor = mIndicator->GetActor();
382 SetIndicatorActorRotation();
383 mOverlay->Add(actor);
385 // else don't create a hidden indicator
387 else // Already have indicator
389 if( mIndicatorVisible == Dali::Window::VISIBLE )
391 // If we are resuming, and rotation has changed,
392 if( mIndicatorIsShown == false && mIndicatorOrientation != mNextIndicatorOrientation )
394 // then close current indicator and open new one
395 mShowRotatedIndicatorOnClose = true;
396 mIndicator->Close(); // May synchronously call IndicatorClosed() callback & 1 level of recursion
397 // Don't show actor - will contain indicator for old orientation.
402 // set indicator visible mode
403 if( mIndicator != NULL )
405 mIndicator->SetVisible( mIndicatorVisible );
408 bool show = (mIndicatorVisible != Dali::Window::INVISIBLE );
409 SetIndicatorProperties( show, lastOrientation );
410 mIndicatorIsShown = show;
413 void Window::DoRotateIndicator( Dali::Window::WindowOrientation orientation )
415 if( mIndicatorIsShown )
417 mShowRotatedIndicatorOnClose = true;
418 mNextIndicatorOrientation = orientation;
419 mIndicator->Close(); // May synchronously call IndicatorClosed() callback
423 // Save orientation for when the indicator is next shown
424 mShowRotatedIndicatorOnClose = false;
425 mNextIndicatorOrientation = orientation;
429 void Window::SetIndicatorProperties( bool isShow, Dali::Window::WindowOrientation lastOrientation )
431 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
434 Ecore_X_Window win = x11Window->GetXWindow();
436 int show_state = static_cast<int>( isShow );
437 ecore_x_window_prop_property_set( win,
438 ECORE_X_ATOM_E_ILLUME_INDICATOR_STATE,
439 ECORE_X_ATOM_CARDINAL, 32, &show_state, 1);
443 ecore_x_e_illume_indicator_state_set(win, ECORE_X_ILLUME_INDICATOR_STATE_ON);
447 ecore_x_e_illume_indicator_state_set(win, ECORE_X_ILLUME_INDICATOR_STATE_OFF);
452 void Window::IndicatorTypeChanged(Indicator::Type type)
454 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
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:
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::SetIndicatorActorRotation()
491 DALI_LOG_TRACE_METHOD( gWindowLogFilter );
492 DALI_ASSERT_DEBUG( mIndicator != NULL );
494 Dali::Actor actor = mIndicator->GetActor();
495 switch( mIndicatorOrientation )
497 case Dali::Window::PORTRAIT:
498 actor.SetParentOrigin( ParentOrigin::TOP_CENTER );
499 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
500 actor.SetRotation( Degree(0), Vector3::ZAXIS );
502 case Dali::Window::PORTRAIT_INVERSE:
503 actor.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
504 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
505 actor.SetRotation( Degree(180), Vector3::ZAXIS );
507 case Dali::Window::LANDSCAPE:
508 actor.SetParentOrigin( ParentOrigin::CENTER_LEFT );
509 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
510 actor.SetRotation( Degree(270), Vector3::ZAXIS );
512 case Dali::Window::LANDSCAPE_INVERSE:
513 actor.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
514 actor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
515 actor.SetRotation( Degree(90), Vector3::ZAXIS );
522 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
525 Ecore_X_Window win = x11Window->GetXWindow();
526 ecore_x_window_raise(win);
532 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
535 Ecore_X_Window win = x11Window->GetXWindow();
536 ecore_x_window_lower(win);
540 void Window::Activate()
542 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
545 Ecore_X_Window win = x11Window->GetXWindow();
546 ecore_x_netwm_client_active_request(ecore_x_window_root_get(win), win, 1 /* request type, 1:application, 2:pager */, 0);
550 Dali::DragAndDropDetector Window::GetDragAndDropDetector() const
552 return mDragAndDropDetector;
555 void Window::OnStart()
557 DoShowIndicator( mIndicatorOrientation );
560 void Window::OnPause()
564 void Window::OnResume()
566 // resume indicator status
567 if( mIndicator != NULL )
569 // Restore own indicator opacity
570 // Send opacity mode to indicator service when app resumed
571 mIndicator->SetOpacityMode( mIndicatorOpacityMode );
575 void Window::OnStop()
586 void Window::OnDestroy()
591 OrientationPtr Window::GetOrientation()
596 void Window::AddAvailableOrientation(Dali::Window::WindowOrientation orientation)
600 for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
602 if(mAvailableOrientations[i] == orientation)
611 mAvailableOrientations.push_back(orientation);
612 SetAvailableOrientations( mAvailableOrientations );
616 void Window::RemoveAvailableOrientation(Dali::Window::WindowOrientation orientation)
618 for( std::vector<Dali::Window::WindowOrientation>::iterator iter = mAvailableOrientations.begin();
619 iter != mAvailableOrientations.end(); ++iter )
621 if( *iter == orientation )
623 mAvailableOrientations.erase( iter );
627 SetAvailableOrientations( mAvailableOrientations );
630 void Window::SetAvailableOrientations(const std::vector<Dali::Window::WindowOrientation>& orientations)
632 DALI_ASSERT_ALWAYS( mAvailableOrientations.size() <= 4 && "Incorrect number of available orientations" );
634 mAvailableOrientations = orientations;
635 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
638 Ecore_X_Window ecoreWindow = x11Window->GetXWindow();
639 if( ! mWMRotationAppSet )
641 mWMRotationAppSet = true;
642 ecore_x_e_window_rotation_app_set(ecoreWindow, EINA_TRUE);
646 for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
648 rotations[i] = static_cast<int>(mAvailableOrientations[i]);
650 ecore_x_e_window_rotation_available_rotations_set(ecoreWindow, rotations, mAvailableOrientations.size() );
655 const std::vector<Dali::Window::WindowOrientation>& Window::GetAvailableOrientations()
657 return mAvailableOrientations;
660 void Window::SetPreferredOrientation(Dali::Window::WindowOrientation orientation)
662 mPreferredOrientation = orientation;
664 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
667 Ecore_X_Window ecoreWindow = x11Window->GetXWindow();
669 if( ! mWMRotationAppSet )
671 mWMRotationAppSet = true;
672 ecore_x_e_window_rotation_app_set(ecoreWindow, EINA_TRUE);
675 ecore_x_e_window_rotation_preferred_rotation_set(ecoreWindow, orientation);
679 Dali::Window::WindowOrientation Window::GetPreferredOrientation()
681 return mPreferredOrientation;
684 void Window::RotationDone( int orientation, int width, int height )
686 // Tell window manager we're done
687 ECore::WindowRenderSurface* x11Window = dynamic_cast< ECore::WindowRenderSurface * >( mSurface );
690 Ecore_X_Window ecoreWindow = x11Window->GetXWindow();
691 Ecore_X_Window root = ecore_x_window_root_get(ecoreWindow);
694 * send rotation done message to wm, even if window is already rotated.
695 * that's why wm must be wait for comming rotation done message
696 * after sending rotation request.
698 ecore_x_e_window_rotation_change_done_send(root, ecoreWindow, orientation, width, height);
701 * set rotate window property
703 int angles[2] = { orientation, orientation };
704 ecore_x_window_prop_property_set( ecoreWindow,
705 ECORE_X_ATOM_E_ILLUME_ROTATE_WINDOW_ANGLE,
706 ECORE_X_ATOM_CARDINAL, 32, &angles, 2 );