Tizen 2.1 base
[framework/osp/uifw.git] / src / ui / FUi_ControlImplManager.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://floralicense.org/license/
10 //
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.
16 //
17 /**
18  * @file                FUi_ControlImplManager.cpp
19  * @brief               This is the implementation file for the _ControlImplManager class.
20  */
21
22 #include <cstdio>
23 #include <cstring>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <new>
27 #include <dlfcn.h>
28
29 #include <FIoFile.h>
30 #include <FBaseSysLog.h>
31 #include <FUiCtrlForm.h>
32 #include <FApp_AppInfo.h>
33 #include <FGrp_CoordinateSystem.h>
34 #include <FSys_SystemInfoImpl.h>
35 #include "FUi_ControlImplManager.h"
36 #include "FUi_ControlManager.h"
37 #include "FUi_WindowImpl.h"
38 #include "FUi_KeyEventManagerImpl.h"
39 #include "FUi_FocusManagerImpl.h"
40
41 #include "FUiCtrl_FrameImpl.h"
42 #include "FUiCtrl_FormImpl.h"
43
44 using namespace Tizen::App;
45 using namespace Tizen::Base;
46 using namespace Tizen::Graphics;
47 using namespace Tizen::Io;
48 using namespace Tizen::Ui;
49 using namespace Tizen::Ui::Controls;
50 using namespace Tizen::Ui::Animations;
51
52 namespace // unnamed
53 {
54
55 OrientationStatus
56 Convert(_ControlRotation rotate, bool allowReverse)
57 {
58         OrientationStatus status = ORIENTATION_STATUS_NONE;
59
60         if (allowReverse == true)
61         {
62                 switch (rotate)
63                 {
64                 case _CONTROL_ROTATION_0:
65                         status = ORIENTATION_STATUS_PORTRAIT;
66                         break;
67                 case _CONTROL_ROTATION_270:
68                         status = ORIENTATION_STATUS_LANDSCAPE;
69                         break;
70                 case _CONTROL_ROTATION_180:
71                         status = ORIENTATION_STATUS_PORTRAIT_REVERSE;
72                         break;
73                 case _CONTROL_ROTATION_90:
74                         status =  ORIENTATION_STATUS_LANDSCAPE_REVERSE;
75                         break;
76                 }
77         }
78         else
79         {
80                 switch (rotate)
81                 {
82                 case _CONTROL_ROTATION_0:
83                         // fall through
84                 case _CONTROL_ROTATION_180:
85                         status = ORIENTATION_STATUS_PORTRAIT;
86                         break;
87                 case _CONTROL_ROTATION_90:
88                         status = ORIENTATION_STATUS_LANDSCAPE_REVERSE;
89                         break;
90                 case _CONTROL_ROTATION_270:
91                         status = ORIENTATION_STATUS_LANDSCAPE;
92                         break;
93                 }
94         }
95
96         return status;
97 }
98
99 _ControlRotation
100 Convert(OrientationStatus orientationStatus)
101 {
102         switch (orientationStatus)
103         {
104         case ORIENTATION_STATUS_PORTRAIT:
105                 return _CONTROL_ROTATION_0;
106         case ORIENTATION_STATUS_LANDSCAPE:
107                 return _CONTROL_ROTATION_270;
108         case ORIENTATION_STATUS_PORTRAIT_REVERSE:
109                 return _CONTROL_ROTATION_180;
110         case ORIENTATION_STATUS_LANDSCAPE_REVERSE:
111                 return _CONTROL_ROTATION_90;
112         default:
113                 return _CONTROL_ROTATION_0;
114         }
115 }
116
117 } // unnamed namespace
118
119 result
120 InitializeTestbuddyManager(void)
121 {
122         result r = E_FAILURE;
123         void* pHandle = null;
124
125         SysLog(NID_UI,"TestbuddyManager is starting.");
126
127         result (*pfnInitialize_TestBuddyManager)(void) = null;
128
129         pHandle = dlopen ("libtestbuddy.so", RTLD_LAZY);
130         if (!pHandle)
131         {
132                 SysLog(NID_UI, "[%s] Failed to dlopen libtestbuddy.so", dlerror());
133                 return E_LIBRARY_NOT_FOUND;
134         }
135
136         pfnInitialize_TestBuddyManager = reinterpret_cast<result (*)(void)>(dlsym(pHandle, "_Initialize_TestBuddyManager"));
137         if (pfnInitialize_TestBuddyManager == null)
138         {
139                 SysLog(NID_UI, "[%s] Failed to dlsym _Initialize_TestBuddyManager", dlerror());
140                 dlclose(pHandle);
141                 return E_SYMBOL_NOT_FOUND;
142         }
143
144         r = pfnInitialize_TestBuddyManager();
145         SysTryReturn(NID_UI, r == E_SUCCESS, E_SYSTEM, E_SYSTEM, "[E_SYSTEM] pfnInitialize_TestBuddyManager() is invaild.");
146
147         return r;
148 }
149
150 result
151 InitializeUiFramework(void)
152 {
153         _ControlImplManager::Initialize();
154
155         // TODO : After completing InitializeTestbuddyManager( )
156         //return GetLastResult();
157         return E_SUCCESS;
158 }
159
160 void
161 FinalizeUiFramework(void)
162 {
163         _ControlImplManager::Release();
164 }
165
166
167 namespace Tizen { namespace Ui {
168
169 _ControlImplManager* _ControlImplManager::__pInstance(null);
170
171 // Explicitly initialize just before the Form is created.
172 void
173 _ControlImplManager::Initialize(void)
174 {
175         static pthread_once_t once_block = PTHREAD_ONCE_INIT;
176
177         if (!__pInstance)
178         {
179                 pthread_once(&once_block, InitInstance);
180         }
181
182         _ControlManager::Initialize();
183
184         result r = InitializeTestbuddyManager();
185         SetLastResult(r);
186 }
187
188 void
189 _ControlImplManager::Release(void)
190 {
191         _ControlManager::Release();
192
193         delete __pInstance;
194         __pInstance = null;
195 }
196
197 _ControlImplManager*
198 _ControlImplManager::GetInstance(void)
199 {
200         return __pInstance;
201 }
202
203 void
204 _ControlImplManager::InitInstance(void)
205 {
206         if (__pInstance != null)
207         {
208                 return;
209         }
210
211         __pInstance = new (std::nothrow) _ControlImplManager;
212         SysAssert(__pInstance);
213 }
214
215 _ControlImplManager::_ControlImplManager(void)
216         : __pFrameImpl(null)
217 {
218         _KeyEventManagerImpl::Initialize();
219         _FocusManagerImpl::Initialize();
220 }
221
222 _ControlImplManager::~_ControlImplManager(void)
223 {
224         _FocusManagerImpl::ReleaseInstance();
225         _KeyEventManagerImpl::ReleaseInstance();
226 }
227
228 _WindowImpl*
229 _ControlImplManager::GetCurrentFrame(void) const
230 {
231         _Window* pFrame = _ControlManager::GetInstance()->GetCurrentFrame();
232         if (pFrame == null)
233         {
234                 return null;
235         }
236
237         _WindowImpl* pImpl = static_cast <_WindowImpl*>(pFrame->GetUserData());
238         return pImpl;
239 }
240
241 void
242 _ControlImplManager::OnScreenRotated(int rotation)
243 {
244         _ControlManager* pCore = _ControlManager::GetInstance();
245         SysAssert(pCore);
246
247         pCore->OnScreenRotated(rotation);
248
249 #if !defined(MULTI_WINDOW)
250         Controls::_FrameImpl* pFrameImpl = dynamic_cast<Controls::_FrameImpl*>(GetCurrentFrame());
251         if (pFrameImpl == null)
252         {
253                 return;
254         }
255
256         Controls::_FormImpl* pCurrentFormImpl = pFrameImpl->GetCurrentForm();
257
258         if (pCurrentFormImpl != null)
259         {
260                 pCurrentFormImpl->UpdateOrientationStatus(true);
261         }
262         else
263         {
264                 pFrameImpl->UpdateOrientationStatus();
265         }
266 #endif
267 }
268
269 void
270 _ControlImplManager::SetOrientationStatus(OrientationStatus orientationStatus)
271 {
272         _ControlManager* pCore = _ControlManager::GetInstance();
273         SysAssert(pCore);
274
275         pCore->SetOrientationStatus(::Convert(orientationStatus));
276 }
277
278 #if !defined(MULTI_WINDOW)
279 void
280 _ControlImplManager::RotateScreen(OrientationStatus orientationStatus)
281 {
282         _ControlManager* pCore = _ControlManager::GetInstance();
283         SysAssert(pCore);
284
285         pCore->RotateScreen(::Convert(orientationStatus));
286 }
287 #else
288 void
289 _ControlImplManager::RotateScreen(_ControlImpl* pControlImpl, OrientationStatus orientationStatus)
290 {
291         _ControlManager* pCore = _ControlManager::GetInstance();
292         SysAssert(pCore);
293
294         pCore->RotateScreen(pControlImpl->GetCore(), ::Convert(orientationStatus));
295 }
296 #endif
297
298 OrientationStatus
299 _ControlImplManager::GetOrientationStatus(Orientation mode) const
300 {
301         _ControlManager* pCore = _ControlManager::GetInstance();
302         SysAssert(pCore);
303
304         const _ControlRotation screenRotation = pCore->GetScreenRotation();
305
306         if (mode == ORIENTATION_AUTOMATIC_FOUR_DIRECTION)
307         {
308                 return ::Convert(screenRotation, true);
309         }
310
311         if (mode == ORIENTATION_AUTOMATIC)
312         {
313                 return ::Convert(screenRotation, false);
314         }
315
316         OrientationStatus status = ORIENTATION_STATUS_NONE;
317         switch (mode)
318         {
319         case ORIENTATION_PORTRAIT:
320                 status =  ORIENTATION_STATUS_PORTRAIT;
321                 break;
322         case ORIENTATION_LANDSCAPE:
323                 status =  ORIENTATION_STATUS_LANDSCAPE;
324                 break;
325         case ORIENTATION_PORTRAIT_REVERSE:
326                 status = ORIENTATION_STATUS_PORTRAIT_REVERSE;
327                 break;
328         case ORIENTATION_LANDSCAPE_REVERSE:
329                 status = ORIENTATION_STATUS_LANDSCAPE_REVERSE;
330                 break;
331         default:
332                 SysAssert(false);
333         }
334
335         return status;
336 }
337
338 OrientationStatus
339 _ControlImplManager::GetFormOrientationStatus(_ControlImpl *pControlImpl)
340 {
341         _FormImpl* pParentImpl = null;
342         OrientationStatus orientation = ORIENTATION_STATUS_NONE;
343         while(pControlImpl)
344         {
345                 pParentImpl = dynamic_cast<_FormImpl*>(pControlImpl->GetParent());
346
347                 if (pParentImpl != null)
348                 {
349                         break;
350                 }
351                 else
352                 {
353                         pControlImpl = pControlImpl->GetParent();
354                 }
355         }
356
357         if (pParentImpl)
358         {
359                 orientation = pParentImpl->GetOrientationStatus();
360         }
361
362         return orientation;
363 }
364
365 }} // Tizen::Ui