Merge "Add a callback for navigation policy in web engine." into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / devel-api / adaptor-framework / accessibility-bridge.h
1 #ifndef DALI_ADAPTOR_ACCESSIBILITY_BRIDGE_H
2 #define DALI_ADAPTOR_ACCESSIBILITY_BRIDGE_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <dali/public-api/actors/actor.h>
23 #include <dali/public-api/math/rect.h>
24 #include <functional>
25 #include <memory>
26 #include <stdexcept>
27 #include <string>
28 #include <unordered_set>
29
30 // INTERNAL INCLUDES
31 #include <dali/devel-api/adaptor-framework/accessibility.h>
32 #include <dali/public-api/adaptor-framework/window.h>
33
34 namespace Dali
35 {
36 namespace Accessibility
37 {
38 class Accessible;
39
40 /**
41  * @brief Base class for different accessibility bridges.
42  *
43  * Bridge is resposible for initializing and managing connection on accessibility bus.
44  * Accessibility clients will not get any information about UI without initialized and upraised bridge.
45  * Concrete implementation depends on the accessibility technology available on the platform.
46  *
47  * @note This class is singleton.
48  */
49 struct DALI_ADAPTOR_API Bridge
50 {
51   enum class ForceUpResult
52   {
53     JUST_STARTED,
54     ALREADY_UP,
55     FAILED
56   };
57
58   /**
59    * @brief Destructor
60    */
61   virtual ~Bridge() = default;
62
63   /**
64    * @brief Gets bus name which bridge is initialized on.
65    *
66    * @return The bus name
67    */
68   virtual const std::string& GetBusName() const = 0;
69
70   /**
71    * @brief Registers top level window.
72    *
73    * Hierarchy of objects visible for accessibility clients is based on tree-like
74    * structure created from Actors objects. This method allows to connect chosen
75    * object as direct ancestor of application and therefore make it visible for
76    * accessibility clients.
77    *
78    * @param[in] object The accessible object
79    */
80   virtual void AddTopLevelWindow(Accessible* object) = 0;
81
82   /**
83    * @brief Removes top level window.
84    *
85    * Hierarchy of objects visible for accessibility clients is based on tree-like
86    * structure created from Actors objects. This method removes previously added
87    * window from visible accessibility objects.
88    *
89    * @param[in] object The accessible object
90    */
91   virtual void RemoveTopLevelWindow(Accessible* object) = 0;
92
93   /**
94    * @brief Adds object on the top of the stack of "default label" sourcing objects.
95    *
96    * @see GetDefaultLabel
97    *
98    * @param[in] object The accessible object
99    */
100   virtual void RegisterDefaultLabel(Accessible* object) = 0;
101
102   /**
103    * @brief Removes object from the stack of "default label" sourcing objects.
104    *
105    * @see GetDefaultLabel
106    *
107    * @param[in] object The accessible object
108    */
109   virtual void UnregisterDefaultLabel(Accessible* object) = 0;
110
111   /**
112    * @brief Gets the top-most object from the stack of "default label" sourcing objects.
113    *
114    * The "default label" is a reading material (text) derived from an accesibility object
115    * that could be read by screen-reader immediately after the navigation context has changed
116    * (window activates, popup shows up, tab changes) and before first UI element is highlighted.
117    *
118    * @return The handler to accessibility object
119    * @note This is a Tizen only feature not present in upstream ATSPI.
120    * Feature can be enabled/disabled for particular context root object
121    * by setting value of its accessibility attribute "default_label".
122    * Following strings are valid values for "default_label" attribute: "enabled", "disabled".
123    * Any other value will be interpreted as "enabled".
124    */
125   virtual Accessible* GetDefaultLabel() const = 0;
126
127   /**
128    * @brief Sets name of current application which will be visible on accessibility bus.
129    *
130    * @param[in] name The application name
131    */
132   virtual void SetApplicationName(std::string name) = 0;
133
134   /**
135    * @brief Gets object being root of accessibility tree.
136    *
137    * @return handler to accessibility object
138    */
139   virtual Accessible* GetApplication() const = 0;
140
141   /**
142    * @brief Finds an object in accessibility tree.
143    *
144    * @param[in] path The path to object
145    *
146    * @return The handler to accessibility object
147    */
148   virtual Accessible* FindByPath(const std::string& path) const = 0;
149
150   /**
151    * @brief Notifies accessibility dbus that window has just been shown.
152    *
153    * @param[in] window The window to be shown
154    */
155   virtual void WindowShown(Window window) = 0;
156
157   /**
158    * @brief Notifies accessibility dbus that window has just been hidden.
159    *
160    * @param[in] window The window to be hidden
161    */
162   virtual void WindowHidden(Window window) = 0;
163
164   /**
165    * @brief Notifies accessibility dbus that window has just been focused.
166    *
167    * @param[in] window The window to be focused
168    */
169   virtual void WindowFocused(Window window) = 0;
170
171   /**
172    * @brief Notifies accessibility dbus that window has just been out of focus.
173    *
174    * @param[in] window The window to be out of focus
175    */
176   virtual void WindowUnfocused(Window window) = 0;
177
178   /**
179    * @brief Initializes accessibility bus.
180    */
181   virtual void Initialize() = 0;
182
183   /**
184    * @brief Terminates accessibility bus.
185    */
186   virtual void Terminate() = 0;
187
188   /**
189    * @brief This method is called, when bridge is being activated.
190    */
191   virtual ForceUpResult ForceUp()
192   {
193     if(mData)
194     {
195       return ForceUpResult::ALREADY_UP;
196     }
197     mData          = std::make_shared<Data>();
198     mData->mBridge = this;
199     return ForceUpResult::JUST_STARTED;
200   }
201
202   /**
203    * @brief This method is called, when bridge is being deactivated.
204    */
205   virtual void ForceDown() = 0;
206
207   /**
208    * @brief Checks if bridge is activated or not.
209    * @return True if brige is activated.
210    */
211   bool IsUp() const
212   {
213     return bool(mData);
214   }
215
216   /**
217    * @brief Emits cursor-moved event on at-spi bus.
218    *
219    * @param[in] obj The accessible object
220    * @param[in] cursorPosition The new cursor position
221    **/
222   virtual void EmitCursorMoved(Accessible* obj, unsigned int cursorPosition) = 0;
223
224   /**
225    * @brief Emits active-descendant-changed event on at-spi bus.
226    *
227    * @param[in] obj The accessible object
228    * @param[in] child The child of the object
229    **/
230   virtual void EmitActiveDescendantChanged(Accessible* obj, Accessible* child) = 0;
231
232   /**
233    * @brief Emits text-changed event on at-spi bus.
234    *
235    * @param[in] obj The accessible object
236    * @param[in] state The changed state for text, such as Inserted or Deleted
237    * @param[in] position The cursor position
238    * @param[in] length The text length
239    * @param[in] content The changed text
240    **/
241   virtual void EmitTextChanged(Accessible* obj, TextChangedState state, unsigned int position, unsigned int length, const std::string& content) = 0;
242
243   /**
244    * @brief Emits MoveOuted event on at-spi bus.
245    *
246    * @param[in] obj Accessible object
247    * @param[in] type Direction type when an Accessible object moves out of screen
248    **/
249   virtual void EmitMovedOutOfScreen(Accessible* obj, ScreenRelativeMoveType type) = 0;
250
251   /**
252    * @brief Emits state-changed event on at-spi bus.
253    *
254    * @param[in] obj The accessible object
255    * @param[in] state The accessibility state (SHOWING, HIGHLIGHTED, etc)
256    * @param[in] newValue Whether the state value is changed to new value or not.
257    * @param[in] reserved Reserved. (Currently, this argument is not implemented in dali)
258    **/
259   virtual void EmitStateChanged(Accessible* obj, State state, int newValue, int reserved = 0) = 0;
260
261   /**
262    * @brief Emits window event on at-spi bus.
263    *
264    * @param[in] obj The accessible object
265    * @param[in] event The enumerated window event
266    * @param[in] detail The additional parameter which interpretation depends on chosen event
267    **/
268   virtual void Emit(Accessible* obj, WindowEvent event, unsigned int detail = 0) = 0;
269
270   /**
271    * @brief Emits property-changed event on at-spi bus.
272    *
273    * @param[in] obj The accessible object
274    * @param[in] event Property changed event
275    **/
276   virtual void Emit(Accessible* obj, ObjectPropertyChangeEvent event) = 0;
277
278   /**
279    * @brief Emits bounds-changed event on at-spi bus.
280    *
281    * @param[in] obj The accessible object
282    * @param[in] rect The rectangle for changed bounds
283    **/
284   virtual void EmitBoundsChanged(Accessible* obj, Rect<> rect) = 0;
285
286   /**
287    * @brief Emits key event on at-spi bus.
288    *
289    * Screen-reader might receive this event and reply, that given keycode is consumed. In that case
290    * further processing of the keycode should be ignored.
291    *
292    * @param[in] type Key event type
293    * @param[in] keyCode Key code
294    * @param[in] keyName Key name
295    * @param[in] timeStamp Time stamp
296    * @param[in] isText Whether it's text or not
297    * @return Whether this event is consumed or not
298    **/
299   virtual Consumed Emit(KeyEventType type, unsigned int keyCode, const std::string& keyName, unsigned int timeStamp, bool isText) = 0;
300
301   /**
302    * @brief Reads given text by screen reader
303    *
304    * @param[in] text The text to read
305    * @param[in] discardable If TRUE, reading can be discarded by subsequent reading requests,
306    * if FALSE the reading must finish before next reading request can be started
307    * @param[in] callback the callback function that is called on reading signals emitted
308    * during processing of this reading request.
309    * Callback can be one of the following signals:
310    * ReadingCancelled, ReadingStopped, ReadingSkipped
311    */
312   virtual void Say(const std::string& text, bool discardable, std::function<void(std::string)> callback) = 0;
313
314   /**
315    * @brief Force accessibility client to pause.
316    */
317   virtual void Pause() = 0;
318
319   /**
320    * @brief Force accessibility client to resume.
321    */
322   virtual void Resume() = 0;
323
324   /**
325    * @brief Cancels anything screen-reader is reading / has queued to read
326    *
327    * @param[in] alsoNonDiscardable whether to cancel non-discardable readings as well
328    */
329   virtual void StopReading(bool alsoNonDiscardable) = 0;
330
331   /**
332    * @brief Suppresses reading of screen-reader
333    *
334    * @param[in] suppress whether to suppress reading of screen-reader
335    */
336   virtual void SuppressScreenReader(bool suppress) = 0;
337
338   /**
339    * @brief Gets screen reader status.
340    *
341    * @return True if screen reader is enabled
342    */
343   virtual bool GetScreenReaderEnabled() = 0;
344
345   /**
346    * @brief Gets ATSPI status.
347    *
348    * @return True if ATSPI is enabled
349    */
350   virtual bool IsEnabled() = 0;
351
352   /**
353    * @brief Returns instance of bridge singleton object.
354    *
355    * @return The current bridge object
356    **/
357   static std::shared_ptr<Bridge> GetCurrentBridge();
358
359   /**
360    * @brief Blocks auto-initialization of AT-SPI bridge
361    *
362    * Use this only if your application starts before DBus does, and call it early in main()
363    * (before GetCurrentBridge() is called by anyone). GetCurrentBridge() will then return an
364    * instance of DummyBridge.
365    *
366    * When DBus is ready, call EnableAutoInit(). Please note that GetCurrentBridge() may still
367    * return an instance of DummyBridge if AT-SPI was disabled at compile time or using an
368    * environment variable, or if creating the real bridge failed.
369    *
370    * @see Dali::Accessibility::DummyBridge
371    * @see Dali::Accessibility::Bridge::EnableAutoInit
372    */
373   static void DisableAutoInit();
374
375   /**
376    * @brief Re-enables auto-initialization of AT-SPI bridge
377    *
378    * Normal applications do not have to call this function. GetCurrentBridge() tries to
379    * initialize the AT-SPI bridge when it is called for the first time.
380    *
381    * @see Dali::Accessibility::Bridge::DisableAutoInit
382    * @see Dali::Accessibility::Bridge::AddTopLevelWindow
383    * @see Dali::Accessibility::Bridge::SetApplicationName
384    */
385   static void EnableAutoInit();
386
387   static Signal<void()>& EnabledSignal()
388   {
389     return mEnabledSignal;
390   }
391
392   static Signal<void()>& DisabledSignal()
393   {
394     return mDisabledSignal;
395   }
396
397 protected:
398   struct Data
399   {
400     std::unordered_set<const Accessible*> mKnownObjects;
401     std::string                           mBusName;
402     Bridge*                               mBridge = nullptr;
403     Actor                                 mHighlightActor;
404     Actor                                 mCurrentlyHighlightedActor;
405   };
406   std::shared_ptr<Data> mData;
407   friend class Accessible;
408
409   enum class AutoInitState
410   {
411     DISABLED,
412     ENABLED
413   };
414
415   inline static AutoInitState mAutoInitState = AutoInitState::ENABLED;
416
417   inline static Signal<void()> mEnabledSignal;
418   inline static Signal<void()> mDisabledSignal;
419
420   /**
421    * @brief Registers accessible object to be known in bridge object.
422    *
423    * Bridge must known about all currently alive accessible objects, as some requst
424    * might come and object will be identified by number id (it's memory address).
425    * To avoid memory corruption number id is checked against set of known objects.
426    *
427    * @param[in] object The accessible object
428    **/
429   void RegisterOnBridge(const Accessible* object);
430
431   /**
432    * @brief Tells bridge, that given object is considered root (doesn't have any parents).
433    *
434    * All root objects will have the same parent - application object. Application object
435    * is controlled by bridge and private.
436    *
437    * @param[in] owner The accessible object
438    **/
439   void SetIsOnRootLevel(Accessible* owner);
440 };
441
442 /**
443  * @brief Checks if ATSPI is activated or not.
444  * @return True if ATSPI is activated.
445  */
446 inline bool IsUp()
447 {
448   if(Bridge::GetCurrentBridge() == nullptr)
449   {
450     return false;
451   }
452
453   if(Bridge::GetCurrentBridge()->IsEnabled() == false)
454   {
455     return false;
456   }
457
458   return Bridge::GetCurrentBridge()->IsUp();
459 }
460
461 } // namespace Accessibility
462 } // namespace Dali
463
464 #endif // DALI_ADAPTOR_ACCESSIBILITY_BRIDGE_H