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