2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.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://www.apache.org/licenses/LICENSE-2.0/
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_ModalLoopManager.cpp
20 * @brief This is the implementation file for the _ModalLoopManager class.
25 #include <FBaseSysLog.h>
26 #include <FBaseRtTimer.h>
27 #include "FUi_ModalLoopManager.h"
28 #include "FUi_EcoreEvasMgr.h"
30 using namespace Tizen::Base::Runtime;
31 using namespace Tizen::Ui;
33 namespace Tizen { namespace Ui
36 _ModalLoopManager::_ModalLoopManager(void)
37 : __pTimerInfoList(null)
41 __pTimerInfoList = new (std::nothrow) TimerInfoList;
42 SysAssert(__pTimerInfoList);
43 __pTimerInfoList->Construct();
46 _ModalLoopManager::~_ModalLoopManager(void)
51 const int count = __pTimerInfoList->GetCount();
52 for (int i = 0; i < count; i++)
54 timerInfo.pTimer = null;
55 __pTimerInfoList->GetAt(i, timerInfo);
58 delete timerInfo.pTimer;
61 __pTimerInfoList->RemoveAll();
62 delete __pTimerInfoList;
63 __pTimerInfoList = null;
66 if (__nestedMainLoop > 0)
68 SysLog(NID_UI, "[ModalLoopManager] The count of nested main loops is: %d", __nestedMainLoop);
73 _ModalLoopManager::BeginMainLoop(void)
77 ::GetEcoreEvasMgr()->BeginMainLoop();
79 return __nestedMainLoop;
83 _ModalLoopManager::BeginMainLoop(unsigned long timeOut, int exitCode)
90 pTimer = new (std::nothrow) Timer;
91 SysTryReturn(NID_UI, pTimer != null, -1, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Unable to create Timer.");
93 r = pTimer->Construct(*this);
94 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
96 r = pTimer->Start(timeOut);
97 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
100 timerInfo.loopId = __nestedMainLoop + 1;
101 timerInfo.pTimer = pTimer;
102 timerInfo.exitCode = exitCode;
103 timerInfo.expired = false;
104 r = __pTimerInfoList->Add(timerInfo);
105 SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
108 return BeginMainLoop();
116 _ModalLoopManager::EndMainLoop(int exitCode, bool endAllLoops)
118 if (__nestedMainLoop == 0)
120 return __nestedMainLoop;
123 __lastExitCode = exitCode;
125 _TimerInfo timerInfo;
127 int loopCount = endAllLoops ? __nestedMainLoop : 1;
130 timerCount = __pTimerInfoList->GetCount();
133 __pTimerInfoList->GetAt(timerCount - 1, timerInfo);
134 if (timerInfo.loopId == __nestedMainLoop)
136 timerInfo.pTimer->Cancel();
137 delete timerInfo.pTimer;
138 __pTimerInfoList->RemoveAt(timerCount - 1);
141 timerCount = __pTimerInfoList->GetCount();
144 __pTimerInfoList->GetAt(timerCount - 1, timerInfo);
145 if ((timerInfo.loopId == __nestedMainLoop - 1) && (timerInfo.expired == true))
150 ::GetEcoreEvasMgr()->EndMainLoop();
154 return __nestedMainLoop;
158 _ModalLoopManager::GetLastExitCode(void)
160 return __lastExitCode;
164 _ModalLoopManager::OnTimerExpired(Timer& timer)
166 const int count = __pTimerInfoList->GetCount();
169 _TimerInfo timerInfo;
173 __pTimerInfoList->GetAt(i, timerInfo);
175 if (timerInfo.pTimer == &timer)
177 pos = timerInfo.loopId;
178 timerInfo.expired = true;
179 __pTimerInfoList->SetAt(timerInfo, i);
185 if (pos == __nestedMainLoop)
187 _ModalLoopManager::GetInstance()->EndMainLoop(timerInfo.exitCode, false);
191 _ModalLoopManager* _ModalLoopManager::__pInstance = null;
194 _ModalLoopManager::Initialize(void)
196 static _ModalLoopManager instance;
197 __pInstance = &instance;
201 _ModalLoopManager::GetInstance(void)
203 static pthread_once_t once_block = PTHREAD_ONCE_INIT;
207 pthread_once(&once_block, Initialize);
213 _ModalLoopManager::_TimerInfo::_TimerInfo(void)
221 _ModalLoopManager::_TimerInfo::~_TimerInfo(void)
226 _ModalLoopManager::_TimerInfo::operator==(const _ModalLoopManager::_TimerInfo& rhs) const
228 return ((loopId == rhs.loopId) && (pTimer == rhs.pTimer) && (exitCode == rhs.exitCode) && (expired == rhs.expired));
231 bool _ModalLoopManager::_TimerInfo::operator!=(const _ModalLoopManager::_TimerInfo& rhs) const
233 return ((loopId != rhs.loopId) || (pTimer != rhs.pTimer) || (exitCode != rhs.exitCode) || (expired != rhs.expired));
236 _ModalLoopManager::_TimerInfo&
237 _ModalLoopManager::_TimerInfo::operator=(const _ModalLoopManager::_TimerInfo& rhs)
241 exitCode = rhs.exitCode;
242 expired = rhs.expired;