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