Fix unParent and rotation bug
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / common / window-impl.cpp
1 /*
2  * Copyright (c) 2019 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 <dali/internal/window-system/common/window-impl.h>
20
21 // EXTERNAL HEADERS
22 #include <dali/integration-api/core.h>
23 #include <dali/public-api/actors/actor.h>
24 #include <dali/public-api/actors/layer.h>
25 #include <dali/public-api/actors/camera-actor.h>
26 #include <dali/public-api/render-tasks/render-task.h>
27 #include <dali/public-api/render-tasks/render-task-list.h>
28 #include <dali/public-api/rendering/frame-buffer.h>
29 #include <dali/devel-api/adaptor-framework/orientation.h>
30 #include <dali/integration-api/events/touch-event-integ.h>
31
32 #ifdef DALI_ADAPTOR_COMPILATION
33 #include <dali/integration-api/render-surface-interface.h>
34 #else
35 #include <dali/integration-api/adaptors/render-surface-interface.h>
36 #endif
37
38 // INTERNAL HEADERS
39 #include <dali/internal/window-system/common/event-handler.h>
40 #include <dali/internal/window-system/common/orientation-impl.h>
41 #include <dali/internal/window-system/common/render-surface-factory.h>
42 #include <dali/internal/window-system/common/window-factory.h>
43 #include <dali/internal/window-system/common/window-base.h>
44 #include <dali/internal/window-system/common/window-render-surface.h>
45 #include <dali/internal/window-system/common/window-visibility-observer.h>
46
47 namespace Dali
48 {
49 namespace Internal
50 {
51 namespace Adaptor
52 {
53
54 namespace
55 {
56
57 #if defined(DEBUG_ENABLED)
58 Debug::Filter* gWindowLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_WINDOW" );
59 #endif
60
61 } // unnamed namespace
62
63 Window* Window::New( const PositionSize& positionSize, const std::string& name, const std::string& className, bool isTransparent )
64 {
65   Window* window = new Window();
66   window->mIsTransparent = isTransparent;
67   window->Initialize( positionSize, name, className );
68   return window;
69 }
70
71 Window::Window()
72 : mWindowSurface( nullptr ),
73   mWindowBase(),
74   mIsTransparent( false ),
75   mIsFocusAcceptable( true ),
76   mIconified( false ),
77   mOpaqueState( false ),
78   mResizeEnabled( false ),
79   mType( Dali::Window::NORMAL ),
80   mParentWindow( NULL ),
81   mPreferredOrientation( Dali::Window::PORTRAIT ),
82   mRotationAngle( 0 ),
83   mWindowWidth( 0 ),
84   mWindowHeight( 0 ),
85   mFocusChangedSignal(),
86   mResizedSignal(),
87   mDeleteRequestSignal(),
88   mFocusChangeSignal(),
89   mResizeSignal()
90 {
91 }
92
93 Window::~Window()
94 {
95   if ( mEventHandler )
96   {
97     mEventHandler->RemoveObserver( *this );
98   }
99 }
100
101 void Window::Initialize(const PositionSize& positionSize, const std::string& name, const std::string& className)
102 {
103   // Create a window render surface
104   Any surface;
105   auto renderSurfaceFactory = Dali::Internal::Adaptor::GetRenderSurfaceFactory();
106   mSurface = renderSurfaceFactory->CreateWindowRenderSurface( positionSize, surface, mIsTransparent );
107   mWindowSurface = static_cast<WindowRenderSurface*>( mSurface.get() );
108
109   // Get a window base
110   mWindowBase = mWindowSurface->GetWindowBase();
111
112   // Connect signals
113   mWindowBase->IconifyChangedSignal().Connect( this, &Window::OnIconifyChanged );
114   mWindowBase->FocusChangedSignal().Connect( this, &Window::OnFocusChanged );
115   mWindowBase->DeleteRequestSignal().Connect( this, &Window::OnDeleteRequest );
116
117   mWindowSurface->OutputTransformedSignal().Connect( this, &Window::OnOutputTransformed );
118
119   if( !positionSize.IsEmpty() )
120   {
121     AddAuxiliaryHint( "wm.policy.win.user.geometry", "1" );
122     mResizeEnabled = true;
123   }
124
125   SetClass( name, className );
126
127   mWindowSurface->Map();
128
129   mOrientation = Orientation::New( this );
130 }
131
132 void Window::OnAdaptorSet(Dali::Adaptor& adaptor)
133 {
134   mEventHandler = EventHandlerPtr(new EventHandler( mWindowSurface, *mAdaptor ) );
135   mEventHandler->AddObserver( *this );
136 }
137
138 void Window::OnSurfaceSet( Dali::RenderSurfaceInterface* surface )
139 {
140   mWindowSurface = static_cast<WindowRenderSurface*>( surface );
141 }
142
143 void Window::ShowIndicator( Dali::Window::IndicatorVisibleMode visibleMode )
144 {
145 }
146
147 void Window::SetIndicatorBgOpacity( Dali::Window::IndicatorBgOpacity opacityMode )
148 {
149 }
150
151 void Window::RotateIndicator( Dali::Window::WindowOrientation orientation )
152 {
153 }
154
155 void Window::SetClass( std::string name, std::string className )
156 {
157   mName = name;
158   mClassName = className;
159   mWindowBase->SetClass( name, className );
160 }
161
162 std::string Window::GetClassName() const
163 {
164   return mClassName;
165 }
166
167 void Window::Raise()
168 {
169   mWindowBase->Raise();
170   DALI_LOG_RELEASE_INFO( "Window (%p) Raise() \n", this );
171 }
172
173 void Window::Lower()
174 {
175   mWindowBase->Lower();
176   DALI_LOG_RELEASE_INFO( "Window (%p) Lower() \n", this );
177 }
178
179 void Window::Activate()
180 {
181   mWindowBase->Activate();
182   DALI_LOG_RELEASE_INFO( "Window (%p) Activate() \n", this );
183 }
184
185 uint32_t Window::GetLayerCount() const
186 {
187   return mScene.GetLayerCount();
188 }
189
190 Dali::Layer Window::GetLayer( uint32_t depth ) const
191 {
192   return mScene.GetLayer( depth );
193 }
194
195 Dali::RenderTaskList Window::GetRenderTaskList() const
196 {
197   return mScene.GetRenderTaskList();
198 }
199
200 void Window::AddAvailableOrientation( Dali::Window::WindowOrientation orientation )
201 {
202   bool found = false;
203
204   if( orientation <= Dali::Window::LANDSCAPE_INVERSE )
205   {
206     for( std::size_t i = 0; i < mAvailableOrientations.size(); i++ )
207     {
208       if( mAvailableOrientations[i] == orientation )
209       {
210         found = true;
211         break;
212       }
213     }
214
215     if( !found )
216     {
217       mAvailableOrientations.push_back( orientation );
218       SetAvailableOrientations( mAvailableOrientations );
219     }
220   }
221 }
222
223 void Window::RemoveAvailableOrientation( Dali::Window::WindowOrientation orientation )
224 {
225   for( std::vector<Dali::Window::WindowOrientation>::iterator iter = mAvailableOrientations.begin();
226        iter != mAvailableOrientations.end(); ++iter )
227   {
228     if( *iter == orientation )
229     {
230       mAvailableOrientations.erase( iter );
231       break;
232     }
233   }
234   SetAvailableOrientations( mAvailableOrientations );
235 }
236
237 void Window::SetAvailableOrientations( const std::vector< Dali::Window::WindowOrientation >& orientations )
238 {
239   if( orientations.size() > 4 )
240   {
241     DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::SetAvailableOrientations: Invalid vector size! [%d]\n", orientations.size() );
242     return;
243   }
244
245   mAvailableOrientations = orientations;
246
247   mWindowBase->SetAvailableOrientations( mAvailableOrientations );
248 }
249
250 const std::vector< Dali::Window::WindowOrientation >& Window::GetAvailableOrientations()
251 {
252   return mAvailableOrientations;
253 }
254
255 void Window::SetPreferredOrientation( Dali::Window::WindowOrientation orientation )
256 {
257   mPreferredOrientation = orientation;
258
259   mWindowBase->SetPreferredOrientation( mPreferredOrientation );
260 }
261
262 Dali::Window::WindowOrientation Window::GetPreferredOrientation()
263 {
264   return mPreferredOrientation;
265 }
266
267 Dali::Any Window::GetNativeHandle() const
268 {
269   return mWindowSurface->GetNativeWindow();
270 }
271
272 void Window::SetAcceptFocus( bool accept )
273 {
274   mIsFocusAcceptable = accept;
275
276   mWindowBase->SetAcceptFocus( accept );
277 }
278
279 bool Window::IsFocusAcceptable() const
280 {
281   return mIsFocusAcceptable;
282 }
283
284 void Window::Show()
285 {
286   mVisible = true;
287
288   mWindowBase->Show();
289
290   if( !mIconified )
291   {
292     WindowVisibilityObserver* observer( mAdaptor );
293     observer->OnWindowShown();
294   }
295
296   DALI_LOG_RELEASE_INFO( "Window (%p) Show(): iconified = %d\n", this, mIconified );
297 }
298
299 void Window::Hide()
300 {
301   mVisible = false;
302
303   mWindowBase->Hide();
304
305   if( !mIconified )
306   {
307     WindowVisibilityObserver* observer( mAdaptor );
308     observer->OnWindowHidden();
309   }
310
311   DALI_LOG_RELEASE_INFO( "Window (%p) Hide(): iconified = %d\n", this, mIconified );
312 }
313
314 bool Window::IsVisible() const
315 {
316   return mVisible && !mIconified;
317 }
318
319 unsigned int Window::GetSupportedAuxiliaryHintCount() const
320 {
321   return mWindowBase->GetSupportedAuxiliaryHintCount();
322 }
323
324 std::string Window::GetSupportedAuxiliaryHint( unsigned int index ) const
325 {
326   return mWindowBase->GetSupportedAuxiliaryHint( index );
327 }
328
329 unsigned int Window::AddAuxiliaryHint( const std::string& hint, const std::string& value )
330 {
331   return mWindowBase->AddAuxiliaryHint( hint, value );
332 }
333
334 bool Window::RemoveAuxiliaryHint( unsigned int id )
335 {
336   return mWindowBase->RemoveAuxiliaryHint( id );
337 }
338
339 bool Window::SetAuxiliaryHintValue( unsigned int id, const std::string& value )
340 {
341   return mWindowBase->SetAuxiliaryHintValue( id, value );
342 }
343
344 std::string Window::GetAuxiliaryHintValue( unsigned int id ) const
345 {
346   return mWindowBase->GetAuxiliaryHintValue( id );
347 }
348
349 unsigned int Window::GetAuxiliaryHintId( const std::string& hint ) const
350 {
351   return mWindowBase->GetAuxiliaryHintId( hint );
352 }
353
354 void Window::SetInputRegion( const Rect< int >& inputRegion )
355 {
356   mWindowBase->SetInputRegion( inputRegion );
357
358   DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::SetInputRegion: x = %d, y = %d, w = %d, h = %d\n", inputRegion.x, inputRegion.y, inputRegion.width, inputRegion.height );
359 }
360
361 void Window::SetType( Dali::Window::Type type )
362 {
363   if( type != mType )
364   {
365     mWindowBase->SetType( type );
366
367     mType = type;
368   }
369 }
370
371 Dali::Window::Type Window::GetType() const
372 {
373   return mType;
374 }
375
376 bool Window::SetNotificationLevel( Dali::Window::NotificationLevel::Type level )
377 {
378   if( mType != Dali::Window::NOTIFICATION )
379   {
380     DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::SetNotificationLevel: Not supported window type [%d]\n", mType );
381     return false;
382   }
383
384   return mWindowBase->SetNotificationLevel( level );
385 }
386
387 Dali::Window::NotificationLevel::Type Window::GetNotificationLevel() const
388 {
389   if( mType != Dali::Window::NOTIFICATION )
390   {
391     DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::GetNotificationLevel: Not supported window type [%d]\n", mType );
392     return Dali::Window::NotificationLevel::NONE;
393   }
394
395   return mWindowBase->GetNotificationLevel();
396 }
397
398 void Window::SetOpaqueState( bool opaque )
399 {
400   mOpaqueState = opaque;
401
402   mWindowBase->SetOpaqueState( opaque );
403
404   DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::SetOpaqueState: opaque = %d\n", opaque );
405 }
406
407 bool Window::IsOpaqueState() const
408 {
409   return mOpaqueState;
410 }
411
412 bool Window::SetScreenOffMode(Dali::Window::ScreenOffMode::Type screenOffMode)
413 {
414   return mWindowBase->SetScreenOffMode( screenOffMode );
415 }
416
417 Dali::Window::ScreenOffMode::Type Window::GetScreenOffMode() const
418 {
419   return mWindowBase->GetScreenOffMode();
420 }
421
422 bool Window::SetBrightness( int brightness )
423 {
424   if( brightness < 0 || brightness > 100 )
425   {
426     DALI_LOG_INFO( gWindowLogFilter, Debug::Verbose, "Window::SetBrightness: Invalid brightness value [%d]\n", brightness );
427     return false;
428   }
429
430   return mWindowBase->SetBrightness( brightness );
431 }
432
433 int Window::GetBrightness() const
434 {
435   return mWindowBase->GetBrightness();
436 }
437
438 void Window::SetSize( Dali::Window::WindowSize size )
439 {
440   if( !mResizeEnabled )
441   {
442     AddAuxiliaryHint( "wm.policy.win.user.geometry", "1" );
443     mResizeEnabled = true;
444   }
445
446   PositionSize oldRect = mSurface->GetPositionSize();
447
448   mWindowSurface->MoveResize( PositionSize( oldRect.x, oldRect.y, size.GetWidth(), size.GetHeight() ) );
449
450   PositionSize newRect = mSurface->GetPositionSize();
451
452   // When surface size is updated, inform adaptor of resizing and emit ResizeSignal
453   if( ( oldRect.width != newRect.width ) || ( oldRect.height != newRect.height ) )
454   {
455     Uint16Pair newSize( newRect.width, newRect.height );
456
457     SurfaceResized();
458
459     mAdaptor->SurfaceResizePrepare( mSurface.get(), newSize );
460
461     Dali::Window handle( this );
462     mResizedSignal.Emit( newSize );
463     mResizeSignal.Emit( handle, newSize );
464
465     mAdaptor->SurfaceResizeComplete( mSurface.get(), newSize );
466   }
467 }
468
469 Dali::Window::WindowSize Window::GetSize() const
470 {
471   PositionSize positionSize = mSurface->GetPositionSize();
472
473   return Dali::Window::WindowSize( positionSize.width, positionSize.height );
474 }
475
476 void Window::SetPosition( Dali::Window::WindowPosition position )
477 {
478   if( !mResizeEnabled )
479   {
480     AddAuxiliaryHint( "wm.policy.win.user.geometry", "1" );
481     mResizeEnabled = true;
482   }
483
484   PositionSize oldRect = mSurface->GetPositionSize();
485
486   mWindowSurface->MoveResize( PositionSize( position.GetX(), position.GetY(), oldRect.width, oldRect.height ) );
487 }
488
489 Dali::Window::WindowPosition Window::GetPosition() const
490 {
491   PositionSize positionSize = mSurface->GetPositionSize();
492
493   return Dali::Window::WindowPosition( positionSize.x, positionSize.y );
494 }
495
496 void Window::SetPositionSize( PositionSize positionSize )
497 {
498   if( !mResizeEnabled )
499   {
500     AddAuxiliaryHint( "wm.policy.win.user.geometry", "1" );
501     mResizeEnabled = true;
502   }
503
504   PositionSize oldRect = mSurface->GetPositionSize();
505
506   mWindowSurface->MoveResize( positionSize );
507
508   PositionSize newRect = mSurface->GetPositionSize();
509
510   // When surface size is updated, inform adaptor of resizing and emit ResizeSignal
511   if( ( oldRect.width != newRect.width ) || ( oldRect.height != newRect.height ) )
512   {
513     Uint16Pair newSize( newRect.width, newRect.height );
514
515     SurfaceResized();
516
517     mAdaptor->SurfaceResizePrepare( mSurface.get(), newSize );
518
519     Dali::Window handle( this );
520     mResizedSignal.Emit( newSize );
521     mResizeSignal.Emit( handle, newSize );
522     mAdaptor->SurfaceResizeComplete( mSurface.get(), newSize );
523   }
524 }
525
526 Dali::Layer Window::GetRootLayer() const
527 {
528   return mScene.GetRootLayer();
529 }
530
531 void Window::SetTransparency( bool transparent )
532 {
533   mWindowSurface->SetTransparency( transparent );
534 }
535
536 bool Window::GrabKey( Dali::KEY key, KeyGrab::KeyGrabMode grabMode )
537 {
538   return mWindowBase->GrabKey( key, grabMode );
539 }
540
541 bool Window::UngrabKey( Dali::KEY key )
542 {
543   return mWindowBase->UngrabKey( key );
544 }
545
546 bool Window::GrabKeyList( const Dali::Vector< Dali::KEY >& key, const Dali::Vector< KeyGrab::KeyGrabMode >& grabMode, Dali::Vector< bool >& result )
547 {
548   return mWindowBase->GrabKeyList( key, grabMode, result );
549 }
550
551 bool Window::UngrabKeyList( const Dali::Vector< Dali::KEY >& key, Dali::Vector< bool >& result )
552 {
553   return mWindowBase->UngrabKeyList( key, result );
554 }
555
556 void Window::OnIconifyChanged( bool iconified )
557 {
558   if( iconified )
559   {
560     mIconified = true;
561
562     if( mVisible )
563     {
564       WindowVisibilityObserver* observer( mAdaptor );
565       observer->OnWindowHidden();
566     }
567
568     DALI_LOG_RELEASE_INFO( "Window (%p) Iconified: visible = %d\n", this, mVisible );
569   }
570   else
571   {
572     mIconified = false;
573
574     if( mVisible )
575     {
576       WindowVisibilityObserver* observer( mAdaptor );
577       observer->OnWindowShown();
578     }
579
580     DALI_LOG_RELEASE_INFO( "Window (%p) Deiconified: visible = %d\n", this, mVisible );
581   }
582 }
583
584 void Window::OnFocusChanged( bool focusIn )
585 {
586   Dali::Window handle( this );
587   mFocusChangedSignal.Emit( focusIn );
588   mFocusChangeSignal.Emit( handle, focusIn );
589 }
590
591 void Window::OnOutputTransformed()
592 {
593   PositionSize positionSize = mSurface->GetPositionSize();
594   SurfaceResized();
595   mAdaptor->SurfaceResizePrepare( mSurface.get(), Adaptor::SurfaceSize( positionSize.width, positionSize.height ) );
596   mAdaptor->SurfaceResizeComplete( mSurface.get(), Adaptor::SurfaceSize( positionSize.width, positionSize.height ) );
597 }
598
599 void Window::OnDeleteRequest()
600 {
601   mDeleteRequestSignal.Emit();
602 }
603
604 void Window::OnTouchPoint( Dali::Integration::Point& point, int timeStamp )
605 {
606   FeedTouchPoint( point, timeStamp );
607 }
608
609 void Window::OnWheelEvent( Dali::Integration::WheelEvent& wheelEvent )
610 {
611   FeedWheelEvent( wheelEvent );
612 }
613
614 void Window::OnKeyEvent( Dali::Integration::KeyEvent& keyEvent )
615 {
616   FeedKeyEvent( keyEvent );
617 }
618
619 void Window::OnRotation( const RotationEvent& rotation )
620 {
621   mRotationAngle = rotation.angle;
622   mWindowWidth = rotation.width;
623   mWindowHeight = rotation.height;
624
625   // Notify that the orientation is changed
626   mOrientation->OnOrientationChange( rotation );
627
628   mWindowSurface->RequestRotation( mRotationAngle, mWindowWidth, mWindowHeight );
629
630   SurfaceResized();
631
632   mAdaptor->SurfaceResizePrepare( mSurface.get(), Adaptor::SurfaceSize( mWindowWidth, mWindowHeight ) );
633
634   // Emit signal
635   Dali::Window handle( this );
636   mResizedSignal.Emit( Dali::Window::WindowSize( mWindowWidth, mWindowHeight ) );
637   mResizeSignal.Emit( handle, Dali::Window::WindowSize( mWindowWidth, mWindowHeight ) );
638
639   mAdaptor->SurfaceResizeComplete( mSurface.get(), Adaptor::SurfaceSize( mWindowWidth, mWindowHeight ) );
640 }
641
642 void Window::OnPause()
643 {
644   if( mEventHandler )
645   {
646     mEventHandler->Pause();
647   }
648 }
649
650 void Window::OnResume()
651 {
652   if( mEventHandler )
653   {
654     mEventHandler->Resume();
655   }
656 }
657
658 void Window::RecalculateTouchPosition( Integration::Point& point )
659 {
660   Vector2 position = point.GetScreenPosition();
661   Vector2 convertedPosition;
662
663   switch( mRotationAngle )
664   {
665     case 90:
666     {
667       convertedPosition.x = static_cast<float>( mWindowWidth ) - position.y;
668       convertedPosition.y = position.x;
669       break;
670     }
671     case 180:
672     {
673       convertedPosition.x = static_cast<float>( mWindowWidth ) - position.x;
674       convertedPosition.y = static_cast<float>( mWindowHeight ) - position.y;
675       break;
676     }
677     case 270:
678     {
679       convertedPosition.x = position.y;
680       convertedPosition.y = static_cast<float>( mWindowHeight ) - position.x;
681       break;
682     }
683     default:
684     {
685       convertedPosition = position;
686       break;
687     }
688   }
689
690   point.SetScreenPosition( convertedPosition );
691 }
692
693 Dali::Window Window::Get( Dali::Actor actor )
694 {
695   Internal::Adaptor::Window* windowImpl = nullptr;
696
697   if ( Internal::Adaptor::Adaptor::IsAvailable() )
698   {
699     Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation( Internal::Adaptor::Adaptor::Get() );
700     windowImpl = static_cast<Internal::Adaptor::Window*>( adaptor.GetWindow( actor ) );
701   }
702
703   return Dali::Window( windowImpl );
704 }
705
706 void Window::SetParent( Dali::Window& parent )
707 {
708   if ( DALI_UNLIKELY( parent ) )
709   {
710     mParentWindow = parent;
711     Dali::Window self = Dali::Window( this );
712     // check circular parent window setting
713     if ( Dali::DevelWindow::GetParent( parent ) == self )
714     {
715       Dali::DevelWindow::Unparent( parent );
716     }
717     mWindowBase->SetParent( GetImplementation( mParentWindow ).mWindowBase );
718   }
719 }
720
721 void Window::Unparent()
722 {
723   mWindowBase->SetParent( nullptr );
724   mParentWindow.Reset();
725 }
726
727 Dali::Window Window::GetParent()
728 {
729   return mParentWindow;
730 }
731
732 } // Adaptor
733
734 } // Internal
735
736 } // Dali