[Tizen] Fix the calculating logic for input region
[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 Dali::Layer Window::GetRootLayer() const
801 {
802   return mScene.GetRootLayer();
803 }
804
805 void Window::SetTransparency(bool transparent)
806 {
807   mWindowSurface->SetTransparency(transparent);
808 }
809
810 bool Window::GrabKey(Dali::KEY key, KeyGrab::KeyGrabMode grabMode)
811 {
812   return mWindowBase->GrabKey(key, grabMode);
813 }
814
815 bool Window::UngrabKey(Dali::KEY key)
816 {
817   return mWindowBase->UngrabKey(key);
818 }
819
820 bool Window::GrabKeyList(const Dali::Vector<Dali::KEY>& key, const Dali::Vector<KeyGrab::KeyGrabMode>& grabMode, Dali::Vector<bool>& result)
821 {
822   return mWindowBase->GrabKeyList(key, grabMode, result);
823 }
824
825 bool Window::UngrabKeyList(const Dali::Vector<Dali::KEY>& key, Dali::Vector<bool>& result)
826 {
827   return mWindowBase->UngrabKeyList(key, result);
828 }
829
830 void Window::OnIconifyChanged(bool iconified)
831 {
832   if(iconified)
833   {
834     mIconified = true;
835
836     if(mVisible)
837     {
838       Dali::Window handle(this);
839       mVisibilityChangedSignal.Emit(handle, false);
840       Dali::Accessibility::Bridge::GetCurrentBridge()->WindowHidden(handle);
841
842       if(DALI_LIKELY(mAdaptor))
843       {
844         WindowVisibilityObserver* observer(mAdaptor);
845         observer->OnWindowHidden();
846       }
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       if(DALI_LIKELY(mAdaptor))
862       {
863         WindowVisibilityObserver* observer(mAdaptor);
864         observer->OnWindowShown();
865       }
866     }
867
868     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Deiconified: visible = %d\n", this, mNativeWindowId, mVisible);
869   }
870
871   mSurface->SetFullSwapNextFrame();
872 }
873
874 void Window::OnFocusChanged(bool focusIn)
875 {
876   Dali::Window handle(this);
877   mFocusChangeSignal.Emit(handle, focusIn);
878
879   mSurface->SetFullSwapNextFrame();
880
881   if(auto bridge = Dali::Accessibility::Bridge::GetCurrentBridge())
882   {
883     if(focusIn)
884     {
885       bridge->WindowFocused(handle);
886     }
887     else
888     {
889       bridge->WindowUnfocused(handle);
890     }
891   }
892   mFocused = focusIn;
893 }
894
895 void Window::OnOutputTransformed()
896 {
897   PositionSize positionSize = GetPositionSize();
898
899   SurfaceRotated(static_cast<float>(positionSize.width), static_cast<float>(positionSize.height), mRotationAngle, mWindowBase->GetScreenRotationAngle());
900
901   if(DALI_LIKELY(mAdaptor))
902   {
903     mAdaptor->SurfaceResizePrepare(mSurface.get(), Adaptor::SurfaceSize(positionSize.width, positionSize.height));
904     mAdaptor->SurfaceResizeComplete(mSurface.get(), Adaptor::SurfaceSize(positionSize.width, positionSize.height));
905   }
906 }
907
908 void Window::OnDeleteRequest()
909 {
910   mDeleteRequestSignal.Emit();
911 }
912
913 void Window::OnTransitionEffectEvent(WindowEffectState state, WindowEffectType type)
914 {
915   Dali::Window handle(this);
916   mTransitionEffectEventSignal.Emit(handle, state, type);
917 }
918
919 void Window::OnKeyboardRepeatSettingsChanged()
920 {
921   Dali::Window handle(this);
922   mKeyboardRepeatSettingsChangedSignal.Emit();
923 }
924
925 void Window::OnWindowRedrawRequest()
926 {
927   if(DALI_LIKELY(mAdaptor))
928   {
929     mAdaptor->RenderOnce();
930   }
931 }
932
933 void Window::OnUpdatePositionSize(Dali::PositionSize& positionSize)
934 {
935   bool moved  = false;
936   bool resize = false;
937
938   Dali::Window handle(this);
939
940   PositionSize oldRect = GetPositionSize();
941   PositionSize newRect = positionSize;
942
943   if((oldRect.x != newRect.x) || (oldRect.y != newRect.y))
944   {
945     moved = true;
946   }
947
948   if((oldRect.width != newRect.width) || (oldRect.height != newRect.height))
949   {
950     resize = true;
951   }
952
953   if(moved || resize)
954   {
955     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);
956     mWindowSurface->UpdatePositionSize(positionSize);
957   }
958
959   if((oldRect.x != newRect.x) || (oldRect.y != newRect.y))
960   {
961     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Moved signal emit (%d, %d)\n", this, mNativeWindowId, newRect.x, newRect.y);
962     Dali::Window::WindowPosition position(newRect.x, newRect.y);
963     mMovedSignal.Emit(handle, position);
964   }
965
966   // When surface size is updated, inform adaptor of resizing and emit ResizeSignal
967   if((oldRect.width != newRect.width) || (oldRect.height != newRect.height))
968   {
969     Uint16Pair newSize(newRect.width, newRect.height);
970
971     mWindowWidth  = newRect.width;
972     mWindowHeight = newRect.height;
973
974     SurfaceResized(static_cast<float>(mWindowWidth), static_cast<float>(mWindowHeight));
975
976     if(DALI_LIKELY(mAdaptor))
977     {
978       mAdaptor->SurfaceResizePrepare(mSurface.get(), newSize);
979     }
980
981     DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), Resized signal emit [%d x %d]\n", this, mNativeWindowId, newRect.width, newRect.height);
982     mResizeSignal.Emit(handle, newSize);
983     if(DALI_LIKELY(mAdaptor))
984     {
985       mAdaptor->SurfaceResizeComplete(mSurface.get(), newSize);
986     }
987   }
988
989   mSurface->SetFullSwapNextFrame();
990
991   if(DALI_LIKELY(mScene))
992   {
993     Dali::Accessibility::Accessible::Get(mScene.GetRootLayer())->EmitBoundsChanged(Dali::Rect<>(positionSize.x, positionSize.y, positionSize.width, positionSize.height));
994   }
995 }
996
997 void Window::OnTouchPoint(Dali::Integration::Point& point, int timeStamp)
998 {
999   mLastTouchEvent = Dali::Integration::NewTouchEvent(timeStamp, point);
1000   FeedTouchPoint(point, timeStamp);
1001 }
1002
1003 void Window::OnWheelEvent(Dali::Integration::WheelEvent& wheelEvent)
1004 {
1005   FeedWheelEvent(wheelEvent);
1006 }
1007
1008 void Window::OnKeyEvent(Dali::Integration::KeyEvent& keyEvent)
1009 {
1010   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);
1011   FeedKeyEvent(keyEvent);
1012 }
1013
1014 void Window::OnRotation(const RotationEvent& rotation)
1015 {
1016   PositionSize newPositionSize(rotation.x, rotation.y, rotation.width, rotation.height);
1017
1018   mRotationAngle = rotation.angle;
1019   mWindowWidth   = rotation.width;
1020   mWindowHeight  = rotation.height;
1021
1022   mIsWindowRotating = true;
1023   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);
1024
1025   // Notify that the orientation is changed
1026   mOrientation->OnOrientationChange(rotation);
1027
1028   mWindowSurface->RequestRotation(mRotationAngle, newPositionSize);
1029
1030   SurfaceRotated(static_cast<float>(mWindowWidth), static_cast<float>(mWindowHeight), mRotationAngle, mWindowBase->GetScreenRotationAngle());
1031
1032   if(DALI_LIKELY(mAdaptor))
1033   {
1034     mAdaptor->SurfaceResizePrepare(mSurface.get(), Adaptor::SurfaceSize(mWindowWidth, mWindowHeight));
1035   }
1036
1037   Dali::Window handle(this);
1038   mResizeSignal.Emit(handle, Dali::Window::WindowSize(mWindowWidth, mWindowHeight));
1039   mOrientationChangedSignal.Emit(handle, GetCurrentOrientation());
1040
1041   if(DALI_LIKELY(mAdaptor))
1042   {
1043     mAdaptor->SurfaceResizeComplete(mSurface.get(), Adaptor::SurfaceSize(mWindowWidth, mWindowHeight));
1044   }
1045 }
1046
1047 void Window::OnRotationFinished()
1048 {
1049   mIsWindowRotating = false;
1050   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), window rotation is finised\n", this, mNativeWindowId);
1051 }
1052
1053 void Window::OnPause()
1054 {
1055   if(mEventHandler)
1056   {
1057     mEventHandler->Pause();
1058   }
1059 }
1060
1061 void Window::OnResume()
1062 {
1063   if(mEventHandler)
1064   {
1065     mEventHandler->Resume();
1066   }
1067
1068   mSurface->SetFullSwapNextFrame();
1069 }
1070
1071 void Window::OnAuxiliaryMessage(const std::string& key, const std::string& value, const Property::Array& options)
1072 {
1073   mAuxiliaryMessageSignal.Emit(key, value, options);
1074 }
1075
1076 void Window::OnAccessibilityEnabled()
1077 {
1078   auto bridge     = Accessibility::Bridge::GetCurrentBridge();
1079   auto rootLayer  = mScene.GetRootLayer();
1080   auto accessible = Accessibility::Accessible::Get(rootLayer);
1081   bridge->AddTopLevelWindow(accessible);
1082
1083   if(!mVisible || mIconified)
1084   {
1085     return;
1086   }
1087
1088   Dali::Window handle(this);
1089   bridge->WindowShown(handle);
1090
1091   if(mFocused)
1092   {
1093     bridge->WindowFocused(handle);
1094   }
1095 }
1096
1097 void Window::OnAccessibilityDisabled()
1098 {
1099   auto bridge     = Accessibility::Bridge::GetCurrentBridge();
1100   auto rootLayer  = mScene.GetRootLayer();
1101   auto accessible = Accessibility::Accessible::Get(rootLayer);
1102   bridge->RemoveTopLevelWindow(accessible);
1103 }
1104
1105 Vector2 Window::RecalculatePosition(const Vector2& position)
1106 {
1107   Vector2 convertedPosition;
1108
1109   switch(mRotationAngle)
1110   {
1111     case 90:
1112     {
1113       convertedPosition.x = static_cast<float>(mWindowWidth) - position.y;
1114       convertedPosition.y = position.x;
1115       break;
1116     }
1117     case 180:
1118     {
1119       convertedPosition.x = static_cast<float>(mWindowWidth) - position.x;
1120       convertedPosition.y = static_cast<float>(mWindowHeight) - position.y;
1121       break;
1122     }
1123     case 270:
1124     {
1125       convertedPosition.x = position.y;
1126       convertedPosition.y = static_cast<float>(mWindowHeight) - position.x;
1127       break;
1128     }
1129     default:
1130     {
1131       convertedPosition = position;
1132       break;
1133     }
1134   }
1135   return convertedPosition;
1136 }
1137
1138 Dali::Window Window::Get(Dali::Actor actor)
1139 {
1140   Internal::Adaptor::Window* windowImpl = nullptr;
1141
1142   if(Internal::Adaptor::Adaptor::IsAvailable())
1143   {
1144     Dali::Internal::Adaptor::Adaptor& adaptor = Internal::Adaptor::Adaptor::GetImplementation(Internal::Adaptor::Adaptor::Get());
1145     windowImpl                                = dynamic_cast<Internal::Adaptor::Window*>(adaptor.GetWindow(actor));
1146     if(windowImpl)
1147     {
1148       return Dali::Window(windowImpl);
1149     }
1150   }
1151
1152   return Dali::Window();
1153 }
1154
1155 void Window::SetParent(Dali::Window& parent)
1156 {
1157   if(DALI_UNLIKELY(parent))
1158   {
1159     mParentWindow     = parent;
1160     Dali::Window self = Dali::Window(this);
1161     // check circular parent window setting
1162     if(Dali::DevelWindow::GetParent(parent) == self)
1163     {
1164       Dali::DevelWindow::Unparent(parent);
1165     }
1166     mWindowBase->SetParent(GetImplementation(mParentWindow).mWindowBase, false);
1167   }
1168 }
1169
1170 void Window::SetParent(Dali::Window& parent, bool belowParent)
1171 {
1172   if(DALI_UNLIKELY(parent))
1173   {
1174     mParentWindow     = parent;
1175     Dali::Window self = Dali::Window(this);
1176     // check circular parent window setting
1177     if(Dali::DevelWindow::GetParent(parent) == self)
1178     {
1179       Dali::DevelWindow::Unparent(parent);
1180     }
1181     mWindowBase->SetParent(GetImplementation(mParentWindow).mWindowBase, belowParent);
1182   }
1183 }
1184
1185 void Window::Unparent()
1186 {
1187   mWindowBase->SetParent(nullptr, false);
1188   mParentWindow.Reset();
1189 }
1190
1191 Dali::Window Window::GetParent()
1192 {
1193   return mParentWindow;
1194 }
1195
1196 WindowOrientation Window::GetCurrentOrientation() const
1197 {
1198   return ConvertToOrientation(mRotationAngle);
1199 }
1200
1201 int Window::GetPhysicalOrientation() const
1202 {
1203   return (mRotationAngle + mWindowBase->GetScreenRotationAngle()) % 360;
1204 }
1205
1206 void Window::SetAvailableOrientations(const Dali::Vector<WindowOrientation>& orientations)
1207 {
1208   Dali::Vector<float>::SizeType count = orientations.Count();
1209   for(Dali::Vector<float>::SizeType index = 0; index < count; ++index)
1210   {
1211     if(IsOrientationAvailable(orientations[index]) == false)
1212     {
1213       DALI_LOG_ERROR("Window::SetAvailableOrientations, invalid orientation: %d\n", orientations[index]);
1214       continue;
1215     }
1216
1217     bool found          = false;
1218     int  convertedAngle = ConvertToAngle(orientations[index]);
1219
1220     for(std::size_t i = 0; i < mAvailableAngles.size(); i++)
1221     {
1222       if(mAvailableAngles[i] == convertedAngle)
1223       {
1224         found = true;
1225         break;
1226       }
1227     }
1228
1229     if(!found)
1230     {
1231       DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), SetAvailableOrientations: %d\n", this, mNativeWindowId, convertedAngle);
1232       mAvailableAngles.push_back(convertedAngle);
1233     }
1234   }
1235   SetAvailableAnlges(mAvailableAngles);
1236 }
1237
1238 int32_t Window::GetNativeId() const
1239 {
1240   return mWindowBase->GetNativeWindowId();
1241 }
1242
1243 void Window::RequestMoveToServer()
1244 {
1245   mWindowBase->RequestMoveToServer();
1246 }
1247
1248 void Window::RequestResizeToServer(WindowResizeDirection direction)
1249 {
1250   mWindowBase->RequestResizeToServer(direction);
1251 }
1252
1253 void Window::EnableFloatingMode(bool enable)
1254 {
1255   mWindowBase->EnableFloatingMode(enable);
1256 }
1257
1258 void Window::IncludeInputRegion(const Rect<int>& inputRegion)
1259 {
1260   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);
1261   mWindowBase->IncludeInputRegion(inputRegion);
1262 }
1263
1264 void Window::ExcludeInputRegion(const Rect<int>& inputRegion)
1265 {
1266   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);
1267   mWindowBase->ExcludeInputRegion(inputRegion);
1268 }
1269
1270 void Window::SetNeedsRotationCompletedAcknowledgement(bool needAcknowledgement)
1271 {
1272   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), needAcknowledgement(%d) Set needs Rotation Completed Acknowledgement\n", this, mNativeWindowId, needAcknowledgement);
1273   mWindowSurface->SetNeedsRotationCompletedAcknowledgement(needAcknowledgement);
1274   mWindowRotationAcknowledgement = needAcknowledgement;
1275 }
1276
1277 void Window::SendRotationCompletedAcknowledgement()
1278 {
1279   DALI_LOG_RELEASE_INFO("Window (%p), WinId (%d), SendRotationCompletedAcknowledgement(): orientation: %d, mWindowRotationAcknowledgement: %d\n", this, mNativeWindowId, mRotationAngle, mWindowRotationAcknowledgement);
1280   if(mWindowRotationAcknowledgement)
1281   {
1282     SetRotationCompletedAcknowledgement();
1283   }
1284 }
1285
1286 bool Window::IsWindowRotating() const
1287 {
1288   return mIsWindowRotating;
1289 }
1290
1291 const Dali::KeyEvent& Window::GetLastKeyEvent() const
1292 {
1293   return mLastKeyEvent;
1294 }
1295
1296 const Dali::TouchEvent& Window::GetLastTouchEvent() const
1297 {
1298   return mLastTouchEvent;
1299 }
1300
1301 } // namespace Adaptor
1302
1303 } // namespace Internal
1304
1305 } // namespace Dali