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);
190 SysLog(NID_UI, "[Window Manager Rotation] Update : __mode(%d) -> status(%d)", __mode, status);
192 if (__updateStatus == true)
194 __statusChanged = false;
195 if (__status != status)
197 __statusChanged = true;
198 __updateStatus = false;
204 pEcoreEvas->AllowSetWindowBounds(false);
206 pEcoreEvas->AllowSetWindowBounds(true);
208 // For the form to be made by Ui-Builder
209 if ((draw == true) && (__statusChanged == true))
211 _ControlOrientation coreOrientation =
212 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
213 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
216 bool exist = pImpl->GetBuilderBounds(coreOrientation, temp);
219 pImpl->Invalidate(true);
223 // Despite not changing status, it needs to rotate screen.
224 _ControlImpl* pControlImpl = _ControlImpl::GetInstance(__publicControl);
225 pImplManager->RotateScreen(pControlImpl, status);
227 _Window* pRootWindow = pImpl->GetCore().GetRootWindow();
230 SetRotation(*pRootWindow, __mode);
236 // Set preffered rotation
238 // _Window* pRootWindow = pImpl->GetCore().GetRootWindow();
241 // SetRotation(*pRootWindow, __mode);
247 _ControlImplManager* pImplManager = _ControlImplManager::GetInstance();
248 SysAssert(pImplManager);
250 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
256 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
262 _Form* pForm = dynamic_cast<_Form*>(&(pImpl->GetCore()));
265 _Control* pParent = pForm->GetParent();
269 int index = pParent->GetChildIndex(*pForm);
270 int childCount = pParent->GetChildCount();
272 if (index == (childCount - 1))
274 _Window* pWindow = pForm->GetRootWindow();
277 if ((__mode == ORIENTATION_AUTOMATIC) || (__mode == ORIENTATION_AUTOMATIC_FOUR_DIRECTION))
279 pEcoreEvas->SetWindowOrientationEnabled(*pWindow, true);
280 pWindow->SetOrientationEnabled(true);
284 pEcoreEvas->SetWindowOrientationEnabled(*pWindow, false);
285 pWindow->SetOrientationEnabled(false);
293 _Frame* pFrame = dynamic_cast<_Frame*>(&(pImpl->GetCore()));
296 int childCount = pFrame->GetChildCount();
299 _Window* pCurrentFrame = _ControlManager::GetInstance()->GetCurrentFrame();
300 if (pCurrentFrame == pFrame)
302 if ((__mode == ORIENTATION_AUTOMATIC) || (__mode == ORIENTATION_AUTOMATIC_FOUR_DIRECTION))
304 pEcoreEvas->SetWindowOrientationEnabled(*pFrame, true);
305 pFrame->SetOrientationEnabled(true);
309 pEcoreEvas->SetWindowOrientationEnabled(*pFrame, false);
310 pFrame->SetOrientationEnabled(false);
317 OrientationStatus status = pImplManager->GetOrientationStatus(__mode);
319 if (__updateStatus == true)
321 __statusChanged = false;
322 if (__status != status)
324 __statusChanged = true;
325 __updateStatus = false;
331 pEcoreEvas->AllowSetWindowBounds(false);
333 pEcoreEvas->AllowSetWindowBounds(true);
335 // For the form to be made by Ui-Builder
336 if ((draw == true) && (__statusChanged == true))
338 _ControlOrientation coreOrientation =
339 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
340 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
343 bool exist = pImpl->GetBuilderBounds(coreOrientation, temp);
346 pImpl->Invalidate(true);
350 // Despite not changing status, it needs to rotate screen.
351 _ControlImpl* pControlImpl = _ControlImpl::GetInstance(__publicControl);
352 pImplManager->RotateScreen(pControlImpl, status);
356 #if defined(WINDOW_BASE_ROTATE)
358 _OrientationAgent::UpdateOrientation(void)
360 // Get window rotation
362 // Fire orientation event
363 // Update window bounds
366 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
372 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
378 _Window* pRootWindow = pImpl->GetCore().GetRootWindow();
384 int rotation = pEcoreEvas->GetWindowRotation(*pRootWindow);
385 OrientationStatus status = ORIENTATION_STATUS_NONE;
389 status = ORIENTATION_STATUS_PORTRAIT;
392 status = ORIENTATION_STATUS_LANDSCAPE;
395 status = ORIENTATION_STATUS_PORTRAIT_REVERSE;
398 status = ORIENTATION_STATUS_LANDSCAPE_REVERSE;
404 if (__updateStatus == true)
406 __statusChanged = false;
407 if (__status != status)
409 __statusChanged = true;
410 __updateStatus = false;
416 pEcoreEvas->AllowSetWindowBounds(false);
418 pEcoreEvas->AllowSetWindowBounds(true);
420 // For the form to be made by Ui-Builder
421 if ((__draw == true) && (__statusChanged == true))
423 _ControlOrientation coreOrientation =
424 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
425 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
428 bool exist = pImpl->GetBuilderBounds(coreOrientation, temp);
431 pImpl->Invalidate(true);
435 pEcoreEvas->RotateWindow(*pRootWindow, rotation);
437 pImpl->Invalidate(true);
439 Rectangle bounds = pRootWindow->GetBounds();
440 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);
445 _OrientationAgent::RequestOrientationEvent(void)
449 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
450 SysTryReturnVoidResult(NID_UI, pImpl, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
452 unique_ptr<ArrayList> pEventArgs(new (std::nothrow) ArrayList());
453 SysTryReturnVoidResult(NID_UI, pEventArgs, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
454 pEventArgs->Construct();
456 unique_ptr<String> pString(new (std::nothrow) Tizen::Base::String(_REQUEST_ORIENTATION_EVENT));
457 SysTryReturnVoidResult(NID_UI, pString, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory is insufficient.");
459 pEventArgs->Add(*(pString.get()));
461 _UiNotificationEvent event(pImpl->GetCore().GetHandle(), pEventArgs.get());
463 result r = _UiEventManager::GetInstance()->PostEvent(event);
464 SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, E_SYSTEM, "[E_SYSTEM] A system error occurred.");
467 pEventArgs.release();
471 _OrientationAgent::FireOrientationEvent(void)
473 if (__firePublicEvent)
478 __firePublicEvent = true;
480 SysLog(NID_UI, "Fire the orientation event.");
484 IEventArg* pArg = _PublicOrientationEvent::CreateOrientationEventArgN(*__pPublicEvent->GetSource(), __status);
485 __pPublicEvent->Fire(*pArg);
487 __updateStatus = true;
492 _OrientationAgent::FireEvent(OrientationStatus status)
496 _ControlManager* pCoreManager = _ControlManager::GetInstance();
497 SysAssert(pCoreManager);
499 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
505 _ControlOrientation coreOrientation =
506 (status == ORIENTATION_STATUS_PORTRAIT || status == ORIENTATION_STATUS_PORTRAIT_REVERSE) ?
507 _CONTROL_ORIENTATION_PORTRAIT : _CONTROL_ORIENTATION_LANDSCAPE;
509 _ControlImplManager* pImplManager = _ControlImplManager::GetInstance();
510 SysAssert(pImplManager);
513 pCoreManager->SetOrientation(coreOrientation);
514 pImplManager->SetOrientationStatus(status);
515 pImpl->GetCore().ChangeLayout(coreOrientation);
517 #if !defined(WINDOW_BASE_ROTATE)
519 int owneeCount = pImpl->GetCore().GetOwneeCount();
520 for (int i = 0; i < owneeCount; i++)
522 _Window* pOwnee = pImpl->GetCore().GetOwnee(i);
526 pOwnee->ChangeLayout(coreOrientation);
532 if (__firePublicEvent && __statusChanged)
534 IEventArg* pArg = _PublicOrientationEvent::CreateOrientationEventArgN(*__pPublicEvent->GetSource(), status);
535 __pPublicEvent->Fire(*pArg);
537 __updateStatus = true;
541 #if defined(WINDOW_BASE_ROTATE)
543 _OrientationAgent::SetRotation(const _Window& window, Orientation orientation)
545 _EcoreEvas* pEcoreEvas = GetEcoreEvasMgr()->GetEcoreEvas();
553 case ORIENTATION_PORTRAIT:
554 pEcoreEvas->SetWindowPreferredRotation(window, 0);
556 case ORIENTATION_LANDSCAPE:
557 pEcoreEvas->SetWindowPreferredRotation(window, 270);
559 case ORIENTATION_PORTRAIT_REVERSE:
560 pEcoreEvas->SetWindowPreferredRotation(window, 180);
562 case ORIENTATION_LANDSCAPE_REVERSE:
563 pEcoreEvas->SetWindowPreferredRotation(window, 90);
565 case ORIENTATION_AUTOMATIC:
567 pEcoreEvas->SetWindowPreferredRotation(window, -1);
568 int autoRotation[3] = {0, 90, 270};
569 pEcoreEvas->SetWindowAvailabledRotation(window, autoRotation, 3);
573 case ORIENTATION_AUTOMATIC_FOUR_DIRECTION:
575 pEcoreEvas->SetWindowPreferredRotation(window, -1);
576 int autoFourRotation[4] = {0, 90, 180, 270};
577 pEcoreEvas->SetWindowAvailabledRotation(window, autoFourRotation, 4);
590 _ControlImpl* pImpl = _ControlImpl::GetInstance(__publicControl);
596 int owneeCount = pImpl->GetCore().GetOwneeCount();
597 for (int i = 0; i < owneeCount; i++)
599 _Window* pOwnee = pImpl->GetCore().GetOwnee(i);
602 pEcoreEvas->SetOwner(*pOwnee, pImpl->GetCore());
606 _Control* pParent = pImpl->GetCore().GetParent();
609 int owneeCount = pParent->GetOwneeCount();
610 for (int i = 0; i < owneeCount; i++)
612 _Window* pOwnee = pParent->GetOwnee(i);
615 pEcoreEvas->SetOwner(*pOwnee, *pParent);