DALi Version 1.2.35
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / common / ecore-indicator-impl.h
1 #ifndef __DALI_INTERNAL_ECORE_INDICATOR_H__
2 #define __DALI_INTERNAL_ECORE_INDICATOR_H__
3
4 /*
5  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/animation/animation.h>
23 #include <dali/public-api/events/pan-gesture.h>
24 #include <dali/public-api/events/pan-gesture-detector.h>
25 #include <dali/public-api/rendering/renderer.h>
26
27 // INTERNAL INCLUDES
28 #include <base/interfaces/indicator-interface.h>
29 #include <indicator-buffer.h>
30 #include <ecore-server-connection.h>
31 #include <shared-file.h>
32 #include <timer.h>
33 #include <window.h>
34
35 namespace Dali
36 {
37 namespace Integration
38 {
39 class Core;
40 }
41
42 namespace Internal
43 {
44 namespace Adaptor
45 {
46 class Adaptor;
47
48 typedef unsigned int PixmapId;
49
50 /**
51  * The Indicator class connects to the indicator server, and gets and draws the indicator
52  * for the given orientation.
53  */
54 class Indicator : public ConnectionTracker, public ServerConnection::Observer, public IndicatorInterface
55 {
56 public:
57
58   enum State
59   {
60     DISCONNECTED,
61     CONNECTED
62   };
63
64   /**
65    * copied from ecore_evas_extn_engine.h
66    */
67   enum BufferType
68   {
69     BUFFER_TYPE_SHM = 0,        ///< shared memory-based buffer backend
70     BUFFER_TYPE_DRI2_PIXMAP,    ///< dri2 pixmap-based buffer backend
71     BUFFER_TYPE_EVASGL_PIXMAP,  ///< pixmap backend for Evas GL only (DEPRECATED)
72     BUFFER_TYPE_GL_PIXMAP,      ///< double buffered GL pixmap backend
73   };
74
75 protected:
76   /**
77    * Class to encapsulate lock file
78    */
79   class LockFile
80   {
81   public:
82     /**
83      * Constructor. open lock file
84      */
85     LockFile(const std::string filename);
86
87     /**
88      * Close lock file
89      */
90     ~LockFile();
91
92     /**
93      * Grab an exclusive lock on this file
94      * @return true if the lock succeeded, false if it failed
95      */
96     bool Lock();
97
98     /**
99      * Remove the lock
100      */
101     void Unlock();
102
103     /**
104      * Test if there is an error with the lock file, and clears
105      * the error flag;
106      * @return true if an error was thrown
107      */
108     bool RetrieveAndClearErrorStatus();
109
110   private:
111     std::string mFilename;
112     int         mFileDescriptor;
113     bool        mErrorThrown;
114   };
115
116   /**
117    * Class to ensure lock/unlock through object destruction
118    */
119   class ScopedLock
120   {
121   public:
122     /**
123      * Constructor - creates a lock on the lockfile
124      * @param[in] lockFile The lockfile to use
125      */
126     ScopedLock( LockFile* lockFile );
127
128     /**
129      * Destructor - removes the lock (if any) on the lockfile
130      */
131     ~ScopedLock();
132
133     /**
134      * Method to test if the locking succeeded
135      * @return TRUE if locked
136      */
137     bool IsLocked();
138
139   private:
140     LockFile* mLockFile; ///< The lock file to use
141     bool      mLocked;   ///< Whether the lock succeeded
142   };
143
144
145 public:  // Dali::Internal::Adaptor::IndicicatorInterface
146   /**
147    * @copydoc Dali::Internal::IndicatorInterface::IndicatorInterface
148    */
149   Indicator( Adaptor* adaptor,
150              Dali::Window::WindowOrientation orientation,
151              IndicatorInterface::Observer* observer );
152
153   /**
154    * @copydoc Dali::Internal::IndicatorInterface::~IndicatorInterface
155    */
156   virtual ~Indicator();
157
158
159   virtual void SetAdaptor(Adaptor* adaptor);
160
161   /**
162    * @copydoc Dali::Internal::IndicatorInterface::GetActor
163    */
164   virtual Dali::Actor GetActor();
165
166   /**
167    * @copydoc Dali::Internal::IndicatorInterface::Open
168    */
169   virtual void Open( Dali::Window::WindowOrientation orientation );
170
171   /**
172    * @copydoc Dali::Internal::IndicatorInterface::Close
173    */
174   virtual void Close();
175
176   /**
177    * @copydoc Dali::Internal::IndicatorInterface::SetOpacityMode
178    */
179   virtual void SetOpacityMode( Dali::Window::IndicatorBgOpacity mode );
180
181   /**
182    * @copydoc Dali::Internal::IndicatorInterface::SetVisible
183    */
184   virtual void SetVisible( Dali::Window::IndicatorVisibleMode visibleMode, bool forceUpdate = false );
185
186   /**
187    * @copydoc Dali::Internal::IndicatorInterface::IsConnected
188    */
189   virtual bool IsConnected();
190
191   /**
192    * @copydoc Dali::Internal::IndicatorInterface::SendMessage
193    */
194   virtual bool SendMessage( int messageDomain, int messageId, const void *data, int size );
195
196 private:
197   /**
198    * Initialize the indicator actors
199    */
200   void Initialize();
201
202   /**
203    * Constructs the renderers used for the background
204    */
205   Dali::Geometry CreateBackgroundGeometry();
206
207   /**
208    * Set the texture to be rendered as indicator foreground
209    * @param[in] texture The foreground texture.
210    */
211   void SetForegroundImage( Dali::Texture texture );
212
213   /**
214    * Touch event callback.
215    * It should pass the valid touch event to indicator server
216    *
217    * @param[in] indicator  The indicator actor that was touched
218    * @param[in] touchEvent The touch event
219    */
220   bool OnTouched(Dali::Actor indicator, const TouchEvent& touchEvent);
221
222   /**
223    * Pan gesture callback.
224    * It finds flick down gesture to show hidden indicator image
225    *
226    * @param[in] actor  The actor for gesture
227    * @param[in] gesture The gesture event
228    */
229   void OnPan( Dali::Actor actor, const Dali::PanGesture& gesture );
230
231   /**
232    * Touch event callback on stage.
233    * If stage is touched, hide showing indicator image
234    *
235    * @param[in] touchEvent The touch event
236    */
237   void OnStageTouched(const Dali::TouchEvent& touchEvent);
238
239   /**
240    * Connect to the indicator service
241    */
242   bool Connect();
243
244   /**
245    * Start the reconnection timer. This will run every second until we reconnect to
246    * the indicator service.
247    */
248   void StartReconnectionTimer();
249
250   /**
251    * If connection failed, attempt to re-connect every second
252    */
253   bool OnReconnectTimer();
254
255   /**
256    * Disconnect from the indicator service
257    */
258   void Disconnect();
259
260   /**
261    * Handle Resize event
262    * @param[in] width The new width
263    * @param[in] height The new height
264    */
265   void Resize( int width, int height );
266
267   /**
268    * Set the lock file info.
269    * @param[in] epcEvent Current ecore event.
270    */
271   void SetLockFileInfo( Ecore_Ipc_Event_Server_Data *epcEvent );
272
273   /**
274    * Set the shared indicator image info
275    * @param[in] epcEvent The event containing the image data
276    */
277   void SetSharedImageInfo( Ecore_Ipc_Event_Server_Data *epcEvent );
278
279   /**
280    * Load the shared indicator image
281    * @param[in] epcEvent The event containing the image data
282    */
283   void LoadSharedImage( Ecore_Ipc_Event_Server_Data *epcEvent );
284
285   /**
286    * Load the pixmap indicator image
287    * @param[in] epcEvent The event containing the image data
288    */
289   void LoadPixmapImage( Ecore_Ipc_Event_Server_Data *epcEvent );
290
291   /**
292    * Update top margin of the stage as much as indicator height
293    */
294   void UpdateTopMargin();
295
296   /**
297    * Update the visibility and position of the actors
298    */
299   void UpdateVisibility();
300
301   /**
302    * Inform dali that the indicator data has been updated.
303    * @param[in] bufferNumber The shared file number
304    */
305   void UpdateImageData( int bufferNumber );
306
307   /**
308    * Lock the temporary file, Copy the shared image into IndicatorBuffer
309    * and then unlock the temporary file.
310    * Caller should ensure we are not writing image to gl texture.
311    * @param[in] bufferNumber The shared file number
312    */
313   bool CopyToBuffer( int bufferNumber );
314
315   /**
316    * Create a new image for the indicator, and set up signal handling for it.
317    * @param[in] bufferNumber The shared file number
318    */
319   void CreateNewImage( int bufferNumber );
320
321   /**
322    * Create a new pixmap image for the indicator, and set up signal handling for it.
323    */
324   void CreateNewPixmapImage();
325
326   /**
327    * Indicator type has changed.
328    * Inform observer
329    * @param[in] type The new indicator type
330    */
331   void OnIndicatorTypeChanged( Type type );
332
333   /**
334    * Check whether the indicator could be visible or invisible
335    * @return true if indicator should be shown
336    */
337   bool CheckVisibleState();
338
339   /**
340    * Show/Hide indicator actor with effect
341    * @param[in] duration how long need to show the indicator,
342    *                     if it equal to 0, hide the indicator
343    *                     if it less than 0, show always
344    */
345   void ShowIndicator( float duration );
346
347   /**
348    * Showing timer callback
349    */
350   bool OnShowTimer();
351
352   /**
353    * Showing animation finished callback
354    * @param[in] animation
355    */
356   void OnAnimationFinished( Dali::Animation& animation );
357
358 private: // Implementation of ServerConnection::Observer
359   /**
360    * @copydoc Dali::Internal::Adaptor::ServerConnection::Observer::DataReceived()
361    */
362   virtual void DataReceived( void* event );
363
364   /**
365    * @copydoc Dali::Internal::Adaptor::ServerConnection::Observer::DataReceived()
366    */
367   virtual void ConnectionClosed();
368
369 private:
370
371   /**
372    * Clear shared file info
373    */
374   void ClearSharedFileInfo();
375
376 private:
377
378   struct SharedFileInfo
379   {
380     SharedFileInfo()
381       : mLock( NULL ),
382         mSharedFile( NULL ),
383         mImageWidth( 0 ),
384         mImageHeight( 0 ),
385         mLockFileName(),
386         mSharedFileName(),
387         mSharedFileID( 0 ),
388         mSharedFileNumber( 0 )
389     {
390     }
391
392     LockFile*                        mLock;              ///< File lock for the shared file
393     SharedFile*                      mSharedFile;        ///< Shared file
394
395     int                              mImageWidth;        ///< Shared image width
396     int                              mImageHeight;       ///< Shared image height
397
398     std::string                      mLockFileName;      ///< Lock file name
399     std::string                      mSharedFileName;    ///< Shared file name
400     int                              mSharedFileID;      ///< Shared file ID
401     int                              mSharedFileNumber;  ///< Shared file number
402   };
403
404   static const int SHARED_FILE_NUMBER = 2;               ///< Shared file number
405
406   Dali::Geometry                   mTranslucentGeometry; ///< Geometry used for rendering the translucent background
407   Dali::Geometry                   mSolidGeometry;       ///< Geometry used for rendering the opaque background
408   Dali::Shader                     mBackgroundShader;    ///< Shader used for rendering the background
409
410   IndicatorBufferPtr               mIndicatorBuffer;     ///< class which handles indicator rendering
411   PixmapId                         mPixmap;              ///< Pixmap including indicator content
412   Dali::Renderer                   mForegroundRenderer;  ///< Renderer renders the indicator foreground
413   Dali::Renderer                   mBackgroundRenderer;  ///< Renderer renders the indicator background
414
415   Dali::Actor                      mIndicatorContentActor; ///< Actor container for image and background
416   Dali::Actor                      mIndicatorActor;      ///< Handle to topmost indicator actor
417   Dali::Actor                      mEventActor;          ///< Handle to event
418   Dali::PanGestureDetector         mPanDetector;         ///< Pan detector to find flick gesture for hidden indicator
419   float                            mGestureDeltaY;       ///< Checking how much panning moved
420   bool                             mGestureDetected;     ///< Whether find the flick gesture
421
422   Dali::Timer                      mReconnectTimer;      ///< Reconnection timer
423   SlotDelegate< Indicator >        mConnection;
424
425   Dali::Window::IndicatorBgOpacity mOpacityMode;         ///< Opacity enum for background
426   Indicator::State                 mState;               ///< The connection state
427
428   Adaptor*                         mAdaptor;
429   ServerConnection*                mServerConnection;
430   IndicatorInterface::Observer*    mObserver;            ///< Upload observer
431
432   Dali::Window::WindowOrientation  mOrientation;
433   int                              mImageWidth;
434   int                              mImageHeight;
435   Dali::Window::IndicatorVisibleMode mVisible;           ///< Whether the indicator is visible
436
437   Dali::Timer                      mShowTimer;           ///< Timer to show indicator
438   bool                             mIsShowing;           ///< Whether the indicator is showing on the screen
439   Dali::Animation                  mIndicatorAnimation;  ///< Animation to show/hide indicator image
440
441   bool                             mIsAnimationPlaying;  ///< Whether the animation is playing
442
443   int                              mCurrentSharedFile;   ///< Current shared file number
444   SharedFileInfo                   mSharedFileInfo[SHARED_FILE_NUMBER];    ///< Table to store shared file info
445
446   BufferType                       mSharedBufferType;    ///< Shared buffer type which is used to render indicator
447
448   struct Impl; ///< Contains Ecore specific information
449   Impl* mImpl; ///< Created on construction and destroyed on destruction.
450
451   bool                             mBackgroundVisible;   ///< Indicate whether background is visible
452   int                              mTopMargin;   ///< Top margin of the stage for indicator
453 };
454
455 } // Adaptor
456 } // Internal
457 } // Dali
458
459 #endif