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