Support Device orientation and window orientation event.
[platform/core/uifw/dali-adaptor.git] / dali / internal / window-system / common / window-impl.cpp
1 /*
2  * Copyright (c) 2022 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/devel-api/adaptor-framework/orientation.h>
23 #include <dali/devel-api/events/key-event-devel.h>
24 #include <dali/integration-api/core.h>
25 #include <dali/integration-api/events/touch-event-integ.h>
26 #include <dali/integration-api/events/touch-integ.h>
27 #include <dali/public-api/actors/actor.h>
28 #include <dali/public-api/actors/camera-actor.h>
29 #include <dali/public-api/actors/layer.h>
30 #include <dali/public-api/adaptor-framework/window-enumerations.h>
31 #include <dali/public-api/render-tasks/render-task-list.h>
32 #include <dali/public-api/render-tasks/render-task.h>
33 #include <dali/public-api/rendering/frame-buffer.h>
34 #include <thread>
35
36 // INTERNAL HEADERS
37 #include <dali/devel-api/adaptor-framework/accessibility-bridge.h>
38 #include <dali/devel-api/atspi-interfaces/accessible.h>
39 #include <dali/integration-api/adaptor-framework/render-surface-interface.h>
40 #include <dali/internal/graphics/gles/egl-graphics.h>
41 #include <dali/internal/window-system/common/event-handler.h>
42 #include <dali/internal/window-system/common/orientation-impl.h>
43 #include <dali/internal/window-system/common/render-surface-factory.h>
44 #include <dali/internal/window-system/common/window-base.h>
45 #include <dali/internal/window-system/common/window-factory.h>
46 #include <dali/internal/window-system/common/window-render-surface.h>
47 #include <dali/internal/window-system/common/window-system.h>
48 #include <dali/internal/window-system/common/window-visibility-observer.h>
49
50 namespace Dali
51 {
52 namespace Internal
53 {
54 namespace Adaptor
55 {
56 namespace
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, Dali::WindowType type, bool isTransparent)
65 {
66   Any surface;
67   return Window::New(surface, positionSize, name, className, type, isTransparent);
68 }
69
70 Window* Window::New(Any surface, const PositionSize& positionSize, const std::string& name, const std::string& className, Dali::WindowType type, bool isTransparent)
71 {
72   Window* window         = new Window();
73   window->mIsTransparent = isTransparent;
74   window->Initialize(surface, positionSize, name, className, type);
75   return window;
76 }
77
78 Window::Window()
79 : mWindowSurface(nullptr),
80   mWindowBase(),
81   mParentWindow(NULL),
82   mPreferredAngle(static_cast<int>(WindowOrientation::NO_ORIENTATION_PREFERENCE)),
83   mRotationAngle(0),
84   mWindowWidth(0),
85   mWindowHeight(0),
86   mNativeWindowId(-1),
87   mOrientationMode(Internal::Adaptor::Window::OrientationMode::PORTRAIT),
88   mDeleteRequestSignal(),
89   mFocusChangeSignal(),
90   mResizeSignal(),
91   mVisibilityChangedSignal(),
92   mTransitionEffectEventSignal(),
93   mKeyboardRepeatSettingsChangedSignal(),
94   mAuxiliaryMessageSignal(),
95   mMovedSignal(),
96   mOrientationChangedSignal(),
97   mLastKeyEvent(),
98   mLastTouchEvent(),
99   mIsTransparent(false),
100   mIsFocusAcceptable(true),
101   mIconified(false),
102   mOpaqueState(false),
103   mWindowRotationAcknowledgement(false),
104   mFocused(false),
105   mIsWindowRotating(false)
106 {
107 }
108
109 Window::~Window()
110 {
111   if(mScene)
112   {
113     auto bridge     = Accessibility::Bridge::GetCurrentBridge();
114     auto rootLayer  = mScene.GetRootLayer();
115     auto accessible = Accessibility::Accessible::Get(rootLayer);
116     bridge->RemoveTopLevelWindow(accessible);
117     // Related to multi-window case. This is called for default window and non-default window, but it is effective for non-default window.
118     bridge->Emit(accessible, Accessibility::WindowEvent::DESTROY);
119   }
120
121   if(mAdaptor)
122   {
123     mAdaptor->RemoveWindow(this);
124   }
125
126   if(mEventHandler)
127   {
128     mEventHandler->RemoveObserver(*this);
129   }
130 }
131
132 void Window::Initialize(Any surface, const PositionSize& positionSize, const std::string& name, const std::string& className, WindowType type)
133 {
134   // Create a window render surface
135   auto renderSurfaceFactory = Dali::Internal::Adaptor::GetRenderSurfaceFactory();
136   mSurface                  = renderSurfaceFactory->CreateWindowRenderSurface(positionSize, surface, mIsTransparent);
137   mWindowSurface            = static_cast<WindowRenderSurface*>(mSurface.get());
138
139   // Get a window base
140   mWindowBase = mWindowSurface->GetWindowBase();
141
142   // Set Window Type
143   mWindowBase->SetType(type);
144
145   // Initialize for Ime window type
146   if(type == WindowType::IME)
147   {
148     mWindowBase->InitializeIme();
149     mWindowSurface->InitializeImeSurface();
150   }
151
152   // Connect signals
153   mWindowBase->IconifyChangedSignal().Connect(this, &Window::OnIconifyChanged);
154   mWindowBase->FocusChangedSignal().Connect(this, &Window::OnFocusChanged);
155   mWindowBase->DeleteRequestSignal().Connect(this, &Window::OnDeleteRequest);
156   mWindowBase->TransitionEffectEventSignal().Connect(this, &Window::OnTransitionEffectEvent);
157   mWindowBase->KeyboardRepeatSettingsChangedSignal().Connect(this, &Window::OnKeyboardRepeatSettingsChanged);
158   mWindowBase->WindowRedrawRequestSignal().Connect(this, &Window::OnWindowRedrawRequest);
159   mWindowBase->UpdatePositionSizeSignal().Connect(this, &Window::OnUpdatePositionSize);
160   mWindowBase->AuxiliaryMessageSignal().Connect(this, &Window::OnAuxiliaryMessage);
161
162   mWindowSurface->OutputTransformedSignal().Connect(this, &Window::OnOutputTransformed);
163   mWindowSurface->RotationFinishedSignal().Connect(this, &Window::OnRotationFinished);
164
165   AddAuxiliaryHint("wm.policy.win.user.geometry", "1");
166
167   SetClass(name, className);
168
169   mOrientation = Orientation::New(this);
170
171   // Get OrientationMode
172   int screenWidth, screenHeight;
173   WindowSystem::GetScreenSize(screenWidth, screenHeight);
174   if(screenWidth > screenHeight)
175   {
176     mOrientationMode = Internal::Adaptor::Window::OrientationMode::LANDSCAPE;
177   }
178   else
179   {
180     mOrientationMode = Internal::Adaptor::Window::OrientationMode::PORTRAIT;
181   }
182
183   if(positionSize.width <= 0 || positionSize.height <= 0)
184   {
185     mWindowWidth  = screenWidth;
186     mWindowHeight = screenHeight;
187   }
188   else
189   {
190     mWindowWidth  = positionSize.width;
191     mWindowHeight = positionSize.height;
192   }
193
194   // For Debugging
195   mNativeWindowId = mWindowBase->GetNativeWindowId();
196 }
197
198 void Window::OnAdaptorSet(Dali::Adaptor& adaptor)
199 {
200   mEventHandler = EventHandlerPtr(new EventHandler(mWindowSurface->GetWindowBase(), *mAdaptor));
201   mEventHandler->AddObserver(*this);
202
203   // Add Window to bridge for ATSPI
204   auto bridge = Accessibility::Bridge::GetCurrentBridge();
205   if(bridge->IsUp())
206   {
207     auto rootLayer  = mScene.GetRootLayer();
208     auto accessible = Accessibility::Accessible::Get(rootLayer);
209     bridge->AddTopLevelWindow(accessible);
210   }
211
212   bridge->EnabledSignal().Connect(this, &Window::OnAccessibilityEnabled);
213   bridge->DisabledSignal().Connect(this, &Window::OnAccessibilityDisabled);
214
215   // If you call the 'Show' before creating the adaptor, the application cannot know the app resource id.
216   // The show must be called after the adaptor is initialized.
217   Show();
218 }
219
220 void Window::OnSurfaceSet(Dali::RenderSurfaceInterface* surface)
221 {
222   mWindowSurface = static_cast<WindowRenderSurface*>(surface);
223 }
224
225 void Window::SetClass(std::string name, std::string className)
226 {
227   mName      = name;
228   mClassName = className;
229   mWindowBase->SetClass(name, className);
230 }
231
232 std::string Window::GetClassName() const
233 {
234   return mClassName;
235 }
236
237 void Window::Raise()
238 {
239   mWindowBase->Raise();
240
241   mSurface->SetFullSwapNextFrame();
242
243   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Raise() \n", this, mNativeWindowId);
244 }
245
246 void Window::Lower()
247 {
248   mWindowBase->Lower();
249
250   mSurface->SetFullSwapNextFrame();
251
252   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Lower() \n", this, mNativeWindowId);
253 }
254
255 void Window::Activate()
256 {
257   mWindowBase->Activate();
258
259   mSurface->SetFullSwapNextFrame();
260
261   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Activate() \n", this, mNativeWindowId);
262 }
263
264 void Window::Maximize(bool maximize)
265 {
266   mWindowBase->Maximize(maximize);
267
268   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Maximize: %d\n", this, mNativeWindowId, maximize);
269 }
270
271 bool Window::IsMaximized() const
272 {
273   return mWindowBase->IsMaximized();
274 }
275
276 void Window::SetMaximumSize(Dali::Window::WindowSize size)
277 {
278   mWindowBase->SetMaximumSize(size);
279 }
280
281 void Window::Minimize(bool minimize)
282 {
283   mWindowBase->Minimize(minimize);
284
285   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Minimize: %d\n", this, mNativeWindowId, minimize);
286 }
287
288 bool Window::IsMinimized() const
289 {
290   return mWindowBase->IsMinimized();
291 }
292
293 void Window::SetMimimumSize(Dali::Window::WindowSize size)
294 {
295   mWindowBase->SetMimimumSize(size);
296 }
297
298 uint32_t Window::GetLayerCount() const
299 {
300   return mScene.GetLayerCount();
301 }
302
303 Dali::Layer Window::GetLayer(uint32_t depth) const
304 {
305   return mScene.GetLayer(depth);
306 }
307
308 Dali::RenderTaskList Window::GetRenderTaskList() const
309 {
310   return mScene.GetRenderTaskList();
311 }
312
313 std::string Window::GetNativeResourceId() const
314 {
315   return mWindowBase->GetNativeWindowResourceId();
316 }
317
318 void Window::AddAvailableOrientation(WindowOrientation orientation)
319 {
320   if(IsOrientationAvailable(orientation) == false)
321   {
322     return;
323   }
324
325   bool found          = false;
326   int  convertedAngle = ConvertToAngle(orientation);
327   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), AddAvailableOrientation: %d\n", this, mNativeWindowId, convertedAngle);
328   for(std::size_t i = 0; i < mAvailableAngles.size(); i++)
329   {
330     if(mAvailableAngles[i] == convertedAngle)
331     {
332       found = true;
333       break;
334     }
335   }
336
337   if(!found)
338   {
339     mAvailableAngles.push_back(convertedAngle);
340     SetAvailableAnlges(mAvailableAngles);
341   }
342 }
343
344 void Window::RemoveAvailableOrientation(WindowOrientation orientation)
345 {
346   if(IsOrientationAvailable(orientation) == false)
347   {
348     return;
349   }
350
351   int convertedAngle = ConvertToAngle(orientation);
352   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), RemoveAvailableOrientation: %d\n", this, mNativeWindowId, convertedAngle);
353   for(std::vector<int>::iterator iter = mAvailableAngles.begin();
354       iter != mAvailableAngles.end();
355       ++iter)
356   {
357     if(*iter == convertedAngle)
358     {
359       mAvailableAngles.erase(iter);
360       break;
361     }
362   }
363
364   SetAvailableAnlges(mAvailableAngles);
365 }
366
367 void Window::SetPreferredOrientation(WindowOrientation orientation)
368 {
369   if(orientation < WindowOrientation::NO_ORIENTATION_PREFERENCE || orientation > WindowOrientation::LANDSCAPE_INVERSE)
370   {
371     DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window::CheckOrientation: Invalid input orientation [%d]\n", orientation);
372     return;
373   }
374   mPreferredAngle = ConvertToAngle(orientation);
375   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), SetPreferredOrientation: %d\n", this, mNativeWindowId, mPreferredAngle);
376   mWindowBase->SetPreferredAngle(mPreferredAngle);
377 }
378
379 WindowOrientation Window::GetPreferredOrientation()
380 {
381   WindowOrientation preferredOrientation = ConvertToOrientation(mPreferredAngle);
382   return preferredOrientation;
383 }
384
385 void Window::SetPositionSizeWithOrientation(PositionSize positionSize, WindowOrientation orientation)
386 {
387   int angle = ConvertToAngle(orientation);
388   mWindowBase->SetPositionSizeWithAngle(positionSize, angle);
389 }
390
391 void Window::EmitAccessibilityHighlightSignal(bool highlight)
392 {
393   Dali::Window handle(this);
394   mAccessibilityHighlightSignal.Emit(handle, highlight);
395 }
396
397 void Window::SetAvailableAnlges(const std::vector<int>& angles)
398 {
399   if(angles.size() > 4)
400   {
401     DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window::SetAvailableAnlges: Invalid vector size! [%d]\n", angles.size());
402     return;
403   }
404
405   mWindowBase->SetAvailableAnlges(angles);
406 }
407
408 int Window::ConvertToAngle(WindowOrientation orientation)
409 {
410   int convertAngle = static_cast<int>(orientation);
411   if(mOrientationMode == Internal::Adaptor::Window::OrientationMode::LANDSCAPE)
412   {
413     switch(orientation)
414     {
415       case WindowOrientation::LANDSCAPE:
416       {
417         convertAngle = 0;
418         break;
419       }
420       case WindowOrientation::PORTRAIT:
421       {
422         convertAngle = 90;
423         break;
424       }
425       case WindowOrientation::LANDSCAPE_INVERSE:
426       {
427         convertAngle = 180;
428         break;
429       }
430       case WindowOrientation::PORTRAIT_INVERSE:
431       {
432         convertAngle = 270;
433         break;
434       }
435       case WindowOrientation::NO_ORIENTATION_PREFERENCE:
436       {
437         convertAngle = -1;
438         break;
439       }
440     }
441   }
442   return convertAngle;
443 }
444
445 WindowOrientation Window::ConvertToOrientation(int angle) const
446 {
447   WindowOrientation orientation = static_cast<WindowOrientation>(angle);
448   if(mOrientationMode == Internal::Adaptor::Window::OrientationMode::LANDSCAPE)
449   {
450     switch(angle)
451     {
452       case 0:
453       {
454         orientation = WindowOrientation::LANDSCAPE;
455         break;
456       }
457       case 90:
458       {
459         orientation = WindowOrientation::PORTRAIT;
460         break;
461       }
462       case 180:
463       {
464         orientation = WindowOrientation::LANDSCAPE_INVERSE;
465         break;
466       }
467       case 270:
468       {
469         orientation = WindowOrientation::PORTRAIT_INVERSE;
470         break;
471       }
472       case -1:
473       {
474         orientation = WindowOrientation::NO_ORIENTATION_PREFERENCE;
475         break;
476       }
477     }
478   }
479   return orientation;
480 }
481
482 bool Window::IsOrientationAvailable(WindowOrientation orientation) const
483 {
484   if(orientation <= WindowOrientation::NO_ORIENTATION_PREFERENCE || orientation > WindowOrientation::LANDSCAPE_INVERSE)
485   {
486     DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window::IsOrientationAvailable: Invalid input orientation [%d]\n", orientation);
487     return false;
488   }
489   return true;
490 }
491
492 Dali::Any Window::GetNativeHandle() const
493 {
494   return mWindowSurface->GetNativeWindow();
495 }
496
497 void Window::SetAcceptFocus(bool accept)
498 {
499   mIsFocusAcceptable = accept;
500
501   mWindowBase->SetAcceptFocus(accept);
502 }
503
504 bool Window::IsFocusAcceptable() const
505 {
506   return mIsFocusAcceptable;
507 }
508
509 void Window::Show()
510 {
511   mVisible = true;
512
513   mWindowBase->Show();
514
515   if(!mIconified)
516   {
517     Dali::Window handle(this);
518     mVisibilityChangedSignal.Emit(handle, true);
519     Dali::Accessibility::Bridge::GetCurrentBridge()->WindowShown(handle);
520
521     WindowVisibilityObserver* observer(mAdaptor);
522     observer->OnWindowShown();
523   }
524
525   mSurface->SetFullSwapNextFrame();
526
527   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Show(): iconified = %d, visible = %d\n", this, mNativeWindowId, mIconified, mVisible);
528 }
529
530 void Window::Hide()
531 {
532   mVisible = false;
533
534   mWindowBase->Hide();
535
536   if(!mIconified)
537   {
538     Dali::Window handle(this);
539     mVisibilityChangedSignal.Emit(handle, false);
540     Dali::Accessibility::Bridge::GetCurrentBridge()->WindowHidden(handle);
541
542     WindowVisibilityObserver* observer(mAdaptor);
543     observer->OnWindowHidden();
544   }
545
546   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Hide(): iconified = %d, visible = %d\n", this, mNativeWindowId, mIconified, mVisible);
547 }
548
549 bool Window::IsVisible() const
550 {
551   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), IsVisible(): iconified = %d, visible = %d\n", this, mNativeWindowId, mIconified, mVisible);
552   return mVisible && !mIconified;
553 }
554
555 unsigned int Window::GetSupportedAuxiliaryHintCount() const
556 {
557   return mWindowBase->GetSupportedAuxiliaryHintCount();
558 }
559
560 std::string Window::GetSupportedAuxiliaryHint(unsigned int index) const
561 {
562   return mWindowBase->GetSupportedAuxiliaryHint(index);
563 }
564
565 unsigned int Window::AddAuxiliaryHint(const std::string& hint, const std::string& value)
566 {
567   return mWindowBase->AddAuxiliaryHint(hint, value);
568 }
569
570 bool Window::RemoveAuxiliaryHint(unsigned int id)
571 {
572   return mWindowBase->RemoveAuxiliaryHint(id);
573 }
574
575 bool Window::SetAuxiliaryHintValue(unsigned int id, const std::string& value)
576 {
577   return mWindowBase->SetAuxiliaryHintValue(id, value);
578 }
579
580 std::string Window::GetAuxiliaryHintValue(unsigned int id) const
581 {
582   return mWindowBase->GetAuxiliaryHintValue(id);
583 }
584
585 unsigned int Window::GetAuxiliaryHintId(const std::string& hint) const
586 {
587   return mWindowBase->GetAuxiliaryHintId(hint);
588 }
589
590 void Window::SetInputRegion(const Rect<int>& inputRegion)
591 {
592   Rect<int> convertRegion = RecalculateRect(inputRegion);
593
594   DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window (%p), WinId (%d), SetInputRegion, RecalculateRegion, (%d,%d), (%d x %d)\n", this, mNativeWindowId, convertRegion.x, convertRegion.y, convertRegion.width, convertRegion.height);
595
596   mWindowBase->SetInputRegion(convertRegion);
597 }
598
599 void Window::SetType(WindowType type)
600 {
601   mWindowBase->SetType(type);
602 }
603
604 WindowType Window::GetType() const
605 {
606   return mWindowBase->GetType();
607 }
608
609 WindowOperationResult Window::SetNotificationLevel(WindowNotificationLevel level)
610 {
611   WindowType type = mWindowBase->GetType();
612   if(type != WindowType::NOTIFICATION)
613   {
614     DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window::SetNotificationLevel: Not supported window type [%d]\n", type);
615     return WindowOperationResult::INVALID_OPERATION;
616   }
617
618   return mWindowBase->SetNotificationLevel(level);
619 }
620
621 WindowNotificationLevel Window::GetNotificationLevel() const
622 {
623   WindowType type = mWindowBase->GetType();
624   if(type != WindowType::NOTIFICATION)
625   {
626     DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window::GetNotificationLevel: Not supported window type [%d]\n", type);
627     return WindowNotificationLevel::NONE;
628   }
629
630   return mWindowBase->GetNotificationLevel();
631 }
632
633 void Window::SetOpaqueState(bool opaque)
634 {
635   mOpaqueState = opaque;
636
637   mWindowBase->SetOpaqueState(opaque);
638
639   DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window::SetOpaqueState: opaque = %d\n", opaque);
640 }
641
642 bool Window::IsOpaqueState() const
643 {
644   return mOpaqueState;
645 }
646
647 WindowOperationResult Window::SetScreenOffMode(WindowScreenOffMode screenOffMode)
648 {
649   return mWindowBase->SetScreenOffMode(screenOffMode);
650 }
651
652 WindowScreenOffMode Window::GetScreenOffMode() const
653 {
654   return mWindowBase->GetScreenOffMode();
655 }
656
657 WindowOperationResult Window::SetBrightness(int brightness)
658 {
659   if(brightness < 0 || brightness > 100)
660   {
661     DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window::SetBrightness: Invalid brightness value [%d]\n", brightness);
662     return WindowOperationResult::INVALID_OPERATION;
663   }
664
665   return mWindowBase->SetBrightness(brightness);
666 }
667
668 int Window::GetBrightness() const
669 {
670   return mWindowBase->GetBrightness();
671 }
672
673 void Window::SetSize(Dali::Window::WindowSize size)
674 {
675   PositionSize oldRect = GetPositionSize();
676
677   PositionSize newRect;
678   newRect.width  = size.GetWidth();
679   newRect.height = size.GetHeight();
680
681   // When surface size is updated, inform adaptor of resizing and emit ResizeSignal
682   if((oldRect.width != newRect.width) || (oldRect.height != newRect.height))
683   {
684     mWindowSurface->MoveResize(PositionSize(oldRect.x, oldRect.y, newRect.width, newRect.height));
685
686     Uint16Pair newSize(newRect.width, newRect.height);
687
688     mWindowWidth  = newRect.width;
689     mWindowHeight = newRect.height;
690
691     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), current angle (%d), SetSize(): (%d, %d), [%d x %d]\n", this, mNativeWindowId, mRotationAngle, oldRect.x, oldRect.y, newRect.width, newRect.height);
692
693     SurfaceResized(static_cast<float>(mWindowWidth), static_cast<float>(mWindowHeight));
694
695     mAdaptor->SurfaceResizePrepare(mSurface.get(), newSize);
696
697     Dali::Window handle(this);
698     mResizeSignal.Emit(handle, newSize);
699
700     mAdaptor->SurfaceResizeComplete(mSurface.get(), newSize);
701   }
702
703   mSurface->SetFullSwapNextFrame();
704
705   Dali::Accessibility::Accessible::Get(mScene.GetRootLayer())->EmitBoundsChanged(Dali::Rect<>(oldRect.x, oldRect.y, size.GetWidth(), size.GetHeight()));
706 }
707
708 Dali::Window::WindowSize Window::GetSize() const
709 {
710   return Dali::Window::WindowSize(mWindowWidth, mWindowHeight);
711 }
712
713 void Window::SetPosition(Dali::Window::WindowPosition position)
714 {
715   PositionSize oldRect = mSurface->GetPositionSize();
716   int32_t      newX    = position.GetX();
717   int32_t      newY    = position.GetY();
718
719   mWindowSurface->MoveResize(PositionSize(newX, newY, oldRect.width, oldRect.height));
720
721   if((oldRect.x != newX) || (oldRect.y != newY))
722   {
723     Dali::Window                 handle(this);
724     Dali::Window::WindowPosition newPosition(newX, newY);
725
726     DALI_LOG_RELEASE_INFO("send moved signal with new position: %d, %d\n", newPosition.GetX(), newPosition.GetY());
727     mMovedSignal.Emit(handle, newPosition);
728   }
729
730   mSurface->SetFullSwapNextFrame();
731
732   Dali::Accessibility::Accessible::Get(mScene.GetRootLayer())->EmitBoundsChanged(Dali::Rect<>(position.GetX(), position.GetY(), oldRect.width, oldRect.height));
733 }
734
735 Dali::Window::WindowPosition Window::GetPosition() const
736 {
737   PositionSize positionSize = GetPositionSize();
738   return Dali::Window::WindowPosition(positionSize.x, positionSize.y);
739 }
740
741 PositionSize Window::GetPositionSize() const
742 {
743   PositionSize positionSize = mSurface->GetPositionSize();
744   positionSize.width        = mWindowWidth;
745   positionSize.height       = mWindowHeight;
746   return positionSize;
747 }
748
749 void Window::SetPositionSize(PositionSize positionSize)
750 {
751   bool moved  = false;
752   bool resize = false;
753
754   PositionSize oldRect = GetPositionSize();
755   Dali::Window handle(this);
756
757   if((oldRect.x != positionSize.x) || (oldRect.y != positionSize.y))
758   {
759     moved = true;
760   }
761
762   if((oldRect.width != positionSize.width) || (oldRect.height != positionSize.height))
763   {
764     resize = true;
765   }
766
767   if(moved || resize)
768   {
769     mWindowSurface->MoveResize(positionSize);
770   }
771
772   // When window is moved, emit Moved Signal
773   if(moved)
774   {
775     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Moved signal emit (%d, %d)\n", this, mNativeWindowId, positionSize.x, positionSize.y);
776     Dali::Window::WindowPosition position(positionSize.x, positionSize.y);
777     mMovedSignal.Emit(handle, position);
778   }
779
780   // When surface size is updated, inform adaptor of resizing and emit ResizeSignal
781   if(resize)
782   {
783     Uint16Pair newSize(positionSize.width, positionSize.height);
784
785     mWindowWidth  = positionSize.width;
786     mWindowHeight = positionSize.height;
787
788     SurfaceResized(static_cast<float>(mWindowWidth), static_cast<float>(mWindowHeight));
789
790     mAdaptor->SurfaceResizePrepare(mSurface.get(), newSize);
791
792     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Resize signal emit [%d x %d]\n", this, mNativeWindowId, positionSize.width, positionSize.height);
793
794     mResizeSignal.Emit(handle, newSize);
795     mAdaptor->SurfaceResizeComplete(mSurface.get(), newSize);
796   }
797
798   mSurface->SetFullSwapNextFrame();
799
800   Dali::Accessibility::Accessible::Get(mScene.GetRootLayer())->EmitBoundsChanged(Dali::Rect<>(positionSize.x, positionSize.y, positionSize.width, positionSize.height));
801 }
802
803 Dali::Layer Window::GetRootLayer() const
804 {
805   return mScene.GetRootLayer();
806 }
807
808 void Window::SetTransparency(bool transparent)
809 {
810   mWindowSurface->SetTransparency(transparent);
811 }
812
813 bool Window::GrabKey(Dali::KEY key, KeyGrab::KeyGrabMode grabMode)
814 {
815   return mWindowBase->GrabKey(key, grabMode);
816 }
817
818 bool Window::UngrabKey(Dali::KEY key)
819 {
820   return mWindowBase->UngrabKey(key);
821 }
822
823 bool Window::GrabKeyList(const Dali::Vector<Dali::KEY>& key, const Dali::Vector<KeyGrab::KeyGrabMode>& grabMode, Dali::Vector<bool>& result)
824 {
825   return mWindowBase->GrabKeyList(key, grabMode, result);
826 }
827
828 bool Window::UngrabKeyList(const Dali::Vector<Dali::KEY>& key, Dali::Vector<bool>& result)
829 {
830   return mWindowBase->UngrabKeyList(key, result);
831 }
832
833 void Window::OnIconifyChanged(bool iconified)
834 {
835   if(iconified)
836   {
837     mIconified = true;
838
839     if(mVisible)
840     {
841       Dali::Window handle(this);
842       mVisibilityChangedSignal.Emit(handle, false);
843       Dali::Accessibility::Bridge::GetCurrentBridge()->WindowHidden(handle);
844
845       WindowVisibilityObserver* observer(mAdaptor);
846       observer->OnWindowHidden();
847     }
848
849     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Iconified: visible = %d\n", this, mNativeWindowId, mVisible);
850   }
851   else
852   {
853     mIconified = false;
854
855     if(mVisible)
856     {
857       Dali::Window handle(this);
858       mVisibilityChangedSignal.Emit(handle, true);
859       Dali::Accessibility::Bridge::GetCurrentBridge()->WindowShown(handle);
860
861       WindowVisibilityObserver* observer(mAdaptor);
862       observer->OnWindowShown();
863     }
864
865     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Deiconified: visible = %d\n", this, mNativeWindowId, mVisible);
866   }
867
868   mSurface->SetFullSwapNextFrame();
869 }
870
871 void Window::OnFocusChanged(bool focusIn)
872 {
873   Dali::Window handle(this);
874   mFocusChangeSignal.Emit(handle, focusIn);
875
876   mSurface->SetFullSwapNextFrame();
877
878   if(auto bridge = Dali::Accessibility::Bridge::GetCurrentBridge())
879   {
880     if(focusIn)
881     {
882       bridge->WindowFocused(handle);
883     }
884     else
885     {
886       bridge->WindowUnfocused(handle);
887     }
888   }
889   mFocused = focusIn;
890 }
891
892 void Window::OnOutputTransformed()
893 {
894   PositionSize positionSize = GetPositionSize();
895
896   SurfaceRotated(static_cast<float>(positionSize.width), static_cast<float>(positionSize.height), mRotationAngle, mWindowBase->GetScreenRotationAngle());
897
898   mAdaptor->SurfaceResizePrepare(mSurface.get(), Adaptor::SurfaceSize(positionSize.width, positionSize.height));
899   mAdaptor->SurfaceResizeComplete(mSurface.get(), Adaptor::SurfaceSize(positionSize.width, positionSize.height));
900 }
901
902 void Window::OnDeleteRequest()
903 {
904   mDeleteRequestSignal.Emit();
905 }
906
907 void Window::OnTransitionEffectEvent(WindowEffectState state, WindowEffectType type)
908 {
909   Dali::Window handle(this);
910   mTransitionEffectEventSignal.Emit(handle, state, type);
911 }
912
913 void Window::OnKeyboardRepeatSettingsChanged()
914 {
915   Dali::Window handle(this);
916   mKeyboardRepeatSettingsChangedSignal.Emit();
917 }
918
919 void Window::OnWindowRedrawRequest()
920 {
921   mAdaptor->RenderOnce();
922 }
923
924 void Window::OnUpdatePositionSize(Dali::PositionSize& positionSize)
925 {
926   bool moved  = false;
927   bool resize = false;
928
929   Dali::Window handle(this);
930
931   PositionSize oldRect = GetPositionSize();
932   PositionSize newRect = positionSize;
933
934   if((oldRect.x != newRect.x) || (oldRect.y != newRect.y))
935   {
936     moved = true;
937   }
938
939   if((oldRect.width != newRect.width) || (oldRect.height != newRect.height))
940   {
941     resize = true;
942   }
943
944   if(moved || resize)
945   {
946     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), current angle (%d), position or size is updated by server , (%d, %d) [%d x %d]\n", this, mNativeWindowId, mRotationAngle, newRect.x, newRect.y, newRect.width, newRect.height);
947     mWindowSurface->UpdatePositionSize(positionSize);
948   }
949
950   if((oldRect.x != newRect.x) || (oldRect.y != newRect.y))
951   {
952     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Moved signal emit (%d, %d)\n", this, mNativeWindowId, newRect.x, newRect.y);
953     Dali::Window::WindowPosition position(newRect.x, newRect.y);
954     mMovedSignal.Emit(handle, position);
955   }
956
957   // When surface size is updated, inform adaptor of resizing and emit ResizeSignal
958   if((oldRect.width != newRect.width) || (oldRect.height != newRect.height))
959   {
960     Uint16Pair newSize(newRect.width, newRect.height);
961
962     mWindowWidth  = newRect.width;
963     mWindowHeight = newRect.height;
964
965     SurfaceResized(static_cast<float>(mWindowWidth), static_cast<float>(mWindowHeight));
966
967     mAdaptor->SurfaceResizePrepare(mSurface.get(), newSize);
968
969     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Resized signal emit [%d x %d]\n", this, mNativeWindowId, newRect.width, newRect.height);
970     mResizeSignal.Emit(handle, newSize);
971     mAdaptor->SurfaceResizeComplete(mSurface.get(), newSize);
972   }
973
974   mSurface->SetFullSwapNextFrame();
975
976   Dali::Accessibility::Accessible::Get(mScene.GetRootLayer())->EmitBoundsChanged(Dali::Rect<>(positionSize.x, positionSize.y, positionSize.width, positionSize.height));
977 }
978
979 void Window::OnTouchPoint(Dali::Integration::Point& point, int timeStamp)
980 {
981   mLastTouchEvent = Dali::Integration::NewTouchEvent(timeStamp, point);
982   FeedTouchPoint(point, timeStamp);
983 }
984
985 void Window::OnWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
986 {
987   FeedWheelEvent(wheelEvent);
988 }
989
990 void Window::OnKeyEvent(Dali::Integration::KeyEvent& keyEvent)
991 {
992   mLastKeyEvent = Dali::DevelKeyEvent::New(keyEvent.keyName, keyEvent.logicalKey, keyEvent.keyString, keyEvent.keyCode, keyEvent.keyModifier, keyEvent.time, static_cast<Dali::KeyEvent::State>(keyEvent.state), keyEvent.compose, keyEvent.deviceName, keyEvent.deviceClass, keyEvent.deviceSubclass);
993   FeedKeyEvent(keyEvent);
994 }
995
996 void Window::OnRotation(const RotationEvent& rotation)
997 {
998   PositionSize newPositionSize(rotation.x, rotation.y, rotation.width, rotation.height);
999
1000   mRotationAngle = rotation.angle;
1001   mWindowWidth   = rotation.width;
1002   mWindowHeight  = rotation.height;
1003
1004   mIsWindowRotating = true;
1005   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), angle(%d), Window Rotation (%d , %d) [%d x %d]\n", this, mNativeWindowId, mRotationAngle, newPositionSize.x, newPositionSize.y, mWindowWidth, mWindowHeight);
1006
1007   // Notify that the orientation is changed
1008   mOrientation->OnOrientationChange(rotation);
1009
1010   mWindowSurface->RequestRotation(mRotationAngle, newPositionSize);
1011
1012   SurfaceRotated(static_cast<float>(mWindowWidth), static_cast<float>(mWindowHeight), mRotationAngle, mWindowBase->GetScreenRotationAngle());
1013
1014   mAdaptor->SurfaceResizePrepare(mSurface.get(), Adaptor::SurfaceSize(mWindowWidth, mWindowHeight));
1015
1016   Dali::Window handle(this);
1017   mResizeSignal.Emit(handle, Dali::Window::WindowSize(mWindowWidth, mWindowHeight));
1018   mOrientationChangedSignal.Emit(handle, GetCurrentOrientation());
1019
1020   mAdaptor->SurfaceResizeComplete(mSurface.get(), Adaptor::SurfaceSize(mWindowWidth, mWindowHeight));
1021 }
1022
1023 void Window::OnRotationFinished()
1024 {
1025   mIsWindowRotating = false;
1026   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), window rotation is finised\n", this, mNativeWindowId);
1027 }
1028
1029 void Window::OnPause()
1030 {
1031   if(mEventHandler)
1032   {
1033     mEventHandler->Pause();
1034   }
1035 }
1036
1037 void Window::OnResume()
1038 {
1039   if(mEventHandler)
1040   {
1041     mEventHandler->Resume();
1042   }
1043
1044   mSurface->SetFullSwapNextFrame();
1045 }
1046
1047 void Window::OnAuxiliaryMessage(const std::string& key, const std::string& value, const Property::Array& options)
1048 {
1049   mAuxiliaryMessageSignal.Emit(key, value, options);
1050 }
1051
1052 void Window::OnAccessibilityEnabled()
1053 {
1054   auto bridge     = Accessibility::Bridge::GetCurrentBridge();
1055   auto rootLayer  = mScene.GetRootLayer();
1056   auto accessible = Accessibility::Accessible::Get(rootLayer);
1057   bridge->AddTopLevelWindow(accessible);
1058
1059   if(!mVisible || mIconified)
1060   {
1061     return;
1062   }
1063
1064   Dali::Window handle(this);
1065   bridge->WindowShown(handle);
1066
1067   if(mFocused)
1068   {
1069     bridge->WindowFocused(handle);
1070   }
1071 }
1072
1073 void Window::OnAccessibilityDisabled()
1074 {
1075   auto bridge     = Accessibility::Bridge::GetCurrentBridge();
1076   auto rootLayer  = mScene.GetRootLayer();
1077   auto accessible = Accessibility::Accessible::Get(rootLayer);
1078   bridge->RemoveTopLevelWindow(accessible);
1079 }
1080
1081 void Window::RecalculateTouchPosition(Integration::Point& point)
1082 {
1083   Vector2 position = point.GetScreenPosition();
1084   Vector2 convertedPosition;
1085
1086   switch(mRotationAngle)
1087   {
1088     case 90:
1089     {
1090       convertedPosition.x = static_cast<float>(mWindowWidth) - position.y;
1091       convertedPosition.y = position.x;
1092       break;
1093     }
1094     case 180:
1095     {
1096       convertedPosition.x = static_cast<float>(mWindowWidth) - position.x;
1097       convertedPosition.y = static_cast<float>(mWindowHeight) - position.y;
1098       break;
1099     }
1100     case 270:
1101     {
1102       convertedPosition.x = position.y;
1103       convertedPosition.y = static_cast<float>(mWindowHeight) - position.x;
1104       break;
1105     }
1106     default:
1107     {
1108       convertedPosition = position;
1109       break;
1110     }
1111   }
1112
1113   point.SetScreenPosition(convertedPosition);
1114 }
1115
1116 Dali::Window Window::Get(Dali::Actor actor)
1117 {
1118   Internal::Adaptor::Window* windowImpl = nullptr;
1119
1120   if(Internal::Adaptor::Adaptor::IsAvailable())
1121   {
1122     Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
1123     windowImpl                                = dynamic_cast<Internal::Adaptor::Window*>(adaptor.GetWindow(actor));
1124     if(windowImpl)
1125     {
1126       return Dali::Window(windowImpl);
1127     }
1128   }
1129
1130   return Dali::Window();
1131 }
1132
1133 void Window::SetParent(Dali::Window& parent)
1134 {
1135   if(DALI_UNLIKELY(parent))
1136   {
1137     mParentWindow     = parent;
1138     Dali::Window self = Dali::Window(this);
1139     // check circular parent window setting
1140     if(Dali::DevelWindow::GetParent(parent) == self)
1141     {
1142       Dali::DevelWindow::Unparent(parent);
1143     }
1144     mWindowBase->SetParent(GetImplementation(mParentWindow).mWindowBase, false);
1145   }
1146 }
1147
1148 void Window::SetParent(Dali::Window& parent, bool belowParent)
1149 {
1150   if(DALI_UNLIKELY(parent))
1151   {
1152     mParentWindow     = parent;
1153     Dali::Window self = Dali::Window(this);
1154     // check circular parent window setting
1155     if(Dali::DevelWindow::GetParent(parent) == self)
1156     {
1157       Dali::DevelWindow::Unparent(parent);
1158     }
1159     mWindowBase->SetParent(GetImplementation(mParentWindow).mWindowBase, belowParent);
1160   }
1161 }
1162
1163 void Window::Unparent()
1164 {
1165   mWindowBase->SetParent(nullptr, false);
1166   mParentWindow.Reset();
1167 }
1168
1169 Dali::Window Window::GetParent()
1170 {
1171   return mParentWindow;
1172 }
1173
1174 WindowOrientation Window::GetCurrentOrientation() const
1175 {
1176   return ConvertToOrientation(mRotationAngle);
1177 }
1178
1179 int Window::GetPhysicalOrientation() const
1180 {
1181   return (mRotationAngle + mWindowBase->GetScreenRotationAngle()) % 360;
1182 }
1183
1184 void Window::SetAvailableOrientations(const Dali::Vector<WindowOrientation>& orientations)
1185 {
1186   Dali::Vector<float>::SizeType count = orientations.Count();
1187   for(Dali::Vector<float>::SizeType index = 0; index < count; ++index)
1188   {
1189     if(IsOrientationAvailable(orientations[index]) == false)
1190     {
1191       DALI_LOG_ERROR("Window::SetAvailableOrientations, invalid orientation: %d\n", orientations[index]);
1192       continue;
1193     }
1194
1195     bool found          = false;
1196     int  convertedAngle = ConvertToAngle(orientations[index]);
1197
1198     for(std::size_t i = 0; i < mAvailableAngles.size(); i++)
1199     {
1200       if(mAvailableAngles[i] == convertedAngle)
1201       {
1202         found = true;
1203         break;
1204       }
1205     }
1206
1207     if(!found)
1208     {
1209       DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), SetAvailableOrientations: %d\n", this, mNativeWindowId, convertedAngle);
1210       mAvailableAngles.push_back(convertedAngle);
1211     }
1212   }
1213   SetAvailableAnlges(mAvailableAngles);
1214 }
1215
1216 int32_t Window::GetNativeId() const
1217 {
1218   return mWindowBase->GetNativeWindowId();
1219 }
1220
1221 void Window::RequestMoveToServer()
1222 {
1223   mWindowBase->RequestMoveToServer();
1224 }
1225
1226 void Window::RequestResizeToServer(WindowResizeDirection direction)
1227 {
1228   mWindowBase->RequestResizeToServer(direction);
1229 }
1230
1231 void Window::EnableFloatingMode(bool enable)
1232 {
1233   mWindowBase->EnableFloatingMode(enable);
1234 }
1235
1236 Rect<int> Window::RecalculateRect(const Rect<int>& rect)
1237 {
1238   Rect<int> newRect;
1239   int       screenWidth, screenHeight;
1240
1241   WindowSystem::GetScreenSize(screenWidth, screenHeight);
1242
1243   if(mRotationAngle == 90)
1244   {
1245     newRect.x      = rect.y;
1246     newRect.y      = screenHeight - (rect.x + rect.width);
1247     newRect.width  = rect.height;
1248     newRect.height = rect.width;
1249   }
1250   else if(mRotationAngle == 180)
1251   {
1252     newRect.x      = screenWidth - (rect.x + rect.width);
1253     newRect.y      = screenHeight - (rect.y + rect.height);
1254     newRect.width  = rect.width;
1255     newRect.height = rect.height;
1256   }
1257   else if(mRotationAngle == 270)
1258   {
1259     newRect.x      = screenWidth - (rect.y + rect.height);
1260     newRect.y      = rect.x;
1261     newRect.width  = rect.height;
1262     newRect.height = rect.width;
1263   }
1264   else
1265   {
1266     newRect.x      = rect.x;
1267     newRect.y      = rect.y;
1268     newRect.width  = rect.width;
1269     newRect.height = rect.height;
1270   }
1271   return newRect;
1272 }
1273
1274 void Window::IncludeInputRegion(const Rect<int>& inputRegion)
1275 {
1276   Rect<int> convertRegion = RecalculateRect(inputRegion);
1277
1278   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), IncludeInputRegion, RecalculateRegion, (%d,%d), (%d x %d)\n", this, mNativeWindowId, convertRegion.x, convertRegion.y, convertRegion.width, convertRegion.height);
1279   mWindowBase->IncludeInputRegion(convertRegion);
1280 }
1281
1282 void Window::ExcludeInputRegion(const Rect<int>& inputRegion)
1283 {
1284   Rect<int> convertRegion = RecalculateRect(inputRegion);
1285
1286   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), ExcludeInputRegion, RecalculateRegion, (%d,%d), (%d x %d)\n", this, mNativeWindowId, convertRegion.x, convertRegion.y, convertRegion.width, convertRegion.height);
1287   mWindowBase->ExcludeInputRegion(convertRegion);
1288 }
1289
1290 void Window::SetNeedsRotationCompletedAcknowledgement(bool needAcknowledgement)
1291 {
1292   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), needAcknowledgement(%d) Set needs Rotation Completed Acknowledgement\n", this, mNativeWindowId, needAcknowledgement);
1293   mWindowSurface->SetNeedsRotationCompletedAcknowledgement(needAcknowledgement);
1294   mWindowRotationAcknowledgement = needAcknowledgement;
1295 }
1296
1297 void Window::SendRotationCompletedAcknowledgement()
1298 {
1299   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), SendRotationCompletedAcknowledgement(): orientation: %d, mWindowRotationAcknowledgement: %d\n", this, mNativeWindowId, mRotationAngle, mWindowRotationAcknowledgement);
1300   if(mWindowRotationAcknowledgement)
1301   {
1302     SetRotationCompletedAcknowledgement();
1303   }
1304 }
1305
1306 bool Window::IsWindowRotating() const
1307 {
1308   return mIsWindowRotating;
1309 }
1310
1311 const Dali::KeyEvent& Window::GetLastKeyEvent() const
1312 {
1313   return mLastKeyEvent;
1314 }
1315
1316 const Dali::TouchEvent& Window::GetLastTouchEvent() const
1317 {
1318   return mLastTouchEvent;
1319 }
1320
1321 } // namespace Adaptor
1322
1323 } // namespace Internal
1324
1325 } // namespace Dali