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