modify license, permission and remove ^M char
[platform/framework/native/uifw.git] / src / ui / FUi_DimmingManager.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0/
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_DimmingManager.cpp
19  * @brief               This is the implementation file for the _DimmingManager class.
20  */
21
22 #include <FBaseSysLog.h>
23 #include "FUi_Window.h"
24 #include "FUi_DimmingManager.h"
25
26 using namespace Tizen::Ui;
27
28 namespace Tizen { namespace Ui {
29
30 _DimmingManager* _DimmingManager::__pInstance = null;
31
32 _DimmingManager*
33 _DimmingManager::GetInstance(void)
34 {
35         if (__pInstance == null)
36         {
37                 Initialize();
38         }
39         return __pInstance;
40 }
41
42 void
43 _DimmingManager::Initialize(void)
44 {
45         static pthread_once_t once_block = PTHREAD_ONCE_INIT;
46
47         if (!__pInstance)
48         {
49                 pthread_once(&once_block, InitializeInstance);
50         }
51 }
52
53 void
54 _DimmingManager::InitializeInstance(void)
55 {
56         ClearLastResult();
57
58         if (__pInstance == null)
59         {
60                 __pInstance = new (std::nothrow) _DimmingManager;
61                 SysTryReturnVoidResult(NID_UI, __pInstance != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
62         }
63 }
64
65 void
66 _DimmingManager::ReleaseInstance(void)
67 {
68         if (__pInstance)
69         {
70                 delete __pInstance;
71                 __pInstance = null;
72         }
73 }
74
75 _DimmingManager::_DimmingManager(void)
76         : __pWindowList(null)
77 {
78         result r = E_SUCCESS;
79         __pWindowList = new (std::nothrow) WindowList;
80         SysTryReturnVoidResult(NID_UI, __pWindowList, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory shortage.");
81
82         r = __pWindowList->Construct();
83         SysTryReturnVoidResult(NID_UI, r == E_SUCCESS, r, "[%s] Propagating.", GetErrorMessage(r));
84 }
85
86 _DimmingManager::~_DimmingManager(void)
87 {
88         delete __pWindowList;
89 }
90
91 result
92 _DimmingManager::RegisterWindow(_Window* pWindow)
93 {
94         result r = E_SUCCESS;
95
96         int count = __pWindowList->GetCount();
97         if (count > 0)
98         {
99                 _Window* pOldWindow = null;
100                 r = __pWindowList->GetAt(count - 1, pOldWindow);
101                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
102                 DeleteDimmingLayer(pOldWindow);
103         }
104
105         r = CreateDimmingLayer(pWindow);
106         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
107         r = __pWindowList->Add(pWindow);
108         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
109
110         return r;
111 }
112
113 result
114 _DimmingManager::UnRegisterWindow(_Window* pWindow)
115 {
116         result r = E_SUCCESS;
117
118         DeleteDimmingLayer(pWindow);
119         int count = __pWindowList->GetCount();
120         if (count > 0)
121         {
122                 r = __pWindowList->Remove(pWindow);
123         }
124
125         count = __pWindowList->GetCount();
126         if (count > 0)
127         {
128                 _Window* pOldWindow = null;
129                 r = __pWindowList->GetAt(count - 1, pOldWindow);
130                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
131                 CreateDimmingLayer(pOldWindow);
132         }
133
134         return r;
135 }
136
137 result
138 _DimmingManager::CreateDimmingLayer(_Window* pWindow)
139 {
140         result r = E_SUCCESS;
141
142         _DimmingLayer* pDimmingLayer = new (std::nothrow) _DimmingLayer();
143         SysTryReturn(NID_UI, pDimmingLayer != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
144
145         r = pDimmingLayer->Construct(*pWindow);
146         SysTryCatch(NID_UI, r == E_SUCCESS, , r, "[%s] Propagating.", GetErrorMessage(r));
147         pWindow->SetDimmingLayer(pDimmingLayer);
148
149         return r;
150
151 CATCH:
152         delete pDimmingLayer;
153         return r;
154 }
155
156 void
157 _DimmingManager::DeleteDimmingLayer(_Window* pWindow)
158 {
159         if (pWindow)
160         {
161                 _DimmingLayer* pLayer = pWindow->GetDimmingLayer();
162                 if (pLayer)
163                 {
164                         pLayer->SetDimmingEnabled(false);
165                         delete pLayer;
166                 }
167                 pWindow->SetDimmingLayer(null);
168         }
169 }
170
171 result
172 _DimmingManager::ShowDimmingLayer(_Window* pWindow)
173 {
174         result r = E_SUCCESS;
175
176         _DimmingLayer* pDimmingLayer = pWindow->GetDimmingLayer();
177         if (pDimmingLayer)
178         {
179                 if (pWindow->GetVisibleState())
180                 {
181                         if (pDimmingLayer->IsDimmingEnabled() == false)
182                         {
183                                 r = pDimmingLayer->SetDimmingEnabled(true);
184                                 SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
185                         }
186                 }
187                 else
188                 {
189                         r = pDimmingLayer->SetDimmingEnabled(false);
190                         SysTryReturn(NID_UI, r == E_SUCCESS, r, r, "[%s] Propagating.", GetErrorMessage(r));
191                 }
192         }
193
194         return r;
195 }
196
197 }} // Tizen::Ui