f34d740ee4a9f76c665ec6c2591e5f991d754de6
[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(DALI_LIKELY(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   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), SetInputRegion, (%d,%d), (%d x %d)\n", this, mNativeWindowId, inputRegion.x, inputRegion.y, inputRegion.width, inputRegion.height);
593   mWindowBase->SetInputRegion(inputRegion);
594 }
595
596 void Window::SetType(WindowType type)
597 {
598   mWindowBase->SetType(type);
599 }
600
601 WindowType Window::GetType() const
602 {
603   return mWindowBase->GetType();
604 }
605
606 WindowOperationResult Window::SetNotificationLevel(WindowNotificationLevel level)
607 {
608   WindowType type = mWindowBase->GetType();
609   if(type != WindowType::NOTIFICATION)
610   {
611     DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window::SetNotificationLevel: Not supported window type [%d]\n", type);
612     return WindowOperationResult::INVALID_OPERATION;
613   }
614
615   return mWindowBase->SetNotificationLevel(level);
616 }
617
618 WindowNotificationLevel Window::GetNotificationLevel() const
619 {
620   WindowType type = mWindowBase->GetType();
621   if(type != WindowType::NOTIFICATION)
622   {
623     DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window::GetNotificationLevel: Not supported window type [%d]\n", type);
624     return WindowNotificationLevel::NONE;
625   }
626
627   return mWindowBase->GetNotificationLevel();
628 }
629
630 void Window::SetOpaqueState(bool opaque)
631 {
632   mOpaqueState = opaque;
633
634   mWindowBase->SetOpaqueState(opaque);
635
636   DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window::SetOpaqueState: opaque = %d\n", opaque);
637 }
638
639 bool Window::IsOpaqueState() const
640 {
641   return mOpaqueState;
642 }
643
644 WindowOperationResult Window::SetScreenOffMode(WindowScreenOffMode screenOffMode)
645 {
646   return mWindowBase->SetScreenOffMode(screenOffMode);
647 }
648
649 WindowScreenOffMode Window::GetScreenOffMode() const
650 {
651   return mWindowBase->GetScreenOffMode();
652 }
653
654 WindowOperationResult Window::SetBrightness(int brightness)
655 {
656   if(brightness < 0 || brightness > 100)
657   {
658     DALI_LOG_INFO(gWindowLogFilter, Debug::Verbose, "Window::SetBrightness: Invalid brightness value [%d]\n", brightness);
659     return WindowOperationResult::INVALID_OPERATION;
660   }
661
662   return mWindowBase->SetBrightness(brightness);
663 }
664
665 int Window::GetBrightness() const
666 {
667   return mWindowBase->GetBrightness();
668 }
669
670 void Window::SetSize(Dali::Window::WindowSize size)
671 {
672   PositionSize oldRect = GetPositionSize();
673
674   PositionSize newRect;
675   newRect.width  = size.GetWidth();
676   newRect.height = size.GetHeight();
677
678   // When surface size is updated, inform adaptor of resizing and emit ResizeSignal
679   if((oldRect.width != newRect.width) || (oldRect.height != newRect.height))
680   {
681     mWindowSurface->MoveResize(PositionSize(oldRect.x, oldRect.y, newRect.width, newRect.height));
682
683     Uint16Pair newSize(newRect.width, newRect.height);
684
685     mWindowWidth  = newRect.width;
686     mWindowHeight = newRect.height;
687
688     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);
689
690     SurfaceResized(static_cast<float>(mWindowWidth), static_cast<float>(mWindowHeight));
691
692     mAdaptor->SurfaceResizePrepare(mSurface.get(), newSize);
693
694     Dali::Window handle(this);
695     mResizeSignal.Emit(handle, newSize);
696
697     mAdaptor->SurfaceResizeComplete(mSurface.get(), newSize);
698   }
699
700   mSurface->SetFullSwapNextFrame();
701
702   Dali::Accessibility::Accessible::Get(mScene.GetRootLayer())->EmitBoundsChanged(Dali::Rect<>(oldRect.x, oldRect.y, size.GetWidth(), size.GetHeight()));
703 }
704
705 Dali::Window::WindowSize Window::GetSize() const
706 {
707   return Dali::Window::WindowSize(mWindowWidth, mWindowHeight);
708 }
709
710 void Window::SetPosition(Dali::Window::WindowPosition position)
711 {
712   PositionSize oldRect = mSurface->GetPositionSize();
713   int32_t      newX    = position.GetX();
714   int32_t      newY    = position.GetY();
715
716   mWindowSurface->MoveResize(PositionSize(newX, newY, oldRect.width, oldRect.height));
717
718   if((oldRect.x != newX) || (oldRect.y != newY))
719   {
720     Dali::Window                 handle(this);
721     Dali::Window::WindowPosition newPosition(newX, newY);
722
723     DALI_LOG_RELEASE_INFO("send moved signal with new position: %d, %d\n", newPosition.GetX(), newPosition.GetY());
724     mMovedSignal.Emit(handle, newPosition);
725   }
726
727   mSurface->SetFullSwapNextFrame();
728
729   Dali::Accessibility::Accessible::Get(mScene.GetRootLayer())->EmitBoundsChanged(Dali::Rect<>(position.GetX(), position.GetY(), oldRect.width, oldRect.height));
730 }
731
732 Dali::Window::WindowPosition Window::GetPosition() const
733 {
734   PositionSize positionSize = GetPositionSize();
735   return Dali::Window::WindowPosition(positionSize.x, positionSize.y);
736 }
737
738 PositionSize Window::GetPositionSize() const
739 {
740   PositionSize positionSize = mSurface->GetPositionSize();
741   positionSize.width        = mWindowWidth;
742   positionSize.height       = mWindowHeight;
743   return positionSize;
744 }
745
746 void Window::SetPositionSize(PositionSize positionSize)
747 {
748   bool moved  = false;
749   bool resize = false;
750
751   PositionSize oldRect = GetPositionSize();
752   Dali::Window handle(this);
753
754   if((oldRect.x != positionSize.x) || (oldRect.y != positionSize.y))
755   {
756     moved = true;
757   }
758
759   if((oldRect.width != positionSize.width) || (oldRect.height != positionSize.height))
760   {
761     resize = true;
762   }
763
764   if(moved || resize)
765   {
766     mWindowSurface->MoveResize(positionSize);
767   }
768
769   // When window is moved, emit Moved Signal
770   if(moved)
771   {
772     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Moved signal emit (%d, %d)\n", this, mNativeWindowId, positionSize.x, positionSize.y);
773     Dali::Window::WindowPosition position(positionSize.x, positionSize.y);
774     mMovedSignal.Emit(handle, position);
775   }
776
777   // When surface size is updated, inform adaptor of resizing and emit ResizeSignal
778   if(resize)
779   {
780     Uint16Pair newSize(positionSize.width, positionSize.height);
781
782     mWindowWidth  = positionSize.width;
783     mWindowHeight = positionSize.height;
784
785     SurfaceResized(static_cast<float>(mWindowWidth), static_cast<float>(mWindowHeight));
786
787     mAdaptor->SurfaceResizePrepare(mSurface.get(), newSize);
788
789     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Resize signal emit [%d x %d]\n", this, mNativeWindowId, positionSize.width, positionSize.height);
790
791     mResizeSignal.Emit(handle, newSize);
792     mAdaptor->SurfaceResizeComplete(mSurface.get(), newSize);
793   }
794
795   mSurface->SetFullSwapNextFrame();
796
797   Dali::Accessibility::Accessible::Get(mScene.GetRootLayer())->EmitBoundsChanged(Dali::Rect<>(positionSize.x, positionSize.y, positionSize.width, positionSize.height));
798 }
799
800 void Window::SetLayout(unsigned int numCols, unsigned int numRows, unsigned int column, unsigned int row, unsigned int colSpan, unsigned int rowSpan)
801 {
802     mWindowBase->SetLayout(numCols, numRows, column, row, colSpan, rowSpan);
803 }
804
805 Dali::Layer Window::GetRootLayer() const
806 {
807   return mScene.GetRootLayer();
808 }
809
810 void Window::SetTransparency(bool transparent)
811 {
812   mWindowSurface->SetTransparency(transparent);
813 }
814
815 bool Window::GrabKey(Dali::KEY key, KeyGrab::KeyGrabMode grabMode)
816 {
817   return mWindowBase->GrabKey(key, grabMode);
818 }
819
820 bool Window::UngrabKey(Dali::KEY key)
821 {
822   return mWindowBase->UngrabKey(key);
823 }
824
825 bool Window::GrabKeyList(const Dali::Vector<Dali::KEY>& key, const Dali::Vector<KeyGrab::KeyGrabMode>& grabMode, Dali::Vector<bool>& result)
826 {
827   return mWindowBase->GrabKeyList(key, grabMode, result);
828 }
829
830 bool Window::UngrabKeyList(const Dali::Vector<Dali::KEY>& key, Dali::Vector<bool>& result)
831 {
832   return mWindowBase->UngrabKeyList(key, result);
833 }
834
835 void Window::OnIconifyChanged(bool iconified)
836 {
837   if(iconified)
838   {
839     mIconified = true;
840
841     if(mVisible)
842     {
843       Dali::Window handle(this);
844       mVisibilityChangedSignal.Emit(handle, false);
845       Dali::Accessibility::Bridge::GetCurrentBridge()->WindowHidden(handle);
846
847       if(DALI_LIKELY(mAdaptor))
848       {
849         WindowVisibilityObserver* observer(mAdaptor);
850         observer->OnWindowHidden();
851       }
852     }
853
854     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Iconified: visible = %d\n", this, mNativeWindowId, mVisible);
855   }
856   else
857   {
858     mIconified = false;
859
860     if(mVisible)
861     {
862       Dali::Window handle(this);
863       mVisibilityChangedSignal.Emit(handle, true);
864       Dali::Accessibility::Bridge::GetCurrentBridge()->WindowShown(handle);
865
866       if(DALI_LIKELY(mAdaptor))
867       {
868         WindowVisibilityObserver* observer(mAdaptor);
869         observer->OnWindowShown();
870       }
871     }
872
873     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Deiconified: visible = %d\n", this, mNativeWindowId, mVisible);
874   }
875
876   mSurface->SetFullSwapNextFrame();
877 }
878
879 void Window::OnFocusChanged(bool focusIn)
880 {
881   Dali::Window handle(this);
882   mFocusChangeSignal.Emit(handle, focusIn);
883
884   mSurface->SetFullSwapNextFrame();
885
886   if(auto bridge = Dali::Accessibility::Bridge::GetCurrentBridge())
887   {
888     if(focusIn)
889     {
890       bridge->WindowFocused(handle);
891     }
892     else
893     {
894       bridge->WindowUnfocused(handle);
895     }
896   }
897   mFocused = focusIn;
898 }
899
900 void Window::OnOutputTransformed()
901 {
902   PositionSize positionSize = GetPositionSize();
903
904   SurfaceRotated(static_cast<float>(positionSize.width), static_cast<float>(positionSize.height), mRotationAngle, mWindowBase->GetScreenRotationAngle());
905
906   if(DALI_LIKELY(mAdaptor))
907   {
908     mAdaptor->SurfaceResizePrepare(mSurface.get(), Adaptor::SurfaceSize(positionSize.width, positionSize.height));
909     mAdaptor->SurfaceResizeComplete(mSurface.get(), Adaptor::SurfaceSize(positionSize.width, positionSize.height));
910   }
911 }
912
913 void Window::OnDeleteRequest()
914 {
915   mDeleteRequestSignal.Emit();
916 }
917
918 void Window::OnTransitionEffectEvent(WindowEffectState state, WindowEffectType type)
919 {
920   Dali::Window handle(this);
921   mTransitionEffectEventSignal.Emit(handle, state, type);
922 }
923
924 void Window::OnKeyboardRepeatSettingsChanged()
925 {
926   Dali::Window handle(this);
927   mKeyboardRepeatSettingsChangedSignal.Emit();
928 }
929
930 void Window::OnWindowRedrawRequest()
931 {
932   if(DALI_LIKELY(mAdaptor))
933   {
934     mAdaptor->RenderOnce();
935   }
936 }
937
938 void Window::OnUpdatePositionSize(Dali::PositionSize& positionSize)
939 {
940   bool moved  = false;
941   bool resize = false;
942
943   Dali::Window handle(this);
944
945   PositionSize oldRect = GetPositionSize();
946   PositionSize newRect = positionSize;
947
948   if((oldRect.x != newRect.x) || (oldRect.y != newRect.y))
949   {
950     moved = true;
951   }
952
953   if((oldRect.width != newRect.width) || (oldRect.height != newRect.height))
954   {
955     resize = true;
956   }
957
958   if(moved || resize)
959   {
960     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);
961     mWindowSurface->UpdatePositionSize(positionSize);
962   }
963
964   if((oldRect.x != newRect.x) || (oldRect.y != newRect.y))
965   {
966     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Moved signal emit (%d, %d)\n", this, mNativeWindowId, newRect.x, newRect.y);
967     Dali::Window::WindowPosition position(newRect.x, newRect.y);
968     mMovedSignal.Emit(handle, position);
969   }
970
971   // When surface size is updated, inform adaptor of resizing and emit ResizeSignal
972   if((oldRect.width != newRect.width) || (oldRect.height != newRect.height))
973   {
974     Uint16Pair newSize(newRect.width, newRect.height);
975
976     mWindowWidth  = newRect.width;
977     mWindowHeight = newRect.height;
978
979     SurfaceResized(static_cast<float>(mWindowWidth), static_cast<float>(mWindowHeight));
980
981     if(DALI_LIKELY(mAdaptor))
982     {
983       mAdaptor->SurfaceResizePrepare(mSurface.get(), newSize);
984     }
985
986     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Resized signal emit [%d x %d]\n", this, mNativeWindowId, newRect.width, newRect.height);
987     mResizeSignal.Emit(handle, newSize);
988     if(DALI_LIKELY(mAdaptor))
989     {
990       mAdaptor->SurfaceResizeComplete(mSurface.get(), newSize);
991     }
992   }
993
994   mSurface->SetFullSwapNextFrame();
995
996   if(DALI_LIKELY(mScene))
997   {
998     Dali::Accessibility::Accessible::Get(mScene.GetRootLayer())->EmitBoundsChanged(Dali::Rect<>(positionSize.x, positionSize.y, positionSize.width, positionSize.height));
999   }
1000 }
1001
1002 void Window::OnTouchPoint(Dali::Integration::Point& point, int timeStamp)
1003 {
1004   mLastTouchEvent = Dali::Integration::NewTouchEvent(timeStamp, point);
1005   FeedTouchPoint(point, timeStamp);
1006 }
1007
1008 void Window::OnWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
1009 {
1010   FeedWheelEvent(wheelEvent);
1011 }
1012
1013 void Window::OnKeyEvent(Dali::Integration::KeyEvent& keyEvent)
1014 {
1015   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);
1016   FeedKeyEvent(keyEvent);
1017 }
1018
1019 void Window::OnRotation(const RotationEvent& rotation)
1020 {
1021   PositionSize newPositionSize(rotation.x, rotation.y, rotation.width, rotation.height);
1022
1023   mRotationAngle = rotation.angle;
1024   mWindowWidth   = rotation.width;
1025   mWindowHeight  = rotation.height;
1026
1027   mIsWindowRotating = true;
1028   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);
1029
1030   // Notify that the orientation is changed
1031   mOrientation->OnOrientationChange(rotation);
1032
1033   mWindowSurface->RequestRotation(mRotationAngle, newPositionSize);
1034
1035   SurfaceRotated(static_cast<float>(mWindowWidth), static_cast<float>(mWindowHeight), mRotationAngle, mWindowBase->GetScreenRotationAngle());
1036
1037   if(DALI_LIKELY(mAdaptor))
1038   {
1039     mAdaptor->SurfaceResizePrepare(mSurface.get(), Adaptor::SurfaceSize(mWindowWidth, mWindowHeight));
1040   }
1041
1042   Dali::Window handle(this);
1043   mResizeSignal.Emit(handle, Dali::Window::WindowSize(mWindowWidth, mWindowHeight));
1044   mOrientationChangedSignal.Emit(handle, GetCurrentOrientation());
1045
1046   if(DALI_LIKELY(mAdaptor))
1047   {
1048     mAdaptor->SurfaceResizeComplete(mSurface.get(), Adaptor::SurfaceSize(mWindowWidth, mWindowHeight));
1049   }
1050 }
1051
1052 void Window::OnRotationFinished()
1053 {
1054   mIsWindowRotating = false;
1055   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), window rotation is finised\n", this, mNativeWindowId);
1056 }
1057
1058 void Window::OnPause()
1059 {
1060   if(mEventHandler)
1061   {
1062     mEventHandler->Pause();
1063   }
1064 }
1065
1066 void Window::OnResume()
1067 {
1068   if(mEventHandler)
1069   {
1070     mEventHandler->Resume();
1071   }
1072
1073   mSurface->SetFullSwapNextFrame();
1074 }
1075
1076 void Window::OnAuxiliaryMessage(const std::string& key, const std::string& value, const Property::Array& options)
1077 {
1078   mAuxiliaryMessageSignal.Emit(key, value, options);
1079 }
1080
1081 void Window::OnAccessibilityEnabled()
1082 {
1083   auto bridge     = Accessibility::Bridge::GetCurrentBridge();
1084   auto rootLayer  = mScene.GetRootLayer();
1085   auto accessible = Accessibility::Accessible::Get(rootLayer);
1086   bridge->AddTopLevelWindow(accessible);
1087
1088   if(!mVisible || mIconified)
1089   {
1090     return;
1091   }
1092
1093   Dali::Window handle(this);
1094   bridge->WindowShown(handle);
1095
1096   if(mFocused)
1097   {
1098     bridge->WindowFocused(handle);
1099   }
1100 }
1101
1102 void Window::OnAccessibilityDisabled()
1103 {
1104   auto bridge     = Accessibility::Bridge::GetCurrentBridge();
1105   auto rootLayer  = mScene.GetRootLayer();
1106   auto accessible = Accessibility::Accessible::Get(rootLayer);
1107   bridge->RemoveTopLevelWindow(accessible);
1108 }
1109
1110 Vector2 Window::RecalculatePosition(const Vector2& position)
1111 {
1112   Vector2 convertedPosition;
1113
1114   switch(mRotationAngle)
1115   {
1116     case 90:
1117     {
1118       convertedPosition.x = static_cast<float>(mWindowWidth) - position.y;
1119       convertedPosition.y = position.x;
1120       break;
1121     }
1122     case 180:
1123     {
1124       convertedPosition.x = static_cast<float>(mWindowWidth) - position.x;
1125       convertedPosition.y = static_cast<float>(mWindowHeight) - position.y;
1126       break;
1127     }
1128     case 270:
1129     {
1130       convertedPosition.x = position.y;
1131       convertedPosition.y = static_cast<float>(mWindowHeight) - position.x;
1132       break;
1133     }
1134     default:
1135     {
1136       convertedPosition = position;
1137       break;
1138     }
1139   }
1140   return convertedPosition;
1141 }
1142
1143 Dali::Window Window::Get(Dali::Actor actor)
1144 {
1145   Internal::Adaptor::Window* windowImpl = nullptr;
1146
1147   if(Internal::Adaptor::Adaptor::IsAvailable())
1148   {
1149     Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
1150     windowImpl                                = dynamic_cast<Internal::Adaptor::Window*>(adaptor.GetWindow(actor));
1151     if(windowImpl)
1152     {
1153       return Dali::Window(windowImpl);
1154     }
1155   }
1156
1157   return Dali::Window();
1158 }
1159
1160 void Window::SetParent(Dali::Window& parent)
1161 {
1162   if(DALI_UNLIKELY(parent))
1163   {
1164     mParentWindow     = parent;
1165     Dali::Window self = Dali::Window(this);
1166     // check circular parent window setting
1167     if(Dali::DevelWindow::GetParent(parent) == self)
1168     {
1169       Dali::DevelWindow::Unparent(parent);
1170     }
1171     mWindowBase->SetParent(GetImplementation(mParentWindow).mWindowBase, false);
1172   }
1173 }
1174
1175 void Window::SetParent(Dali::Window& parent, bool belowParent)
1176 {
1177   if(DALI_UNLIKELY(parent))
1178   {
1179     mParentWindow     = parent;
1180     Dali::Window self = Dali::Window(this);
1181     // check circular parent window setting
1182     if(Dali::DevelWindow::GetParent(parent) == self)
1183     {
1184       Dali::DevelWindow::Unparent(parent);
1185     }
1186     mWindowBase->SetParent(GetImplementation(mParentWindow).mWindowBase, belowParent);
1187   }
1188 }
1189
1190 void Window::Unparent()
1191 {
1192   mWindowBase->SetParent(nullptr, false);
1193   mParentWindow.Reset();
1194 }
1195
1196 Dali::Window Window::GetParent()
1197 {
1198   return mParentWindow;
1199 }
1200
1201 WindowOrientation Window::GetCurrentOrientation() const
1202 {
1203   return ConvertToOrientation(mRotationAngle);
1204 }
1205
1206 int Window::GetPhysicalOrientation() const
1207 {
1208   return (mRotationAngle + mWindowBase->GetScreenRotationAngle()) % 360;
1209 }
1210
1211 void Window::SetAvailableOrientations(const Dali::Vector<WindowOrientation>& orientations)
1212 {
1213   Dali::Vector<float>::SizeType count = orientations.Count();
1214   for(Dali::Vector<float>::SizeType index = 0; index < count; ++index)
1215   {
1216     if(IsOrientationAvailable(orientations[index]) == false)
1217     {
1218       DALI_LOG_ERROR("Window::SetAvailableOrientations, invalid orientation: %d\n", orientations[index]);
1219       continue;
1220     }
1221
1222     bool found          = false;
1223     int  convertedAngle = ConvertToAngle(orientations[index]);
1224
1225     for(std::size_t i = 0; i < mAvailableAngles.size(); i++)
1226     {
1227       if(mAvailableAngles[i] == convertedAngle)
1228       {
1229         found = true;
1230         break;
1231       }
1232     }
1233
1234     if(!found)
1235     {
1236       DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), SetAvailableOrientations: %d\n", this, mNativeWindowId, convertedAngle);
1237       mAvailableAngles.push_back(convertedAngle);
1238     }
1239   }
1240   SetAvailableAnlges(mAvailableAngles);
1241 }
1242
1243 int32_t Window::GetNativeId() const
1244 {
1245   return mWindowBase->GetNativeWindowId();
1246 }
1247
1248 void Window::RequestMoveToServer()
1249 {
1250   mWindowBase->RequestMoveToServer();
1251 }
1252
1253 void Window::RequestResizeToServer(WindowResizeDirection direction)
1254 {
1255   mWindowBase->RequestResizeToServer(direction);
1256 }
1257
1258 void Window::EnableFloatingMode(bool enable)
1259 {
1260   mWindowBase->EnableFloatingMode(enable);
1261 }
1262
1263 void Window::IncludeInputRegion(const Rect<int>& inputRegion)
1264 {
1265   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), IncludeInputRegion, (%d,%d), (%d x %d)\n", this, mNativeWindowId, inputRegion.x, inputRegion.y, inputRegion.width, inputRegion.height);
1266   mWindowBase->IncludeInputRegion(inputRegion);
1267 }
1268
1269 void Window::ExcludeInputRegion(const Rect<int>& inputRegion)
1270 {
1271   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), ExcludeInputRegion, (%d,%d), (%d x %d)\n", this, mNativeWindowId, inputRegion.x, inputRegion.y, inputRegion.width, inputRegion.height);
1272   mWindowBase->ExcludeInputRegion(inputRegion);
1273 }
1274
1275 void Window::SetNeedsRotationCompletedAcknowledgement(bool needAcknowledgement)
1276 {
1277   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), needAcknowledgement(%d) Set needs Rotation Completed Acknowledgement\n", this, mNativeWindowId, needAcknowledgement);
1278   mWindowSurface->SetNeedsRotationCompletedAcknowledgement(needAcknowledgement);
1279   mWindowRotationAcknowledgement = needAcknowledgement;
1280 }
1281
1282 void Window::SendRotationCompletedAcknowledgement()
1283 {
1284   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), SendRotationCompletedAcknowledgement(): orientation: %d, mWindowRotationAcknowledgement: %d\n", this, mNativeWindowId, mRotationAngle, mWindowRotationAcknowledgement);
1285   if(mWindowRotationAcknowledgement)
1286   {
1287     SetRotationCompletedAcknowledgement();
1288   }
1289 }
1290
1291 bool Window::IsWindowRotating() const
1292 {
1293   return mIsWindowRotating;
1294 }
1295
1296 const Dali::KeyEvent& Window::GetLastKeyEvent() const
1297 {
1298   return mLastKeyEvent;
1299 }
1300
1301 const Dali::TouchEvent& Window::GetLastTouchEvent() const
1302 {
1303   return mLastTouchEvent;
1304 }
1305
1306 } // namespace Adaptor
1307
1308 } // namespace Internal
1309
1310 } // namespace Dali