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