Merge "Add support for new accessibility actions" into devel/master
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / accessibility-adaptor-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-adaptor-impl.h"
20
21 // EXTERNAL INCLUDES
22 #include <vconf.h>
23
24 #include <dali/public-api/object/type-registry.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
46 // TODO: Update vconf-key.h ?
47 const char * DALI_VCONFKEY_SETAPPL_ACCESSIBILITY_DBUS_TTS = "db/setting/accessibility/atspi";
48
49 bool GetEnabledVConf()
50 {
51   int isEnabled = 0;
52   vconf_get_bool( DALI_VCONFKEY_SETAPPL_ACCESSIBILITY_DBUS_TTS, &isEnabled );
53
54   if( isEnabled == 0 )
55   {
56     vconf_get_bool( VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, &isEnabled );
57   }
58
59   return (bool)isEnabled;
60 }
61
62 #if defined(DEBUG_ENABLED)
63 Debug::Filter* gAccessibilityAdaptorLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_ACCESSIBILITY_ADAPTOR" );
64 #endif
65
66 void AccessibilityOnOffNotification(keynode_t* node, void* data)
67 {
68   AccessibilityAdaptor* adaptor = static_cast<AccessibilityAdaptor*>( data );
69
70   bool isEnabled = GetEnabledVConf();
71
72   DALI_LOG_INFO( gAccessibilityAdaptorLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, isEnabled ? "ENABLED" : "DISABLED" );
73
74   if( isEnabled )
75   {
76     adaptor->EnableAccessibility();
77   }
78   else
79   {
80     adaptor->DisableAccessibility();
81   }
82 }
83
84 BaseHandle Create()
85 {
86   BaseHandle handle( AccessibilityAdaptor::Get() );
87
88   if ( !handle )
89   {
90     Dali::SingletonService service( SingletonService::Get() );
91     if ( service )
92     {
93       Dali::AccessibilityAdaptor adaptor = Dali::AccessibilityAdaptor( new AccessibilityAdaptor() );
94       service.Register( typeid( adaptor ), adaptor );
95       handle = adaptor;
96     }
97   }
98
99   return handle;
100 }
101 TypeRegistration ACCESSIBILITY_ADAPTOR_TYPE( typeid(Dali::AccessibilityAdaptor), typeid(Dali::BaseHandle), Create, true /* Create Instance At Startup */ );
102
103 } // unnamed namespace
104
105 Dali::AccessibilityAdaptor AccessibilityAdaptor::Get()
106 {
107   Dali::AccessibilityAdaptor adaptor;
108
109   Dali::SingletonService service( SingletonService::Get() );
110   if ( service )
111   {
112     // Check whether the singleton is already created
113     Dali::BaseHandle handle = service.GetSingleton( typeid( Dali::AccessibilityAdaptor ) );
114     if(handle)
115     {
116       // If so, downcast the handle
117       adaptor = Dali::AccessibilityAdaptor( dynamic_cast< AccessibilityAdaptor* >( handle.GetObjectPtr() ) );
118     }
119   }
120
121   return adaptor;
122 }
123
124 Vector2 AccessibilityAdaptor::GetReadPosition() const
125 {
126   return mReadPosition;
127 }
128
129 void AccessibilityAdaptor::SetActionHandler(AccessibilityActionHandler& handler)
130 {
131   mActionHandler = &handler;
132 }
133
134 void AccessibilityAdaptor::SetGestureHandler(AccessibilityGestureHandler& handler)
135 {
136   if( mAccessibilityGestureDetector )
137   {
138     mAccessibilityGestureDetector->SetGestureHandler(handler);
139   }
140 }
141
142 bool AccessibilityAdaptor::HandleActionClearFocusEvent()
143 {
144   bool ret = false;
145
146   if( mActionHandler )
147   {
148     ret = mActionHandler->ClearAccessibilityFocus();
149   }
150
151   DALI_LOG_INFO(gAccessibilityAdaptorLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
152
153   return ret;
154 }
155
156 bool AccessibilityAdaptor::HandleActionScrollEvent(const TouchPoint& point, unsigned long timeStamp)
157 {
158   bool ret = false;
159
160   // We always need to emit a scroll signal, whether it's only a hover or not,
161   // so always send the action to the action handler.
162   if( mActionHandler )
163   {
164     Dali::TouchEvent event(timeStamp);
165     event.points.push_back(point);
166     ret = mActionHandler->AccessibilityActionScroll( event );
167   }
168
169   Integration::TouchEvent touchEvent;
170   Integration::HoverEvent hoverEvent;
171   Integration::TouchEventCombiner::EventDispatchType type = mCombiner.GetNextTouchEvent(point, timeStamp, touchEvent, hoverEvent);
172   if(type == Integration::TouchEventCombiner::DispatchTouch || type == Integration::TouchEventCombiner::DispatchBoth) // hover event is ignored
173   {
174     // Process the touch event in accessibility gesture detector
175     if( mAccessibilityGestureDetector )
176     {
177       mAccessibilityGestureDetector->SendEvent(touchEvent);
178       ret = true;
179     }
180   }
181
182   return ret;
183 }
184
185 bool AccessibilityAdaptor::HandleActionTouchEvent(const TouchPoint& point, unsigned long timeStamp)
186 {
187   bool ret = false;
188
189   Dali::TouchEvent touchEvent(timeStamp);
190   touchEvent.points.push_back(point);
191
192   if( mActionHandler )
193   {
194     ret = mActionHandler->AccessibilityActionTouch(touchEvent);
195   }
196   return ret;
197 }
198
199 bool AccessibilityAdaptor::HandleActionBackEvent()
200 {
201   bool ret = false;
202
203   if( mActionHandler )
204   {
205     ret = mActionHandler->AccessibilityActionBack();
206   }
207
208   DALI_LOG_INFO(gAccessibilityAdaptorLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
209
210   return ret;
211 }
212
213 void AccessibilityAdaptor::HandleActionEnableEvent()
214 {
215   EnableAccessibility();
216 }
217
218 void AccessibilityAdaptor::HandleActionDisableEvent()
219 {
220   DisableAccessibility();
221 }
222
223 void AccessibilityAdaptor::EnableAccessibility()
224 {
225   if(mIsEnabled == false)
226   {
227     mIsEnabled = true;
228
229     if( mActionHandler )
230     {
231       mActionHandler->ChangeAccessibilityStatus();
232     }
233   }
234 }
235
236 void AccessibilityAdaptor::DisableAccessibility()
237 {
238   if(mIsEnabled == true)
239   {
240     mIsEnabled = false;
241
242     if( mActionHandler )
243     {
244       mActionHandler->ChangeAccessibilityStatus();
245     }
246
247     // Destroy the TtsPlayer if exists.
248     if ( Adaptor::IsAvailable() )
249     {
250       Dali::Adaptor& adaptor = Dali::Adaptor::Get();
251       Adaptor& adaptorIpml = Adaptor::GetImplementation( adaptor );
252       adaptorIpml.DestroyTtsPlayer( Dali::TtsPlayer::SCREEN_READER );
253     }
254   }
255 }
256
257 bool AccessibilityAdaptor::IsEnabled() const
258 {
259   return mIsEnabled;
260 }
261
262 void AccessibilityAdaptor::SetIndicator(Indicator* indicator)
263 {
264   mIndicator = indicator;
265 }
266
267 AccessibilityAdaptor::AccessibilityAdaptor()
268 : mIsEnabled(false),
269   mActionHandler(NULL),
270   mIndicator(NULL),
271   mIndicatorFocused(false)
272 {
273   mIsEnabled = GetEnabledVConf();
274   DALI_LOG_INFO( gAccessibilityAdaptorLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, mIsEnabled ? "ENABLED" : "DISABLED" );
275
276   vconf_notify_key_changed( DALI_VCONFKEY_SETAPPL_ACCESSIBILITY_DBUS_TTS, AccessibilityOnOffNotification, this );
277   vconf_notify_key_changed( VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, AccessibilityOnOffNotification, this );
278
279   mAccessibilityGestureDetector = new AccessibilityGestureDetector();
280 }
281
282 AccessibilityAdaptor::~AccessibilityAdaptor()
283 {
284   vconf_ignore_key_changed( VCONFKEY_SETAPPL_ACCESSIBILITY_TTS, AccessibilityOnOffNotification );
285   vconf_ignore_key_changed( DALI_VCONFKEY_SETAPPL_ACCESSIBILITY_DBUS_TTS, AccessibilityOnOffNotification );
286 }
287
288 } // namespace Adaptor
289
290 } // namespace Internal
291
292 } // namespace Dali