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