808ae52e550f13bb255250e0d34951641ec2abec
[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( bool show )
157 {
158   DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "%s\n", show?"SHOW":"HIDE" );
159   DALI_ASSERT_DEBUG(mOverlay);
160
161   if(show)
162   {
163     mIndicatorVisible = Dali::Window::VISIBLE;
164   }
165   else
166   {
167     mIndicatorVisible = Dali::Window::INVISIBLE;
168   }
169
170   DoShowIndicator( mIndicatorOrientation );
171 }
172
173 void Window::ShowIndicator( Dali::Window::IndicatorVisibleMode visibleMode )
174 {
175   DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "visible : %d\n", visibleMode );
176   DALI_ASSERT_DEBUG(mOverlay);
177
178   DoShowIndicator( mIndicatorOrientation );
179 }
180
181 void Window::RotateIndicator(Dali::Window::WindowOrientation orientation)
182 {
183   DALI_LOG_TRACE_METHOD_FMT( gWindowLogFilter, "Orientation: %d\n", orientation );
184
185   DoRotateIndicator( orientation );
186 }
187
188 void Window::SetIndicatorBgOpacity( Dali::Window::IndicatorBgOpacity opacityMode )
189 {
190   mIndicatorOpacityMode = opacityMode;
191
192   if( mIndicator != NULL )
193   {
194     mIndicator->SetOpacityMode( opacityMode );
195   }
196 }
197
198 void Window::SetClass(std::string name, std::string klass)
199 {
200 }
201
202 Window::Window()
203 : mSurface(NULL),
204   mIndicatorStyle(Dali::Window::CHANGEABLE_COLOR),
205   mIndicatorVisible(Dali::Window::VISIBLE),
206   mIndicatorIsShown(false),
207   mShowRotatedIndicatorOnClose(false),
208   mStarted(false),
209   mIsTransparent(false),
210   mWMRotationAppSet(false),
211   mIndicator(NULL),
212   mIndicatorOrientation(Dali::Window::PORTRAIT),
213   mNextIndicatorOrientation(Dali::Window::PORTRAIT),
214   mIndicatorOpacityMode(Dali::Window::OPAQUE),
215   mOverlay(NULL),
216   mAdaptor(NULL)
217 {
218 }
219
220 Window::~Window()
221 {
222   delete mEventHandler;
223
224   if ( mAdaptor )
225   {
226     mAdaptor->RemoveObserver( *this );
227     mAdaptor->SetDragAndDropDetector( NULL );
228     mAdaptor = NULL;
229   }
230
231   delete mSurface;
232 }
233
234 void Window::Initialize(const PositionSize& windowPosition, const std::string& name)
235 {
236   // create an Wayland window by default
237   Any surface;
238   Any display;
239   mSurface = new ECore::WindowRenderSurface( windowPosition, surface, display, name, mIsTransparent );
240   mOrientation = Orientation::New(this);
241
242   // create event handler for Wayland window
243   mEventHandler = new EventHandler( this );
244 }
245
246 void Window::DoShowIndicator( Dali::Window::WindowOrientation lastOrientation )
247 {
248   if( mIndicator == NULL )
249   {
250     if( mIndicatorVisible != Dali::Window::INVISIBLE )
251     {
252       mIndicator = new Indicator( mAdaptor, mIndicatorOrientation, mIndicatorStyle, this );
253       mIndicator->SetOpacityMode( mIndicatorOpacityMode );
254       Dali::Actor actor = mIndicator->GetActor();
255       SetIndicatorActorRotation();
256       mOverlay->Add(actor);
257     }
258     // else don't create a hidden indicator
259   }
260   else // Already have indicator
261   {
262     if( mIndicatorVisible == Dali::Window::VISIBLE )
263     {
264       // If we are resuming, and rotation has changed,
265       if( mIndicatorIsShown == false && mIndicatorOrientation != mNextIndicatorOrientation )
266       {
267         // then close current indicator and open new one
268         mShowRotatedIndicatorOnClose = true;
269         mIndicator->Close(); // May synchronously call IndicatorClosed() callback & 1 level of recursion
270         // Don't show actor - will contain indicator for old orientation.
271       }
272     }
273   }
274
275   // set indicator visible mode
276   if( mIndicator != NULL )
277   {
278     mIndicator->SetVisible( mIndicatorVisible );
279   }
280
281   bool show = (mIndicatorVisible != Dali::Window::INVISIBLE );
282   SetIndicatorProperties( show, lastOrientation );
283   mIndicatorIsShown = show;
284 }
285
286 void Window::DoRotateIndicator( Dali::Window::WindowOrientation orientation )
287 {
288   if( mIndicatorIsShown )
289   {
290     mShowRotatedIndicatorOnClose = true;
291     mNextIndicatorOrientation = orientation;
292     mIndicator->Close(); // May synchronously call IndicatorClosed() callback
293   }
294   else
295   {
296     // Save orientation for when the indicator is next shown
297     mShowRotatedIndicatorOnClose = false;
298     mNextIndicatorOrientation = orientation;
299   }
300 }
301
302 void Window::SetIndicatorProperties( bool isShow, Dali::Window::WindowOrientation lastOrientation )
303 {
304 }
305
306 void Window::IndicatorTypeChanged(Indicator::Type type)
307 {
308 }
309
310 void Window::IndicatorClosed( Indicator* indicator )
311 {
312   DALI_LOG_TRACE_METHOD( gWindowLogFilter );
313
314   if( mShowRotatedIndicatorOnClose )
315   {
316     Dali::Window::WindowOrientation currentOrientation = mIndicatorOrientation;
317     mIndicator->Open(mNextIndicatorOrientation);
318     mIndicatorOrientation = mNextIndicatorOrientation;
319     SetIndicatorActorRotation();
320     DoShowIndicator(currentOrientation);
321   }
322 }
323
324 void Window::SetIndicatorActorRotation()
325 {
326   DALI_LOG_TRACE_METHOD( gWindowLogFilter );
327   DALI_ASSERT_DEBUG( mIndicator != NULL );
328
329   Dali::Actor actor = mIndicator->GetActor();
330   switch( mIndicatorOrientation )
331   {
332     case Dali::Window::PORTRAIT:
333       actor.SetParentOrigin( ParentOrigin::TOP_CENTER );
334       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
335       actor.SetRotation( Degree(0), Vector3::ZAXIS );
336       break;
337     case Dali::Window::PORTRAIT_INVERSE:
338       actor.SetParentOrigin( ParentOrigin::BOTTOM_CENTER );
339       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
340       actor.SetRotation( Degree(180), Vector3::ZAXIS );
341       break;
342     case Dali::Window::LANDSCAPE:
343       actor.SetParentOrigin( ParentOrigin::CENTER_LEFT );
344       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
345       actor.SetRotation( Degree(270), Vector3::ZAXIS );
346       break;
347     case Dali::Window::LANDSCAPE_INVERSE:
348       actor.SetParentOrigin( ParentOrigin::CENTER_RIGHT );
349       actor.SetAnchorPoint(  AnchorPoint::TOP_CENTER );
350       actor.SetRotation( Degree(90), Vector3::ZAXIS );
351       break;
352   }
353 }
354
355 void Window::Raise()
356 {
357 }
358
359 void Window::Lower()
360 {
361 }
362
363 void Window::Activate()
364 {
365 }
366
367 Dali::DragAndDropDetector Window::GetDragAndDropDetector() const
368 {
369   return mDragAndDropDetector;
370 }
371
372 void Window::OnStart()
373 {
374   DoShowIndicator( mIndicatorOrientation );
375 }
376
377 void Window::OnPause()
378 {
379 }
380
381 void Window::OnResume()
382 {
383   // resume indicator status
384   if( mIndicator != NULL )
385   {
386     // Restore own indicator opacity
387     // Send opacity mode to indicator service when app resumed
388     mIndicator->SetOpacityMode( mIndicatorOpacityMode );
389   }
390 }
391
392 void Window::OnStop()
393 {
394   if( mIndicator )
395   {
396     mIndicator->Close();
397   }
398
399   delete mIndicator;
400   mIndicator = NULL;
401 }
402
403 void Window::OnDestroy()
404 {
405   mAdaptor = NULL;
406 }
407
408 OrientationPtr Window::GetOrientation()
409 {
410   return mOrientation;
411 }
412
413 void Window::AddAvailableOrientation(Dali::Window::WindowOrientation orientation)
414 {
415   bool found = false;
416
417   for( std::size_t i=0; i<mAvailableOrientations.size(); i++ )
418   {
419     if(mAvailableOrientations[i] == orientation)
420     {
421       found = true;
422       break;
423     }
424   }
425
426   if( ! found )
427   {
428     mAvailableOrientations.push_back(orientation);
429     SetAvailableOrientations( mAvailableOrientations );
430   }
431 }
432
433 void Window::RemoveAvailableOrientation(Dali::Window::WindowOrientation orientation)
434 {
435   for( std::vector<Dali::Window::WindowOrientation>::iterator iter = mAvailableOrientations.begin();
436        iter != mAvailableOrientations.end(); ++iter )
437   {
438     if( *iter == orientation )
439     {
440       mAvailableOrientations.erase( iter );
441       break;
442     }
443   }
444   SetAvailableOrientations( mAvailableOrientations );
445 }
446
447 void Window::SetAvailableOrientations(const std::vector<Dali::Window::WindowOrientation>& orientations)
448 {
449   DALI_ASSERT_ALWAYS( mAvailableOrientations.size() <= 4 && "Incorrect number of available orientations" );
450 }
451
452 const std::vector<Dali::Window::WindowOrientation>& Window::GetAvailableOrientations()
453 {
454   return mAvailableOrientations;
455 }
456
457 void Window::SetPreferredOrientation(Dali::Window::WindowOrientation orientation)
458 {
459   mPreferredOrientation = orientation;
460 }
461
462 Dali::Window::WindowOrientation Window::GetPreferredOrientation()
463 {
464   return mPreferredOrientation;
465 }
466
467 void Window::RotationDone( int orientation, int width, int height )
468 {
469 }
470
471
472 } // Adaptor
473 } // Internal
474 } // Dali