Doxygen grouping
[platform/core/uifw/dali-adaptor.git] / adaptors / common / indicator-impl.h
1 #ifndef __DALI_INTERNAL_INDICATOR_H__
2 #define __DALI_INTERNAL_INDICATOR_H__
3
4 /*
5  * Copyright (c) 2014 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/actors/image-actor.h>
23 #include <dali/public-api/animation/animation.h>
24 #include <dali/public-api/events/pan-gesture.h>
25 #include <dali/public-api/events/pan-gesture-detector.h>
26 #include <dali/devel-api/rendering/renderer.h>
27
28 // INTERNAL INCLUDES
29 #include <indicator-buffer.h>
30 #include <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
55 {
56 public:
57   enum State
58   {
59     DISCONNECTED,
60     CONNECTED
61   };
62
63   enum Type
64   {
65     INDICATOR_TYPE_UNKNOWN,
66     INDICATOR_TYPE_1,
67     INDICATOR_TYPE_2
68   };
69
70 public:
71   class Observer
72   {
73   public:
74     /**
75      * Notify the observer if the indicator type changes
76      * @param[in] type The new indicator type
77      */
78     virtual void IndicatorTypeChanged( Type type ) = 0;
79
80     /**
81      * Notify the observer when the upload has completed.
82      * @param[in] indicator The indicator that has finished uploading.
83      */
84     virtual void IndicatorClosed(Indicator* indicator) = 0;
85
86     /**
87      * Notify the observer when the indicator visible status is changed.
88      * @param[in] isShowing Whether the indicator is visible.
89      */
90     virtual void IndicatorVisibilityChanged( bool isVisible ) = 0;
91   };
92
93 protected:
94   /**
95    * Class to encapsulate lock file
96    */
97   class LockFile
98   {
99   public:
100     /**
101      * Constructor. open lock file
102      */
103     LockFile(const std::string filename);
104
105     /**
106      * Close lock file
107      */
108     ~LockFile();
109
110     /**
111      * Grab an exclusive lock on this file
112      * @return true if the lock succeeded, false if it failed
113      */
114     bool Lock();
115
116     /**
117      * Remove the lock
118      */
119     void Unlock();
120
121     /**
122      * Test if there is an error with the lock file, and clears
123      * the error flag;
124      * @return true if an error was thrown
125      */
126     bool RetrieveAndClearErrorStatus();
127
128   private:
129     std::string mFilename;
130     int         mFileDescriptor;
131     bool        mErrorThrown;
132   };
133
134   /**
135    * Class to ensure lock/unlock through object destruction
136    */
137   class ScopedLock
138   {
139   public:
140     /**
141      * Constructor - creates a lock on the lockfile
142      * @param[in] lockFile The lockfile to use
143      */
144     ScopedLock( LockFile* lockFile );
145
146     /**
147      * Destructor - removes the lock (if any) on the lockfile
148      */
149     ~ScopedLock();
150
151     /**
152      * Method to test if the locking succeeded
153      * @return TRUE if locked
154      */
155     bool IsLocked();
156
157   private:
158     LockFile* mLockFile; ///< The lock file to use
159     bool      mLocked;   ///< Whether the lock succeeded
160   };
161
162
163 public:
164   /**
165    * Constructor. Creates a new indicator and opens a connection for
166    * the required orientation.
167    * @param[in] orientation The orientation in which to draw the indicator
168    * @param[in] observer The indicator closed
169    */
170   Indicator( Adaptor* adaptor,
171              Dali::Window::WindowOrientation orientation,
172              Observer* observer );
173
174   /**
175    * Destructor
176    */
177   virtual ~Indicator();
178
179   void SetAdaptor(Adaptor* adaptor);
180
181   /**
182    * Get the actor which contains the indicator image. Ensure that the handle is
183    * released when no longer needed.
184    * Changes from the indicator service will modify the image and resize the actor appropriately.
185    * @return The indicator actor.
186    */
187   Dali::Actor GetActor();
188
189   /**
190    * Opens a new connection for the required orientation.
191    * @param[in] orientation The new orientation
192    */
193   void Open( Dali::Window::WindowOrientation orientation );
194
195   /**
196    * Close the current connection. Will respond with Observer::IndicatorClosed()
197    * when done.
198    * @note, IndicatorClosed() will be called synchronously if there's no update
199    * in progress, or asychronously if waiting for SignalUploaded )
200    */
201   void Close();
202
203   /**
204    * Set the opacity mode of the indicator background.
205    * @param[in] mode opacity mode
206    */
207   void SetOpacityMode( Dali::Window::IndicatorBgOpacity mode );
208
209   /**
210    * Set whether the indicator is visible or not.
211    * @param[in] visibleMode visible mode for indicator bar.
212    * @param[in] forceUpdate true if want to change visible mode forcely
213    */
214   void SetVisible( Dali::Window::IndicatorVisibleMode visibleMode, bool forceUpdate = false );
215
216   /**
217    * Check whether the indicator is connected to the indicator service.
218    * @return whether the indicator is connected or not.
219    */
220   bool IsConnected();
221
222   /**
223    * Send message to the indicator service.
224    * @param[in] messageDomain Message Reference number
225    * @param[in] messageId Reference number of the message this message refers to
226    * @param[in] data The data to send as part of the message
227    * @param[in] size Length of the data, in bytes, to send
228    * @return whether the message is sent successfully or not
229    */
230   bool SendMessage( int messageDomain, int messageId, const void *data, int size );
231
232 private:
233   /**
234    * Initialize the indicator actors
235    */
236   void Initialize();
237
238   /**
239    * Constructs the renderers used for the background
240    */
241   Dali::Geometry CreateBackgroundGeometry();
242
243   /**
244    * Touch event callback.
245    * It should pass the valid touch event to indicator server
246    *
247    * @param[in] indicator  The indicator actor that was touched
248    * @param[in] touchEvent The touch event
249    */
250   bool OnTouched(Dali::Actor indicator, const TouchEvent& touchEvent);
251
252   /**
253    * Pan gesture callback.
254    * It finds flick down gesture to show hidden indicator image
255    *
256    * @param[in] actor  The actor for gesture
257    * @param[in] gesture The gesture event
258    */
259   void OnPan( Dali::Actor actor, const Dali::PanGesture& gesture );
260
261   /**
262    * Touch event callback on stage.
263    * If stage is touched, hide showing indicator image
264    *
265    * @param[in] touchEvent The touch event
266    */
267   void OnStageTouched(const Dali::TouchEvent& touchEvent);
268
269   /**
270    * Connect to the indicator service
271    */
272   bool Connect();
273
274   /**
275    * Start the reconnection timer. This will run every second until we reconnect to
276    * the indicator service.
277    */
278   void StartReconnectionTimer();
279
280   /**
281    * If connection failed, attempt to re-connect every second
282    */
283   bool OnReconnectTimer();
284
285   /**
286    * Disconnect from the indicator service
287    */
288   void Disconnect();
289
290   /**
291    * Handle Resize event
292    * @param[in] width The new width
293    * @param[in] height The new height
294    */
295   void Resize( int width, int height );
296
297   /**
298    * Set the lock file info.
299    * @param[in] epcEvent Current ecore event.
300    */
301   void SetLockFileInfo( Ecore_Ipc_Event_Server_Data *epcEvent );
302
303   /**
304    * Set the shared indicator image info
305    * @param[in] epcEvent The event containing the image data
306    */
307   void SetSharedImageInfo( Ecore_Ipc_Event_Server_Data *epcEvent );
308
309   /**
310    * Load the shared indicator image
311    * @param[in] epcEvent The event containing the image data
312    */
313   void LoadSharedImage( Ecore_Ipc_Event_Server_Data *epcEvent );
314
315   /**
316    * Load the pixmap indicator image
317    * @param[in] epcEvent The event containing the image data
318    */
319   void LoadPixmapImage( Ecore_Ipc_Event_Server_Data *epcEvent );
320
321   /**
322    * Inform dali that the indicator data has been updated.
323    * @param[in] bufferNumber The shared file number
324    */
325   void UpdateImageData( int bufferNumber );
326
327   /**
328    * Lock the temporary file, Copy the shared image into IndicatorBuffer
329    * and then unlock the temporary file.
330    * Caller should ensure we are not writing image to gl texture.
331    * @param[in] bufferNumber The shared file number
332    */
333   bool CopyToBuffer( int bufferNumber );
334
335   /**
336    * Create a new image for the indicator, and set up signal handling for it.
337    * @param[in] bufferNumber The shared file number
338    */
339   void CreateNewImage( int bufferNumber );
340
341   /**
342    * Create a new pixmap image for the indicator, and set up signal handling for it.
343    */
344   void CreateNewPixmapImage();
345
346   /**
347    * Indicator type has changed.
348    * Inform observer
349    * @param[in] type The new indicator type
350    */
351   void OnIndicatorTypeChanged( Type type );
352
353   /**
354    * Check whether the indicator could be visible or invisible
355    * @return true if indicator should be shown
356    */
357   bool CheckVisibleState();
358
359   /**
360    * Show/Hide indicator actor with effect
361    * @param[in] duration how long need to show the indicator,
362    *                     if it equal to 0, hide the indicator
363    *                     if it less than 0, show always
364    */
365   void ShowIndicator( float duration );
366
367   /**
368    * Showing timer callback
369    */
370   bool OnShowTimer();
371
372   /**
373    * Showing animation finished callback
374    * @param[in] animation
375    */
376   void OnAnimationFinished( Dali::Animation& animation );
377
378 private: // Implementation of ServerConnection::Observer
379   /**
380    * @copydoc Dali::Internal::Adaptor::ServerConnection::Observer::DataReceived()
381    */
382   virtual void DataReceived( void* event );
383
384   /**
385    * @copydoc Dali::Internal::Adaptor::ServerConnection::Observer::DataReceived()
386    */
387   virtual void ConnectionClosed();
388
389 private:
390
391   /**
392    * Clear shared file info
393    */
394   void ClearSharedFileInfo();
395
396 private:
397
398   struct SharedFileInfo
399   {
400     SharedFileInfo()
401       : mLock( NULL ),
402         mSharedFile( NULL ),
403         mImageWidth( 0 ),
404         mImageHeight( 0 ),
405         mLockFileName(),
406         mSharedFileName(),
407         mSharedFileID( 0 ),
408         mSharedFileNumber( 0 )
409     {
410     }
411
412     LockFile*                        mLock;              ///< File lock for the shared file
413     SharedFile*                      mSharedFile;        ///< Shared file
414
415     int                              mImageWidth;        ///< Shared image width
416     int                              mImageHeight;       ///< Shared image height
417
418     std::string                      mLockFileName;      ///< Lock file name
419     std::string                      mSharedFileName;    ///< Shared file name
420     int                              mSharedFileID;      ///< Shared file ID
421     int                              mSharedFileNumber;  ///< Shared file number
422   };
423
424   static const int SHARED_FILE_NUMBER = 2;               ///< Shared file number
425
426   Dali::Geometry                   mTranslucentGeometry; ///< Geometry used for rendering the translucent background
427   Dali::Geometry                   mSolidGeometry;       ///< Geometry used for rendering the opaque background
428   Dali::Material                   mBackgroundMaterial;  ///< Material used for rendering the background
429
430   IndicatorBufferPtr               mIndicatorBuffer;     ///< class which handles indicator rendering
431   PixmapId                         mPixmap;              ///< Pixmap including indicator content
432   Dali::Image                      mImage;               ///< Image created from mIndicatorBuffer
433   Dali::ImageActor                 mIndicatorImageActor; ///< Actor created from mImage
434
435   Dali::Actor                      mIndicatorImageContainerActor; ///< Actor container for image and background
436   Dali::Actor                      mBackgroundActor;     ///< Actor for background
437   Dali::Actor                      mIndicatorActor;      ///< Handle to topmost indicator actor
438   Dali::Actor                      mEventActor;          ///< Handle to event
439   Dali::PanGestureDetector         mPanDetector;         ///< Pan detector to find flick gesture for hidden indicator
440   float                            mGestureDeltaY;       ///< Checking how much panning moved
441   bool                             mGestureDetected;     ///< Whether find the flick gesture
442
443   Dali::Timer                      mReconnectTimer;      ///< Reconnection timer
444   SlotDelegate< Indicator >        mConnection;
445
446   Dali::Window::IndicatorBgOpacity mOpacityMode;         ///< Opacity enum for background
447   Indicator::State                 mState;               ///< The connection state
448
449   Adaptor*                         mAdaptor;
450   ServerConnection*                mServerConnection;
451   Indicator::Observer*             mObserver;            ///< Upload observer
452
453   Dali::Window::WindowOrientation  mOrientation;
454   int                              mImageWidth;
455   int                              mImageHeight;
456   Dali::Window::IndicatorVisibleMode mVisible;           ///< Whether the indicator is visible
457
458   Dali::Timer                      mShowTimer;           ///< Timer to show indicator
459   bool                             mIsShowing;           ///< Whether the indicator is showing on the screen
460   Dali::Animation                  mIndicatorAnimation;  ///< Animation to show/hide indicator image
461
462   bool                             mIsAnimationPlaying;  ///< Whether the animation is playing
463
464   int                              mCurrentSharedFile;   ///< Current shared file number
465   SharedFileInfo                   mSharedFileInfo[SHARED_FILE_NUMBER];    ///< Table to store shared file info
466 };
467
468 } // Adaptor
469 } // Internal
470 } // Dali
471
472 #endif