(Indicator) Enhanced the accuracy to get out the hidden indicator
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / internal / 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 Flora License, Version 1.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://floralicense.org/license/
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 // EXTERNAL INCLUDES
21 #include <dali/public-api/images/bitmap-image.h>
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/adaptor-framework/common/window.h>
26 #include <dali/public-api/adaptor-framework/common/timer.h>
27 #include <dali/public-api/animation/animation.h>
28
29 // INTERNAL INCLUDES
30 #include <internal/common/indicator-buffer.h>
31 #include <internal/common/server-connection.h>
32 #include <internal/common/shared-file.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 protected:
87   /**
88    * Class to encapsulate lock file
89    */
90   class LockFile
91   {
92   public:
93     /**
94      * Constructor. open lock file
95      */
96     LockFile(const char* filename);
97
98     /**
99      * Close lock file
100      */
101     ~LockFile();
102
103     /**
104      * Grab an exclusive lock on this file
105      * @return true if the lock succeeded, false if it failed
106      */
107     bool Lock();
108
109     /**
110      * Remove the lock
111      */
112     void Unlock();
113
114     /**
115      * Test if there is an error with the lock file, and clears
116      * the error flag;
117      * @return true if an error was thrown
118      */
119     bool RetrieveAndClearErrorStatus();
120
121   private:
122     std::string mFilename;
123     int         mFileDescriptor;
124     bool        mErrorThrown;
125   };
126
127   /**
128    * Class to ensure lock/unlock through object destruction
129    */
130   class ScopedLock
131   {
132   public:
133     /**
134      * Constructor - creates a lock on the lockfile
135      * @param[in] lockFile The lockfile to use
136      */
137     ScopedLock( LockFile* lockFile );
138
139     /**
140      * Destructor - removes the lock (if any) on the lockfile
141      */
142     ~ScopedLock();
143
144     /**
145      * Method to test if the locking succeeded
146      * @return TRUE if locked
147      */
148     bool IsLocked();
149
150   private:
151     LockFile* mLockFile; ///< The lock file to use
152     bool      mLocked;   ///< Whether the lock succeeded
153   };
154
155
156 public:
157   /**
158    * Constructor. Creates a new indicator and opens a connection for
159    * the required orientation.
160    * @param[in] orientation The orientation in which to draw the indicator
161    * @param[in] observer The indicator closed
162    */
163   Indicator( Adaptor* adaptor,
164              Dali::Window::WindowOrientation orientation,
165              Observer* observer );
166
167   /**
168    * Destructor
169    */
170   virtual ~Indicator();
171
172   void SetAdaptor(Adaptor* adaptor);
173
174   /**
175    * Get the actor which contains the indicator image. Ensure that the handle is
176    * released when no longer needed.
177    * Changes from the indicator service will modify the image and resize the actor appropriately.
178    * @return The indicator actor.
179    */
180   Dali::Actor GetActor();
181
182   /**
183    * Opens a new connection for the required orientation.
184    * @param[in] orientation The new orientation
185    */
186   void Open( Dali::Window::WindowOrientation orientation );
187
188   /**
189    * Close the current connection. Will respond with Observer::IndicatorClosed()
190    * when done.
191    * @note, IndicatorClosed() will be called synchronously if there's no update
192    * in progress, or asychronously if waiting for SignalUploaded )
193    */
194   void Close();
195
196   /**
197    * Set the opacity mode of the indicator background.
198    * @param[in] mode opacity mode
199    */
200   void SetOpacityMode( Dali::Window::IndicatorBgOpacity mode );
201
202   /**
203    * Set whether the indicator is visible or not.
204    * @param[in] visibleMode visible mode for indicator bar.
205    */
206   void SetVisible( Dali::Window::IndicatorVisibleMode visibleMode );
207
208   /**
209    * Check whether the indicator is connected to the indicator service.
210    * @return whether the indicator is connected or not.
211    */
212   bool IsConnected();
213
214   /**
215    * Send message to the indicator service.
216    * @param[in] messageDomain Message Reference number
217    * @param[in] messageId Reference number of the message this message refers to
218    * @param[in] data The data to send as part of the message
219    * @param[in] size Length of the data, in bytes, to send
220    * @return whether the message is sent successfully or not
221    */
222   bool SendMessage( int messageDomain, int messageId, const void *data, int size );
223
224 private:
225   /**
226    * Initialize the indicator actors
227    */
228   void Initialize();
229
230   /**
231    * Set the opacity of the background image
232    */
233   void SetBackgroundOpacity( Dali::Window::IndicatorBgOpacity opacity );
234
235   /**
236    * Touch event callback.
237    * @param[in] indicator  The indicator actor that was touched
238    * @param[in] touchEvent The touch event
239    */
240   bool OnTouched(Dali::Actor indicator, const TouchEvent& touchEvent);
241
242   /**
243    * Touch event callback on stage.
244    * @param[in] touchEvent The touch event
245    */
246   void OnStageTouched(const Dali::TouchEvent& touchEvent);
247
248   /**
249    * Return the given orientation in degrees
250    * @param[in] orientation The given indicator orientation
251    * @return value of 0, 90, 180 or 270
252    */
253   int OrientationToDegrees( Dali::Window::WindowOrientation orientation );
254
255   /**
256    * Connect to the indicator service matching the orientation
257    * @param[in] orientation The current indicator orientation
258    */
259   bool Connect( Dali::Window::WindowOrientation orientation );
260
261   /**
262    * Connect to the indicator service
263    * @param[in] serviceName The indicator service name
264    */
265   bool Connect( const char *serviceName );
266
267   /**
268    * Start the reconnection timer. This will run every second until we reconnect to
269    * the indicator service.
270    */
271   void StartReconnectionTimer();
272
273   /**
274    * If connection failed, attempt to re-connect every second
275    */
276   bool OnReconnectTimer();
277
278   /**
279    * Disconnect from the indicator service
280    */
281   void Disconnect();
282
283   /**
284    * Close existing lock file and open the new lock file.
285    * @param[in] epcEvent Current ecore event.
286    */
287   void NewLockFile(Ecore_Ipc_Event_Server_Data *epcEvent);
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    * Load the shared indicator image
298    * @param[in] epcEvent The event containing the image data
299    */
300   void LoadSharedImage(Ecore_Ipc_Event_Server_Data *epcEvent);
301
302   /**
303    * Load the pixmap indicator image
304    * @param[in] epcEvent The event containing the image data
305    */
306   void LoadPixmapImage( Ecore_Ipc_Event_Server_Data *epcEvent );
307
308   /**
309    * Inform dali that the indicator data has been updated.
310    */
311   void UpdateImageData();
312
313   /**
314    * Lock the temporary file, Copy the shared image into IndicatorBuffer
315    * and then unlock the temporary file.
316    * Caller should ensure we are not writing image to gl texture.
317    */
318   bool CopyToBuffer();
319
320   /**
321    * Update the background with the correct colors
322    */
323   void SetBackground();
324
325   /**
326    * Create a new image for the indicator, and set up signal handling for it.
327    */
328   void CreateNewImage();
329
330   /**
331    * Create a new pixmap image for the indicator, and set up signal handling for it.
332    */
333   void CreateNewPixmapImage();
334
335   /**
336    * Indicator type has changed.
337    * Inform observer
338    * @param[in] type The new indicator type
339    */
340   void OnIndicatorTypeChanged( Type type );
341
342   /**
343    * Check whether the indicator could be visible or invisible
344    * @return true if indicator should be shown
345    */
346   bool CheckVisibleState();
347
348   /**
349    * Show/Hide indicator actor with effect
350    * @param[in] duration how long need to show the indicator,
351    *                     if it equal to 0, hide the indicator
352    *                     if it less than 0, show always
353    */
354   void ShowIndicator(float duration);
355
356   /**
357    * Showing timer callback
358    */
359   bool OnShowTimer();
360
361   /**
362    * Showing animation finished callback
363    * @param[in] animation
364    */
365   void OnAnimationFinished(Dali::Animation& animation);
366
367 private: // Implementation of ServerConnection::Observer
368   /**
369    * @copydoc Dali::Internal::Adaptor::ServerConnection::Observer::DataReceived()
370    */
371   virtual void DataReceived(void* event);
372
373   /**
374    * @copydoc Dali::Internal::Adaptor::ServerConnection::Observer::DataReceived()
375    */
376   virtual void ConnectionClosed();
377
378 private:
379   /**
380    * Construct the gradient mesh
381    */
382   void ConstructBackgroundMesh();
383
384 private:
385
386   IndicatorBufferPtr               mIndicatorBuffer;     ///< class which handles indicator rendering
387   PixmapId                         mPixmap;              ///< Pixmap including indicator content
388   Dali::Image                      mImage;               ///< Image created from mIndicatorBuffer
389   Dali::ImageActor                 mIndicatorImageActor; ///< Actor created from mImage
390
391   Dali::AnimatableMesh             mBackgroundMesh;
392   Dali::MeshActor                  mBackgroundActor;     ///< Actor for background
393   Dali::Actor                      mIndicatorActor;      ///< Handle to topmost indicator actor
394   Dali::Actor                      mEventActor;          ///< Handle to event
395
396   Dali::Timer                      mReconnectTimer;      ///< Reconnection timer
397   SlotDelegate< Indicator >        mConnection;
398
399   Dali::Window::IndicatorBgOpacity mOpacityMode;         ///< Opacity enum for background
400   Indicator::State                 mState;               ///< The connection state
401
402   Adaptor*                         mAdaptor;
403   ServerConnection*                mServerConnection;
404   LockFile*                        mLock;                ///< File lock for the shared file
405   SharedFile*                      mSharedFile;          ///< Shared file
406   Indicator::Observer*             mObserver;            ///< Upload observer
407
408   Dali::Window::WindowOrientation  mOrientation;
409   int                              mRotation;            ///< Orientation in degrees
410   int                              mImageWidth;
411   int                              mImageHeight;
412   Dali::Window::IndicatorVisibleMode mVisible;           ///< Whether the indicator is visible
413
414   Dali::Timer                      mShowTimer;           ///< Timer to show indicator
415   bool                             mIsShowing;           ///< Whether the indicator is showing on the screen
416   Dali::Animation                  mIndicatorAnimation;  ///< Animation to show/hide indicator image
417
418   bool                             mIsAnimationPlaying;  ///< Whether the animation is playing
419   bool                             mTouchedDown;         ///< Whether the indicator area touched down
420   Dali::Vector2                    mTouchDownPosition;   ///< Indicator area touched down position
421
422 };
423
424 } // Adaptor
425 } // Internal
426 } // Dali
427
428 #endif