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.
18 * @file FUi_ModalLoopManager.cpp
19 * @brief This is the implementation file for the _ModalLoopManager class.
24 #include <FBaseSysLog.h>
25 #include <FBaseRtTimer.h>
26 #include "FUi_ModalLoopManager.h"
27 #include "FUi_EcoreEvasMgr.h"
29 using namespace Tizen::Base::Runtime;
30 using namespace Tizen::Ui;
32 namespace Tizen { namespace Ui
35 _ModalLoopManager::_ModalLoopManager(void)
36 : __pTimerInfoList(null)
40 __pTimerInfoList = new (std::nothrow) TimerInfoList;
41 SysAssert(__pTimerInfoList);
42 __pTimerInfoList->Construct();
45 _ModalLoopManager::~_ModalLoopManager(void)
50 const int count = __pTimerInfoList->GetCount();
51 for (int i = 0; i < count; i++)
53 timerInfo.pTimer = null;
54 __pTimerInfoList->GetAt(i, timerInfo);
57 delete timerInfo.pTimer;
60 __pTimerInfoList->RemoveAll();
61 delete __pTimerInfoList;
62 __pTimerInfoList = null;
65 if (__nestedMainLoop > 0)
67 SysLog(NID_UI, "[ModalLoopManager] The count of nested main loops is: %d", __nestedMainLoop);
72 _ModalLoopManager::BeginMainLoop(void)
75 ::GetEcoreEvasMgr()->BeginMainLoop();
77 return __nestedMainLoop;
81 _ModalLoopManager::BeginMainLoop(unsigned long timeOut, int exitCode)
88 pTimer = new (std::nothrow) Timer;
89 SysTryReturn(NID_UI, pTimer != null, -1, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Unable to create Timer.");
91 r = pTimer->Construct(*this);
92 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
94 r = pTimer->Start(timeOut);
95 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
98 timerInfo.loopId = __nestedMainLoop + 1;
99 timerInfo.pTimer = pTimer;
100 timerInfo.exitCode = exitCode;
101 timerInfo.expired = false;
102 r = __pTimerInfoList->Add(timerInfo);
103 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
106 return BeginMainLoop();
114 _ModalLoopManager::EndMainLoop(int exitCode, bool endAllLoops)
116 if (__nestedMainLoop == 0)
118 return __nestedMainLoop;
121 __lastExitCode = exitCode;
123 _TimerInfo timerInfo;
125 int loopCount = endAllLoops ? __nestedMainLoop : 1;
128 timerCount = __pTimerInfoList->GetCount();
131 __pTimerInfoList->GetAt(timerCount - 1, timerInfo);
132 if (timerInfo.loopId == __nestedMainLoop)
134 timerInfo.pTimer->Cancel();
135 delete timerInfo.pTimer;
136 __pTimerInfoList->RemoveAt(timerCount - 1);
139 timerCount = __pTimerInfoList->GetCount();
142 __pTimerInfoList->GetAt(timerCount - 1, timerInfo);
143 if ((timerInfo.loopId == __nestedMainLoop - 1) && (timerInfo.expired == true))
148 ::GetEcoreEvasMgr()->EndMainLoop();
152 return __nestedMainLoop;
156 _ModalLoopManager::GetLastExitCode(void)
158 return __lastExitCode;
162 _ModalLoopManager::OnTimerExpired(Timer& timer)
164 const int count = __pTimerInfoList->GetCount();
167 _TimerInfo timerInfo;
171 __pTimerInfoList->GetAt(i, timerInfo);
173 if (timerInfo.pTimer == &timer)
175 pos = timerInfo.loopId;
176 timerInfo.expired = true;
177 __pTimerInfoList->SetAt(timerInfo, i);
183 if (pos == __nestedMainLoop)
185 _ModalLoopManager::GetInstance()->EndMainLoop(timerInfo.exitCode, false);
189 _ModalLoopManager* _ModalLoopManager::__pInstance = null;
192 _ModalLoopManager::Initialize(void)
194 static _ModalLoopManager instance;
195 __pInstance = &instance;
199 _ModalLoopManager::GetInstance(void)
201 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
205 pthread_once(&once_block, Initialize);
211 _ModalLoopManager::_TimerInfo::_TimerInfo(void)
219 _ModalLoopManager::_TimerInfo::~_TimerInfo(void)
224 _ModalLoopManager::_TimerInfo::operator==(const _ModalLoopManager::_TimerInfo& rhs) const
226 return ((loopId == rhs.loopId) && (pTimer == rhs.pTimer) && (exitCode == rhs.exitCode) && (expired == rhs.expired));
229 bool _ModalLoopManager::_TimerInfo::operator!=(const _ModalLoopManager::_TimerInfo& rhs) const
231 return ((loopId != rhs.loopId) || (pTimer != rhs.pTimer) || (exitCode != rhs.exitCode) || (expired != rhs.expired));
234 _ModalLoopManager::_TimerInfo&
235 _ModalLoopManager::_TimerInfo::operator=(const _ModalLoopManager::_TimerInfo& rhs)
239 exitCode = rhs.exitCode;
240 expired = rhs.expired;