Check NULL pointer dereference of the return value
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / wayland / window-impl-ecore-wl.cpp
index e2ad5d6..564999f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
  */
 
 // CLASS HEADER
-#include "window-impl.h"
+#include <window-impl.h>
 
 // EXTERNAL HEADERS
 #include <Ecore.h>
@@ -64,9 +64,25 @@ struct Window::EventHandler
   EventHandler( Window* window )
   : mWindow( window ),
     mWindowPropertyHandler( NULL ),
-    mClientMessageHandler( NULL ),
+    mWindowIconifyStateHandler( NULL ),
+    mWindowFocusInHandler( NULL ),
+    mWindowFocusOutHandler( NULL ),
     mEcoreWindow( 0 )
   {
+    // store ecore window handle
+    ECore::WindowRenderSurface* wlWindow( dynamic_cast< ECore::WindowRenderSurface * >( mWindow->mSurface ) );
+    if( wlWindow )
+    {
+      mEcoreWindow = wlWindow->GetWlWindow();
+    }
+    DALI_ASSERT_ALWAYS( mEcoreWindow != 0 && "There is no ecore Wl window");
+
+    if( mWindow->mEcoreEventHander )
+    {
+      mWindowIconifyStateHandler = ecore_event_handler_add( ECORE_WL_EVENT_WINDOW_ICONIFY_STATE_CHANGE, EcoreEventWindowIconifyStateChanged, this );
+      mWindowFocusInHandler = ecore_event_handler_add( ECORE_WL_EVENT_FOCUS_IN, EcoreEventWindowFocusIn, this );
+      mWindowFocusOutHandler = ecore_event_handler_add( ECORE_WL_EVENT_FOCUS_OUT, EcoreEventWindowFocusOut, this );
+    }
   }
 
   /**
@@ -78,9 +94,17 @@ struct Window::EventHandler
     {
       ecore_event_handler_del( mWindowPropertyHandler );
     }
-    if ( mClientMessageHandler )
+    if ( mWindowIconifyStateHandler )
+    {
+      ecore_event_handler_del( mWindowIconifyStateHandler );
+    }
+    if( mWindowFocusInHandler )
     {
-      ecore_event_handler_del( mClientMessageHandler );
+      ecore_event_handler_del( mWindowFocusInHandler );
+    }
+    if( mWindowFocusOutHandler )
+    {
+      ecore_event_handler_del( mWindowFocusOutHandler );
     }
   }
 
@@ -92,20 +116,76 @@ struct Window::EventHandler
     return EINA_FALSE;
   }
 
-  /// Called when the window properties are changed.
-  static Eina_Bool EcoreEventClientMessage( void* data, int type, void* event )
+  /// Called when the window iconify state is changed.
+  static Eina_Bool EcoreEventWindowIconifyStateChanged( void* data, int type, void* event )
   {
-    return EINA_FALSE;
+    Ecore_Wl_Event_Window_Iconify_State_Change* iconifyChangedEvent( static_cast< Ecore_Wl_Event_Window_Iconify_State_Change* >( event ) );
+    EventHandler* handler( static_cast< EventHandler* >( data ) );
+    Eina_Bool handled( ECORE_CALLBACK_PASS_ON );
+
+    if ( handler && handler->mWindow )
+    {
+      WindowVisibilityObserver* observer( handler->mWindow->mAdaptor );
+      if ( observer && ( iconifyChangedEvent->win == static_cast< unsigned int> ( ecore_wl_window_id_get( handler->mEcoreWindow ) ) ) )
+      {
+        if( iconifyChangedEvent->iconified == EINA_TRUE )
+        {
+          observer->OnWindowHidden();
+          DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Iconfied\n", handler->mEcoreWindow );
+        }
+        else
+        {
+          observer->OnWindowShown();
+          DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Shown\n", handler->mEcoreWindow );
+        }
+        handled = ECORE_CALLBACK_DONE;
+      }
+    }
+
+    return handled;
+  }
+
+  /// Called when the window gains focus
+  static Eina_Bool EcoreEventWindowFocusIn( void* data, int type, void* event )
+  {
+    Ecore_Wl_Event_Focus_In* focusInEvent( static_cast< Ecore_Wl_Event_Focus_In* >( event ) );
+    EventHandler* handler( static_cast< EventHandler* >( data ) );
+
+    if ( handler && handler->mWindow && focusInEvent->win == static_cast< unsigned int >( ecore_wl_window_id_get( handler->mEcoreWindow ) ) )
+    {
+      DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window EcoreEventWindowFocusIn\n" );
+
+      handler->mWindow->mFocusChangedSignal.Emit( true );
+    }
+
+    return ECORE_CALLBACK_PASS_ON;
+  }
+
+  /// Called when the window loses focus
+  static Eina_Bool EcoreEventWindowFocusOut( void* data, int type, void* event )
+  {
+    Ecore_Wl_Event_Focus_Out* focusOutEvent( static_cast< Ecore_Wl_Event_Focus_Out* >( event ) );
+    EventHandler* handler( static_cast< EventHandler* >( data ) );
+
+    if ( handler && handler->mWindow && focusOutEvent->win == static_cast< unsigned int >(ecore_wl_window_id_get( handler->mEcoreWindow ) ) )
+    {
+      DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window EcoreEventWindowFocusOut\n" );
+
+      handler->mWindow->mFocusChangedSignal.Emit( false );
+    }
+
+    return ECORE_CALLBACK_PASS_ON;
   }
 
   // Data
   Window* mWindow;
   Ecore_Event_Handler* mWindowPropertyHandler;
-  Ecore_Event_Handler* mClientMessageHandler;
+  Ecore_Event_Handler* mWindowIconifyStateHandler;
+  Ecore_Event_Handler* mWindowFocusInHandler;
+  Ecore_Event_Handler* mWindowFocusOutHandler;
   Ecore_Wl_Window* mEcoreWindow;
 };
 
-
 Window* Window::New(const PositionSize& posSize, const std::string& name, const std::string& className, bool isTransparent)
 {
   Window* window = new Window();
@@ -155,10 +235,42 @@ void Window::ShowIndicator( Dali::Window::IndicatorVisibleMode visibleMode )
   DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "visible : %d\n", visibleMode );
   DALI_ASSERT_DEBUG(mOverlay);
 
+  ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
+  DALI_ASSERT_DEBUG(wlSurface);
+
+  if( wlSurface )
+  {
+    Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
+
+    mIndicatorVisible = visibleMode;
+
+    if ( mIndicatorVisible == Dali::Window::VISIBLE )
+    {
+      // when the indicator is visible, set proper mode for indicator server according to bg mode
+      if ( mIndicatorOpacityMode == Dali::Window::OPAQUE )
+      {
+        ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_OPAQUE);
+      }
+      else if ( mIndicatorOpacityMode == Dali::Window::TRANSLUCENT )
+      {
+        ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_TRANSLUCENT);
+      }
+      else if ( mIndicatorOpacityMode == Dali::Window::TRANSPARENT )
+      {
+        ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_OPAQUE);
+      }
+    }
+    else
+    {
+      // when the indicator is not visible, set TRANSPARENT mode for indicator server
+      ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_TRANSPARENT); // it means hidden indicator
+    }
+  }
+
   DoShowIndicator( mIndicatorOrientation );
 }
 
-void Window::RotateIndicator(Dali::Window::WindowOrientation orientation)
+void Window::RotateIndicator( Dali::Window::WindowOrientation orientation )
 {
   DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "Orientation: %d\n", orientation );
 
@@ -177,24 +289,39 @@ void Window::SetIndicatorBgOpacity( Dali::Window::IndicatorBgOpacity opacityMode
 
 void Window::SetClass(std::string name, std::string klass)
 {
+  ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
+
+  if( wlSurface )
+  {
+    Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
+    ecore_wl_window_title_set( wlWindow, name.c_str() );
+    ecore_wl_window_class_name_set( wlWindow, klass.c_str() );
+  }
+  else
+  {
+    DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window has no surface\n" );
+  }
 }
 
 Window::Window()
-: mSurface(NULL),
-  mIndicatorVisible(Dali::Window::VISIBLE),
-  mIndicatorIsShown(false),
-  mShowRotatedIndicatorOnClose(false),
-  mStarted(false),
-  mIsTransparent(false),
-  mWMRotationAppSet(false),
-  mIndicator(NULL),
-  mIndicatorOrientation(Dali::Window::PORTRAIT),
-  mNextIndicatorOrientation(Dali::Window::PORTRAIT),
-  mIndicatorOpacityMode(Dali::Window::OPAQUE),
-  mOverlay(NULL),
-  mAdaptor(NULL),
-  mEventHandler(NULL),
-  mPreferredOrientation(Dali::Window::PORTRAIT)
+: mSurface( NULL ),
+  mIndicatorVisible( Dali::Window::VISIBLE ),
+  mIndicatorIsShown( false ),
+  mShowRotatedIndicatorOnClose( false ),
+  mStarted( false ),
+  mIsTransparent( false ),
+  mWMRotationAppSet( false ),
+  mEcoreEventHander( true ),
+  mIsFocusAcceptable( true ),
+  mVisible( true ),
+  mIndicator( NULL ),
+  mIndicatorOrientation( Dali::Window::PORTRAIT ),
+  mNextIndicatorOrientation( Dali::Window::PORTRAIT ),
+  mIndicatorOpacityMode( Dali::Window::OPAQUE ),
+  mOverlay( NULL ),
+  mAdaptor( NULL ),
+  mEventHandler( NULL ),
+  mPreferredOrientation( Dali::Window::PORTRAIT )
 {
 }
 
@@ -223,10 +350,10 @@ void Window::Initialize(const PositionSize& windowPosition, const std::string& n
   // create an Wayland window by default
   Any surface;
   ECore::WindowRenderSurface* windowSurface = new ECore::WindowRenderSurface( windowPosition, surface, name, mIsTransparent );
-  SetClass( name, className );
-  windowSurface->Map();
 
   mSurface = windowSurface;
+  SetClass( name, className );
+  windowSurface->Map();
 
   mOrientation = Orientation::New(this);
 
@@ -292,10 +419,46 @@ void Window::DoRotateIndicator( Dali::Window::WindowOrientation orientation )
 
 void Window::SetIndicatorProperties( bool isShow, Dali::Window::WindowOrientation lastOrientation )
 {
+  ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
+
+  if( wlSurface )
+  {
+    Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
+    if ( isShow )
+    {
+      ecore_wl_window_indicator_state_set(wlWindow, ECORE_WL_INDICATOR_STATE_ON);
+    }
+    else
+    {
+      ecore_wl_window_indicator_state_set(wlWindow, ECORE_WL_INDICATOR_STATE_OFF);
+    }
+  }
 }
 
 void Window::IndicatorTypeChanged(Indicator::Type type)
 {
+#if defined(DALI_PROFILE_MOBILE)
+  ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
+
+  if( wlSurface )
+  {
+    Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
+    switch(type)
+    {
+      case Indicator::INDICATOR_TYPE_1:
+        ecore_wl_indicator_visible_type_set(wlWindow, ECORE_WL_INDICATOR_VISIBLE_TYPE_SHOWN);
+        break;
+
+      case Indicator::INDICATOR_TYPE_2:
+        ecore_wl_indicator_visible_type_set(wlWindow, ECORE_WL_INDICATOR_VISIBLE_TYPE_HIDDEN);
+        break;
+
+      case Indicator::INDICATOR_TYPE_UNKNOWN:
+      default:
+        break;
+    }
+  }
+#endif //MOBILE
 }
 
 void Window::IndicatorClosed( IndicatorInterface* indicator )
@@ -350,14 +513,17 @@ void Window::SetIndicatorActorRotation()
 
 void Window::Raise()
 {
+  ecore_wl_window_raise( mEventHandler->mEcoreWindow );
 }
 
 void Window::Lower()
 {
+  ecore_wl_window_lower( mEventHandler->mEcoreWindow );
 }
 
 void Window::Activate()
 {
+  ecore_wl_window_activate( mEventHandler->mEcoreWindow );
 }
 
 Dali::DragAndDropDetector Window::GetDragAndDropDetector() const
@@ -449,7 +615,12 @@ void Window::RemoveAvailableOrientation(Dali::Window::WindowOrientation orientat
 
 void Window::SetAvailableOrientations(const std::vector<Dali::Window::WindowOrientation>& orientations)
 {
-  DALI_ASSERT_ALWAYS( mAvailableOrientations.size() <= 4 && "Incorrect number of available orientations" );
+  int rotations[4];
+  for( std::size_t i = 0; i < mAvailableOrientations.size(); ++i )
+  {
+    rotations[i] = static_cast< int >( mAvailableOrientations[i] );
+  }
+  ecore_wl_window_rotation_available_rotations_set( mEventHandler->mEcoreWindow, rotations, mAvailableOrientations.size() );
 }
 
 const std::vector<Dali::Window::WindowOrientation>& Window::GetAvailableOrientations()
@@ -460,6 +631,8 @@ const std::vector<Dali::Window::WindowOrientation>& Window::GetAvailableOrientat
 void Window::SetPreferredOrientation(Dali::Window::WindowOrientation orientation)
 {
   mPreferredOrientation = orientation;
+
+  ecore_wl_window_rotation_preferred_rotation_set( mEventHandler->mEcoreWindow, orientation );
 }
 
 Dali::Window::WindowOrientation Window::GetPreferredOrientation()
@@ -467,10 +640,45 @@ Dali::Window::WindowOrientation Window::GetPreferredOrientation()
   return mPreferredOrientation;
 }
 
-void Window::RotationDone( int orientation, int width, int height )
+void Window::SetAcceptFocus( bool accept )
+{
+  mIsFocusAcceptable = accept;
+
+  ecore_wl_window_focus_skip_set( mEventHandler->mEcoreWindow, !accept );
+}
+
+bool Window::IsFocusAcceptable()
+{
+  return mIsFocusAcceptable;
+}
+
+void Window::Show()
 {
+  mVisible = true;
+  ecore_wl_window_show( mEventHandler->mEcoreWindow );
+
+  // Need an update request
+  if( mAdaptor )
+  {
+    mAdaptor->RequestUpdateOnce();
+  }
+}
+
+void Window::Hide()
+{
+  mVisible = false;
+  ecore_wl_window_hide( mEventHandler->mEcoreWindow );
 }
 
+bool Window::IsVisible() const
+{
+  return mVisible;
+}
+
+void Window::RotationDone( int orientation, int width, int height )
+{
+  ecore_wl_window_rotation_change_done_send( mEventHandler->mEcoreWindow );
+}
 
 } // Adaptor
 } // Internal