47a2ac54c382e2db093462f9a74678990a4b256d
[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 the visibility and position of the actors
293    */
294   void UpdateVisibility();
295
296   /**
297    * Inform dali that the indicator data has been updated.
298    * @param[in] bufferNumber The shared file number
299    */
300   void UpdateImageData( int bufferNumber );
301
302   /**
303    * Lock the temporary file, Copy the shared image into IndicatorBuffer
304    * and then unlock the temporary file.
305    * Caller should ensure we are not writing image to gl texture.
306    * @param[in] bufferNumber The shared file number
307    */
308   bool CopyToBuffer( int bufferNumber );
309
310   /**
311    * Create a new image for the indicator, and set up signal handling for it.
312    * @param[in] bufferNumber The shared file number
313    */
314   void CreateNewImage( int bufferNumber );
315
316   /**
317    * Create a new pixmap image for the indicator, and set up signal handling for it.
318    */
319   void CreateNewPixmapImage();
320
321   /**
322    * Indicator type has changed.
323    * Inform observer
324    * @param[in] type The new indicator type
325    */
326   void OnIndicatorTypeChanged( Type type );
327
328   /**
329    * Check whether the indicator could be visible or invisible
330    * @return true if indicator should be shown
331    */
332   bool CheckVisibleState();
333
334   /**
335    * Show/Hide indicator actor with effect
336    * @param[in] duration how long need to show the indicator,
337    *                     if it equal to 0, hide the indicator
338    *                     if it less than 0, show always
339    */
340   void ShowIndicator( float duration );
341
342   /**
343    * Showing timer callback
344    */
345   bool OnShowTimer();
346
347   /**
348    * Showing animation finished callback
349    * @param[in] animation
350    */
351   void OnAnimationFinished( Dali::Animation& animation );
352
353 private: // Implementation of ServerConnection::Observer
354   /**
355    * @copydoc Dali::Internal::Adaptor::ServerConnection::Observer::DataReceived()
356    */
357   virtual void DataReceived( void* event );
358
359   /**
360    * @copydoc Dali::Internal::Adaptor::ServerConnection::Observer::DataReceived()
361    */
362   virtual void ConnectionClosed();
363
364 private:
365
366   /**
367    * Clear shared file info
368    */
369   void ClearSharedFileInfo();
370
371 private:
372
373   struct SharedFileInfo
374   {
375     SharedFileInfo()
376       : mLock( NULL ),
377         mSharedFile( NULL ),
378         mImageWidth( 0 ),
379         mImageHeight( 0 ),
380         mLockFileName(),
381         mSharedFileName(),
382         mSharedFileID( 0 ),
383         mSharedFileNumber( 0 )
384     {
385     }
386
387     LockFile*                        mLock;              ///< File lock for the shared file
388     SharedFile*                      mSharedFile;        ///< Shared file
389
390     int                              mImageWidth;        ///< Shared image width
391     int                              mImageHeight;       ///< Shared image height
392
393     std::string                      mLockFileName;      ///< Lock file name
394     std::string                      mSharedFileName;    ///< Shared file name
395     int                              mSharedFileID;      ///< Shared file ID
396     int                              mSharedFileNumber;  ///< Shared file number
397   };
398
399   static const int SHARED_FILE_NUMBER = 2;               ///< Shared file number
400
401   Dali::Geometry                   mTranslucentGeometry; ///< Geometry used for rendering the translucent background
402   Dali::Geometry                   mSolidGeometry;       ///< Geometry used for rendering the opaque background
403   Dali::Shader                     mBackgroundShader;    ///< Shader used for rendering the background
404
405   IndicatorBufferPtr               mIndicatorBuffer;     ///< class which handles indicator rendering
406   PixmapId                         mPixmap;              ///< Pixmap including indicator content
407   Dali::Renderer                   mForegroundRenderer;  ///< Renderer renders the indicator foreground
408   Dali::Renderer                   mBackgroundRenderer;  ///< Renderer renders the indicator background
409
410   Dali::Actor                      mIndicatorContentActor; ///< Actor container for image and background
411   Dali::Actor                      mIndicatorActor;      ///< Handle to topmost indicator actor
412   Dali::Actor                      mEventActor;          ///< Handle to event
413   Dali::PanGestureDetector         mPanDetector;         ///< Pan detector to find flick gesture for hidden indicator
414   float                            mGestureDeltaY;       ///< Checking how much panning moved
415   bool                             mGestureDetected;     ///< Whether find the flick gesture
416
417   Dali::Timer                      mReconnectTimer;      ///< Reconnection timer
418   SlotDelegate< Indicator >        mConnection;
419
420   Dali::Window::IndicatorBgOpacity mOpacityMode;         ///< Opacity enum for background
421   Indicator::State                 mState;               ///< The connection state
422
423   Adaptor*                         mAdaptor;
424   ServerConnection*                mServerConnection;
425   IndicatorInterface::Observer*    mObserver;            ///< Upload observer
426
427   Dali::Window::WindowOrientation  mOrientation;
428   int                              mImageWidth;
429   int                              mImageHeight;
430   Dali::Window::IndicatorVisibleMode mVisible;           ///< Whether the indicator is visible
431
432   Dali::Timer                      mShowTimer;           ///< Timer to show indicator
433   bool                             mIsShowing;           ///< Whether the indicator is showing on the screen
434   Dali::Animation                  mIndicatorAnimation;  ///< Animation to show/hide indicator image
435
436   bool                             mIsAnimationPlaying;  ///< Whether the animation is playing
437
438   int                              mCurrentSharedFile;   ///< Current shared file number
439   SharedFileInfo                   mSharedFileInfo[SHARED_FILE_NUMBER];    ///< Table to store shared file info
440
441   BufferType                       mSharedBufferType;    ///< Shared buffer type which is used to render indicator
442
443   struct Impl; ///< Contains Ecore specific information
444   Impl* mImpl; ///< Created on construction and destroyed on destruction.
445
446   bool                             mBackgroundVisible;   ///< Indicate whether background is visible
447 };
448
449 } // Adaptor
450 } // Internal
451 } // Dali
452
453 #endif