Merge "Remove non-public APIs of Animation" into tizen
[platform/core/uifw/dali-adaptor.git] / adaptors / wayland / window-impl-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 <indicator-impl.h>
35 #include <window-visibility-observer.h>
36 #include <orientation-impl.h>
37
38 namespace
39 {
40 const float INDICATOR_ANIMATION_DURATION( 0.18f ); // 180 milli seconds
41 const float INDICATOR_SHOW_Y_POSITION( 0.0f );
42 const float INDICATOR_HIDE_Y_POSITION( -52.0f );
43 }
44
45 namespace Dali
46 {
47 namespace Internal
48 {
49 namespace Adaptor
50 {
51 #if defined(DEBUG_ENABLED)
52 Debug::Filter* gWindowLogFilter = Debug::Filter::New(Debug::Concise, false, "LOG_WINDOW");
53 #endif
54
55 /**
56  * TODO: Abstract Window class out and move this into a window implementation for Ecore
57  */
58 struct Window::EventHandler
59 {
60   /**
61    * Constructor
62    * @param[in]  window  A pointer to the window class.
63    */
64   EventHandler( Window* window )
65   : mWindow( window ),
66     mEcoreWindow( 0 )
67   {
68   }
69
70   /**
71    * Destructor
72    */
73   ~EventHandler()
74   {
75     if ( mWindowPropertyHandler )
76     {
77       ecore_event_handler_del( mWindowPropertyHandler );
78     }
79     if ( mClientMessagehandler )
80     {
81       ecore_event_handler_del( mClientMessagehandler );
82     }
83   }
84
85   // Static methods
86
87   /// Called when the window properties are changed.
88   static Eina_Bool EcoreEventWindowPropertyChanged( void* data, int type, void* event )
89   {
90     return EINA_FALSE;
91   }
92
93   /// Called when the window properties are changed.
94   static Eina_Bool EcoreEventClientMessage( void* data, int type, void* event )
95   {
96     return EINA_FALSE;
97   }
98
99   // Data
100   Window* mWindow;
101   Ecore_Event_Handler* mWindowPropertyHandler;
102   Ecore_Event_Handler* mClientMessagehandler;
103   Ecore_Wl_Window* mEcoreWindow;
104 };
105
106
107 Window* Window::New(const PositionSize& posSize, const std::string& name, bool isTransparent)
108 {
109   Window* window = new Window();
110   window->mIsTransparent = isTransparent;
111   window->Initialize(posSize, name);
112   return window;
113 }
114
115 void Window::SetAdaptor(Dali::Adaptor& adaptor)
116 {
117   DALI_ASSERT_ALWAYS( !mStarted && "Adaptor already started" );
118   mStarted = true;
119
120   // Only create one overlay per window
121   Internal::Adaptor::Adaptor& adaptorImpl = Internal::Adaptor::Adaptor::GetImplementation(adaptor);
122   Integration::Core& core = adaptorImpl.GetCore();
123   mOverlay = &core.GetSystemOverlay();
124
125   Dali::RenderTaskList taskList = mOverlay->GetOverlayRenderTasks();
126   taskList.CreateTask();
127
128   mAdaptor = &adaptorImpl;
129   mAdaptor->AddObserver( *this );
130
131   // Can only create the detector when we know the Core has been instantiated.
132   mDragAndDropDetector = DragAndDropDetector::New();
133   mAdaptor->SetDragAndDropDetector( &GetImplementation( mDragAndDropDetector ) );
134
135   if( mOrientation )
136   {
137     mOrientation->SetAdaptor(adaptor);
138   }
139
140   if( mIndicator != NULL )
141   {
142     mIndicator->SetAdaptor(mAdaptor);
143   }
144 }
145
146 RenderSurface* Window::GetSurface()
147 {
148   return mSurface;
149 }
150
151 void Window::SetIndicatorStyle( Dali::Window::IndicatorStyle style )
152 {
153   mIndicatorStyle = style;
154 }
155
156 void Window::ShowIndicator( Dali::Window::IndicatorVisibleMode visibleMode )
157 {
158   DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "visible : %d\n", visibleMode );
159   DALI_ASSERT_DEBUG(mOverlay);
160
161   DoShowIndicator( mIndicatorOrientation );
162 }
163
164 void Window::RotateIndicator(Dali::Window::WindowOrientation orientation)
165 {
166   DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "Orientation: %d\n", orientation );
167
168   DoRotateIndicator( orientation );
169 }
170
171 void Window::SetIndicatorBgOpacity( Dali::Window::IndicatorBgOpacity opacityMode )
172 {
173   mIndicatorOpacityMode = opacityMode;
174
175   if( mIndicator != NULL )
176   {
177     mIndicator->SetOpacityMode( opacityMode );
178   }
179 }
180
181 void Window::SetClass(std::string name, std::string klass)
182 {
183 }
184
185 Window::Window()
186 : mSurface(NULL),
187   mIndicatorStyle(Dali::Window::CHANGEABLE_COLOR),
188   mIndicatorVisible(Dali::Window::VISIBLE),
189   mIndicatorIsShown(false),
190   mShowRotatedIndicatorOnClose(false),
191   mStarted(false),
192   mIsTransparent(false),
193   mWMRotationAppSet(false),
194   mIndicator(NULL),
195   mIndicatorOrientation(Dali::Window::PORTRAIT),
196   mNextIndicatorOrientation(Dali::Window::PORTRAIT),
197   mIndicatorOpacityMode(Dali::Window::OPAQUE),
198   mOverlay(NULL),
199   mAdaptor(NULL)
200 {
201 }
202
203 Window::~Window()
204 {
205   delete mEventHandler;
206
207   if( mIndicator )
208   {
209     mIndicator->Close();
210     delete mIndicator;
211   }
212
213   if ( mAdaptor )
214   {
215     mAdaptor->RemoveObserver( *this );
216     mAdaptor->SetDragAndDropDetector( NULL );
217     mAdaptor = NULL;
218   }
219
220   delete mSurface;
221 }
222
223 void Window::Initialize(const PositionSize& windowPosition, const std::string& name)
224 {
225   // create an Wayland window by default
226   Any surface;
227   ECore::WindowRenderSurface* windowSurface = new ECore::WindowRenderSurface( windowPosition, surface, name, mIsTransparent );
228   windowSurface->Map();
229
230   mSurface = windowSurface;
231
232   mOrientation = Orientation::New(this);
233
234   // create event handler for Wayland window
235   mEventHandler = new EventHandler( this );
236 }
237
238 void Window::DoShowIndicator( Dali::Window::WindowOrientation lastOrientation )
239 {
240   if( mIndicator == NULL )
241   {
242     if( mIndicatorVisible != Dali::Window::INVISIBLE )
243     {
244       mIndicator = new Indicator( mAdaptor, mIndicatorOrientation, mIndicatorStyle, this );
245       mIndicator->SetOpacityMode( mIndicatorOpacityMode );
246       Dali::Actor actor = mIndicator->GetActor();
247       SetIndicatorActorRotation();
248       mOverlay->Add(actor);
249     }
250     // else don't create a hidden indicator
251   }
252   else // Already have indicator
253   {
254     if( mIndicatorVisible == Dali::Window::VISIBLE )
255     {
256       // If we are resuming, and rotation has changed,
257       if( mIndicatorIsShown == false && mIndicatorOrientation != mNextIndicatorOrientation )
258       {
259         // then close current indicator and open new one
260         mShowRotatedIndicatorOnClose = true;
261         mIndicator->Close(); // May synchronously call IndicatorClosed() callback & 1 level of recursion
262         // Don't show actor - will contain indicator for old orientation.
263       }
264     }
265   }
266
267   // set indicator visible mode
268   if( mIndicator != NULL )
269   {
270     mIndicator->SetVisible( mIndicatorVisible );
271   }
272
273   bool show = (mIndicatorVisible != Dali::Window::INVISIBLE );
274   SetIndicatorProperties( show, lastOrientation );
275   mIndicatorIsShown = show;
276 }
277
278 void Window::DoRotateIndicator( Dali::Window::WindowOrientation orientation )
279 {
280   if( mIndicatorIsShown )
281   {
282     mShowRotatedIndicatorOnClose = true;
283     mNextIndicatorOrientation = orientation;
284     mIndicator->Close(); // May synchronously call IndicatorClosed() callback
285   }
286   else
287   {
288     // Save orientation for when the indicator is next shown
289     mShowRotatedIndicatorOnClose = false;
290     mNextIndicatorOrientation = orientation;
291   }
292 }
293
294 void Window::SetIndicatorProperties( bool isShow, Dali::Window::WindowOrientation lastOrientation )
295 {
296 }
297
298 void Window::IndicatorTypeChanged(Indicator::Type type)
299 {
300 }
301
302 void Window::IndicatorClosed( Indicator* indicator )
303 {
304   DALI_LOG_TRACE_METHOD( gWindowLogFilter );
305
306   if( mShowRotatedIndicatorOnClose )
307   {
308     Dali::Window::WindowOrientation currentOrientation = mIndicatorOrientation;
309     mIndicator->Open(mNextIndicatorOrientation);
310     mIndicatorOrientation = mNextIndicatorOrientation;
311     SetIndicatorActorRotation();
312     DoShowIndicator(currentOrientation);
313   }
314 }
315
316 void Window::IndicatorVisibilityChanged(bool isVisible)
317 {
318   mIndicatorVisibilityChangedSignal.Emit(isVisible);
319 }
320
321 void Window::SetIndicatorActorRotation()
322 {
323   DALI_LOG_TRACE_METHOD( gWindowLogFilter );
324   DALI_ASSERT_DEBUG( mIndicator != NULL );
325
326   Dali::Actor actor = mIndicator->GetActor();
327   switch( mIndicatorOrientation )
328   {
329     case Dali::Window::PORTRAIT:
330       actor.SetParentOrigin( ParentOrigin::TOP_CENTER );
331       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
332       actor.SetOrientation( Degree(0), Vector3::ZAXIS );
333       break;
334     case Dali::Window::PORTRAIT_INVERSE:
335       actor.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
336       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
337       actor.SetOrientation( Degree(180), Vector3::ZAXIS );
338       break;
339     case Dali::Window::LANDSCAPE:
340       actor.SetParentOrigin( ParentOrigin::CENTER_LEFT );
341       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
342       actor.SetOrientation( Degree(270), Vector3::ZAXIS );
343       break;
344     case Dali::Window::LANDSCAPE_INVERSE:
345       actor.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
346       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
347       actor.SetOrientation( Degree(90), Vector3::ZAXIS );
348       break;
349   }
350 }
351
352 void Window::Raise()
353 {
354 }
355
356 void Window::Lower()
357 {
358 }
359
360 void Window::Activate()
361 {
362 }
363
364 Dali::DragAndDropDetector Window::GetDragAndDropDetector() const
365 {
366   return mDragAndDropDetector;
367 }
368
369 Dali::Any Window::GetNativeHandle() const
370 {
371   if(mEventHandler)
372   {
373     return mEventHandler->mEcoreWindow;
374   }
375   else
376   {
377     return Dali::Any();
378   }
379 }
380
381 void Window::OnStart()
382 {
383   DoShowIndicator( mIndicatorOrientation );
384 }
385
386 void Window::OnPause()
387 {
388 }
389
390 void Window::OnResume()
391 {
392   // resume indicator status
393   if( mIndicator != NULL )
394   {
395     // Restore own indicator opacity
396     // Send opacity mode to indicator service when app resumed
397     mIndicator->SetOpacityMode( mIndicatorOpacityMode );
398   }
399 }
400
401 void Window::OnStop()
402 {
403   if( mIndicator )
404   {
405     mIndicator->Close();
406   }
407
408   delete mIndicator;
409   mIndicator = NULL;
410 }
411
412 void Window::OnDestroy()
413 {
414   mAdaptor = NULL;
415 }
416
417 OrientationPtr Window::GetOrientation()
418 {
419   return mOrientation;
420 }
421
422 void Window::AddAvailableOrientation(Dali::Window::WindowOrientation orientation)
423 {
424   bool found = false;
425
426   for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
427   {
428     if(mAvailableOrientations[i] == orientation)
429     {
430       found = true;
431       break;
432     }
433   }
434
435   if( ! found )
436   {
437     mAvailableOrientations.push_back(orientation);
438     SetAvailableOrientations( mAvailableOrientations );
439   }
440 }
441
442 void Window::RemoveAvailableOrientation(Dali::Window::WindowOrientation orientation)
443 {
444   for( std::vector<Dali::Window::WindowOrientation>::iterator iter = mAvailableOrientations.begin();
445        iter != mAvailableOrientations.end(); ++iter )
446   {
447     if( *iter == orientation )
448     {
449       mAvailableOrientations.erase( iter );
450       break;
451     }
452   }
453   SetAvailableOrientations( mAvailableOrientations );
454 }
455
456 void Window::SetAvailableOrientations(const std::vector<Dali::Window::WindowOrientation>& orientations)
457 {
458   DALI_ASSERT_ALWAYS( mAvailableOrientations.size() <= 4 && "Incorrect number of available orientations" );
459 }
460
461 const std::vector<Dali::Window::WindowOrientation>& Window::GetAvailableOrientations()
462 {
463   return mAvailableOrientations;
464 }
465
466 void Window::SetPreferredOrientation(Dali::Window::WindowOrientation orientation)
467 {
468   mPreferredOrientation = orientation;
469 }
470
471 Dali::Window::WindowOrientation Window::GetPreferredOrientation()
472 {
473   return mPreferredOrientation;
474 }
475
476 void Window::RotationDone( int orientation, int width, int height )
477 {
478 }
479
480
481 } // Adaptor
482 } // Internal
483 } // Dali