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