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