Changed indicator bg color.
[platform/framework/native/uifw.git] / src / ui / FUi_KeyEventManagerImpl.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_KeyEventManagerImpl.cpp
19 * @brief                This is the implementation file for _KeyEventManagerImpl class.
20 * @version              2.0
21 * @see                  Tizen::Ui::_KeyEventManagerImpl
22 *
23 */
24
25 // includes
26 #include <new>
27 #include <FBaseColIEnumeratorT.h>
28 #include <FBaseRtIEventListener.h>
29 #include <FUiIKeyEventListener.h>
30 #include <FUiControl.h>
31 #include <FBaseSysLog.h>
32 #include "FUi_KeyEventManagerImpl.h"
33 #include "FUi_ControlImpl.h"
34 #include "FUi_Control.h"
35 #include "FUi_KeyEventManager.h"
36
37 // using namespaces
38 using namespace Tizen::Ui;
39
40 namespace Tizen { namespace Ui
41 {
42
43 ////////////////////////////////////////////////////////////////////////////////
44 /// _KeyEventManagerImpl class Lifecycle
45
46 class _KeyEventManagerImpl::PublicEventListener
47         : public _IKeyEventListener
48         , virtual public _IUiEventListener
49         , virtual public Tizen::Base::Runtime::IEventListener
50 {
51 public:
52         PublicEventListener(_KeyEventManagerImpl& mgrImpl)
53                 : __keyMgrImpl(mgrImpl)
54         {
55         }
56
57         virtual bool OnKeyPressed(const _Control& source, const _KeyInfo& keyInfo)
58         {
59                 if (keyInfo.GetKeyCode() > _KEY_HARDWARE_MAX)
60                 {
61                         return false;
62                 }
63
64                 Tizen::Base::Collection::IEnumeratorT<IKeyEventListener*>* pEnum = __keyMgrImpl.__pPublicKeyEventListeners->GetEnumeratorN();
65                 _ControlImpl* pImpl = static_cast<_ControlImpl*>(source.GetUserData());
66                 SysTryCatch(NID_UI, pImpl != null, , E_SYSTEM, "[E_SYSTEM] System error occurred. ");
67
68                 if (pEnum)
69                 {
70                         while (pEnum->MoveNext() == E_SUCCESS)
71                         {
72                                 IKeyEventListener* pKeyEventListener = null;
73                                 pEnum->GetCurrent(pKeyEventListener);
74
75                                 if (pKeyEventListener != null)
76                                 {
77                                         pKeyEventListener->OnKeyPressed(pImpl->GetPublic(), static_cast<KeyCode>(keyInfo.GetKeyCode()));
78                                 }
79                         }
80                 }
81
82                 delete pEnum;
83                 return true;
84
85 CATCH:
86                 delete pEnum;
87                 return false;
88         };
89
90         virtual bool OnKeyReleased(const _Control& source, const _KeyInfo& keyInfo)
91         {
92                 if (keyInfo.GetKeyCode() > _KEY_HARDWARE_MAX)
93                 {
94                         return false;
95                 }
96
97                 Tizen::Base::Collection::IEnumeratorT<IKeyEventListener*>* pEnum = __keyMgrImpl.__pPublicKeyEventListeners->GetEnumeratorN();
98                 _ControlImpl* pImpl = static_cast<_ControlImpl*>(source.GetUserData());
99                 SysTryCatch(NID_UI, pImpl != null, , E_SYSTEM, "[E_SYSTEM] System error occurred. ");
100
101                 if (pEnum)
102                 {
103                         while (pEnum->MoveNext() == E_SUCCESS)
104                         {
105                                 IKeyEventListener* pKeyEventListener = null;
106                                 pEnum->GetCurrent(pKeyEventListener);
107
108                                 if (pKeyEventListener != null)
109                                 {
110                                         pKeyEventListener->OnKeyReleased(pImpl->GetPublic(), static_cast<KeyCode>(keyInfo.GetKeyCode()));
111                                 }
112                         }
113                 }
114
115                 delete pEnum;
116                 return true;
117
118 CATCH:
119                 delete pEnum;
120                 return false;
121         };
122
123 private:
124         PublicEventListener(const PublicEventListener&);
125         PublicEventListener& operator =(const PublicEventListener&);
126
127 private:
128         _KeyEventManagerImpl& __keyMgrImpl;
129 };
130
131
132 _KeyEventManagerImpl* _KeyEventManagerImpl::__pInstance = null;
133
134 _KeyEventManagerImpl::_KeyEventManagerImpl(void)
135 {
136         __pPublicKeyEventListeners = new (std::nothrow) Tizen::Base::Collection::LinkedListT <IKeyEventListener*>;
137         SysTryReturnVoidResult(NID_UI, __pPublicKeyEventListeners, E_OUT_OF_MEMORY,
138                                         "[E_OUT_OF_MEMORY] Memory allocation failed.");
139
140         __pKeyEventListeners = new (std::nothrow) PublicEventListener(*this);
141         SysTryReturnVoidResult(NID_UI, __pKeyEventListeners, E_OUT_OF_MEMORY,
142                                         "[E_OUT_OF_MEMORY] Memory allocation failed.");
143
144         _KeyEventManager* pKeyMgr = _KeyEventManager::GetInstance();
145         if (pKeyMgr != null)
146         {
147                 pKeyMgr->AddKeyEventListener(*__pKeyEventListeners);
148         }
149 }
150
151 _KeyEventManagerImpl::~_KeyEventManagerImpl(void)
152 {
153         if (__pPublicKeyEventListeners)
154         {
155                 delete __pPublicKeyEventListeners;
156                 __pPublicKeyEventListeners = null;
157         }
158
159         if (__pKeyEventListeners)
160         {
161                 delete __pKeyEventListeners;
162                 __pKeyEventListeners = null;
163         }
164 }
165
166 void
167 _KeyEventManagerImpl::Initialize(void)
168 {
169         static pthread_once_t once_block = PTHREAD_ONCE_INIT;
170
171         if (__pInstance == null)
172         {
173                 pthread_once(&once_block, InitializeInstance);
174         }
175 }
176
177 _KeyEventManagerImpl*
178 _KeyEventManagerImpl::GetInstance(void)
179 {
180         return __pInstance;
181 }
182
183 void
184 _KeyEventManagerImpl::InitializeInstance(void)
185 {
186         ClearLastResult();
187
188         if (__pInstance == null)
189         {
190                 __pInstance = new (std::nothrow) _KeyEventManagerImpl;
191                 SysTryReturnVoidResult(NID_UI, __pInstance != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Memory allocation failed.");
192         }
193 }
194
195 void
196 _KeyEventManagerImpl::ReleaseInstance(void)
197 {
198         if (__pInstance)
199         {
200                 delete __pInstance;
201                 __pInstance = null;
202         }
203 }
204
205 ////////////////////////////////////////////////////////////////////////////////
206 /// KeyEventManager class Operation
207
208 void
209 _KeyEventManagerImpl::AddKeyEventListener(IKeyEventListener& listener)
210 {
211         __pPublicKeyEventListeners->Add(&listener);
212 }
213
214
215 void
216 _KeyEventManagerImpl::RemoveKeyEventListener(IKeyEventListener& listener)
217 {
218         __pPublicKeyEventListeners->Remove(&listener);
219 }
220
221 } } //Tizen::Ui