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