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