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 OrientationStatus status = pImplManager->GetOrientationStatus(__mode);
192 SysLog(NID_UI, "[Window Manager Rotation] Update : __mode(%d) -> status(%d)", __mode, status);
194 if (__updateStatus == true)
196 __statusChanged = false;
197 if (__status != status)
199 __statusChanged = true;
200 __updateStatus = false;
206 pEcoreEvas->AllowSetWindowBounds(false);
208 pEcoreEvas->AllowSetWindowBounds(true);
210 // For the form to be made by Ui-Builder
211 if ((draw == true) && (__statusChanged == true))
213 _ControlOrientation coreOrientation =
214 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
215 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
218 bool exist = pImpl->GetBuilderBounds(coreOrientation, temp);
221 pImpl->Invalidate(true);
225 // Despite not changing status, it needs to rotate screen.
226 _ControlImpl* pControlImpl = _ControlImpl::GetInstance(__publicControl);
227 pImplManager->RotateScreen(pControlImpl, status);
229 _Window* pRootWindow = pImpl->GetCore().GetRootWindow();
232 SetRotation(*pRootWindow, __mode);
238 // Set preffered rotation
240 // _Window* pRootWindow = pImpl->GetCore().GetRootWindow();
243 // SetRotation(*pRootWindow, __mode);
249 SysLog(NID_UI, "[Window Manager Rotation] <<<<<<<<<< Update : END >>>>>>>>>>");
251 _ControlImplManager* pImplManager = _ControlImplManager::GetInstance();
252 SysAssert(pImplManager);
254 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
260 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
266 _Form* pForm = dynamic_cast<_Form*>(&(pImpl->GetCore()));
269 _Control* pParent = pForm->GetParent();
273 int index = pParent->GetChildIndex(*pForm);
274 int childCount = pParent->GetChildCount();
276 if (index == (childCount - 1))
278 _Window* pWindow = pForm->GetRootWindow();
281 if ((__mode == ORIENTATION_AUTOMATIC) || (__mode == ORIENTATION_AUTOMATIC_FOUR_DIRECTION))
283 pEcoreEvas->SetWindowOrientationEnabled(*pWindow, true);
284 pWindow->SetOrientationEnabled(true);
288 pEcoreEvas->SetWindowOrientationEnabled(*pWindow, false);
289 pWindow->SetOrientationEnabled(false);
297 _Frame* pFrame = dynamic_cast<_Frame*>(&(pImpl->GetCore()));
300 int childCount = pFrame->GetChildCount();
303 _Window* pCurrentFrame = _ControlManager::GetInstance()->GetCurrentFrame();
304 if (pCurrentFrame == pFrame)
306 if ((__mode == ORIENTATION_AUTOMATIC) || (__mode == ORIENTATION_AUTOMATIC_FOUR_DIRECTION))
308 pEcoreEvas->SetWindowOrientationEnabled(*pFrame, true);
309 pFrame->SetOrientationEnabled(true);
313 pEcoreEvas->SetWindowOrientationEnabled(*pFrame, false);
314 pFrame->SetOrientationEnabled(false);
321 OrientationStatus status = pImplManager->GetOrientationStatus(__mode);
323 if (__updateStatus == true)
325 __statusChanged = false;
326 if (__status != status)
328 __statusChanged = true;
329 __updateStatus = false;
335 pEcoreEvas->AllowSetWindowBounds(false);
337 pEcoreEvas->AllowSetWindowBounds(true);
339 // For the form to be made by Ui-Builder
340 if ((draw == true) && (__statusChanged == true))
342 _ControlOrientation coreOrientation =
343 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
344 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
347 bool exist = pImpl->GetBuilderBounds(coreOrientation, temp);
350 pImpl->Invalidate(true);
354 // Despite not changing status, it needs to rotate screen.
355 _ControlImpl* pControlImpl = _ControlImpl::GetInstance(__publicControl);
356 pImplManager->RotateScreen(pControlImpl, status);
360 #if defined(WINDOW_BASE_ROTATE)
362 _OrientationAgent::UpdateOrientation(void)
364 // Get window rotation
366 // Fire orientation event
367 // Update window bounds
370 SysLog(NID_UI, "[Window Manager Rotation] <<<<<<<<<< UpdateOrientation : START >>>>>>>>>>");
372 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
378 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
384 _Window* pRootWindow = pImpl->GetCore().GetRootWindow();
390 int rotation = pEcoreEvas->GetWindowRotation(*pRootWindow);
391 OrientationStatus status = ORIENTATION_STATUS_NONE;
395 status = ORIENTATION_STATUS_PORTRAIT;
398 status = ORIENTATION_STATUS_LANDSCAPE;
401 status = ORIENTATION_STATUS_PORTRAIT_REVERSE;
404 status = ORIENTATION_STATUS_LANDSCAPE_REVERSE;
410 if (__updateStatus == true)
412 __statusChanged = false;
413 if (__status != status)
415 __statusChanged = true;
416 __updateStatus = false;
422 pEcoreEvas->AllowSetWindowBounds(false);
424 pEcoreEvas->AllowSetWindowBounds(true);
426 // For the form to be made by Ui-Builder
427 if ((__draw == true) && (__statusChanged == true))
429 _ControlOrientation coreOrientation =
430 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
431 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
434 bool exist = pImpl->GetBuilderBounds(coreOrientation, temp);
437 pImpl->Invalidate(true);
441 pEcoreEvas->RotateWindow(*pRootWindow, rotation);
443 pImpl->Invalidate(true);
445 Rectangle bounds = pRootWindow->GetBounds();
446 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);
448 SysLog(NID_UI, "[Window Manager Rotation] <<<<<<<<<< UpdateOrientation : END >>>>>>>>>>");
453 _OrientationAgent::RequestOrientationEvent(void)
457 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
458 SysTryReturnVoidResult(NID_UI, pImpl, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
460 unique_ptr<ArrayList> pEventArgs(new (std::nothrow) ArrayList());
461 SysTryReturnVoidResult(NID_UI, pEventArgs, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
462 pEventArgs->Construct();
464 unique_ptr<String> pString(new (std::nothrow) Tizen::Base::String(_REQUEST_ORIENTATION_EVENT));
465 SysTryReturnVoidResult(NID_UI, pString, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
467 pEventArgs->Add(*(pString.get()));
469 _UiNotificationEvent event(pImpl->GetCore().GetHandle(), pEventArgs.get());
471 result r = _UiEventManager::GetInstance()->PostEvent(event);
472 SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
475 pEventArgs.release();
479 _OrientationAgent::FireOrientationEvent(void)
481 if (__firePublicEvent)
486 __firePublicEvent = true;
488 SysLog(NID_UI, "Fire the orientation event.");
492 IEventArg* pArg = _PublicOrientationEvent::CreateOrientationEventArgN(*__pPublicEvent->GetSource(), __status);
493 __pPublicEvent->Fire(*pArg);
495 __updateStatus = true;
500 _OrientationAgent::FireEvent(OrientationStatus status)
504 _ControlManager* pCoreManager = _ControlManager::GetInstance();
505 SysAssert(pCoreManager);
507 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
513 _ControlOrientation coreOrientation =
514 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
515 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
517 _ControlImplManager* pImplManager = _ControlImplManager::GetInstance();
518 SysAssert(pImplManager);
521 pCoreManager->SetOrientation(coreOrientation);
522 pImplManager->SetOrientationStatus(status);
523 pImpl->GetCore().ChangeLayout(coreOrientation);
525 #if !defined(WINDOW_BASE_ROTATE)
527 int owneeCount = pImpl->GetCore().GetOwneeCount();
528 for (int i = 0; i < owneeCount; i++)
530 _Window* pOwnee = pImpl->GetCore().GetOwnee(i);
534 pOwnee->ChangeLayout(coreOrientation);
540 if (__firePublicEvent && __statusChanged)
542 IEventArg* pArg = _PublicOrientationEvent::CreateOrientationEventArgN(*__pPublicEvent->GetSource(), status);
543 __pPublicEvent->Fire(*pArg);
545 __updateStatus = true;
549 #if defined(WINDOW_BASE_ROTATE)
551 _OrientationAgent::SetRotation(const _Window& window, Orientation orientation)
553 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
561 case ORIENTATION_PORTRAIT:
562 pEcoreEvas->SetWindowPreferredRotation(window, 0);
564 case ORIENTATION_LANDSCAPE:
565 pEcoreEvas->SetWindowPreferredRotation(window, 270);
567 case ORIENTATION_PORTRAIT_REVERSE:
568 pEcoreEvas->SetWindowPreferredRotation(window, 180);
570 case ORIENTATION_LANDSCAPE_REVERSE:
571 pEcoreEvas->SetWindowPreferredRotation(window, 90);
573 case ORIENTATION_AUTOMATIC:
575 pEcoreEvas->SetWindowPreferredRotation(window, -1);
576 int autoRotation[3] = {0, 90, 270};
577 pEcoreEvas->SetWindowAvailabledRotation(window, autoRotation, 3);
581 case ORIENTATION_AUTOMATIC_FOUR_DIRECTION:
583 pEcoreEvas->SetWindowPreferredRotation(window, -1);
584 int autoFourRotation[4] = {0, 90, 180, 270};
585 pEcoreEvas->SetWindowAvailabledRotation(window, autoFourRotation, 4);
598 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
604 int owneeCount = pImpl->GetCore().GetOwneeCount();
605 for (int i = 0; i < owneeCount; i++)
607 _Window* pOwnee = pImpl->GetCore().GetOwnee(i);
610 pEcoreEvas->SetOwner(*pOwnee, pImpl->GetCore());
614 _Control* pParent = pImpl->GetCore().GetParent();
617 int owneeCount = pParent->GetOwneeCount();
618 for (int i = 0; i < owneeCount; i++)
620 _Window* pOwnee = pParent->GetOwnee(i);
623 pEcoreEvas->SetOwner(*pOwnee, *pParent);