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