Fix indicator crash
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / common / ecore-indicator-impl.cpp
index c817c47..574e0bd 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.
@@ -19,6 +19,9 @@
 #include "ecore-indicator-impl.h"
 
 // EXTERNAL INCLUDES
+// Ecore is littered with C style cast
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wold-style-cast"
 #include <Ecore.h>
 #include <Evas.h>
 #ifdef WAYLAND
@@ -256,9 +259,9 @@ struct IpcDataEvMouseMove
   unsigned int       timestamp;
   Evas_Event_Flags   event_flags;
 
-  IpcDataEvMouseMove(const Dali::TouchPoint& touchPoint, unsigned long timestamp)
-  : x(static_cast<Evas_Coord>(touchPoint.local.x)),
-    y(static_cast<Evas_Coord>(touchPoint.local.y)),
+  IpcDataEvMouseMove(const Dali::Vector2& touchPoint, unsigned long timestamp)
+  : x(static_cast<Evas_Coord>(touchPoint.x)),
+    y(static_cast<Evas_Coord>(touchPoint.y)),
     flags(EVAS_BUTTON_NONE),
     mask(0),
     timestamp(static_cast<unsigned int>(timestamp)),
@@ -465,7 +468,10 @@ Indicator::LockFile::LockFile(const std::string filename)
 Indicator::LockFile::~LockFile()
 {
   // Closing file descriptor also unlocks file.
-  close( mFileDescriptor );
+  if( mFileDescriptor > 0 )
+  {
+    close( mFileDescriptor );
+  }
 }
 
 bool Indicator::LockFile::Lock()
@@ -475,21 +481,27 @@ bool Indicator::LockFile::Lock()
   bool locked = false;
   if( mFileDescriptor > 0 )
   {
-    if( lockf( mFileDescriptor, F_LOCK, 0 ) == 0 ) // Note, operation may block.
+    struct flock filelock;
+
+    filelock.l_type = F_RDLCK;
+    filelock.l_whence = SEEK_SET;
+    filelock.l_start = 0;
+    filelock.l_len = 0;
+    if( fcntl( mFileDescriptor, F_SETLKW, &filelock ) == -1 )
     {
-      locked = true;
+      mErrorThrown = true;
+      DALI_LOG_ERROR( "### Failed to lock with fd : %s ###\n", mFilename.c_str() );
     }
     else
     {
-      if( errno == EBADF )
-      {
-        // file descriptor is no longer valid or not writable
-        mFileDescriptor = 0;
-        mErrorThrown = true;
-        DALI_LOG_ERROR( "### Cannot lock indicator: bad file descriptor for %s ###\n", mFilename.c_str() );
-      }
+      locked = true;
     }
   }
+  else
+  {
+    mErrorThrown = true;
+    DALI_LOG_ERROR( "### Invalid fd ###\n" );
+  }
 
   return locked;
 }
@@ -497,14 +509,19 @@ bool Indicator::LockFile::Lock()
 void Indicator::LockFile::Unlock()
 {
   DALI_LOG_TRACE_METHOD( gIndicatorLogFilter );
-  if( lockf( mFileDescriptor, F_ULOCK, 0 ) != 0 )
+
+  if( mFileDescriptor > 0 )
   {
-    if( errno == EBADF )
+    struct flock filelock;
+
+    filelock.l_type = F_UNLCK;
+    filelock.l_whence = SEEK_SET;
+    filelock.l_start = 0;
+    filelock.l_len = 0;
+    if (fcntl(mFileDescriptor, F_SETLKW, &filelock) == -1)
     {
-      // file descriptor is no longer valid or not writable
-      mFileDescriptor = 0;
       mErrorThrown = true;
-      DALI_LOG_ERROR( "### Cannot unlock indicator: bad file descriptor for %s\n", mFilename.c_str() );
+      DALI_LOG_ERROR( "### Failed to lock with fd : %s ###\n", mFilename.c_str() );
     }
   }
 }
@@ -541,6 +558,7 @@ bool Indicator::ScopedLock::IsLocked()
 
 Indicator::Indicator( Adaptor* adaptor, Dali::Window::WindowOrientation orientation, IndicatorInterface::Observer* observer )
 : mPixmap( 0 ),
+  mGestureDeltaY( 0.0f ),
   mGestureDetected( false ),
   mConnection( this ),
   mOpacityMode( Dali::Window::OPAQUE ),
@@ -566,7 +584,7 @@ Indicator::Indicator( Adaptor* adaptor, Dali::Window::WindowOrientation orientat
 
   // Indicator image handles the touch event including "leave"
   mIndicatorContentActor.SetLeaveRequired( true );
-  mIndicatorContentActor.TouchedSignal().Connect( this, &Indicator::OnTouched );
+  mIndicatorContentActor.TouchSignal().Connect( this, &Indicator::OnTouch );
   mIndicatorContentActor.SetColor( Color::BLACK );
 
   mIndicatorActor = Dali::Actor::New();
@@ -610,7 +628,7 @@ Indicator::~Indicator()
 
   if(mEventActor)
   {
-    mEventActor.TouchedSignal().Disconnect( this, &Indicator::OnTouched );
+    mEventActor.TouchSignal().Disconnect( this, &Indicator::OnTouch );
   }
   Disconnect();
 }
@@ -780,21 +798,19 @@ bool Indicator::SendMessage( int messageDomain, int messageId, const void *data,
  }
 }
 
-bool Indicator::OnTouched(Dali::Actor indicator, const Dali::TouchEvent& touchEvent)
+bool Indicator::OnTouch(Dali::Actor indicator, const Dali::TouchData& touchData)
 {
   if( mServerConnection )
   {
-    const TouchPoint& touchPoint = touchEvent.GetPoint( 0 );
-
     // Send touch event to indicator server when indicator is showing
     if( CheckVisibleState() || mIsShowing )
     {
-      switch( touchPoint.state )
+      switch( touchData.GetState(0) )
       {
         case Dali::PointState::DOWN:
         {
-          IpcDataEvMouseMove ipcMove( touchPoint, touchEvent.time );
-          IpcDataEvMouseDown ipcDown( touchEvent.time );
+          IpcDataEvMouseMove ipcMove( touchData.GetLocalPosition(0), touchData.GetTime() );
+          IpcDataEvMouseDown ipcDown( touchData.GetTime() );
           mServerConnection->SendEvent( OP_EV_MOUSE_MOVE, &ipcMove, sizeof(ipcMove) );
           mServerConnection->SendEvent( OP_EV_MOUSE_DOWN, &ipcDown, sizeof(ipcDown) );
 
@@ -808,7 +824,7 @@ bool Indicator::OnTouched(Dali::Actor indicator, const Dali::TouchEvent& touchEv
 
         case Dali::PointState::MOTION:
         {
-          IpcDataEvMouseMove ipcMove( touchPoint, touchEvent.time );
+          IpcDataEvMouseMove ipcMove( touchData.GetLocalPosition(0), touchData.GetTime() );
           mServerConnection->SendEvent( OP_EV_MOUSE_MOVE, &ipcMove, sizeof(ipcMove) );
         }
         break;
@@ -816,7 +832,7 @@ bool Indicator::OnTouched(Dali::Actor indicator, const Dali::TouchEvent& touchEv
         case Dali::PointState::UP:
         case Dali::PointState::INTERRUPTED:
         {
-          IpcDataEvMouseUp ipcUp( touchEvent.time );
+          IpcDataEvMouseUp ipcUp( touchData.GetTime() );
           mServerConnection->SendEvent( OP_EV_MOUSE_UP, &ipcUp, sizeof(ipcUp) );
 
           if( mVisible == Dali::Window::AUTO )
@@ -829,9 +845,9 @@ bool Indicator::OnTouched(Dali::Actor indicator, const Dali::TouchEvent& touchEv
 
         case Dali::TouchPoint::Leave:
         {
-          IpcDataEvMouseMove ipcMove( touchPoint, touchEvent.time );
+          IpcDataEvMouseMove ipcMove( touchData.GetLocalPosition(0), touchData.GetTime() );
           mServerConnection->SendEvent( OP_EV_MOUSE_MOVE, &ipcMove, sizeof(ipcMove) );
-          IpcDataEvMouseUp ipcOut( touchEvent.time );
+          IpcDataEvMouseUp ipcOut( touchData.GetTime() );
           mServerConnection->SendEvent( OP_EV_MOUSE_OUT, &ipcOut, sizeof(ipcOut) );
         }
         break;
@@ -1045,9 +1061,11 @@ void Indicator::LoadSharedImage( Ecore_Ipc_Event_Server_Data *epcEvent )
       {
         DALI_LOG_ERROR( "### Indicator error: Cannot open lock file %s ###\n", mSharedFileInfo[n].mLockFileName.c_str() );
       }
-
-      CreateNewImage( n );
-      UpdateVisibility();
+      else
+      {
+        CreateNewImage( n );
+        UpdateVisibility();
+      }
     }
   }
 }
@@ -1140,7 +1158,7 @@ bool Indicator::CopyToBuffer( int bufferNumber )
     else if( scopedLock.IsLocked() )
     {
       unsigned char *src = mSharedFileInfo[bufferNumber].mSharedFile->GetAddress();
-      size_t size = mSharedFileInfo[bufferNumber].mImageWidth * mSharedFileInfo[bufferNumber].mImageHeight * 4;
+      size_t size = static_cast< size_t >( mSharedFileInfo[bufferNumber].mImageWidth ) * mSharedFileInfo[bufferNumber].mImageHeight * 4;
 
       if( mIndicatorBuffer->UpdatePixels( src, size ) )
       {
@@ -1202,14 +1220,7 @@ void Indicator::CreateNewImage( int bufferNumber )
 
   if( !success )
   {
-    DALI_LOG_WARNING("### Cannot create indicator image - disconnecting ###\n");
-    Disconnect();
-    if( mObserver != NULL )
-    {
-      mObserver->IndicatorClosed( this );
-    }
-    // Don't do connection in this callback - strange things happen!
-    StartReconnectionTimer();
+    DALI_LOG_WARNING("### Cannot create indicator image ###\n");
   }
 }
 
@@ -1383,7 +1394,7 @@ void Indicator::DataReceived( void* event )
       DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "Indicator client received: OP_UPDATE\n" );
       if( mIsShowing )
       {
-        //mAdaptor->RequestUpdateOnce();
+        mAdaptor->RequestUpdateOnce();
       }
       break;
     }
@@ -1391,7 +1402,7 @@ void Indicator::DataReceived( void* event )
     {
       DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "Indicator client received: OP_UPDATE_DONE [%d]\n", epcEvent->response );
       // epcEvent->response == display buffer #
-      //UpdateImageData( epcEvent->response );
+      UpdateImageData( epcEvent->response );
       break;
     }
     case OP_SHM_REF0:
@@ -1583,7 +1594,7 @@ void Indicator::ShowIndicator(float duration)
     if( mVisible == Dali::Window::AUTO )
     {
       // check the stage touch
-      Dali::Stage::GetCurrent().TouchedSignal().Connect( this, &Indicator::OnStageTouched );
+      Dali::Stage::GetCurrent().TouchSignal().Connect( this, &Indicator::OnStageTouch );
     }
   }
   else
@@ -1596,7 +1607,7 @@ void Indicator::ShowIndicator(float duration)
     if( mVisible == Dali::Window::AUTO )
     {
       // check the stage touch
-      Dali::Stage::GetCurrent().TouchedSignal().Disconnect( this, &Indicator::OnStageTouched );
+      Dali::Stage::GetCurrent().TouchSignal().Disconnect( this, &Indicator::OnStageTouch );
     }
   }
 }
@@ -1621,73 +1632,20 @@ void Indicator::OnAnimationFinished(Dali::Animation& animation)
 
 void Indicator::OnPan( Dali::Actor actor, const Dali::PanGesture& gesture )
 {
-  return ;
-
-  if( mServerConnection )
-  {
-    switch( gesture.state )
-    {
-      case Gesture::Started:
-      {
-        mGestureDetected = false;
-
-        // The gesture position is the current position after it has moved by the displacement.
-        // We want to reference the original position.
-        mGestureDeltaY = gesture.position.y - gesture.displacement.y;
-      }
-
-      // No break, Fall through
-      case Gesture::Continuing:
-      {
-        if( mVisible == Dali::Window::AUTO && !mIsShowing )
-        {
-          // Only take one touch point
-          if( gesture.numberOfTouches == 1 && mGestureDetected == false )
-          {
-            mGestureDeltaY += gesture.displacement.y;
-
-            if( mGestureDeltaY >= mImageHeight * SHOWING_DISTANCE_HEIGHT_RATE )
-            {
-              ShowIndicator( AUTO_INDICATOR_STAY_DURATION );
-              mGestureDetected = true;
-            }
-          }
-        }
-
-        break;
-      }
-
-      case Gesture::Finished:
-      case Gesture::Cancelled:
-      {
-        // if indicator is showing, hide again when touching is finished (Since touch leave is activated, checking it in gesture::finish instead of touch::up)
-        if( mVisible == Dali::Window::AUTO && mIsShowing )
-        {
-          ShowIndicator( AUTO_INDICATOR_STAY_DURATION );
-        }
-        break;
-      }
-
-
-      default:
-        break;
-    }
-  }
+  // Nothing to do, but we still want to consume pan
 }
 
-void Indicator::OnStageTouched(const Dali::TouchEvent& touchEvent)
+void Indicator::OnStageTouch(const Dali::TouchData& touchData)
 {
-  const TouchPoint& touchPoint = touchEvent.GetPoint( 0 );
-
   // when stage is touched while indicator is showing temporary, hide it
   if( mIsShowing && ( CheckVisibleState() == false || mVisible == Dali::Window::AUTO ) )
   {
-    switch( touchPoint.state )
+    switch( touchData.GetState(0) )
     {
       case Dali::PointState::DOWN:
       {
         // if touch point is inside the indicator, indicator is not hidden
-        if( mImageHeight < int(touchPoint.screen.y) )
+        if( mImageHeight < int( touchData.GetScreenPosition(0).y ) )
         {
           ShowIndicator( HIDE_NOW );
         }
@@ -1701,5 +1659,9 @@ void Indicator::OnStageTouched(const Dali::TouchEvent& touchEvent)
 }
 
 } // Adaptor
+
 } // Internal
+
 } // Dali
+
+#pragma GCC diagnostic pop