Merge "Add to set MIME type to the input panel and add the callback" 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   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     mAdaptor->SurfaceResizePrepare( mSurface.get(), newSize );
511
512     mResizedSignal.Emit( newSize );
513
514     mAdaptor->SurfaceResizeComplete( mSurface.get(), newSize );
515   }
516 }
517
518 Dali::Layer Window::GetRootLayer() const
519 {
520   return mScene.GetRootLayer();
521 }
522
523 void Window::SetTransparency( bool transparent )
524 {
525   mWindowSurface->SetTransparency( transparent );
526 }
527
528 bool Window::GrabKey( Dali::KEY key, KeyGrab::KeyGrabMode grabMode )
529 {
530   return mWindowBase->GrabKey( key, grabMode );
531 }
532
533 bool Window::UngrabKey( Dali::KEY key )
534 {
535   return mWindowBase->UngrabKey( key );
536 }
537
538 bool Window::GrabKeyList( const Dali::Vector< Dali::KEY >& key, const Dali::Vector< KeyGrab::KeyGrabMode >& grabMode, Dali::Vector< bool >& result )
539 {
540   return mWindowBase->GrabKeyList( key, grabMode, result );
541 }
542
543 bool Window::UngrabKeyList( const Dali::Vector< Dali::KEY >& key, Dali::Vector< bool >& result )
544 {
545   return mWindowBase->UngrabKeyList( key, result );
546 }
547
548 void Window::OnIconifyChanged( bool iconified )
549 {
550   if( iconified )
551   {
552     mIconified = true;
553
554     if( mVisible )
555     {
556       WindowVisibilityObserver* observer( mAdaptor );
557       observer->OnWindowHidden();
558     }
559
560     DALI_LOG_RELEASE_INFO( "Window (%p) Iconified: visible = %d\n", this, mVisible );
561   }
562   else
563   {
564     mIconified = false;
565
566     if( mVisible )
567     {
568       WindowVisibilityObserver* observer( mAdaptor );
569       observer->OnWindowShown();
570     }
571
572     DALI_LOG_RELEASE_INFO( "Window (%p) Deiconified: visible = %d\n", this, mVisible );
573   }
574 }
575
576 void Window::OnFocusChanged( bool focusIn )
577 {
578   mFocusChangedSignal.Emit( focusIn );
579 }
580
581 void Window::OnOutputTransformed()
582 {
583   PositionSize positionSize = mSurface->GetPositionSize();
584   mAdaptor->SurfaceResizePrepare( mSurface.get(), Adaptor::SurfaceSize( positionSize.width, positionSize.height ) );
585   mAdaptor->SurfaceResizeComplete( mSurface.get(), Adaptor::SurfaceSize( positionSize.width, positionSize.height ) );
586 }
587
588 void Window::OnDeleteRequest()
589 {
590   mDeleteRequestSignal.Emit();
591 }
592
593 void Window::OnTouchPoint( Dali::Integration::Point& point, int timeStamp )
594 {
595   FeedTouchPoint( point, timeStamp );
596 }
597
598 void Window::OnWheelEvent( Dali::Integration::WheelEvent& wheelEvent )
599 {
600   FeedWheelEvent( wheelEvent );
601 }
602
603 void Window::OnKeyEvent( Dali::Integration::KeyEvent& keyEvent )
604 {
605   FeedKeyEvent( keyEvent );
606 }
607
608 void Window::OnRotation( const RotationEvent& rotation )
609 {
610   mRotationAngle = rotation.angle;
611   mWindowWidth = rotation.width;
612   mWindowHeight = rotation.height;
613
614   // Notify that the orientation is changed
615   mOrientation->OnOrientationChange( rotation );
616
617   mWindowSurface->RequestRotation( mRotationAngle, mWindowWidth, mWindowHeight );
618
619   mAdaptor->SurfaceResizePrepare( mSurface.get(), Adaptor::SurfaceSize( mRotationAngle, mWindowHeight ) );
620
621   // Emit signal
622   mResizedSignal.Emit( Dali::Window::WindowSize( mRotationAngle, mWindowHeight ) );
623
624   mAdaptor->SurfaceResizeComplete( mSurface.get(), Adaptor::SurfaceSize( mRotationAngle, mWindowHeight ) );
625 }
626
627 void Window::OnPause()
628 {
629   if( mEventHandler )
630   {
631     mEventHandler->Pause();
632   }
633 }
634
635 void Window::OnResume()
636 {
637   if( mEventHandler )
638   {
639     mEventHandler->Resume();
640   }
641 }
642
643 void Window::RecalculateTouchPosition( Integration::Point& point )
644 {
645   Vector2 position = point.GetScreenPosition();
646   Vector2 convertedPosition;
647
648   switch( mRotationAngle )
649   {
650     case 90:
651     {
652       convertedPosition.x = static_cast<float>( mWindowWidth ) - position.y;
653       convertedPosition.y = position.x;
654       break;
655     }
656     case 180:
657     {
658       convertedPosition.x = static_cast<float>( mWindowWidth ) - position.x;
659       convertedPosition.y = static_cast<float>( mWindowHeight ) - position.y;
660       break;
661     }
662     case 270:
663     {
664       convertedPosition.x = position.y;
665       convertedPosition.y = static_cast<float>( mWindowHeight ) - position.x;
666       break;
667     }
668     default:
669     {
670       convertedPosition = position;
671       break;
672     }
673   }
674
675   point.SetScreenPosition( convertedPosition );
676 }
677
678 Dali::Window Window::Get( Dali::Actor actor )
679 {
680   Internal::Adaptor::Window* windowImpl = nullptr;
681
682   if ( Internal::Adaptor::Adaptor::IsAvailable() )
683   {
684     Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation( Internal::Adaptor::Adaptor::Get() );
685     windowImpl = static_cast<Internal::Adaptor::Window*>( adaptor.GetWindow( actor ) );
686   }
687
688   return Dali::Window( windowImpl );
689 }
690
691 } // Adaptor
692
693 } // Internal
694
695 } // Dali