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