2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://floralicense.org/license/
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FUi_OrientationAgent.cpp
20 * @brief This is the implementation file for the _OrientationAgent class.
23 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
26 #include <FBaseColArrayList.h>
27 #include "FUi_OrientationAgent.h"
28 #include "FUi_ControlManager.h"
29 #include "FUi_ControlImplManager.h"
30 #include "FUi_PublicOrientationEvent.h"
31 #include "FUi_ControlImpl.h"
32 #include "FUi_Window.h"
33 #include "FUiCtrl_FormImpl.h"
34 #include "FUiCtrl_FrameImpl.h"
35 #include "FUiCtrl_Frame.h"
36 #include "FUiCtrl_Form.h"
37 #include "FUi_EcoreEvasMgr.h"
38 #include "FUi_EcoreEvas.h"
39 #include "FUi_ControlManager.h"
40 #include "FUi_UiNotificationEvent.h"
41 #include "FUi_UiEventManager.h"
44 using namespace Tizen::Base;
45 using namespace Tizen::Base::Collection;
46 using namespace Tizen::Base::Runtime;
47 using namespace Tizen::Ui::Controls;
48 using namespace Tizen::Graphics;
50 namespace Tizen { namespace Ui {
52 const String _REQUEST_ORIENTATION_EVENT = L"RequestOrientationEvent";
55 _OrientationAgent::CreateInstanceN(Control& publicControl)
57 _OrientationAgent* pAgent = new (std::nothrow) _OrientationAgent(publicControl);
58 SysTryReturn(NID_UI, pAgent, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
60 result r = GetLastResult();
61 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
63 SetLastResult(E_SUCCESS);
72 _OrientationAgent::_OrientationAgent(Control& publicControl)
73 : __publicControl(publicControl)
74 , __pPublicEvent(null)
75 , __mode(ORIENTATION_PORTRAIT)
76 , __status(ORIENTATION_STATUS_PORTRAIT)
77 , __tempStatus(ORIENTATION_STATUS_PORTRAIT)
78 , __firePublicEvent(false)
79 , __statusChanged(false)
80 , __updateStatus(true)
81 #if defined(WINDOW_BASE_ROTATE)
85 _PublicOrientationEvent* pPublicEvent = _PublicOrientationEvent::CreateInstanceN(publicControl);
87 result r = GetLastResult();
88 SysTryReturnVoidResult(NID_UI, pPublicEvent, r, "[%s] Propagating.", GetErrorMessage(r));
90 __pPublicEvent = pPublicEvent;
92 SetLastResult(E_SUCCESS);
95 _OrientationAgent::~_OrientationAgent(void)
99 delete __pPublicEvent;
100 __pPublicEvent = null;
105 _OrientationAgent::AddListener(IOrientationEventListener& listener)
107 result r = __pPublicEvent->AddListener(listener);
108 SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
110 SetLastResult(E_SUCCESS);
114 _OrientationAgent::RemoveListener(IOrientationEventListener& listener)
116 result r = __pPublicEvent->RemoveListener(listener);
117 SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
119 SetLastResult(E_SUCCESS);
123 _OrientationAgent::GetMode(void) const
129 _OrientationAgent::GetStatus(void) const
135 _OrientationAgent::SetMode(Orientation mode)
137 SysTryReturnVoidResult(NID_UI, mode != ORIENTATION_NONE, E_INVALID_ARG, "[E_INVALID_ARG] A specified input parameter is invalid.");
139 SetLastResult(E_SUCCESS);
151 _OrientationAgent::Update(bool draw)
153 #if defined(WINDOW_BASE_ROTATE)
154 // Update orientation status(Not Auto-mode)
155 // Request rotation to window manager -> async -> Update VEs
156 // Window(not rotation) -> sync -> UpdateVEs
158 _ControlImplManager* pImplManager = _ControlImplManager::GetInstance();
159 SysAssert(pImplManager);
161 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
167 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
173 bool autoMode = false;
175 if ((__mode == ORIENTATION_AUTOMATIC) || (__mode == ORIENTATION_AUTOMATIC_FOUR_DIRECTION))
180 // if (autoMode == false)
183 // Update orientation status
185 // Fire orientation event
187 // Set preffered rotation
189 OrientationStatus status = pImplManager->GetOrientationStatus(__mode);
191 if (__updateStatus == true)
193 __statusChanged = false;
194 if (__status != status)
196 __statusChanged = true;
197 __updateStatus = false;
203 pEcoreEvas->AllowSetWindowBounds(false);
205 pEcoreEvas->AllowSetWindowBounds(true);
207 // For the form to be made by Ui-Builder
208 if ((draw == true) && (__statusChanged == true))
210 _ControlOrientation coreOrientation =
211 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
212 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
215 bool exist = pImpl->GetBuilderBounds(coreOrientation, temp);
218 pImpl->Invalidate(true);
222 // Despite not changing status, it needs to rotate screen.
223 _ControlImpl* pControlImpl = _ControlImpl::GetInstance(__publicControl);
224 pImplManager->RotateScreen(pControlImpl, status);
226 _Window* pRootWindow = pImpl->GetCore().GetRootWindow();
229 SetRotation(*pRootWindow, __mode);
235 // Set preffered rotation
237 // _Window* pRootWindow = pImpl->GetCore().GetRootWindow();
240 // SetRotation(*pRootWindow, __mode);
246 _ControlImplManager* pImplManager = _ControlImplManager::GetInstance();
247 SysAssert(pImplManager);
249 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
255 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
261 _Form* pForm = dynamic_cast<_Form*>(&(pImpl->GetCore()));
264 _Control* pParent = pForm->GetParent();
268 int index = pParent->GetChildIndex(*pForm);
269 int childCount = pParent->GetChildCount();
271 if (index == (childCount - 1))
273 _Window* pWindow = pForm->GetRootWindow();
276 if ((__mode == ORIENTATION_AUTOMATIC) || (__mode == ORIENTATION_AUTOMATIC_FOUR_DIRECTION))
278 pEcoreEvas->SetWindowOrientationEnabled(*pWindow, true);
279 pWindow->SetOrientationEnabled(true);
283 pEcoreEvas->SetWindowOrientationEnabled(*pWindow, false);
284 pWindow->SetOrientationEnabled(false);
292 _Frame* pFrame = dynamic_cast<_Frame*>(&(pImpl->GetCore()));
295 int childCount = pFrame->GetChildCount();
298 _Window* pCurrentFrame = _ControlManager::GetInstance()->GetCurrentFrame();
299 if (pCurrentFrame == pFrame)
301 if ((__mode == ORIENTATION_AUTOMATIC) || (__mode == ORIENTATION_AUTOMATIC_FOUR_DIRECTION))
303 pEcoreEvas->SetWindowOrientationEnabled(*pFrame, true);
304 pFrame->SetOrientationEnabled(true);
308 pEcoreEvas->SetWindowOrientationEnabled(*pFrame, false);
309 pFrame->SetOrientationEnabled(false);
316 OrientationStatus status = pImplManager->GetOrientationStatus(__mode);
318 if (__updateStatus == true)
320 __statusChanged = false;
321 if (__status != status)
323 __statusChanged = true;
324 __updateStatus = false;
330 pEcoreEvas->AllowSetWindowBounds(false);
332 pEcoreEvas->AllowSetWindowBounds(true);
334 // For the form to be made by Ui-Builder
335 if ((draw == true) && (__statusChanged == true))
337 _ControlOrientation coreOrientation =
338 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
339 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
342 bool exist = pImpl->GetBuilderBounds(coreOrientation, temp);
345 pImpl->Invalidate(true);
349 // Despite not changing status, it needs to rotate screen.
350 _ControlImpl* pControlImpl = _ControlImpl::GetInstance(__publicControl);
351 pImplManager->RotateScreen(pControlImpl, status);
355 #if defined(WINDOW_BASE_ROTATE)
357 _OrientationAgent::UpdateOrientation(void)
359 // Get window rotation
361 // Fire orientation event
362 // Update window bounds
365 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
371 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
377 _Window* pRootWindow = pImpl->GetCore().GetRootWindow();
383 int rotation = pEcoreEvas->GetWindowRotation(*pRootWindow);
384 OrientationStatus status = ORIENTATION_STATUS_NONE;
388 status = ORIENTATION_STATUS_PORTRAIT;
391 status = ORIENTATION_STATUS_LANDSCAPE;
394 status = ORIENTATION_STATUS_PORTRAIT_REVERSE;
397 status = ORIENTATION_STATUS_LANDSCAPE_REVERSE;
403 if (__updateStatus == true)
405 __statusChanged = false;
406 if (__status != status)
408 __statusChanged = true;
409 __updateStatus = false;
415 pEcoreEvas->AllowSetWindowBounds(false);
417 pEcoreEvas->AllowSetWindowBounds(true);
419 // For the form to be made by Ui-Builder
420 if ((__draw == true) && (__statusChanged == true))
422 _ControlOrientation coreOrientation =
423 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
424 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
427 bool exist = pImpl->GetBuilderBounds(coreOrientation, temp);
430 pImpl->Invalidate(true);
434 pEcoreEvas->RotateWindow(*pRootWindow, rotation);
436 pImpl->Invalidate(true);
438 Rectangle bounds = pRootWindow->GetBounds();
439 SysLog(NID_UI, "[Window Manager Rotation][Window : 0x%x, rot = %d, %d, %d, %d, %d] Update Orientation.", pRootWindow->GetNativeHandle(), rotation, bounds.x, bounds.y, bounds.width, bounds.height);
444 _OrientationAgent::RequestOrientationEvent(void)
448 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
449 SysTryReturnVoidResult(NID_UI, pImpl, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
451 unique_ptr<ArrayList> pEventArgs(new (std::nothrow) ArrayList());
452 SysTryReturnVoidResult(NID_UI, pEventArgs, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
453 pEventArgs->Construct();
455 unique_ptr<String> pString(new (std::nothrow) Tizen::Base::String(_REQUEST_ORIENTATION_EVENT));
456 SysTryReturnVoidResult(NID_UI, pString, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
458 pEventArgs->Add(*(pString.get()));
460 _UiNotificationEvent event(pImpl->GetCore().GetHandle(), pEventArgs.get());
462 result r = _UiEventManager::GetInstance()->PostEvent(event);
463 SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
466 pEventArgs.release();
470 _OrientationAgent::FireOrientationEvent(void)
472 if (__firePublicEvent)
477 __firePublicEvent = true;
479 SysLog(NID_UI, "Fire the orientation event.");
483 IEventArg* pArg = _PublicOrientationEvent::CreateOrientationEventArgN(*__pPublicEvent->GetSource(), __status);
484 __pPublicEvent->Fire(*pArg);
486 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
489 _Window* pWindow = pImpl->GetCore().GetRootWindow();
492 SysLog(NID_UI, "[Window Manager Rotation][Window : 0x%x] Fire the public orientation event..", pWindow->GetNativeHandle());
496 __updateStatus = true;
501 _OrientationAgent::FireEvent(OrientationStatus status)
505 _ControlManager* pCoreManager = _ControlManager::GetInstance();
506 SysAssert(pCoreManager);
508 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
514 _ControlOrientation coreOrientation =
515 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
516 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
518 _ControlImplManager* pImplManager = _ControlImplManager::GetInstance();
519 SysAssert(pImplManager);
522 pCoreManager->SetOrientation(coreOrientation);
523 pImplManager->SetOrientationStatus(status);
524 pImpl->GetCore().ChangeLayout(coreOrientation);
526 #if !defined(WINDOW_BASE_ROTATE)
528 int owneeCount = pImpl->GetCore().GetOwneeCount();
529 for (int i = 0; i < owneeCount; i++)
531 _Window* pOwnee = pImpl->GetCore().GetOwnee(i);
535 pOwnee->ChangeLayout(coreOrientation);
541 if (__firePublicEvent && __statusChanged)
543 IEventArg* pArg = _PublicOrientationEvent::CreateOrientationEventArgN(*__pPublicEvent->GetSource(), status);
544 __pPublicEvent->Fire(*pArg);
546 __updateStatus = true;
550 #if defined(WINDOW_BASE_ROTATE)
552 _OrientationAgent::SetRotation(const _Window& window, Orientation orientation)
554 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
562 case ORIENTATION_PORTRAIT:
563 pEcoreEvas->SetWindowPreferredRotation(window, 0);
565 case ORIENTATION_LANDSCAPE:
566 pEcoreEvas->SetWindowPreferredRotation(window, 270);
568 case ORIENTATION_PORTRAIT_REVERSE:
569 pEcoreEvas->SetWindowPreferredRotation(window, 180);
571 case ORIENTATION_LANDSCAPE_REVERSE:
572 pEcoreEvas->SetWindowPreferredRotation(window, 90);
574 case ORIENTATION_AUTOMATIC:
576 pEcoreEvas->SetWindowPreferredRotation(window, -1);
577 int autoRotation[3] = {0, 90, 270};
578 pEcoreEvas->SetWindowAvailabledRotation(window, autoRotation, 3);
582 case ORIENTATION_AUTOMATIC_FOUR_DIRECTION:
584 pEcoreEvas->SetWindowPreferredRotation(window, -1);
585 int autoFourRotation[4] = {0, 90, 180, 270};
586 pEcoreEvas->SetWindowAvailabledRotation(window, autoFourRotation, 4);
599 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
605 int owneeCount = pImpl->GetCore().GetOwneeCount();
606 for (int i = 0; i < owneeCount; i++)
608 _Window* pOwnee = pImpl->GetCore().GetOwnee(i);
611 pEcoreEvas->SetOwner(*pOwnee, pImpl->GetCore());
615 _Control* pParent = pImpl->GetCore().GetParent();
618 int owneeCount = pParent->GetOwneeCount();
619 for (int i = 0; i < owneeCount; i++)
621 _Window* pOwnee = pParent->GetOwnee(i);
624 pEcoreEvas->SetOwner(*pOwnee, *pParent);