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 SysLog(NID_UI, "[Window Manager Rotation] ---------- Update : START ----------");
160 _ControlImplManager* pImplManager = _ControlImplManager::GetInstance();
161 SysAssert(pImplManager);
163 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
169 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
175 bool autoMode = false;
177 if ((__mode == ORIENTATION_AUTOMATIC) || (__mode == ORIENTATION_AUTOMATIC_FOUR_DIRECTION))
182 // if (autoMode == false)
185 // Update orientation status
187 // Fire orientation event
189 // Set preffered rotation
191 _Window* pWindow = pImpl->GetCore().GetRootWindow();
192 NativeWindowHandle handle = 0;
195 handle = pWindow->GetNativeHandle();
198 OrientationStatus status = pImplManager->GetOrientationStatus(__mode);
199 SysLog(NID_UI, "[Window Manager Rotation][Window : 0x%x] Update : __mode(%d) -> status(%d)", handle, __mode, status);
201 if (__updateStatus == true)
203 __statusChanged = false;
204 if (__status != status)
206 __statusChanged = true;
207 __updateStatus = false;
213 pEcoreEvas->AllowSetWindowBounds(false);
215 pEcoreEvas->AllowSetWindowBounds(true);
217 // For the form to be made by Ui-Builder
218 if ((draw == true) && (__statusChanged == true))
220 _ControlOrientation coreOrientation =
221 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
222 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
225 bool exist = pImpl->GetBuilderBounds(coreOrientation, temp);
228 pImpl->Invalidate(true);
232 // Despite not changing status, it needs to rotate screen.
233 _ControlImpl* pControlImpl = _ControlImpl::GetInstance(__publicControl);
234 pImplManager->RotateScreen(pControlImpl, status);
236 _Window* pRootWindow = pImpl->GetCore().GetRootWindow();
239 SetRotation(*pRootWindow, __mode);
245 // Set preffered rotation
247 // _Window* pRootWindow = pImpl->GetCore().GetRootWindow();
250 // SetRotation(*pRootWindow, __mode);
256 SysLog(NID_UI, "[Window Manager Rotation] ---------- Update : END ----------");
258 _ControlImplManager* pImplManager = _ControlImplManager::GetInstance();
259 SysAssert(pImplManager);
261 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
267 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
273 _Form* pForm = dynamic_cast<_Form*>(&(pImpl->GetCore()));
276 _Control* pParent = pForm->GetParent();
280 int index = pParent->GetChildIndex(*pForm);
281 int childCount = pParent->GetChildCount();
283 if (index == (childCount - 1))
285 _Window* pWindow = pForm->GetRootWindow();
288 if ((__mode == ORIENTATION_AUTOMATIC) || (__mode == ORIENTATION_AUTOMATIC_FOUR_DIRECTION))
290 pEcoreEvas->SetWindowOrientationEnabled(*pWindow, true);
291 pWindow->SetOrientationEnabled(true);
295 pEcoreEvas->SetWindowOrientationEnabled(*pWindow, false);
296 pWindow->SetOrientationEnabled(false);
304 _Frame* pFrame = dynamic_cast<_Frame*>(&(pImpl->GetCore()));
307 int childCount = pFrame->GetChildCount();
310 _Window* pCurrentFrame = _ControlManager::GetInstance()->GetCurrentFrame();
311 if (pCurrentFrame == pFrame)
313 if ((__mode == ORIENTATION_AUTOMATIC) || (__mode == ORIENTATION_AUTOMATIC_FOUR_DIRECTION))
315 pEcoreEvas->SetWindowOrientationEnabled(*pFrame, true);
316 pFrame->SetOrientationEnabled(true);
320 pEcoreEvas->SetWindowOrientationEnabled(*pFrame, false);
321 pFrame->SetOrientationEnabled(false);
328 OrientationStatus status = pImplManager->GetOrientationStatus(__mode);
330 if (__updateStatus == true)
332 __statusChanged = false;
333 if (__status != status)
335 __statusChanged = true;
336 __updateStatus = false;
342 pEcoreEvas->AllowSetWindowBounds(false);
344 pEcoreEvas->AllowSetWindowBounds(true);
346 // For the form to be made by Ui-Builder
347 if ((draw == true) && (__statusChanged == true))
349 _ControlOrientation coreOrientation =
350 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
351 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
354 bool exist = pImpl->GetBuilderBounds(coreOrientation, temp);
357 pImpl->Invalidate(true);
361 // Despite not changing status, it needs to rotate screen.
362 _ControlImpl* pControlImpl = _ControlImpl::GetInstance(__publicControl);
363 pImplManager->RotateScreen(pControlImpl, status);
367 #if defined(WINDOW_BASE_ROTATE)
369 _OrientationAgent::UpdateOrientation(void)
371 // Get window rotation
373 // Fire orientation event
374 // Update window bounds
377 SysLog(NID_UI, "[Window Manager Rotation] ---------- UpdateOrientation : START ----------");
379 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
385 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
391 _Window* pRootWindow = pImpl->GetCore().GetRootWindow();
397 int rotation = pEcoreEvas->GetWindowRotation(*pRootWindow);
398 OrientationStatus status = ORIENTATION_STATUS_NONE;
402 status = ORIENTATION_STATUS_PORTRAIT;
405 status = ORIENTATION_STATUS_LANDSCAPE;
408 status = ORIENTATION_STATUS_PORTRAIT_REVERSE;
411 status = ORIENTATION_STATUS_LANDSCAPE_REVERSE;
417 if (__updateStatus == true)
419 __statusChanged = false;
420 if (__status != status)
422 __statusChanged = true;
423 __updateStatus = false;
429 pEcoreEvas->AllowSetWindowBounds(false);
431 pEcoreEvas->AllowSetWindowBounds(true);
433 // For the form to be made by Ui-Builder
434 if ((__draw == true) && (__statusChanged == true))
436 _ControlOrientation coreOrientation =
437 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
438 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
441 bool exist = pImpl->GetBuilderBounds(coreOrientation, temp);
444 pImpl->Invalidate(true);
448 pEcoreEvas->RotateWindow(*pRootWindow, rotation);
450 pImpl->Invalidate(true);
452 Rectangle bounds = pRootWindow->GetBounds();
453 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);
455 SysLog(NID_UI, "[Window Manager Rotation] ---------- UpdateOrientation : END ----------");
460 _OrientationAgent::RequestOrientationEvent(void)
464 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
465 SysTryReturnVoidResult(NID_UI, pImpl, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
467 unique_ptr<ArrayList> pEventArgs(new (std::nothrow) ArrayList());
468 SysTryReturnVoidResult(NID_UI, pEventArgs, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
469 pEventArgs->Construct();
471 unique_ptr<String> pString(new (std::nothrow) Tizen::Base::String(_REQUEST_ORIENTATION_EVENT));
472 SysTryReturnVoidResult(NID_UI, pString, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
474 pEventArgs->Add(*(pString.get()));
476 _UiNotificationEvent event(pImpl->GetCore().GetHandle(), pEventArgs.get());
478 result r = _UiEventManager::GetInstance()->PostEvent(event);
479 SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
482 pEventArgs.release();
486 _OrientationAgent::FireOrientationEvent(void)
488 if (__firePublicEvent)
493 __firePublicEvent = true;
495 SysLog(NID_UI, "Fire the orientation event.");
499 IEventArg* pArg = _PublicOrientationEvent::CreateOrientationEventArgN(*__pPublicEvent->GetSource(), __status);
500 __pPublicEvent->Fire(*pArg);
502 __updateStatus = true;
507 _OrientationAgent::FireEvent(OrientationStatus status)
511 _ControlManager* pCoreManager = _ControlManager::GetInstance();
512 SysAssert(pCoreManager);
514 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
520 #if defined(WINDOW_OWNEE_PREFERRED)
521 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
528 _ControlOrientation coreOrientation =
529 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
530 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
532 _ControlImplManager* pImplManager = _ControlImplManager::GetInstance();
533 SysAssert(pImplManager);
536 pCoreManager->SetOrientation(coreOrientation);
537 pImplManager->SetOrientationStatus(status);
538 pImpl->GetCore().ChangeLayout(coreOrientation);
540 #if !defined(WINDOW_BASE_ROTATE)
542 int owneeCount = pImpl->GetCore().GetOwneeCount();
543 for (int i = 0; i < owneeCount; i++)
545 _Window* pOwnee = pImpl->GetCore().GetOwnee(i);
549 pOwnee->ChangeLayout(coreOrientation);
554 #if defined(WINDOW_OWNEE_PREFERRED)
555 int owneeCount = pImpl->GetCore().GetOwneeCount();
556 for (int i = 0; i < owneeCount; i++)
558 _Window* pOwnee = pImpl->GetCore().GetOwnee(i);
561 pOwnee->ChangeLayout(coreOrientation); // Need to test
562 pEcoreEvas->SetOwner(*pOwnee, pImpl->GetCore());
566 _Control* pParent = pImpl->GetCore().GetParent();
569 int owneeCount = pParent->GetOwneeCount();
570 for (int i = 0; i < owneeCount; i++)
572 _Window* pOwnee = pParent->GetOwnee(i);
575 pOwnee->ChangeLayout(coreOrientation); // Need to test
576 pEcoreEvas->SetOwner(*pOwnee, *pParent);
583 if (__firePublicEvent && __statusChanged)
585 IEventArg* pArg = _PublicOrientationEvent::CreateOrientationEventArgN(*__pPublicEvent->GetSource(), status);
586 __pPublicEvent->Fire(*pArg);
588 __updateStatus = true;
592 #if defined(WINDOW_BASE_ROTATE)
594 _OrientationAgent::SetRotation(const _Window& window, Orientation orientation)
596 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
604 case ORIENTATION_PORTRAIT:
605 pEcoreEvas->SetWindowPreferredRotation(window, 0);
607 case ORIENTATION_LANDSCAPE:
608 pEcoreEvas->SetWindowPreferredRotation(window, 270);
610 case ORIENTATION_PORTRAIT_REVERSE:
611 pEcoreEvas->SetWindowPreferredRotation(window, 180);
613 case ORIENTATION_LANDSCAPE_REVERSE:
614 pEcoreEvas->SetWindowPreferredRotation(window, 90);
616 case ORIENTATION_AUTOMATIC:
618 pEcoreEvas->SetWindowPreferredRotation(window, -1);
619 int autoRotation[3] = {0, 90, 270};
620 pEcoreEvas->SetWindowAvailabledRotation(window, autoRotation, 3);
624 case ORIENTATION_AUTOMATIC_FOUR_DIRECTION:
626 pEcoreEvas->SetWindowPreferredRotation(window, -1);
627 int autoFourRotation[4] = {0, 90, 180, 270};
628 pEcoreEvas->SetWindowAvailabledRotation(window, autoFourRotation, 4);
636 #if !defined(WINDOW_OWNEE_PREFERRED)
642 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
648 int owneeCount = pImpl->GetCore().GetOwneeCount();
649 for (int i = 0; i < owneeCount; i++)
651 _Window* pOwnee = pImpl->GetCore().GetOwnee(i);
654 pEcoreEvas->SetOwner(*pOwnee, pImpl->GetCore());
658 _Control* pParent = pImpl->GetCore().GetParent();
661 int owneeCount = pParent->GetOwneeCount();
662 for (int i = 0; i < owneeCount; i++)
664 _Window* pOwnee = pParent->GetOwnee(i);
667 pEcoreEvas->SetOwner(*pOwnee, *pParent);