Support to build against ubuntu environment.
[platform/core/uifw/dali-adaptor.git] / adaptors / ubuntu / accessibility-manager-impl-ubuntu.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
23 #include <dali/public-api/dali-core.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/integration-api/events/touch-event-integ.h>
26 #include <dali/integration-api/events/gesture-requests.h>
27 #include "system-settings.h"
28
29 namespace Dali
30 {
31
32 namespace Internal
33 {
34
35 namespace Adaptor
36 {
37
38 namespace
39 {
40 #if defined(DEBUG_ENABLED)
41 Debug::Filter* gAccessibilityManagerLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_ACCESSIBILITY_MANAGER");
42 #endif
43
44 BaseHandle Create()
45 {
46   BaseHandle handle( AccessibilityManager::Get() );
47
48   if ( !handle && Adaptor::IsAvailable() )
49   {
50     Adaptor& adaptorImpl( Adaptor::GetImplementation( Adaptor::Get() ) );
51     Dali::AccessibilityManager manager = Dali::AccessibilityManager( new AccessibilityManager() );
52     adaptorImpl.RegisterSingleton( typeid( manager ), manager );
53     handle = manager;
54   }
55
56   return handle;
57 }
58 TypeRegistration ACCESSIBILITY_MANAGER_TYPE( typeid(Dali::AccessibilityManager), typeid(Dali::BaseHandle), Create, true /* Create Instance At Startup */ );
59
60 } // unnamed namespace
61
62 Dali::AccessibilityManager AccessibilityManager::Get()
63 {
64   Dali::AccessibilityManager manager;
65
66   if ( Adaptor::IsAvailable() )
67   {
68     // Check whether the singleton is already created
69     Dali::BaseHandle handle = Dali::Adaptor::Get().GetSingleton( typeid( Dali::AccessibilityManager ) );
70     if(handle)
71     {
72       // If so, downcast the handle
73       manager = Dali::AccessibilityManager( dynamic_cast< AccessibilityManager* >( handle.GetObjectPtr() ) );
74     }
75   }
76
77   return manager;
78 }
79
80 Vector2 AccessibilityManager::GetReadPosition() const
81 {
82   return mReadPosition;
83 }
84
85 void AccessibilityManager::SetActionHandler(AccessibilityActionHandler& handler)
86 {
87   mActionHandler = &handler;
88 }
89
90 void AccessibilityManager::SetGestureHandler(AccessibilityGestureHandler& handler)
91 {
92   if( mAccessibilityGestureDetector )
93   {
94     mAccessibilityGestureDetector->SetGestureHandler(handler);
95   }
96 }
97
98 bool AccessibilityManager::HandleActionClearFocusEvent()
99 {
100   bool ret = false;
101
102   Dali::AccessibilityManager handle( this );
103
104   /*
105    * In order to application decide reading action first,
106    * emit ActionClearFocus signal in first, ClearAccessibilityFocus for handler in next
107    */
108   if ( !mIndicatorFocused )
109   {
110     if( !mActionClearFocusSignalV2.Empty() )
111     {
112       mActionClearFocusSignalV2.Emit( handle );
113     }
114   }
115
116   if( mActionHandler )
117   {
118     ret = mActionHandler->ClearAccessibilityFocus();
119   }
120
121   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
122
123   return ret;
124 }
125
126 bool AccessibilityManager::HandleActionScrollEvent(const TouchPoint& point, unsigned long timeStamp)
127 {
128   bool ret = false;
129
130   Dali::AccessibilityManager handle( this );
131
132   Dali::TouchEvent touchEvent(timeStamp);
133   touchEvent.points.push_back(point);
134
135   /*
136    * In order to application decide touch action first,
137    * emit ActionScroll signal in first, AccessibilityActionScroll  for handler in next
138    */
139   if ( !mIndicatorFocused )
140   {
141     if( !mActionScrollSignalV2.Empty() )
142     {
143       mActionScrollSignalV2.Emit( handle, touchEvent );
144     }
145   }
146
147   Integration::TouchEvent event;
148   if (mCombiner.GetNextTouchEvent(point, timeStamp, event))
149   {
150     // Process the touch event in accessibility gesture detector
151     if( mAccessibilityGestureDetector )
152     {
153       mAccessibilityGestureDetector->SendEvent(event);
154       ret = true;
155     }
156   }
157
158   return ret;
159 }
160
161 bool AccessibilityManager::HandleActionTouchEvent(const TouchPoint& point, unsigned long timeStamp)
162 {
163   bool ret = false;
164
165   Dali::TouchEvent touchEvent(timeStamp);
166   touchEvent.points.push_back(point);
167
168   if( mActionHandler )
169   {
170     ret = mActionHandler->AccessibilityActionTouch(touchEvent);
171   }
172   return ret;
173 }
174
175 bool AccessibilityManager::HandleActionBackEvent()
176 {
177   bool ret = false;
178
179   Dali::AccessibilityManager handle( this );
180
181   /*
182    * In order to application decide reading action first,
183    * emit ActionBack signal in first, AccessibilityActionBack for handler in next
184    */
185   if ( !mIndicatorFocused )
186   {
187     if( !mActionBackSignalV2.Empty() )
188     {
189       mActionBackSignalV2.Emit( handle );
190     }
191   }
192
193   if( mActionHandler )
194   {
195     ret = mActionHandler->AccessibilityActionBack();
196   }
197
198   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
199
200   return ret;
201 }
202
203 void AccessibilityManager::HandleActionEnableEvent()
204 {
205   EnableAccessibility();
206 }
207
208 void AccessibilityManager::HandleActionDisableEvent()
209 {
210   DisableAccessibility();
211 }
212
213 void AccessibilityManager::EnableAccessibility()
214 {
215   if(mIsEnabled == false)
216   {
217     mIsEnabled = true;
218
219     if( mActionHandler )
220     {
221       mActionHandler->ChangeAccessibilityStatus();
222     }
223
224     //emit status changed signal
225     Dali::AccessibilityManager handle( this );
226     mStatusChangedSignalV2.Emit( handle );
227   }
228 }
229
230 void AccessibilityManager::DisableAccessibility()
231 {
232   if(mIsEnabled == true)
233   {
234     mIsEnabled = false;
235
236     if( mActionHandler )
237     {
238       mActionHandler->ChangeAccessibilityStatus();
239     }
240
241     //emit status changed signal
242     Dali::AccessibilityManager handle( this );
243     mStatusChangedSignalV2.Emit( handle );
244
245     // Destroy the TtsPlayer if exists.
246     Dali::Adaptor& adaptor = Dali::Adaptor::Get();
247     Adaptor::GetImplementation(adaptor).DestroyTtsPlayer(Dali::TtsPlayer::SCREEN_READER);
248   }
249 }
250
251 bool AccessibilityManager::IsEnabled() const
252 {
253   return mIsEnabled;
254 }
255
256 void AccessibilityManager::SetIndicator(Indicator* indicator)
257 {
258   mIndicator = indicator;
259 }
260
261 AccessibilityManager::AccessibilityManager()
262 : mIsEnabled(false),
263   mActionHandler(NULL),
264   mIndicator(NULL),
265   mIndicatorFocused(false)
266 {
267   int isEnabled = 0;
268   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, isEnabled?"ENABLED":"DISABLED");
269
270   if(isEnabled == 1)
271   {
272     mIsEnabled = true;
273   }
274   else
275   {
276     mIsEnabled = false;
277   }
278
279   mAccessibilityGestureDetector = new AccessibilityGestureDetector();
280 }
281
282 AccessibilityManager::~AccessibilityManager()
283 {
284 }
285
286 bool AccessibilityManager::HandleActionNextEvent(bool allowEndFeedback)
287 {
288   bool ret = false;
289   Dali::AccessibilityManager handle( this );
290
291   /*
292    * In order to application decide reading action first,
293    * emit ActionNext signal in first, AccessibilityActionNext for handler in next
294    */
295   if( !mIndicatorFocused )
296   {
297     if( !mActionNextSignalV2.Empty() )
298     {
299       mActionNextSignalV2.Emit( handle );
300     }
301   }
302
303   if( mActionHandler )
304   {
305     ret = mActionHandler->AccessibilityActionNext(allowEndFeedback);
306   }
307
308   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
309
310   return ret;
311 }
312
313 bool AccessibilityManager::HandleActionPreviousEvent(bool allowEndFeedback)
314 {
315   bool ret = false;
316
317   Dali::AccessibilityManager handle( this );
318
319    /*
320    * In order to application decide reading action first,
321    * emit ActionPrevious signal in first, AccessibilityActionPrevious for handler in next
322    */
323  if ( !mIndicatorFocused )
324   {
325     if( !mActionPreviousSignalV2.Empty() )
326     {
327       mActionPreviousSignalV2.Emit( handle );
328     }
329   }
330
331   if( mActionHandler )
332   {
333     ret = mActionHandler->AccessibilityActionPrevious(allowEndFeedback);
334   }
335
336   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
337
338   return ret;
339 }
340
341 bool AccessibilityManager::HandleActionActivateEvent()
342 {
343   bool ret = false;
344
345   Dali::AccessibilityManager handle( this );
346
347   /*
348    * In order to application decide reading action first,
349    * emit ActionActivate signal in first, AccessibilityActionActivate for handler in next
350    */
351   if ( !mIndicatorFocused )
352   {
353     if( !mActionActivateSignalV2.Empty() )
354     {
355       mActionActivateSignalV2.Emit( handle );
356     }
357   }
358
359   if( mActionHandler )
360   {
361     ret = mActionHandler->AccessibilityActionActivate();
362   }
363
364   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
365
366   return ret;
367 }
368
369 bool AccessibilityManager::HandleActionReadEvent(unsigned int x, unsigned int y, bool allowReadAgain)
370 {
371   bool ret = false;
372
373   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %d , %d\n", __FUNCTION__, __LINE__, x, y);
374
375   mReadPosition.x = x;
376   mReadPosition.y = y;
377
378   Dali::AccessibilityManager handle( this );
379
380   bool indicatorFocused = false;
381
382   // Check whether the Indicator is focused
383   if( mIndicator && mIndicator->IsConnected() )
384   {
385     // Check the position and size of Indicator actor
386     Dali::Actor indicatorActor = mIndicator->GetActor();
387     Vector3 position = Vector3(0.0f, 0.0f, 0.0f);
388     Vector3 size = indicatorActor.GetCurrentSize();
389
390     if(mReadPosition.x >= position.x &&
391        mReadPosition.x <= position.x + size.width &&
392        mReadPosition.y >= position.y &&
393        mReadPosition.y <= position.y + size.height)
394     {
395       indicatorFocused = true;
396       DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] Indicator area!!!!\n", __FUNCTION__, __LINE__);
397     }
398   }
399
400   if( mIndicator )
401   {
402     if( !mIndicatorFocused && indicatorFocused )
403     {
404       // If Indicator is focused, the focus should be cleared in Dali focus chain.
405       if( mActionHandler )
406       {
407         mActionHandler->ClearAccessibilityFocus();
408       }
409     }
410
411     mIndicatorFocused = indicatorFocused;
412
413     // Send accessibility READ action information to Indicator
414     if( mIndicatorFocused )
415     {
416       DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] Send READ message to indicator!!!!\n", __FUNCTION__, __LINE__);
417     }
418   }
419
420   if(allowReadAgain)
421   {
422     /*
423      * In order to application decide reading action first,
424      * emit ActionRead signal in first, AccessibilityActionRead for handler in next
425      */
426     if( !mIndicatorFocused )
427     {
428       if ( !mActionReadSignalV2.Empty() )
429       {
430         mActionReadSignalV2.Emit( handle );
431       }
432     }
433   }
434   else
435   {
436     /*
437      * In order to application decide reading action first,
438      * emit ActionRead signal in first, AccessibilityActionRead for handler in next
439      */
440     if( !mIndicatorFocused )
441     {
442       if ( !mActionOverSignalV2.Empty() )
443       {
444         mActionOverSignalV2.Emit( handle );
445       }
446     }
447   }
448
449   if( mActionHandler && !mIndicatorFocused)
450   {
451     // If Indicator is not focused, the accessibility actions should be handled by the registered
452     // accessibility action handler (e.g. focus manager)
453     ret = mActionHandler->AccessibilityActionRead(allowReadAgain);
454     DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
455   }
456
457   return ret;
458 }
459
460 bool AccessibilityManager::HandleActionReadNextEvent(bool allowEndFeedback)
461 {
462   bool ret = false;
463
464   Dali::AccessibilityManager handle( this );
465
466   /*
467    * In order to application decide reading action first,
468    * emit ActionReadNext signal in first, AccessibilityActionReadNext for handler in next
469    */
470   if ( !mIndicatorFocused )
471   {
472     if( !mActionReadNextSignalV2.Empty() )
473     {
474       mActionReadNextSignalV2.Emit( handle );
475     }
476   }
477
478   if( mActionHandler )
479   {
480     ret = mActionHandler->AccessibilityActionReadNext(allowEndFeedback);
481   }
482
483   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
484
485   return ret;
486 }
487
488 bool AccessibilityManager::HandleActionReadPreviousEvent(bool allowEndFeedback)
489 {
490   bool ret = false;
491
492   Dali::AccessibilityManager handle( this );
493
494   /*
495    * In order to application decide reading action first,
496    * emit ActionReadPrevious signal in first, AccessibilityActionReadPrevious for handler in next
497    */
498   if ( !mIndicatorFocused )
499   {
500     if( !mActionReadPreviousSignalV2.Empty() )
501     {
502       mActionReadPreviousSignalV2.Emit( handle );
503     }
504   }
505
506   if( mActionHandler )
507   {
508     ret = mActionHandler->AccessibilityActionReadPrevious(allowEndFeedback);
509   }
510
511   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
512
513   return ret;
514 }
515
516 bool AccessibilityManager::HandleActionUpEvent()
517 {
518   bool ret = false;
519
520   Dali::AccessibilityManager handle( this );
521
522   /*
523    * In order to application decide reading action first,
524    * emit ActionUp signal in first, AccessibilityActionUp for handler in next
525    */
526   if ( !mIndicatorFocused )
527   {
528     if( !mActionUpSignalV2.Empty() )
529     {
530       mActionUpSignalV2.Emit( handle );
531     }
532   }
533
534   if( mActionHandler )
535   {
536     ret = mActionHandler->AccessibilityActionUp();
537   }
538
539   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
540
541   return ret;
542 }
543
544 bool AccessibilityManager::HandleActionDownEvent()
545 {
546   bool ret = false;
547
548   Dali::AccessibilityManager handle( this );
549
550   /*
551    * In order to application decide reading action first,
552    * emit ActionDown signal in first, AccessibilityActionDown for handler in next
553    */
554   if ( !mIndicatorFocused )
555   {
556     if( !mActionDownSignalV2.Empty() )
557     {
558       mActionDownSignalV2.Emit( handle );
559     }
560   }
561
562   if( mActionHandler )
563   {
564     ret = mActionHandler->AccessibilityActionDown();
565   }
566
567   DALI_LOG_INFO(gAccessibilityManagerLogFilter, Debug::General, "[%s:%d] %s\n", __FUNCTION__, __LINE__, ret?"TRUE":"FALSE");
568
569   return ret;
570 }
571
572 } // namespace Adaptor
573
574 } // namespace Internal
575
576 } // namespace Dali