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