Adaptor: Fix Klocwork issues
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / accessibility-manager-impl-tizen.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include "accessibility-manager-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <vconf.h>
23
24 #include <dali/public-api/dali-core.h>
25 #include <dali/integration-api/debug.h>
26 #include <dali/integration-api/events/touch-event-integ.h>
27 #include <dali/integration-api/events/hover-event-integ.h>
28 #include <dali/integration-api/events/gesture-requests.h>
29
30 // INTERNAL INCLUDES
31 #include <singleton-service-impl.h>
32 #include <system-settings.h>
33
34 namespace Dali
35 {
36
37 namespace Internal
38 {
39
40 namespace Adaptor
41 {
42
43 namespace
44 {
45 #if defined(DEBUG_ENABLED)
46 Debug::Filter* gAccessibilityManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ACCESSIBILITY_MANAGER");
47 #endif
48
49 void AccessibilityOnOffNotification(keynode_t* node, void* data)
50 {
51   AccessibilityManager* manager = static_cast<AccessibilityManager*>(data);
52   int isEnabled = 0;
53   vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &isEnabled);
54
55   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, isEnabled?"ENABLED":"DISABLED");
56
57   if(isEnabled == 1)
58   {
59     manager->EnableAccessibility();
60   }
61   else
62   {
63     manager->DisableAccessibility();
64   }
65 }
66
67 BaseHandle Create()
68 {
69   BaseHandle handle( AccessibilityManager::Get() );
70
71   if ( !handle )
72   {
73     Dali::SingletonService service( SingletonService::Get() );
74     if ( service )
75     {
76       Dali::AccessibilityManager manager = Dali::AccessibilityManager( new AccessibilityManager() );
77       service.Register( typeid( manager ), manager );
78       handle = manager;
79     }
80   }
81
82   return handle;
83 }
84 TypeRegistration ACCESSIBILITY_MANAGER_TYPE( typeid(Dali::AccessibilityManager), typeid(Dali::BaseHandle), Create, true /* Create Instance At Startup */ );
85
86 } // unnamed namespace
87
88 Dali::AccessibilityManager AccessibilityManager::Get()
89 {
90   Dali::AccessibilityManager manager;
91
92   Dali::SingletonService service( SingletonService::Get() );
93   if ( service )
94   {
95     // Check whether the singleton is already created
96     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::AccessibilityManager ) );
97     if(handle)
98     {
99       // If so, downcast the handle
100       manager = Dali::AccessibilityManager( dynamic_cast< AccessibilityManager* >( handle.GetObjectPtr() ) );
101     }
102   }
103
104   return manager;
105 }
106
107 Vector2 AccessibilityManager::GetReadPosition() const
108 {
109   return mReadPosition;
110 }
111
112 void AccessibilityManager::SetActionHandler(AccessibilityActionHandler& handler)
113 {
114   mActionHandler = &handler;
115 }
116
117 void AccessibilityManager::SetGestureHandler(AccessibilityGestureHandler& handler)
118 {
119   if( mAccessibilityGestureDetector )
120   {
121     mAccessibilityGestureDetector->SetGestureHandler(handler);
122   }
123 }
124
125 bool AccessibilityManager::HandleActionClearFocusEvent()
126 {
127   bool ret = false;
128
129   Dali::AccessibilityManager handle( this );
130
131   /*
132    * In order to application decide reading action first,
133    * emit ActionClearFocus signal in first, ClearAccessibilityFocus for handler in next
134    */
135   if ( !mIndicatorFocused )
136   {
137     if( !mActionClearFocusSignalV2.Empty() )
138     {
139       mActionClearFocusSignalV2.Emit( handle );
140     }
141   }
142
143   if( mActionHandler )
144   {
145     ret = mActionHandler->ClearAccessibilityFocus();
146   }
147
148   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
149
150   return ret;
151 }
152
153 bool AccessibilityManager::HandleActionScrollEvent(const TouchPoint& point, unsigned long timeStamp)
154 {
155   bool ret = false;
156
157   Dali::AccessibilityManager handle( this );
158
159   Dali::TouchEvent event(timeStamp);
160   event.points.push_back(point);
161
162   /*
163    * In order to application decide touch action first,
164    * emit ActionScroll signal in first, AccessibilityActionScroll  for handler in next
165    */
166   if ( !mIndicatorFocused )
167   {
168     if( !mActionScrollSignalV2.Empty() )
169     {
170       mActionScrollSignalV2.Emit( handle, event );
171     }
172   }
173
174   Integration::TouchEvent touchEvent;
175   Integration::HoverEvent hoverEvent;
176   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
177   if(type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth) // hover event is ignored
178   {
179     // Process the touch event in accessibility gesture detector
180     if( mAccessibilityGestureDetector )
181     {
182       mAccessibilityGestureDetector->SendEvent(touchEvent);
183       ret = true;
184     }
185   }
186
187   return ret;
188 }
189
190 bool AccessibilityManager::HandleActionTouchEvent(const TouchPoint& point, unsigned long timeStamp)
191 {
192   bool ret = false;
193
194   Dali::TouchEvent touchEvent(timeStamp);
195   touchEvent.points.push_back(point);
196
197   if( mActionHandler )
198   {
199     ret = mActionHandler->AccessibilityActionTouch(touchEvent);
200   }
201   return ret;
202 }
203
204 bool AccessibilityManager::HandleActionBackEvent()
205 {
206   bool ret = false;
207
208   Dali::AccessibilityManager handle( this );
209
210   /*
211    * In order to application decide reading action first,
212    * emit ActionBack signal in first, AccessibilityActionBack for handler in next
213    */
214   if ( !mIndicatorFocused )
215   {
216     if( !mActionBackSignalV2.Empty() )
217     {
218       mActionBackSignalV2.Emit( handle );
219     }
220   }
221
222   if( mActionHandler )
223   {
224     ret = mActionHandler->AccessibilityActionBack();
225   }
226
227   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
228
229   return ret;
230 }
231
232 void AccessibilityManager::HandleActionEnableEvent()
233 {
234   EnableAccessibility();
235 }
236
237 void AccessibilityManager::HandleActionDisableEvent()
238 {
239   DisableAccessibility();
240 }
241
242 void AccessibilityManager::EnableAccessibility()
243 {
244   if(mIsEnabled == false)
245   {
246     mIsEnabled = true;
247
248     if( mActionHandler )
249     {
250       mActionHandler->ChangeAccessibilityStatus();
251     }
252
253     //emit status changed signal
254     Dali::AccessibilityManager handle( this );
255     mStatusChangedSignalV2.Emit( handle );
256   }
257 }
258
259 void AccessibilityManager::DisableAccessibility()
260 {
261   if(mIsEnabled == true)
262   {
263     mIsEnabled = false;
264
265     if( mActionHandler )
266     {
267       mActionHandler->ChangeAccessibilityStatus();
268     }
269
270     //emit status changed signal
271     Dali::AccessibilityManager handle( this );
272     mStatusChangedSignalV2.Emit( handle );
273
274     // Destroy the TtsPlayer if exists.
275     if ( Adaptor::IsAvailable() )
276     {
277       Dali::Adaptor& adaptor = Dali::Adaptor::Get();
278       Adaptor& adaptorIpml = Adaptor::GetImplementation( adaptor );
279       adaptorIpml.DestroyTtsPlayer( Dali::TtsPlayer::SCREEN_READER );
280     }
281   }
282 }
283
284 bool AccessibilityManager::IsEnabled() const
285 {
286   return mIsEnabled;
287 }
288
289 void AccessibilityManager::SetIndicator(Indicator* indicator)
290 {
291   mIndicator = indicator;
292 }
293
294 AccessibilityManager::AccessibilityManager()
295 : mIsEnabled(false),
296   mActionHandler(NULL),
297   mIndicator(NULL),
298   mIndicatorFocused(false)
299 {
300   int isEnabled = 0;
301   vconf_get_bool(VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &isEnabled);
302   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, isEnabled?"ENABLED":"DISABLED");
303
304   if(isEnabled == 1)
305   {
306     mIsEnabled = true;
307   }
308   else
309   {
310     mIsEnabled = false;
311   }
312
313   vconf_notify_key_changed( VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, AccessibilityOnOffNotification, this );
314
315   mAccessibilityGestureDetector = new AccessibilityGestureDetector();
316 }
317
318 AccessibilityManager::~AccessibilityManager()
319 {
320   vconf_ignore_key_changed( VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, AccessibilityOnOffNotification );
321 }
322
323 } // namespace Adaptor
324
325 } // namespace Internal
326
327 } // namespace Dali