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 "ecore-indicator-impl.h"
25 #include <Ecore_Wayland.h>
30 #include <sys/types.h>
36 #include <dali/public-api/images/native-image.h>
37 #include <dali/public-api/events/touch-event.h>
38 #include <dali/public-api/events/touch-point.h>
39 #include <dali/public-api/common/stage.h>
40 #include <dali/public-api/images/buffer-image.h>
42 #include <dali/integration-api/debug.h>
45 #include <adaptor-impl.h>
46 #include <accessibility-adaptor-impl.h>
47 #include <native-image-source.h>
51 #if defined(DEBUG_ENABLED)
52 #define STATE_DEBUG_STRING(state) (state==DISCONNECTED?"DISCONNECTED":state==CONNECTED?"CONNECTED":"UNKNOWN")
58 const float SLIDING_ANIMATION_DURATION( 0.2f ); // 200 milli seconds
59 const float AUTO_INDICATOR_STAY_DURATION(3.0f); // 3 seconds
60 const float SHOWING_DISTANCE_HEIGHT_RATE(0.34f); // 20 pixels
68 const int NUM_GRADIENT_INTERVALS(5); // Number of gradient intervals
69 const float GRADIENT_ALPHA[NUM_GRADIENT_INTERVALS+1] = { 0.6f, 0.38f, 0.20f, 0.08f, 0.0f, 0.0f };
71 #define MAKE_SHADER(A)#A
73 const char* BACKGROUND_VERTEX_SHADER = MAKE_SHADER(
74 attribute mediump vec2 aPosition;
75 attribute mediump float aAlpha;
76 varying mediump float vAlpha;
77 uniform mediump mat4 uMvpMatrix;
78 uniform mediump vec3 uSize;
82 mediump vec4 vertexPosition = vec4( aPosition * uSize.xy, 0.0, 1.0 );
83 vertexPosition = uMvpMatrix * vertexPosition;
86 gl_Position = vertexPosition;
90 const char* BACKGROUND_FRAGMENT_SHADER = MAKE_SHADER(
91 uniform lowp vec4 uColor;
92 varying mediump float vAlpha;
96 gl_FragColor = uColor;
97 gl_FragColor.a *= vAlpha;
101 const char* FOREGROUND_VERTEX_SHADER = DALI_COMPOSE_SHADER(
102 attribute mediump vec2 aPosition;\n
103 varying mediump vec2 vTexCoord;\n
104 uniform mediump mat4 uMvpMatrix;\n
105 uniform mediump vec3 uSize;\n
106 uniform mediump vec4 sTextureRect;\n
110 gl_Position = uMvpMatrix * vec4(aPosition * uSize.xy, 0.0, 1.0);\n
111 vTexCoord = aPosition + vec2(0.5);\n
115 const char* FOREGROUND_FRAGMENT_SHADER = DALI_COMPOSE_SHADER(
116 varying mediump vec2 vTexCoord;\n
117 uniform sampler2D sTexture;\n
121 gl_FragColor = texture2D( sTexture, vTexCoord );\n // the foreground does not apply actor color
125 Dali::Geometry CreateQuadGeometry()
127 Dali::Property::Map quadVertexFormat;
128 quadVertexFormat["aPosition"] = Dali::Property::VECTOR2;
129 Dali::PropertyBuffer vertexData = Dali::PropertyBuffer::New( quadVertexFormat );
131 const float halfQuadSize = .5f;
132 struct QuadVertex { Dali::Vector2 position; };
133 QuadVertex quadVertexData[4] = {
134 { Dali::Vector2(-halfQuadSize, -halfQuadSize) },
135 { Dali::Vector2(-halfQuadSize, halfQuadSize) },
136 { Dali::Vector2( halfQuadSize, -halfQuadSize) },
137 { Dali::Vector2( halfQuadSize, halfQuadSize) } };
138 vertexData.SetData(quadVertexData, 4);
140 Dali::Geometry quad = Dali::Geometry::New();
141 quad.AddVertexBuffer( vertexData );
142 quad.SetType( Dali::Geometry::TRIANGLE_STRIP );
146 const float OPAQUE_THRESHOLD(0.99f);
147 const float TRANSPARENT_THRESHOLD(0.05f);
149 // indicator service name
150 const char* INDICATOR_SERVICE_NAME("elm_indicator");
152 // Copied from ecore_evas_extn_engine.h
168 OP_PROFILE_CHANGE_REQUEST,
169 OP_PROFILE_CHANGE_DONE,
187 // Copied from elm_conform.c
189 const int MSG_DOMAIN_CONTROL_INDICATOR( 0x10001 );
190 const int MSG_ID_INDICATOR_REPEAT_EVENT( 0x10002 );
191 const int MSG_ID_INDICATOR_ROTATION( 0x10003 );
192 const int MSG_ID_INDICATOR_OPACITY( 0X1004 );
193 const int MSG_ID_INDICATOR_TYPE( 0X1005 );
194 const int MSG_ID_INDICATOR_START_ANIMATION( 0X10006 );
206 struct IpcIndicatorDataAnimation
212 struct IpcDataEvMouseUp
215 Evas_Button_Flags flags;
217 unsigned int timestamp;
218 Evas_Event_Flags event_flags;
220 IpcDataEvMouseUp(unsigned long timestamp)
222 flags(EVAS_BUTTON_NONE),
224 timestamp(static_cast<unsigned int>(timestamp)),
225 event_flags(EVAS_EVENT_FLAG_NONE)
230 struct IpcDataEvMouseDown
233 Evas_Button_Flags flags;
235 unsigned int timestamp;
236 Evas_Event_Flags event_flags;
238 IpcDataEvMouseDown(unsigned long timestamp)
240 flags(EVAS_BUTTON_NONE),
242 timestamp(static_cast<unsigned int>(timestamp)),
243 event_flags(EVAS_EVENT_FLAG_NONE)
248 struct IpcDataEvMouseMove
251 Evas_Button_Flags flags;
253 unsigned int timestamp;
254 Evas_Event_Flags event_flags;
256 IpcDataEvMouseMove(const Dali::TouchPoint& touchPoint, unsigned long timestamp)
257 : x(static_cast<Evas_Coord>(touchPoint.local.x)),
258 y(static_cast<Evas_Coord>(touchPoint.local.y)),
259 flags(EVAS_BUTTON_NONE),
261 timestamp(static_cast<unsigned int>(timestamp)),
262 event_flags(EVAS_EVENT_FLAG_NONE)
267 struct IpcDataEvMouseOut
269 unsigned int timestamp;
271 Evas_Event_Flags event_flags;
273 IpcDataEvMouseOut(unsigned long timestamp)
274 : timestamp(static_cast<unsigned int>(timestamp)),
276 event_flags(EVAS_EVENT_FLAG_NONE)
281 } // anonymous namespace
290 #if defined(DEBUG_ENABLED)
291 Debug::Filter* gIndicatorLogFilter = Debug::Filter::New(Debug::Concise, false, "LOG_INDICATOR");
294 // Impl to hide EFL implementation.
296 struct Indicator::Impl
298 enum // operation mode
301 INDICATOR_STAY_WITH_DURATION
307 Impl(Indicator* indicator)
308 : mIndicator(indicator),
309 mEcoreEventHandler(NULL)
311 #if defined(DALI_PROFILE_MOBILE)
313 mEcoreEventHandler = ecore_event_handler_add(ECORE_WL_EVENT_INDICATOR_FLICK, EcoreEventIndicator, this);
315 mEcoreEventHandler = ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, EcoreEventClientMessage, this);
317 #endif // WAYLAND && DALI_PROFILE_MOBILE
325 if ( mEcoreEventHandler )
327 ecore_event_handler_del(mEcoreEventHandler);
331 static void SetIndicatorVisibility( void* data, int operation )
333 Indicator::Impl* indicatorImpl((Indicator::Impl*)data);
335 if ( indicatorImpl == NULL || indicatorImpl->mIndicator == NULL)
339 if ( operation == INDICATOR_STAY_WITH_DURATION )
341 // if indicator is not showing, INDICATOR_FLICK_DONE is given
342 if( indicatorImpl->mIndicator->mVisible == Dali::Window::AUTO &&
343 !indicatorImpl->mIndicator->mIsShowing )
345 indicatorImpl->mIndicator->ShowIndicator( AUTO_INDICATOR_STAY_DURATION );
348 else if( operation == INDICATOR_HIDE )
350 if( indicatorImpl->mIndicator->mVisible == Dali::Window::AUTO &&
351 indicatorImpl->mIndicator->mIsShowing )
353 indicatorImpl->mIndicator->ShowIndicator( HIDE_NOW );
357 #if defined(DALI_PROFILE_MOBILE)
360 * Called when the Ecore indicator event is received.
362 static Eina_Bool EcoreEventIndicator( void* data, int type, void* event )
364 SetIndicatorVisibility( data, INDICATOR_STAY_WITH_DURATION );
365 return ECORE_CALLBACK_PASS_ON;
369 * Called when the client messages (i.e. quick panel state) are received.
371 static Eina_Bool EcoreEventClientMessage( void* data, int type, void* event )
373 Ecore_X_Event_Client_Message* clientMessageEvent((Ecore_X_Event_Client_Message*)event);
375 if ( clientMessageEvent != NULL )
377 if (clientMessageEvent->message_type == ECORE_X_ATOM_E_INDICATOR_FLICK_DONE)
379 SetIndicatorVisibility( data, INDICATOR_STAY_WITH_DURATION );
381 else if ( clientMessageEvent->message_type == ECORE_X_ATOM_E_MOVE_QUICKPANEL_STATE )
383 SetIndicatorVisibility( data, INDICATOR_HIDE );
386 return ECORE_CALLBACK_PASS_ON;
389 #endif // WAYLAND && DALI_PROFILE_MOBILE
392 Indicator* mIndicator;
393 Ecore_Event_Handler* mEcoreEventHandler;
396 Indicator::LockFile::LockFile(const std::string filename)
397 : mFilename(filename),
400 mFileDescriptor = open(filename.c_str(), O_RDWR);
401 if( mFileDescriptor == -1 )
405 DALI_LOG_ERROR( "### Cannot open %s for indicator lock ###\n", mFilename.c_str() );
409 Indicator::LockFile::~LockFile()
411 // Closing file descriptor also unlocks file.
412 close( mFileDescriptor );
415 bool Indicator::LockFile::Lock()
417 DALI_LOG_TRACE_METHOD( gIndicatorLogFilter );
420 if( mFileDescriptor > 0 )
422 struct flock filelock;
424 filelock.l_type = F_RDLCK;
425 filelock.l_whence = SEEK_SET;
426 filelock.l_start = 0;
428 if( fcntl( mFileDescriptor, F_SETLKW, &filelock ) == -1 )
431 DALI_LOG_ERROR( "### Failed to lock with fd : %s ###\n", mFilename.c_str() );
441 DALI_LOG_ERROR( "### Invalid fd ###\n" );
447 void Indicator::LockFile::Unlock()
449 DALI_LOG_TRACE_METHOD( gIndicatorLogFilter );
451 struct flock filelock;
453 filelock.l_type = F_UNLCK;
454 filelock.l_whence = SEEK_SET;
455 filelock.l_start = 0;
457 if (fcntl(mFileDescriptor, F_SETLKW, &filelock) == -1)
460 DALI_LOG_ERROR( "### Failed to lock with fd : %s ###\n", mFilename.c_str() );
464 bool Indicator::LockFile::RetrieveAndClearErrorStatus()
466 bool error = mErrorThrown;
467 mErrorThrown = false;
471 Indicator::ScopedLock::ScopedLock(LockFile* lockFile)
472 : mLockFile(lockFile),
477 mLocked = mLockFile->Lock();
481 Indicator::ScopedLock::~ScopedLock()
489 bool Indicator::ScopedLock::IsLocked()
494 Indicator::Indicator( Adaptor* adaptor, Dali::Window::WindowOrientation orientation, IndicatorInterface::Observer* observer )
496 mGestureDetected( false ),
498 mOpacityMode( Dali::Window::OPAQUE ),
499 mState( DISCONNECTED ),
501 mServerConnection( NULL ),
502 mObserver( observer ),
503 mOrientation( orientation ),
506 mVisible( Dali::Window::INVISIBLE ),
508 mIsAnimationPlaying( false ),
509 mCurrentSharedFile( 0 ),
510 mSharedBufferType( BUFFER_TYPE_SHM ),
512 mBackgroundVisible( false ),
515 mIndicatorContentActor = Dali::Actor::New();
516 mIndicatorContentActor.SetParentOrigin( ParentOrigin::TOP_CENTER );
517 mIndicatorContentActor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
519 // Indicator image handles the touch event including "leave"
520 mIndicatorContentActor.SetLeaveRequired( true );
521 mIndicatorContentActor.TouchedSignal().Connect( this, &Indicator::OnTouched );
522 mIndicatorContentActor.SetColor( Color::BLACK );
524 mIndicatorActor = Dali::Actor::New();
525 mIndicatorActor.Add( mIndicatorContentActor );
527 // Event handler to find out flick down gesture
528 mEventActor = Dali::Actor::New();
529 mEventActor.SetParentOrigin( ParentOrigin::TOP_CENTER );
530 mEventActor.SetAnchorPoint( AnchorPoint::TOP_CENTER );
531 mIndicatorActor.Add( mEventActor );
533 // Attach pan gesture to find flick down during hiding.
534 // It can prevent the problem that scrollview gets pan gesture even indicator area is touched,
535 // since it consumes the pan gesture in advance.
536 mPanDetector = Dali::PanGestureDetector::New();
537 mPanDetector.DetectedSignal().Connect( this, &Indicator::OnPan );
538 mPanDetector.Attach( mEventActor );
542 // register indicator to accessibility adaptor
543 Dali::AccessibilityAdaptor accessibilityAdaptor = AccessibilityAdaptor::Get();
544 if(accessibilityAdaptor)
546 AccessibilityAdaptor::GetImplementation( accessibilityAdaptor ).SetIndicator( this );
548 // hide the indicator by default
549 mIndicatorActor.SetVisible( false );
551 // create impl to handle ecore event
552 mImpl = new Impl(this);
555 Indicator::~Indicator()
565 mEventActor.TouchedSignal().Disconnect( this, &Indicator::OnTouched );
570 void Indicator::SetAdaptor(Adaptor* adaptor)
573 mIndicatorBuffer->SetAdaptor( adaptor );
576 Dali::Actor Indicator::GetActor()
578 return mIndicatorActor;
581 void Indicator::Open( Dali::Window::WindowOrientation orientation )
583 DALI_LOG_TRACE_METHOD( gIndicatorLogFilter );
585 // Calls from Window should be set up to ensure we are in a
586 // disconnected state before opening a second time.
587 DALI_ASSERT_DEBUG( mState == DISCONNECTED );
589 mOrientation = orientation;
593 // Change background visibility depending on orientation
594 if( mOrientation == Dali::Window::LANDSCAPE || mOrientation == Dali::Window::LANDSCAPE_INVERSE )
596 if( mBackgroundRenderer )
598 mIndicatorContentActor.RemoveRenderer( mBackgroundRenderer );
599 mBackgroundVisible = false;
604 SetOpacityMode( mOpacityMode );
608 void Indicator::Close()
610 DALI_LOG_TRACE_METHOD_FMT( gIndicatorLogFilter, "State: %s", STATE_DEBUG_STRING(mState) );
612 if( mState == CONNECTED )
615 if( mObserver != NULL )
617 mObserver->IndicatorClosed( this );
621 Dali::Texture emptyTexture;
622 SetForegroundImage( emptyTexture );
625 void Indicator::SetOpacityMode( Dali::Window::IndicatorBgOpacity mode )
629 Dali::Geometry geometry = CreateBackgroundGeometry();
632 if( mBackgroundRenderer )
634 if( mBackgroundRenderer.GetGeometry() != geometry )
636 mBackgroundRenderer.SetGeometry( geometry );
641 if( !mBackgroundShader )
643 mBackgroundShader = Dali::Shader::New( BACKGROUND_VERTEX_SHADER, BACKGROUND_FRAGMENT_SHADER, Dali::Shader::Hint::OUTPUT_IS_TRANSPARENT );
646 mBackgroundRenderer = Dali::Renderer::New( geometry, mBackgroundShader );
649 if( !mBackgroundVisible )
651 mIndicatorContentActor.AddRenderer( mBackgroundRenderer );
652 mBackgroundVisible = true;
655 else if( mBackgroundRenderer )
657 mIndicatorContentActor.RemoveRenderer( mBackgroundRenderer );
658 mBackgroundVisible = false;
663 void Indicator::SetVisible( Dali::Window::IndicatorVisibleMode visibleMode, bool forceUpdate )
665 if ( visibleMode != mVisible || forceUpdate )
667 // If we were previously hidden, then we should update the image data before we display the indicator
668 if ( mVisible == Dali::Window::INVISIBLE )
670 UpdateImageData( mCurrentSharedFile );
673 if ( visibleMode == Dali::Window::INVISIBLE )
675 if (mServerConnection)
677 mServerConnection->SendEvent( OP_HIDE, NULL, 0 );
682 mIndicatorActor.SetVisible( true );
684 if( mServerConnection )
686 mServerConnection->SendEvent( OP_SHOW, NULL, 0 );
690 mVisible = visibleMode;
693 if( mForegroundRenderer && mForegroundRenderer.GetTextures().GetTexture( 0u ) )
695 if( CheckVisibleState() && mVisible == Dali::Window::AUTO )
698 ShowIndicator( AUTO_INDICATOR_STAY_DURATION /* stay n sec */ );
700 else if( CheckVisibleState() && mVisible == Dali::Window::VISIBLE )
703 ShowIndicator( KEEP_SHOWING );
708 ShowIndicator( HIDE_NOW );
718 bool Indicator::IsConnected()
720 return ( mState == CONNECTED );
723 bool Indicator::SendMessage( int messageDomain, int messageId, const void *data, int size )
727 return mServerConnection->SendEvent( OP_MSG, messageDomain, messageId, data, size );
735 bool Indicator::OnTouched(Dali::Actor indicator, const Dali::TouchEvent& touchEvent)
737 if( mServerConnection )
739 const TouchPoint& touchPoint = touchEvent.GetPoint( 0 );
741 // Send touch event to indicator server when indicator is showing
742 if( CheckVisibleState() || mIsShowing )
744 switch( touchPoint.state )
746 case Dali::PointState::DOWN:
748 IpcDataEvMouseMove ipcMove( touchPoint, touchEvent.time );
749 IpcDataEvMouseDown ipcDown( touchEvent.time );
750 mServerConnection->SendEvent( OP_EV_MOUSE_MOVE, &ipcMove, sizeof(ipcMove) );
751 mServerConnection->SendEvent( OP_EV_MOUSE_DOWN, &ipcDown, sizeof(ipcDown) );
753 if( mVisible == Dali::Window::AUTO )
755 // Stop hiding indicator
756 ShowIndicator( KEEP_SHOWING );
761 case Dali::PointState::MOTION:
763 IpcDataEvMouseMove ipcMove( touchPoint, touchEvent.time );
764 mServerConnection->SendEvent( OP_EV_MOUSE_MOVE, &ipcMove, sizeof(ipcMove) );
768 case Dali::PointState::UP:
769 case Dali::PointState::INTERRUPTED:
771 IpcDataEvMouseUp ipcUp( touchEvent.time );
772 mServerConnection->SendEvent( OP_EV_MOUSE_UP, &ipcUp, sizeof(ipcUp) );
774 if( mVisible == Dali::Window::AUTO )
777 ShowIndicator( 0.5f /* hide after 0.5 sec */ );
782 case Dali::TouchPoint::Leave:
784 IpcDataEvMouseMove ipcMove( touchPoint, touchEvent.time );
785 mServerConnection->SendEvent( OP_EV_MOUSE_MOVE, &ipcMove, sizeof(ipcMove) );
786 IpcDataEvMouseUp ipcOut( touchEvent.time );
787 mServerConnection->SendEvent( OP_EV_MOUSE_OUT, &ipcOut, sizeof(ipcOut) );
802 bool Indicator::Connect()
804 DALI_LOG_TRACE_METHOD( gIndicatorLogFilter );
806 DALI_ASSERT_DEBUG( mState == DISCONNECTED );
808 bool connected = false;
810 mServerConnection = new ServerConnection( INDICATOR_SERVICE_NAME, 0, false, this );
811 if( mServerConnection )
813 connected = mServerConnection->IsConnected();
816 delete mServerConnection;
817 mServerConnection = NULL;
823 StartReconnectionTimer();
833 void Indicator::StartReconnectionTimer()
835 if( ! mReconnectTimer )
837 mReconnectTimer = Dali::Timer::New(1000);
838 mConnection.DisconnectAll();
839 mReconnectTimer.TickSignal().Connect( mConnection, &Indicator::OnReconnectTimer );
841 mReconnectTimer.Start();
844 bool Indicator::OnReconnectTimer()
848 if( mState == DISCONNECTED )
859 void Indicator::Disconnect()
861 DALI_LOG_TRACE_METHOD( gIndicatorLogFilter );
863 mState = DISCONNECTED;
865 delete mServerConnection;
866 mServerConnection = NULL;
868 ClearSharedFileInfo();
871 void Indicator::Resize( int width, int height )
882 if( mImageWidth != width || mImageHeight != height )
885 mImageHeight = height;
887 mIndicatorContentActor.SetSize( mImageWidth, mImageHeight );
888 mIndicatorActor.SetSize( mImageWidth, mImageHeight );
889 mEventActor.SetSize(mImageWidth, mImageHeight);
894 void Indicator::SetLockFileInfo( Ecore_Ipc_Event_Server_Data *epcEvent )
896 DALI_LOG_TRACE_METHOD( gIndicatorLogFilter );
898 // epcEvent->ref == w
899 // epcEvent->ref_to == h
900 // epcEvent->response == buffer num
901 // epcEvent->data = lockfile + nul byte
903 if( (epcEvent->ref > 0) && (epcEvent->ref_to > 0) && (epcEvent->data) &&
904 (((unsigned char *)epcEvent->data)[epcEvent->size - 1] == 0) )
906 int n = epcEvent->response;
908 if( n >= 0 && n < SHARED_FILE_NUMBER )
910 mCurrentSharedFile = n;
912 mSharedFileInfo[n].mImageWidth = epcEvent->ref;
913 mSharedFileInfo[n].mImageHeight = epcEvent->ref_to;
915 mSharedFileInfo[n].mLockFileName.clear();
917 mSharedFileInfo[n].mLockFileName = static_cast< char* >( epcEvent->data );
919 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "SetLockFileInfo: buffer num = %d, w = %d, h = %d, lock = %s\n",
920 n, mSharedFileInfo[n].mImageWidth, mSharedFileInfo[n].mImageHeight, mSharedFileInfo[n].mLockFileName.c_str() );
925 void Indicator::SetSharedImageInfo( Ecore_Ipc_Event_Server_Data *epcEvent )
927 DALI_LOG_TRACE_METHOD( gIndicatorLogFilter );
929 // epcEvent->ref == shm id
930 // epcEvent->ref_to == shm num
931 // epcEvent->response == buffer num
932 // epcEvent->data = shm ref string + nul byte
934 if ( (epcEvent->data) &&
935 (epcEvent->size > 0) &&
936 (((unsigned char *)epcEvent->data)[epcEvent->size - 1] == 0) )
938 int n = epcEvent->response;
940 if( n >= 0 && n < SHARED_FILE_NUMBER )
942 mCurrentSharedFile = n;
944 mSharedFileInfo[n].mSharedFileName.clear();
946 mSharedFileInfo[n].mSharedFileName = static_cast< char* >( epcEvent->data );
948 mSharedFileInfo[n].mSharedFileID = epcEvent->ref;
949 mSharedFileInfo[n].mSharedFileNumber = epcEvent->ref_to;
951 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "SetSharedImageInfo: buffer num %d, shared file = %s, id = %d, num = %d\n",
952 n, mSharedFileInfo[n].mSharedFileName.c_str(), mSharedFileInfo[n].mSharedFileID, mSharedFileInfo[n].mSharedFileNumber );
957 void Indicator::LoadSharedImage( Ecore_Ipc_Event_Server_Data *epcEvent )
959 DALI_LOG_TRACE_METHOD( gIndicatorLogFilter );
961 // epcEvent->ref == alpha
962 // epcEvent->ref_to == sys
963 // epcEvent->response == buffer num
965 if ( mSharedBufferType != BUFFER_TYPE_SHM )
970 int n = epcEvent->response;
972 if( n >= 0 && n < SHARED_FILE_NUMBER )
974 mCurrentSharedFile = n;
976 delete mSharedFileInfo[n].mSharedFile;
977 mSharedFileInfo[n].mSharedFile = NULL;
979 delete mSharedFileInfo[n].mLock;
980 mSharedFileInfo[n].mLock = NULL;
982 std::stringstream sharedFileID;
983 std::stringstream sharedFileNumber;
985 sharedFileID << mSharedFileInfo[n].mSharedFileID;
986 sharedFileNumber << mSharedFileInfo[n].mSharedFileNumber;
988 std::string sharedFilename = "/" + mSharedFileInfo[n].mSharedFileName + "-" + sharedFileID.str() + "." + sharedFileNumber.str();
990 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "LoadSharedImage: file name = %s\n", sharedFilename.c_str() );
992 mSharedFileInfo[n].mSharedFile = SharedFile::New( sharedFilename.c_str(), mSharedFileInfo[n].mImageWidth * mSharedFileInfo[n].mImageWidth * 4, true );
993 if( mSharedFileInfo[n].mSharedFile != NULL )
995 mSharedFileInfo[n].mLock = new Indicator::LockFile( mSharedFileInfo[n].mLockFileName );
996 if( mSharedFileInfo[n].mLock->RetrieveAndClearErrorStatus() )
998 DALI_LOG_ERROR( "### Indicator error: Cannot open lock file %s ###\n", mSharedFileInfo[n].mLockFileName.c_str() );
1003 CreateNewImage( n );
1009 void Indicator::LoadPixmapImage( Ecore_Ipc_Event_Server_Data *epcEvent )
1011 DALI_LOG_TRACE_METHOD( gIndicatorLogFilter );
1013 // epcEvent->ref == pixmap id
1014 // epcEvent->ref_to == type
1015 // epcEvent->response == buffer num
1017 if( (epcEvent->ref > 0) && (epcEvent->ref_to > 0) )
1019 mSharedBufferType = (BufferType)(epcEvent->ref_to);
1021 ClearSharedFileInfo();
1023 mPixmap = static_cast<PixmapId>(epcEvent->ref);
1024 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "mPixmap [%x]", mPixmap);
1026 CreateNewPixmapImage();
1031 void Indicator::UpdateTopMargin()
1033 int newMargin = (mVisible == Dali::Window::VISIBLE && mOpacityMode == Dali::Window::OPAQUE) ? mImageHeight : 0;
1034 if (mTopMargin != newMargin)
1036 mTopMargin = newMargin;
1037 mAdaptor->IndicatorSizeChanged( mTopMargin );
1041 void Indicator::UpdateVisibility()
1043 if( CheckVisibleState() )
1045 // set default indicator type (enable the quick panel)
1046 OnIndicatorTypeChanged( INDICATOR_TYPE_1 );
1050 // set default indicator type (disable the quick panel)
1051 OnIndicatorTypeChanged( INDICATOR_TYPE_2 );
1056 mIndicatorContentActor.SetPosition( 0.0f, -mImageHeight, 0.0f );
1059 SetVisible(mVisible, true);
1062 void Indicator::UpdateImageData( int bufferNumber )
1064 DALI_LOG_TRACE_METHOD_FMT( gIndicatorLogFilter, "State: %s mVisible: %s", STATE_DEBUG_STRING(mState), mVisible?"T":"F" );
1066 if( mState == CONNECTED && mVisible )
1070 // in case of shm indicator (not pixmap), not sure we can skip it when mIsShowing is false
1071 CopyToBuffer( bufferNumber );
1077 mAdaptor->RequestUpdateOnce();
1083 bool Indicator::CopyToBuffer( int bufferNumber )
1085 bool success = false;
1087 if( mSharedFileInfo[bufferNumber].mLock )
1089 Indicator::ScopedLock scopedLock(mSharedFileInfo[bufferNumber].mLock);
1090 if( mSharedFileInfo[bufferNumber].mLock->RetrieveAndClearErrorStatus() )
1094 else if( scopedLock.IsLocked() )
1096 unsigned char *src = mSharedFileInfo[bufferNumber].mSharedFile->GetAddress();
1097 size_t size = mSharedFileInfo[bufferNumber].mImageWidth * mSharedFileInfo[bufferNumber].mImageHeight * 4;
1099 if( mIndicatorBuffer->UpdatePixels( src, size ) )
1101 mAdaptor->RequestUpdateOnce();
1110 void Indicator::CreateNewPixmapImage()
1112 DALI_LOG_TRACE_METHOD_FMT( gIndicatorLogFilter, "W:%d H:%d", mImageWidth, mImageHeight );
1113 Dali::NativeImageSourcePtr nativeImageSource = Dali::NativeImageSource::New( mPixmap );
1115 if( nativeImageSource )
1117 Dali::Texture texture = Dali::Texture::New( *nativeImageSource );
1118 SetForegroundImage( texture );
1119 mIndicatorContentActor.SetSize( mImageWidth, mImageHeight );
1120 mIndicatorActor.SetSize( mImageWidth, mImageHeight );
1121 mEventActor.SetSize( mImageWidth, mImageHeight );
1126 DALI_LOG_WARNING("### Cannot create indicator image - disconnecting ###\n");
1128 if( mObserver != NULL )
1130 mObserver->IndicatorClosed( this );
1132 // Don't do connection in this callback - strange things happen!
1133 StartReconnectionTimer();
1137 void Indicator::CreateNewImage( int bufferNumber )
1139 DALI_LOG_TRACE_METHOD_FMT( gIndicatorLogFilter, "W:%d H:%d", mSharedFileInfo[bufferNumber].mImageWidth, mSharedFileInfo[bufferNumber].mImageHeight );
1140 mIndicatorBuffer = new IndicatorBuffer( mAdaptor, mSharedFileInfo[bufferNumber].mImageWidth, mSharedFileInfo[bufferNumber].mImageHeight, Pixel::BGRA8888 );
1141 bool success = false;
1143 if( CopyToBuffer( bufferNumber ) ) // Only create images if we have valid image buffer
1145 Dali::Texture texture = Dali::Texture::New( mIndicatorBuffer->GetNativeImage() );
1148 SetForegroundImage( texture );
1155 DALI_LOG_WARNING("### Cannot create indicator image - disconnecting ###\n");
1157 if( mObserver != NULL )
1159 mObserver->IndicatorClosed( this );
1161 // Don't do connection in this callback - strange things happen!
1162 StartReconnectionTimer();
1166 Dali::Geometry Indicator::CreateBackgroundGeometry()
1168 switch( mOpacityMode )
1170 case Dali::Window::TRANSLUCENT:
1171 if( !mTranslucentGeometry )
1173 // Construct 5 interval mesh
1187 struct BackgroundVertex
1193 unsigned int numVertices = 2 * ( NUM_GRADIENT_INTERVALS + 1 );
1194 BackgroundVertex vertices[ numVertices ];
1197 float delta = 1.0f / NUM_GRADIENT_INTERVALS;
1198 BackgroundVertex* currentVertex = vertices;
1199 for( int y = 0; y < NUM_GRADIENT_INTERVALS + 1; ++y, d += delta )
1201 currentVertex->mPosition = Vector2( -0.5f, d );
1202 currentVertex->mAlpha = GRADIENT_ALPHA[ y ];
1205 currentVertex->mPosition = Vector2( 0.5f, d );
1206 currentVertex->mAlpha = GRADIENT_ALPHA[ y ];
1211 unsigned int numIndices = 2 * 3 * NUM_GRADIENT_INTERVALS;
1212 unsigned short indices[ numIndices ];
1214 unsigned short* currentIndex = indices;
1215 for( int y = 0; y < NUM_GRADIENT_INTERVALS; ++y )
1217 *currentIndex++ = (2 * y);
1218 *currentIndex++ = (2 * y) + 3;
1219 *currentIndex++ = (2 * y) + 1;
1221 *currentIndex++ = (2 * y);
1222 *currentIndex++ = (2 * y) + 2;
1223 *currentIndex++ = (2 * y) + 3;
1226 Dali::Property::Map vertexFormat;
1227 vertexFormat[ "aPosition" ] = Dali::Property::VECTOR2;
1228 vertexFormat[ "aAlpha" ] = Dali::Property::FLOAT;
1229 Dali::PropertyBuffer vertexPropertyBuffer = Dali::PropertyBuffer::New( vertexFormat );
1230 vertexPropertyBuffer.SetData( vertices, numVertices );
1232 // Create the geometry object
1233 mTranslucentGeometry = Dali::Geometry::New();
1234 mTranslucentGeometry.AddVertexBuffer( vertexPropertyBuffer );
1235 mTranslucentGeometry.SetIndexBuffer( &indices[0], numIndices );
1238 return mTranslucentGeometry;
1239 case Dali::Window::OPAQUE:
1241 if( !mSolidGeometry )
1244 struct BackgroundVertex
1250 BackgroundVertex vertices[ 4 ] = { { Vector2( -0.5f, -0.5f ), 1.0f }, { Vector2( 0.5f, -0.5f ), 1.0f },
1251 { Vector2( -0.5f, 0.5f ), 1.0f }, { Vector2( 0.5f, 0.5f ), 1.0f } };
1254 unsigned short indices[ 6 ] = { 0, 3, 1, 0, 2, 3 };
1256 Dali::Property::Map vertexFormat;
1257 vertexFormat[ "aPosition" ] = Dali::Property::VECTOR2;
1258 vertexFormat[ "aAlpha" ] = Dali::Property::FLOAT;
1259 Dali::PropertyBuffer vertexPropertyBuffer = Dali::PropertyBuffer::New( vertexFormat );
1260 vertexPropertyBuffer.SetData( vertices, 4 );
1263 // Create the geometry object
1264 mSolidGeometry = Dali::Geometry::New();
1265 mSolidGeometry.AddVertexBuffer( vertexPropertyBuffer );
1266 mSolidGeometry.SetIndexBuffer( &indices[0], 6 );
1269 return mSolidGeometry;
1270 case Dali::Window::TRANSPARENT:
1274 return Dali::Geometry();
1277 void Indicator::SetForegroundImage( Dali::Texture texture )
1279 if( !mForegroundRenderer && texture )
1282 Dali::Shader shader = Dali::Shader::New( FOREGROUND_VERTEX_SHADER, FOREGROUND_FRAGMENT_SHADER );
1284 // Create renderer from geometry and material
1285 Dali::Geometry quad = CreateQuadGeometry();
1286 mForegroundRenderer = Dali::Renderer::New( quad, shader );
1287 // Make sure the foreground stays in front of the background
1288 mForegroundRenderer.SetProperty( Dali::Renderer::Property::DEPTH_INDEX, 1.f );
1290 // Set blend function
1291 mForegroundRenderer.SetProperty( Dali::Renderer::Property::BLEND_FACTOR_SRC_RGB, Dali::BlendFactor::ONE );
1292 mForegroundRenderer.SetProperty( Dali::Renderer::Property::BLEND_FACTOR_DEST_RGB, Dali::BlendFactor::ONE_MINUS_SRC_ALPHA );
1293 mForegroundRenderer.SetProperty( Dali::Renderer::Property::BLEND_FACTOR_SRC_ALPHA, Dali::BlendFactor::ONE );
1294 mForegroundRenderer.SetProperty( Dali::Renderer::Property::BLEND_FACTOR_DEST_ALPHA, Dali::BlendFactor::ONE );
1296 // Create a texture-set and add to renderer
1298 Dali::TextureSet textureSet = Dali::TextureSet::New();
1299 textureSet.SetTexture( 0u, texture );
1300 mForegroundRenderer.SetTextures( textureSet );
1302 mIndicatorContentActor.AddRenderer( mForegroundRenderer );
1304 else if( mForegroundRenderer )
1306 Dali::TextureSet textureSet = mForegroundRenderer.GetTextures();
1307 textureSet.SetTexture( 0u, texture );
1310 if( mImageWidth == 0 && mImageHeight == 0 && texture)
1312 Resize( texture.GetWidth(), texture.GetHeight() );
1316 void Indicator::OnIndicatorTypeChanged( Type indicatorType )
1318 if( mObserver != NULL )
1320 mObserver->IndicatorTypeChanged( indicatorType );
1324 void Indicator::DataReceived( void* event )
1326 DALI_LOG_TRACE_METHOD( gIndicatorLogFilter );
1327 Ecore_Ipc_Event_Server_Data *epcEvent = static_cast<Ecore_Ipc_Event_Server_Data *>( event );
1329 switch( epcEvent->minor )
1333 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "Indicator client received: OP_UPDATE\n" );
1336 mAdaptor->RequestUpdateOnce();
1340 case OP_UPDATE_DONE:
1342 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "Indicator client received: OP_UPDATE_DONE [%d]\n", epcEvent->response );
1343 // epcEvent->response == display buffer #
1344 UpdateImageData( epcEvent->response );
1349 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "Indicator client received: OP_SHM_REF0\n" );
1350 SetSharedImageInfo( epcEvent );
1355 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "Indicator client received: OP_SHM_REF1\n" );
1356 SetLockFileInfo( epcEvent );
1361 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "Indicator client received: OP_SHM_REF2\n" );
1362 LoadSharedImage( epcEvent );
1367 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "Indicator client received: OP_PIXMAP_REF\n" );
1368 LoadPixmapImage( epcEvent );
1373 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "Indicator client received: OP_RESIZE\n" );
1375 if( (epcEvent->data) && (epcEvent->size >= (int)sizeof(IpcDataResize)) )
1377 IpcDataResize *newSize = static_cast<IpcDataResize*>( epcEvent->data );
1378 Resize( newSize->w, newSize->h );
1384 int msgDomain = epcEvent->ref;
1385 int msgId = epcEvent->ref_to;
1387 void *msgData = NULL;
1388 int msgDataSize = 0;
1389 msgData = epcEvent->data;
1390 msgDataSize = epcEvent->size;
1392 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "Indicator client received: OP_MSG_PARENT. msgDomain = %d\n", msgDomain );
1394 if( msgDomain == MSG_DOMAIN_CONTROL_INDICATOR )
1398 case MSG_ID_INDICATOR_TYPE:
1400 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "Indicator client received: OP_MSG_PARENT, INDICATOR_TYPE\n" );
1401 Type* indicatorType = static_cast<Type*>( epcEvent->data );
1402 OnIndicatorTypeChanged( *indicatorType );
1406 case MSG_ID_INDICATOR_START_ANIMATION:
1408 DALI_LOG_INFO( gIndicatorLogFilter, Debug::General, "Indicator client received: MSG_ID_INDICATOR_START_ANIMATION\n" );
1410 if (msgDataSize != (int)sizeof(IpcIndicatorDataAnimation))
1412 DALI_LOG_ERROR("Message data is incorrect\n");
1416 IpcIndicatorDataAnimation *animData = static_cast<IpcIndicatorDataAnimation*>(msgData);
1418 if(!CheckVisibleState())
1420 ShowIndicator( animData->duration /* n sec */ );
1431 void Indicator::ConnectionClosed()
1433 DALI_LOG_TRACE_METHOD( gIndicatorLogFilter );
1435 // Will get this callback if the server connection failed to start up.
1436 delete mServerConnection;
1437 mServerConnection = NULL;
1438 mState = DISCONNECTED;
1440 // Attempt to re-connect
1444 bool Indicator::CheckVisibleState()
1446 if( mOrientation == Dali::Window::LANDSCAPE
1447 || mOrientation == Dali::Window::LANDSCAPE_INVERSE
1448 || (mVisible == Dali::Window::INVISIBLE)
1449 || (mVisible == Dali::Window::AUTO && !mIsShowing) )
1457 void Indicator::ClearSharedFileInfo()
1459 for( int i = 0; i < SHARED_FILE_NUMBER; i++ )
1461 delete mSharedFileInfo[i].mLock;
1462 mSharedFileInfo[i].mLock = NULL;
1464 delete mSharedFileInfo[i].mSharedFile;
1465 mSharedFileInfo[i].mSharedFile = NULL;
1467 mSharedFileInfo[i].mLockFileName.clear();
1468 mSharedFileInfo[i].mSharedFileName.clear();
1473 * duration can be this
1477 * KEEP_SHOWING = -1,
1481 void Indicator::ShowIndicator(float duration)
1483 if( !mIndicatorAnimation )
1485 mIndicatorAnimation = Dali::Animation::New(SLIDING_ANIMATION_DURATION);
1486 mIndicatorAnimation.FinishedSignal().Connect(this, &Indicator::OnAnimationFinished);
1489 if(mIsShowing && !EqualsZero(duration))
1491 // If need to show during showing, do nothing.
1492 // In 2nd phase (below) will update timer
1494 else if(!mIsShowing && mIsAnimationPlaying && EqualsZero(duration))
1496 // If need to hide during hiding or hidden already, do nothing
1500 mIndicatorAnimation.Clear();
1502 if( EqualsZero(duration) )
1504 mIndicatorAnimation.AnimateTo( Property( mIndicatorContentActor, Dali::Actor::Property::POSITION ), Vector3(0, -mImageHeight, 0), Dali::AlphaFunction::EASE_OUT );
1508 OnIndicatorTypeChanged( INDICATOR_TYPE_2 ); // un-toucable
1512 mIndicatorAnimation.AnimateTo( Property( mIndicatorContentActor, Dali::Actor::Property::POSITION ), Vector3(0, 0, 0), Dali::AlphaFunction::EASE_OUT );
1516 OnIndicatorTypeChanged( INDICATOR_TYPE_1 ); // touchable
1519 mIndicatorAnimation.Play();
1520 mIsAnimationPlaying = true;
1527 mShowTimer = Dali::Timer::New(1000 * duration);
1528 mShowTimer.TickSignal().Connect(this, &Indicator::OnShowTimer);
1530 mShowTimer.SetInterval(1000* duration);
1533 if( mVisible == Dali::Window::AUTO )
1535 // check the stage touch
1536 Dali::Stage::GetCurrent().TouchedSignal().Connect( this, &Indicator::OnStageTouched );
1541 if(mShowTimer && mShowTimer.IsRunning())
1546 if( mVisible == Dali::Window::AUTO )
1548 // check the stage touch
1549 Dali::Stage::GetCurrent().TouchedSignal().Disconnect( this, &Indicator::OnStageTouched );
1554 bool Indicator::OnShowTimer()
1556 // after time up, hide indicator
1557 ShowIndicator( HIDE_NOW );
1562 void Indicator::OnAnimationFinished(Dali::Animation& animation)
1564 mIsAnimationPlaying = false;
1565 // once animation is finished and indicator is hidden, take it off stage
1566 if( mObserver != NULL )
1568 mObserver->IndicatorVisibilityChanged( mIsShowing ); // is showing?
1572 void Indicator::OnPan( Dali::Actor actor, const Dali::PanGesture& gesture )
1576 if( mServerConnection )
1578 switch( gesture.state )
1580 case Gesture::Started:
1582 mGestureDetected = false;
1584 // The gesture position is the current position after it has moved by the displacement.
1585 // We want to reference the original position.
1586 mGestureDeltaY = gesture.position.y - gesture.displacement.y;
1589 // No break, Fall through
1590 case Gesture::Continuing:
1592 if( mVisible == Dali::Window::AUTO && !mIsShowing )
1594 // Only take one touch point
1595 if( gesture.numberOfTouches == 1 && mGestureDetected == false )
1597 mGestureDeltaY += gesture.displacement.y;
1599 if( mGestureDeltaY >= mImageHeight * SHOWING_DISTANCE_HEIGHT_RATE )
1601 ShowIndicator( AUTO_INDICATOR_STAY_DURATION );
1602 mGestureDetected = true;
1610 case Gesture::Finished:
1611 case Gesture::Cancelled:
1613 // if indicator is showing, hide again when touching is finished (Since touch leave is activated, checking it in gesture::finish instead of touch::up)
1614 if( mVisible == Dali::Window::AUTO && mIsShowing )
1616 ShowIndicator( AUTO_INDICATOR_STAY_DURATION );
1628 void Indicator::OnStageTouched(const Dali::TouchEvent& touchEvent)
1630 const TouchPoint& touchPoint = touchEvent.GetPoint( 0 );
1632 // when stage is touched while indicator is showing temporary, hide it
1633 if( mIsShowing && ( CheckVisibleState() == false || mVisible == Dali::Window::AUTO ) )
1635 switch( touchPoint.state )
1637 case Dali::PointState::DOWN:
1639 // if touch point is inside the indicator, indicator is not hidden
1640 if( mImageHeight < int(touchPoint.screen.y) )
1642 ShowIndicator( HIDE_NOW );