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