576e3485be5dfec4592ed23885d93bbba9c95876
[platform/core/uifw/dali-adaptor.git] / adaptors / ecore / wayland / window-impl-ecore-wl.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "window-impl.h"
20
21 // EXTERNAL HEADERS
22 #include <Ecore.h>
23 #include <Ecore_Wayland.h>
24
25 #include <dali/integration-api/core.h>
26 #include <dali/integration-api/system-overlay.h>
27 #include <dali/public-api/render-tasks/render-task.h>
28 #include <dali/public-api/render-tasks/render-task-list.h>
29 #include <orientation.h>
30
31 // INTERNAL HEADERS
32 #include <window-render-surface.h>
33 #include <drag-and-drop-detector-impl.h>
34 #include <ecore-indicator-impl.h>
35 #include <window-visibility-observer.h>
36 #include <orientation-impl.h>
37 namespace
38 {
39 const float INDICATOR_ANIMATION_DURATION( 0.18f ); // 180 milli seconds
40 const float INDICATOR_SHOW_Y_POSITION( 0.0f );
41 const float INDICATOR_HIDE_Y_POSITION( -52.0f );
42 }
43
44 namespace Dali
45 {
46 namespace Internal
47 {
48 namespace Adaptor
49 {
50 #if defined(DEBUG_ENABLED)
51 Debug::Filter* gWindowLogFilter = Debug::Filter::New(Debug::Concise, false, "LOG_WINDOW");
52 #endif
53
54 /**
55  * TODO: Abstract Window class out and move this into a window implementation for Ecore
56  */
57 struct Window::EventHandler
58 {
59   /**
60    * Constructor
61    * @param[in]  window  A pointer to the window class.
62    */
63   EventHandler( Window* window )
64   : mWindow( window ),
65     mWindowPropertyHandler( NULL ),
66     mWindowIconifyStateHandler( NULL ),
67     mEcoreWindow( 0 )
68   {
69     // store ecore window handle
70     ECore::WindowRenderSurface* wlWindow( dynamic_cast< ECore::WindowRenderSurface * >( mWindow->mSurface ) );
71     if( wlWindow )
72     {
73       mEcoreWindow = wlWindow->GetWlWindow();
74     }
75     DALI_ASSERT_ALWAYS( mEcoreWindow != 0 && "There is no ecore Wl window");
76
77 #ifndef DALI_PROFILE_UBUNTU
78     if( mWindow->mEcoreEventHander )
79     {
80       mWindowIconifyStateHandler = ecore_event_handler_add( ECORE_WL_EVENT_WINDOW_ICONIFY_STATE_CHANGE, EcoreEventWindowIconifyStateChanged, this );
81     }
82 #endif
83
84   }
85
86   /**
87    * Destructor
88    */
89   ~EventHandler()
90   {
91     if ( mWindowPropertyHandler )
92     {
93       ecore_event_handler_del( mWindowPropertyHandler );
94     }
95     if ( mWindowIconifyStateHandler )
96     {
97       ecore_event_handler_del( mWindowIconifyStateHandler );
98     }
99   }
100
101   // Static methods
102
103   /// Called when the window properties are changed.
104   static Eina_Bool EcoreEventWindowPropertyChanged( void* data, int type, void* event )
105   {
106     return EINA_FALSE;
107   }
108
109 #ifndef DALI_PROFILE_UBUNTU
110   /// Called when the window iconify state is changed.
111   static Eina_Bool EcoreEventWindowIconifyStateChanged( void* data, int type, void* event )
112   {
113     Ecore_Wl_Event_Window_Iconify_State_Change* iconifyChangedEvent( (Ecore_Wl_Event_Window_Iconify_State_Change*)event );
114     EventHandler* handler( (EventHandler*)data );
115     Eina_Bool handled( ECORE_CALLBACK_PASS_ON );
116
117     if ( handler && handler->mWindow )
118     {
119       WindowVisibilityObserver* observer( handler->mWindow->mAdaptor );
120       if ( observer && ( iconifyChangedEvent->win == (unsigned int) ecore_wl_window_id_get( handler->mEcoreWindow ) ) )
121       {
122         if( iconifyChangedEvent->iconified == EINA_TRUE )
123         {
124           observer->OnWindowHidden();
125           DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Iconfied\n", handler->mEcoreWindow );
126         }
127         else
128         {
129           observer->OnWindowShown();
130           DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window (%d) Shown\n", handler->mEcoreWindow );
131         }
132         handled = ECORE_CALLBACK_DONE;
133       }
134     }
135
136     return handled;
137   }
138 #endif
139
140   // Data
141   Window* mWindow;
142   Ecore_Event_Handler* mWindowPropertyHandler;
143   Ecore_Event_Handler* mWindowIconifyStateHandler;
144   Ecore_Wl_Window* mEcoreWindow;
145 };
146
147
148 Window* Window::New(const PositionSize& posSize, const std::string& name, const std::string& className, bool isTransparent)
149 {
150   Window* window = new Window();
151   window->mIsTransparent = isTransparent;
152   window->Initialize(posSize, name, className);
153   return window;
154 }
155
156 void Window::SetAdaptor(Dali::Adaptor& adaptor)
157 {
158   DALI_ASSERT_ALWAYS( !mStarted && "Adaptor already started" );
159   mStarted = true;
160
161   // Only create one overlay per window
162   Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
163   Integration::Core& core = adaptorImpl.GetCore();
164   mOverlay = &core.GetSystemOverlay();
165
166   Dali::RenderTaskList taskList = mOverlay->GetOverlayRenderTasks();
167   taskList.CreateTask();
168
169   mAdaptor = &adaptorImpl;
170   mAdaptor->AddObserver( *this );
171
172   // Can only create the detector when we know the Core has been instantiated.
173   mDragAndDropDetector = DragAndDropDetector::New();
174   mAdaptor->SetDragAndDropDetector( &GetImplementation( mDragAndDropDetector ) );
175
176   if( mOrientation )
177   {
178     mOrientation->SetAdaptor(adaptor);
179   }
180
181   if( mIndicator != NULL )
182   {
183     mIndicator->SetAdaptor(mAdaptor);
184   }
185 }
186
187 RenderSurface* Window::GetSurface()
188 {
189   return mSurface;
190 }
191
192 void Window::ShowIndicator( Dali::Window::IndicatorVisibleMode visibleMode )
193 {
194   DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "visible : %d\n", visibleMode );
195   DALI_ASSERT_DEBUG(mOverlay);
196
197   ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
198   DALI_ASSERT_DEBUG(wlSurface);
199
200   Ecore_Wl_Window* wlWindow = NULL;
201   if( wlSurface )
202   {
203     wlWindow = wlSurface->GetWlWindow();
204   }
205
206   mIndicatorVisible = visibleMode;
207
208   if ( mIndicatorVisible == Dali::Window::VISIBLE )
209   {
210     // when the indicator is visible, set proper mode for indicator server according to bg mode
211     if ( mIndicatorOpacityMode == Dali::Window::OPAQUE )
212     {
213       ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_OPAQUE);
214     }
215     else if ( mIndicatorOpacityMode == Dali::Window::TRANSLUCENT )
216     {
217       ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_TRANSLUCENT);
218     }
219     else if ( mIndicatorOpacityMode == Dali::Window::TRANSPARENT )
220     {
221       ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_OPAQUE);
222     }
223   }
224   else
225   {
226     // when the indicator is not visible, set TRANSPARENT mode for indicator server
227     ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_TRANSPARENT); // it means hidden indicator
228   }
229
230   DoShowIndicator( mIndicatorOrientation );
231 }
232
233 void Window::RotateIndicator(Dali::Window::WindowOrientation orientation)
234 {
235   DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "Orientation: %d\n", orientation );
236
237   DoRotateIndicator( orientation );
238 }
239
240 void Window::SetIndicatorBgOpacity( Dali::Window::IndicatorBgOpacity opacityMode )
241 {
242   mIndicatorOpacityMode = opacityMode;
243
244   if( mIndicator != NULL )
245   {
246     mIndicator->SetOpacityMode( opacityMode );
247   }
248 }
249
250 void Window::SetClass(std::string name, std::string klass)
251 {
252   ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
253
254   if( wlSurface )
255   {
256     Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
257     ecore_wl_window_title_set( wlWindow, name.c_str() );
258     ecore_wl_window_class_name_set( wlWindow, klass.c_str() );
259   }
260   else
261   {
262     DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window has no surface\n" );
263   }
264 }
265
266 Window::Window()
267 : mSurface(NULL),
268   mIndicatorVisible(Dali::Window::VISIBLE),
269   mIndicatorIsShown(false),
270   mShowRotatedIndicatorOnClose(false),
271   mStarted(false),
272   mIsTransparent(false),
273   mWMRotationAppSet(false),
274   mEcoreEventHander(true),
275   mIndicator(NULL),
276   mIndicatorOrientation(Dali::Window::PORTRAIT),
277   mNextIndicatorOrientation(Dali::Window::PORTRAIT),
278   mIndicatorOpacityMode(Dali::Window::OPAQUE),
279   mOverlay(NULL),
280   mAdaptor(NULL),
281   mEventHandler(NULL),
282   mPreferredOrientation(Dali::Window::PORTRAIT)
283 {
284 }
285
286 Window::~Window()
287 {
288   delete mEventHandler;
289
290   if( mIndicator )
291   {
292     mIndicator->Close();
293     delete mIndicator;
294   }
295
296   if ( mAdaptor )
297   {
298     mAdaptor->RemoveObserver( *this );
299     mAdaptor->SetDragAndDropDetector( NULL );
300     mAdaptor = NULL;
301   }
302
303   delete mSurface;
304 }
305
306 void Window::Initialize(const PositionSize& windowPosition, const std::string& name, const std::string& className)
307 {
308   // create an Wayland window by default
309   Any surface;
310   ECore::WindowRenderSurface* windowSurface = new ECore::WindowRenderSurface( windowPosition, surface, name, mIsTransparent );
311
312   mSurface = windowSurface;
313   SetClass( name, className );
314   windowSurface->Map();
315
316   mOrientation = Orientation::New(this);
317
318   // create event handler for Wayland window
319   mEventHandler = new EventHandler( this );
320 }
321
322 void Window::DoShowIndicator( Dali::Window::WindowOrientation lastOrientation )
323 {
324   if( mIndicator == NULL )
325   {
326     if( mIndicatorVisible != Dali::Window::INVISIBLE )
327     {
328       mIndicator = new Indicator( mAdaptor, mIndicatorOrientation, this );
329       mIndicator->SetOpacityMode( mIndicatorOpacityMode );
330       Dali::Actor actor = mIndicator->GetActor();
331       SetIndicatorActorRotation();
332       mOverlay->Add(actor);
333     }
334     // else don't create a hidden indicator
335   }
336   else // Already have indicator
337   {
338     if( mIndicatorVisible == Dali::Window::VISIBLE )
339     {
340       // If we are resuming, and rotation has changed,
341       if( mIndicatorIsShown == false && mIndicatorOrientation != mNextIndicatorOrientation )
342       {
343         // then close current indicator and open new one
344         mShowRotatedIndicatorOnClose = true;
345         mIndicator->Close(); // May synchronously call IndicatorClosed() callback & 1 level of recursion
346         // Don't show actor - will contain indicator for old orientation.
347       }
348     }
349   }
350
351   // set indicator visible mode
352   if( mIndicator != NULL )
353   {
354     mIndicator->SetVisible( mIndicatorVisible );
355   }
356
357   bool show = (mIndicatorVisible != Dali::Window::INVISIBLE );
358   SetIndicatorProperties( show, lastOrientation );
359   mIndicatorIsShown = show;
360 }
361
362 void Window::DoRotateIndicator( Dali::Window::WindowOrientation orientation )
363 {
364   if( mIndicatorIsShown )
365   {
366     mShowRotatedIndicatorOnClose = true;
367     mNextIndicatorOrientation = orientation;
368     mIndicator->Close(); // May synchronously call IndicatorClosed() callback
369   }
370   else
371   {
372     // Save orientation for when the indicator is next shown
373     mShowRotatedIndicatorOnClose = false;
374     mNextIndicatorOrientation = orientation;
375   }
376 }
377
378 void Window::SetIndicatorProperties( bool isShow, Dali::Window::WindowOrientation lastOrientation )
379 {
380   ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
381
382   if( wlSurface )
383   {
384     Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
385     if ( isShow )
386     {
387       ecore_wl_window_indicator_state_set(wlWindow, ECORE_WL_INDICATOR_STATE_ON);
388     }
389     else
390     {
391       ecore_wl_window_indicator_state_set(wlWindow, ECORE_WL_INDICATOR_STATE_OFF);
392     }
393   }
394 }
395
396 void Window::IndicatorTypeChanged(Indicator::Type type)
397 {
398 #if defined(DALI_PROFILE_MOBILE)
399   ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
400
401   if( wlSurface )
402   {
403     Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
404     switch(type)
405     {
406       case Indicator::INDICATOR_TYPE_1:
407         ecore_wl_indicator_visible_type_set(wlWindow, ECORE_WL_INDICATOR_VISIBLE_TYPE_SHOWN);
408         break;
409
410       case Indicator::INDICATOR_TYPE_2:
411         ecore_wl_indicator_visible_type_set(wlWindow, ECORE_WL_INDICATOR_VISIBLE_TYPE_HIDDEN);
412         break;
413
414       case Indicator::INDICATOR_TYPE_UNKNOWN:
415       default:
416         break;
417     }
418   }
419 #endif //MOBILE
420 }
421
422 void Window::IndicatorClosed( IndicatorInterface* indicator )
423 {
424   DALI_LOG_TRACE_METHOD( gWindowLogFilter );
425
426   if( mShowRotatedIndicatorOnClose )
427   {
428     Dali::Window::WindowOrientation currentOrientation = mIndicatorOrientation;
429     mIndicator->Open(mNextIndicatorOrientation);
430     mIndicatorOrientation = mNextIndicatorOrientation;
431     SetIndicatorActorRotation();
432     DoShowIndicator(currentOrientation);
433   }
434 }
435
436 void Window::IndicatorVisibilityChanged(bool isVisible)
437 {
438   mIndicatorVisibilityChangedSignal.Emit(isVisible);
439 }
440
441 void Window::SetIndicatorActorRotation()
442 {
443   DALI_LOG_TRACE_METHOD( gWindowLogFilter );
444   DALI_ASSERT_DEBUG( mIndicator != NULL );
445
446   Dali::Actor actor = mIndicator->GetActor();
447   switch( mIndicatorOrientation )
448   {
449     case Dali::Window::PORTRAIT:
450       actor.SetParentOrigin( ParentOrigin::TOP_CENTER );
451       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
452       actor.SetOrientation( Degree(0), Vector3::ZAXIS );
453       break;
454     case Dali::Window::PORTRAIT_INVERSE:
455       actor.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
456       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
457       actor.SetOrientation( Degree(180), Vector3::ZAXIS );
458       break;
459     case Dali::Window::LANDSCAPE:
460       actor.SetParentOrigin( ParentOrigin::CENTER_LEFT );
461       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
462       actor.SetOrientation( Degree(270), Vector3::ZAXIS );
463       break;
464     case Dali::Window::LANDSCAPE_INVERSE:
465       actor.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
466       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
467       actor.SetOrientation( Degree(90), Vector3::ZAXIS );
468       break;
469   }
470 }
471
472 void Window::Raise()
473 {
474   ecore_wl_window_raise( mEventHandler->mEcoreWindow );
475 }
476
477 void Window::Lower()
478 {
479   ecore_wl_window_lower( mEventHandler->mEcoreWindow );
480 }
481
482 void Window::Activate()
483 {
484   ecore_wl_window_activate( mEventHandler->mEcoreWindow );
485 }
486
487 Dali::DragAndDropDetector Window::GetDragAndDropDetector() const
488 {
489   return mDragAndDropDetector;
490 }
491
492 Dali::Any Window::GetNativeHandle() const
493 {
494   if(mEventHandler)
495   {
496     return mEventHandler->mEcoreWindow;
497   }
498   else
499   {
500     return Dali::Any();
501   }
502 }
503
504 void Window::OnStart()
505 {
506   DoShowIndicator( mIndicatorOrientation );
507 }
508
509 void Window::OnPause()
510 {
511 }
512
513 void Window::OnResume()
514 {
515   // resume indicator status
516   if( mIndicator != NULL )
517   {
518     // Restore own indicator opacity
519     // Send opacity mode to indicator service when app resumed
520     mIndicator->SetOpacityMode( mIndicatorOpacityMode );
521   }
522 }
523
524 void Window::OnStop()
525 {
526   if( mIndicator )
527   {
528     mIndicator->Close();
529   }
530
531   delete mIndicator;
532   mIndicator = NULL;
533 }
534
535 void Window::OnDestroy()
536 {
537   mAdaptor = NULL;
538 }
539
540 void Window::AddAvailableOrientation(Dali::Window::WindowOrientation orientation)
541 {
542   bool found = false;
543
544   for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
545   {
546     if(mAvailableOrientations[i] == orientation)
547     {
548       found = true;
549       break;
550     }
551   }
552
553   if( ! found )
554   {
555     mAvailableOrientations.push_back(orientation);
556     SetAvailableOrientations( mAvailableOrientations );
557   }
558 }
559
560 void Window::RemoveAvailableOrientation(Dali::Window::WindowOrientation orientation)
561 {
562   for( std::vector<Dali::Window::WindowOrientation>::iterator iter = mAvailableOrientations.begin();
563        iter != mAvailableOrientations.end(); ++iter )
564   {
565     if( *iter == orientation )
566     {
567       mAvailableOrientations.erase( iter );
568       break;
569     }
570   }
571   SetAvailableOrientations( mAvailableOrientations );
572 }
573
574 void Window::SetAvailableOrientations(const std::vector<Dali::Window::WindowOrientation>& orientations)
575 {
576   int rotations[4];
577   for( std::size_t i = 0; i < mAvailableOrientations.size(); ++i )
578   {
579     rotations[i] = static_cast< int >( mAvailableOrientations[i] );
580   }
581   ecore_wl_window_rotation_available_rotations_set( mEventHandler->mEcoreWindow, rotations, mAvailableOrientations.size() );
582 }
583
584 const std::vector<Dali::Window::WindowOrientation>& Window::GetAvailableOrientations()
585 {
586   return mAvailableOrientations;
587 }
588
589 void Window::SetPreferredOrientation(Dali::Window::WindowOrientation orientation)
590 {
591   mPreferredOrientation = orientation;
592
593   ecore_wl_window_rotation_preferred_rotation_set( mEventHandler->mEcoreWindow, orientation );
594 }
595
596 Dali::Window::WindowOrientation Window::GetPreferredOrientation()
597 {
598   return mPreferredOrientation;
599 }
600
601 void Window::RotationDone( int orientation, int width, int height )
602 {
603   ecore_wl_window_rotation_change_done_send( mEventHandler->mEcoreWindow );
604 }
605
606
607 } // Adaptor
608 } // Internal
609 } // Dali