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