Merge "[Tizen] Remove profile build dependencies" into tizen
[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   Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
200
201   mIndicatorVisible = visibleMode;
202
203   if ( mIndicatorVisible == Dali::Window::VISIBLE )
204   {
205     // when the indicator is visible, set proper mode for indicator server according to bg mode
206     if ( mIndicatorOpacityMode == Dali::Window::OPAQUE )
207     {
208       ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_OPAQUE);
209     }
210     else if ( mIndicatorOpacityMode == Dali::Window::TRANSLUCENT )
211     {
212       ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_TRANSLUCENT);
213     }
214     else if ( mIndicatorOpacityMode == Dali::Window::TRANSPARENT )
215     {
216       ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_OPAQUE);
217     }
218   }
219   else
220   {
221     // when the indicator is not visible, set TRANSPARENT mode for indicator server
222     ecore_wl_window_indicator_opacity_set(wlWindow, ECORE_WL_INDICATOR_TRANSPARENT); // it means hidden indicator
223   }
224
225   DoShowIndicator( mIndicatorOrientation );
226 }
227
228 void Window::RotateIndicator(Dali::Window::WindowOrientation orientation)
229 {
230   DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "Orientation: %d\n", orientation );
231
232   DoRotateIndicator( orientation );
233 }
234
235 void Window::SetIndicatorBgOpacity( Dali::Window::IndicatorBgOpacity opacityMode )
236 {
237   mIndicatorOpacityMode = opacityMode;
238
239   if( mIndicator != NULL )
240   {
241     mIndicator->SetOpacityMode( opacityMode );
242   }
243 }
244
245 void Window::SetClass(std::string name, std::string klass)
246 {
247   ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
248
249   if( wlSurface )
250   {
251     Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
252     ecore_wl_window_title_set( wlWindow, name.c_str() );
253     ecore_wl_window_class_name_set( wlWindow, klass.c_str() );
254   }
255   else
256   {
257     DALI_LOG_INFO( gWindowLogFilter, Debug::General, "Window has no surface\n" );
258   }
259 }
260
261 Window::Window()
262 : mSurface(NULL),
263   mIndicatorVisible(Dali::Window::VISIBLE),
264   mIndicatorIsShown(false),
265   mShowRotatedIndicatorOnClose(false),
266   mStarted(false),
267   mIsTransparent(false),
268   mWMRotationAppSet(false),
269   mEcoreEventHander(true),
270   mIndicator(NULL),
271   mIndicatorOrientation(Dali::Window::PORTRAIT),
272   mNextIndicatorOrientation(Dali::Window::PORTRAIT),
273   mIndicatorOpacityMode(Dali::Window::OPAQUE),
274   mOverlay(NULL),
275   mAdaptor(NULL),
276   mEventHandler(NULL),
277   mPreferredOrientation(Dali::Window::PORTRAIT)
278 {
279 }
280
281 Window::~Window()
282 {
283   delete mEventHandler;
284
285   if( mIndicator )
286   {
287     mIndicator->Close();
288     delete mIndicator;
289   }
290
291   if ( mAdaptor )
292   {
293     mAdaptor->RemoveObserver( *this );
294     mAdaptor->SetDragAndDropDetector( NULL );
295     mAdaptor = NULL;
296   }
297
298   delete mSurface;
299 }
300
301 void Window::Initialize(const PositionSize& windowPosition, const std::string& name, const std::string& className)
302 {
303   // create an Wayland window by default
304   Any surface;
305   ECore::WindowRenderSurface* windowSurface = new ECore::WindowRenderSurface( windowPosition, surface, name, mIsTransparent );
306
307   mSurface = windowSurface;
308   SetClass( name, className );
309   windowSurface->Map();
310
311   mOrientation = Orientation::New(this);
312
313   // create event handler for Wayland window
314   mEventHandler = new EventHandler( this );
315 }
316
317 void Window::DoShowIndicator( Dali::Window::WindowOrientation lastOrientation )
318 {
319   if( mIndicator == NULL )
320   {
321     if( mIndicatorVisible != Dali::Window::INVISIBLE )
322     {
323       mIndicator = new Indicator( mAdaptor, mIndicatorOrientation, this );
324       mIndicator->SetOpacityMode( mIndicatorOpacityMode );
325       Dali::Actor actor = mIndicator->GetActor();
326       SetIndicatorActorRotation();
327       mOverlay->Add(actor);
328     }
329     // else don't create a hidden indicator
330   }
331   else // Already have indicator
332   {
333     if( mIndicatorVisible == Dali::Window::VISIBLE )
334     {
335       // If we are resuming, and rotation has changed,
336       if( mIndicatorIsShown == false && mIndicatorOrientation != mNextIndicatorOrientation )
337       {
338         // then close current indicator and open new one
339         mShowRotatedIndicatorOnClose = true;
340         mIndicator->Close(); // May synchronously call IndicatorClosed() callback & 1 level of recursion
341         // Don't show actor - will contain indicator for old orientation.
342       }
343     }
344   }
345
346   // set indicator visible mode
347   if( mIndicator != NULL )
348   {
349     mIndicator->SetVisible( mIndicatorVisible );
350   }
351
352   bool show = (mIndicatorVisible != Dali::Window::INVISIBLE );
353   SetIndicatorProperties( show, lastOrientation );
354   mIndicatorIsShown = show;
355 }
356
357 void Window::DoRotateIndicator( Dali::Window::WindowOrientation orientation )
358 {
359   if( mIndicatorIsShown )
360   {
361     mShowRotatedIndicatorOnClose = true;
362     mNextIndicatorOrientation = orientation;
363     mIndicator->Close(); // May synchronously call IndicatorClosed() callback
364   }
365   else
366   {
367     // Save orientation for when the indicator is next shown
368     mShowRotatedIndicatorOnClose = false;
369     mNextIndicatorOrientation = orientation;
370   }
371 }
372
373 void Window::SetIndicatorProperties( bool isShow, Dali::Window::WindowOrientation lastOrientation )
374 {
375   ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
376
377   if( wlSurface )
378   {
379     Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
380     if ( isShow )
381     {
382       ecore_wl_window_indicator_state_set(wlWindow, ECORE_WL_INDICATOR_STATE_ON);
383     }
384     else
385     {
386       ecore_wl_window_indicator_state_set(wlWindow, ECORE_WL_INDICATOR_STATE_OFF);
387     }
388   }
389 }
390
391 void Window::IndicatorTypeChanged(Indicator::Type type)
392 {
393 #if defined(DALI_PROFILE_MOBILE)
394   ECore::WindowRenderSurface* wlSurface( dynamic_cast< ECore::WindowRenderSurface * >( mSurface ) );
395
396   if( wlSurface )
397   {
398     Ecore_Wl_Window* wlWindow = wlSurface->GetWlWindow();
399     switch(type)
400     {
401       case Indicator::INDICATOR_TYPE_1:
402         ecore_wl_indicator_visible_type_set(wlWindow, ECORE_WL_INDICATOR_VISIBLE_TYPE_SHOWN);
403         break;
404
405       case Indicator::INDICATOR_TYPE_2:
406         ecore_wl_indicator_visible_type_set(wlWindow, ECORE_WL_INDICATOR_VISIBLE_TYPE_HIDDEN);
407         break;
408
409       case Indicator::INDICATOR_TYPE_UNKNOWN:
410       default:
411         break;
412     }
413   }
414 #endif //MOBILE
415 }
416
417 void Window::IndicatorClosed( IndicatorInterface* indicator )
418 {
419   DALI_LOG_TRACE_METHOD( gWindowLogFilter );
420
421   if( mShowRotatedIndicatorOnClose )
422   {
423     Dali::Window::WindowOrientation currentOrientation = mIndicatorOrientation;
424     mIndicator->Open(mNextIndicatorOrientation);
425     mIndicatorOrientation = mNextIndicatorOrientation;
426     SetIndicatorActorRotation();
427     DoShowIndicator(currentOrientation);
428   }
429 }
430
431 void Window::IndicatorVisibilityChanged(bool isVisible)
432 {
433   mIndicatorVisibilityChangedSignal.Emit(isVisible);
434 }
435
436 void Window::SetIndicatorActorRotation()
437 {
438   DALI_LOG_TRACE_METHOD( gWindowLogFilter );
439   DALI_ASSERT_DEBUG( mIndicator != NULL );
440
441   Dali::Actor actor = mIndicator->GetActor();
442   switch( mIndicatorOrientation )
443   {
444     case Dali::Window::PORTRAIT:
445       actor.SetParentOrigin( ParentOrigin::TOP_CENTER );
446       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
447       actor.SetOrientation( Degree(0), Vector3::ZAXIS );
448       break;
449     case Dali::Window::PORTRAIT_INVERSE:
450       actor.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
451       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
452       actor.SetOrientation( Degree(180), Vector3::ZAXIS );
453       break;
454     case Dali::Window::LANDSCAPE:
455       actor.SetParentOrigin( ParentOrigin::CENTER_LEFT );
456       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
457       actor.SetOrientation( Degree(270), Vector3::ZAXIS );
458       break;
459     case Dali::Window::LANDSCAPE_INVERSE:
460       actor.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
461       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
462       actor.SetOrientation( Degree(90), Vector3::ZAXIS );
463       break;
464   }
465 }
466
467 void Window::Raise()
468 {
469 }
470
471 void Window::Lower()
472 {
473 }
474
475 void Window::Activate()
476 {
477 }
478
479 Dali::DragAndDropDetector Window::GetDragAndDropDetector() const
480 {
481   return mDragAndDropDetector;
482 }
483
484 Dali::Any Window::GetNativeHandle() const
485 {
486   if(mEventHandler)
487   {
488     return mEventHandler->mEcoreWindow;
489   }
490   else
491   {
492     return Dali::Any();
493   }
494 }
495
496 void Window::OnStart()
497 {
498   DoShowIndicator( mIndicatorOrientation );
499 }
500
501 void Window::OnPause()
502 {
503 }
504
505 void Window::OnResume()
506 {
507   // resume indicator status
508   if( mIndicator != NULL )
509   {
510     // Restore own indicator opacity
511     // Send opacity mode to indicator service when app resumed
512     mIndicator->SetOpacityMode( mIndicatorOpacityMode );
513   }
514 }
515
516 void Window::OnStop()
517 {
518   if( mIndicator )
519   {
520     mIndicator->Close();
521   }
522
523   delete mIndicator;
524   mIndicator = NULL;
525 }
526
527 void Window::OnDestroy()
528 {
529   mAdaptor = NULL;
530 }
531
532 void Window::AddAvailableOrientation(Dali::Window::WindowOrientation orientation)
533 {
534   bool found = false;
535
536   for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
537   {
538     if(mAvailableOrientations[i] == orientation)
539     {
540       found = true;
541       break;
542     }
543   }
544
545   if( ! found )
546   {
547     mAvailableOrientations.push_back(orientation);
548     SetAvailableOrientations( mAvailableOrientations );
549   }
550 }
551
552 void Window::RemoveAvailableOrientation(Dali::Window::WindowOrientation orientation)
553 {
554   for( std::vector<Dali::Window::WindowOrientation>::iterator iter = mAvailableOrientations.begin();
555        iter != mAvailableOrientations.end(); ++iter )
556   {
557     if( *iter == orientation )
558     {
559       mAvailableOrientations.erase( iter );
560       break;
561     }
562   }
563   SetAvailableOrientations( mAvailableOrientations );
564 }
565
566 void Window::SetAvailableOrientations(const std::vector<Dali::Window::WindowOrientation>& orientations)
567 {
568   DALI_ASSERT_ALWAYS( mAvailableOrientations.size() <= 4 && "Incorrect number of available orientations" );
569 }
570
571 const std::vector<Dali::Window::WindowOrientation>& Window::GetAvailableOrientations()
572 {
573   return mAvailableOrientations;
574 }
575
576 void Window::SetPreferredOrientation(Dali::Window::WindowOrientation orientation)
577 {
578   mPreferredOrientation = orientation;
579 }
580
581 Dali::Window::WindowOrientation Window::GetPreferredOrientation()
582 {
583   return mPreferredOrientation;
584 }
585
586 void Window::RotationDone( int orientation, int width, int height )
587 {
588 }
589
590
591 } // Adaptor
592 } // Internal
593 } // Dali