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