58a98fed947a1bb415c13ef0d092a8b7fdd843c5
[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 class ProxyAccessible;
40
41 /**
42  * @brief Base class for different accessibility bridges.
43  *
44  * Bridge is resposible for initializing and managing connection on accessibility bus.
45  * Accessibility clients will not get any information about UI without initialized and upraised bridge.
46  * Concrete implementation depends on the accessibility technology available on the platform.
47  *
48  * @note This class is singleton.
49  */
50 struct DALI_ADAPTOR_API Bridge
51 {
52   enum class ForceUpResult
53   {
54     JUST_STARTED,
55     ALREADY_UP,
56     FAILED
57   };
58
59   /**
60    * @brief Destructor
61    */
62   virtual ~Bridge() = default;
63
64   /**
65    * @brief Gets bus name which bridge is initialized on.
66    *
67    * @return The bus name
68    */
69   virtual const std::string& GetBusName() const = 0;
70
71   /**
72    * @brief Registers top level window.
73    *
74    * Hierarchy of objects visible for accessibility clients is based on tree-like
75    * structure created from Actors objects. This method allows to connect chosen
76    * object as direct ancestor of application and therefore make it visible for
77    * accessibility clients.
78    *
79    * @param[in] object The accessible object
80    */
81   virtual void AddTopLevelWindow(Accessible* object) = 0;
82
83   /**
84    * @brief Removes top level window.
85    *
86    * Hierarchy of objects visible for accessibility clients is based on tree-like
87    * structure created from Actors objects. This method removes previously added
88    * window from visible accessibility objects.
89    *
90    * @param[in] object The accessible object
91    */
92   virtual void RemoveTopLevelWindow(Accessible* object) = 0;
93
94   /**
95    * @brief Adds object on the top of the stack of "default label" sourcing objects.
96    *
97    * @see GetDefaultLabel
98    *
99    * @param[in] object The accessible object
100    */
101   virtual void RegisterDefaultLabel(Accessible* object) = 0;
102
103   /**
104    * @brief Removes object from the stack of "default label" sourcing objects.
105    *
106    * @see GetDefaultLabel
107    *
108    * @param[in] object The accessible object
109    */
110   virtual void UnregisterDefaultLabel(Accessible* object) = 0;
111
112   /**
113    * @brief Gets the top-most object from the stack of "default label" sourcing objects.
114    *
115    * The "default label" is a reading material (text) derived from an accesibility object
116    * that could be read by screen-reader immediately after the navigation context has changed
117    * (window activates, popup shows up, tab changes) and before first UI element is highlighted.
118    *
119    * @param[in] root The root of the navigation context for which to retrieve the default label.
120    *
121    * @return The handler to accessibility object
122    * @note This is a Tizen only feature not present in upstream ATSPI.
123    * Feature can be enabled/disabled for particular context root object
124    * by setting value of its accessibility attribute "default_label".
125    * Following strings are valid values for "default_label" attribute: "enabled", "disabled".
126    * Any other value will be interpreted as "enabled".
127    */
128   virtual Accessible* GetDefaultLabel(Accessible* root) const = 0;
129
130   /**
131    * @brief Sets name of current application which will be visible on accessibility bus.
132    *
133    * @param[in] name The application name
134    */
135   virtual void SetApplicationName(std::string name) = 0;
136
137   /**
138    * @brief Sets the name of the GUI toolkit that AT-SPI clients can query.
139    *
140    * The default name is "dali".
141    *
142    * @param toolkitName The toolkit name
143    */
144   virtual void SetToolkitName(std::string_view toolkitName) = 0;
145
146   /**
147    * @brief Gets object being root of accessibility tree.
148    *
149    * @return handler to accessibility object
150    */
151   virtual Accessible* GetApplication() const = 0;
152
153   /**
154    * @brief Finds an object in accessibility tree.
155    *
156    * @param[in] path The path to object
157    *
158    * @return The handler to accessibility object
159    */
160   virtual Accessible* FindByPath(const std::string& path) const = 0;
161
162   /**
163    * @brief Notifies accessibility dbus that window has just been created.
164    *
165    * @param[in] window The window to be created
166    */
167   virtual void WindowCreated(Window window) = 0;
168
169   /**
170    * @brief Notifies accessibility dbus that window has just been shown.
171    *
172    * @param[in] window The window to be shown
173    */
174   virtual void WindowShown(Window window) = 0;
175
176   /**
177    * @brief Notifies accessibility dbus that window has just been hidden.
178    *
179    * @param[in] window The window to be hidden
180    */
181   virtual void WindowHidden(Window window) = 0;
182
183   /**
184    * @brief Notifies accessibility dbus that window has just been focused.
185    *
186    * @param[in] window The window to be focused
187    */
188   virtual void WindowFocused(Window window) = 0;
189
190   /**
191    * @brief Notifies accessibility dbus that window has just been out of focus.
192    *
193    * @param[in] window The window to be out of focus
194    */
195   virtual void WindowUnfocused(Window window) = 0;
196
197   /**
198    * @brief Initializes accessibility bus.
199    */
200   virtual void Initialize() = 0;
201
202   /**
203    * @brief Terminates accessibility bus.
204    */
205   virtual void Terminate() = 0;
206
207   /**
208    * @brief This method is called, when bridge is being activated.
209    */
210   virtual ForceUpResult ForceUp()
211   {
212     if(mData)
213     {
214       return ForceUpResult::ALREADY_UP;
215     }
216     mData          = std::make_shared<Data>();
217     mData->mBridge = this;
218     return ForceUpResult::JUST_STARTED;
219   }
220
221   /**
222    * @brief This method is called, when bridge is being deactivated.
223    */
224   virtual void ForceDown() = 0;
225
226   /**
227    * @brief Checks if bridge is activated or not.
228    * @return True if brige is activated.
229    */
230   bool IsUp() const
231   {
232     return bool(mData);
233   }
234
235   /**
236    * @brief Emits cursor-moved event on at-spi bus.
237    *
238    * @param[in] obj The accessible object
239    * @param[in] cursorPosition The new cursor position
240    **/
241   virtual void EmitCursorMoved(Accessible* obj, unsigned int cursorPosition) = 0;
242
243   /**
244    * @brief Emits active-descendant-changed event on at-spi bus.
245    *
246    * @param[in] obj The accessible object
247    * @param[in] child The child of the object
248    **/
249   virtual void EmitActiveDescendantChanged(Accessible* obj, Accessible* child) = 0;
250
251   /**
252    * @brief Emits text-changed event on at-spi bus.
253    *
254    * @param[in] obj The accessible object
255    * @param[in] state The changed state for text, such as Inserted or Deleted
256    * @param[in] position The cursor position
257    * @param[in] length The text length
258    * @param[in] content The changed text
259    **/
260   virtual void EmitTextChanged(Accessible* obj, TextChangedState state, unsigned int position, unsigned int length, const std::string& content) = 0;
261
262   /**
263    * @brief Emits MoveOuted event on at-spi bus.
264    *
265    * @param[in] obj Accessible object
266    * @param[in] type Direction type when an Accessible object moves out of screen
267    **/
268   virtual void EmitMovedOutOfScreen(Accessible* obj, ScreenRelativeMoveType type) = 0;
269
270   /**
271    * @brief Emits "org.a11y.atspi.Socket.Available" event on AT-SPI bus.
272    *
273    * @param obj Accessible object
274    */
275   virtual void EmitSocketAvailable(Accessible* obj) = 0;
276
277   /**
278    * @brief Emits state-changed event on at-spi bus.
279    *
280    * @param[in] obj The accessible object
281    * @param[in] state The accessibility state (SHOWING, HIGHLIGHTED, etc)
282    * @param[in] newValue Whether the state value is changed to new value or not.
283    * @param[in] reserved Reserved. (Currently, this argument is not implemented in dali)
284    **/
285   virtual void EmitStateChanged(Accessible* obj, State state, int newValue, int reserved = 0) = 0;
286
287   /**
288    * @brief Emits window event on at-spi bus.
289    *
290    * @param[in] obj The accessible object
291    * @param[in] event The enumerated window event
292    * @param[in] detail The additional parameter which interpretation depends on chosen event
293    **/
294   virtual void Emit(Accessible* obj, WindowEvent event, unsigned int detail = 0) = 0;
295
296   /**
297    * @brief Emits property-changed event on at-spi bus.
298    *
299    * @param[in] obj The accessible object
300    * @param[in] event Property changed event
301    **/
302   virtual void Emit(Accessible* obj, ObjectPropertyChangeEvent event) = 0;
303
304   /**
305    * @brief Emits bounds-changed event on at-spi bus.
306    *
307    * @param[in] obj The accessible object
308    * @param[in] rect The rectangle for changed bounds
309    **/
310   virtual void EmitBoundsChanged(Accessible* obj, Rect<> rect) = 0;
311
312   /**
313    * @brief Emits key event on at-spi bus.
314    *
315    * Screen-reader might receive this event and reply, that given keycode is consumed. In that case
316    * further processing of the keycode should be ignored.
317    *
318    * @param[in] type Key event type
319    * @param[in] keyCode Key code
320    * @param[in] keyName Key name
321    * @param[in] timeStamp Time stamp
322    * @param[in] isText Whether it's text or not
323    * @return Whether this event is consumed or not
324    **/
325   virtual Consumed Emit(KeyEventType type, unsigned int keyCode, const std::string& keyName, unsigned int timeStamp, bool isText) = 0;
326
327   /**
328    * @brief Reads given text by screen reader
329    *
330    * @param[in] text The text to read
331    * @param[in] discardable If TRUE, reading can be discarded by subsequent reading requests,
332    * if FALSE the reading must finish before next reading request can be started
333    * @param[in] callback the callback function that is called on reading signals emitted
334    * during processing of this reading request.
335    * Callback can be one of the following signals:
336    * ReadingCancelled, ReadingStopped, ReadingSkipped
337    */
338   virtual void Say(const std::string& text, bool discardable, std::function<void(std::string)> callback) = 0;
339
340   /**
341    * @brief Force accessibility client to pause.
342    */
343   virtual void Pause() = 0;
344
345   /**
346    * @brief Force accessibility client to resume.
347    */
348   virtual void Resume() = 0;
349
350   /**
351    * @brief Cancels anything screen-reader is reading / has queued to read
352    *
353    * @param[in] alsoNonDiscardable whether to cancel non-discardable readings as well
354    */
355   virtual void StopReading(bool alsoNonDiscardable) = 0;
356
357   /**
358    * @brief Suppresses reading of screen-reader
359    *
360    * @param[in] suppress whether to suppress reading of screen-reader
361    */
362   virtual void SuppressScreenReader(bool suppress) = 0;
363
364   /**
365    * @brief Gets screen reader status.
366    *
367    * @return True if screen reader is enabled
368    */
369   virtual bool GetScreenReaderEnabled() = 0;
370
371   /**
372    * @brief Gets ATSPI status.
373    *
374    * @return True if ATSPI is enabled
375    */
376   virtual bool IsEnabled() = 0;
377
378   /**
379    * @brief Calls socket.Embed(plug) via D-Bus.
380    *
381    * @param[in] plug The plug
382    * @param[in] socket The socket
383    *
384    * @return Address returned by the D-Bus call.
385    *
386    * @note Remote object pointed to by 'socket' must implement 'org.a11y.atspi.Socket'.
387    * @see UnembedSocket()
388    */
389   virtual Address EmbedSocket(const Address& plug, const Address& socket) = 0;
390
391   /**
392    * @brief Calls socket.Embedded(plug) via D-Bus.
393    *
394    * The "Embedded" D-Bus method is an ATK extension.
395    * See 'impl_Embedded' in AT_SPI2_ATK/atk-adaptor/adaptors/socket-adaptor.c for more information.
396    *
397    * @param[in] plug The plug
398    * @param[in] socket The socket
399    */
400   virtual void EmbedAtkSocket(const Address& plug, const Address& socket) = 0;
401
402   /**
403    * @brief Calls socket.Unmbed(plug) via D-Bus.
404    *
405    * @param[in] plug The plug
406    * @param[in] socket The socket
407    *
408    * @note Remote object pointed to by 'socket' must implement 'org.a11y.atspi.Socket'.
409    * @see EmbedSocket()
410    */
411   virtual void UnembedSocket(const Address& plug, const Address& socket) = 0;
412
413   /**
414    * @brief Calls socket.SetOffset(x, y) via D-Bus.
415    *
416    * The "SetOffset" D-Bus method is a DALi extension. It can be used to inform a DALi widget about
417    * its position on the screen.
418    *
419    * @param[in] socket The socket
420    * @param[in] x Horizontal offset
421    * @param[in] y Vertical offset
422    *
423    * @note Remote object pointed to by 'socket' must implement 'org.a11y.atspi.Socket'.
424    * @see EmbedSocket()
425    * @see SetExtentsOffset()
426    */
427   virtual void SetSocketOffset(ProxyAccessible* socket, std::int32_t x, std::int32_t y) = 0;
428
429   /**
430    * @brief Sets the global extents offset.
431    *
432    * This offset will be added during serialization of GetExtents() return value to D-Bus.
433    * Local calls to GetExtents() are unaffected.
434    *
435    * @param[in] x Horizontal offset
436    * @param[in] y Vertical offset
437    *
438    * @see SetSocketOffset()
439    * @see Dali::Accessibility::Component::GetExtents()
440    */
441   virtual void SetExtentsOffset(std::int32_t x, std::int32_t y) = 0;
442
443   /**
444    * @brief Sets the preferred bus name.
445    *
446    * If the Bridge is enabled, it will immediately release the previous name and request the new one.
447    *
448    * Otherwise, the Bridge will request this name on AT-SPI activation (and release it on deactivation).
449    * It is up to the caller to determine whether a given name will be available in the system.
450    *
451    * @param preferredBusName The preferred bus name
452    */
453   virtual void SetPreferredBusName(std::string_view preferredBusName) = 0;
454
455   /**
456    * @brief Returns instance of bridge singleton object.
457    *
458    * @return The current bridge object
459    **/
460   static std::shared_ptr<Bridge> GetCurrentBridge();
461
462   /**
463    * @brief Blocks auto-initialization of AT-SPI bridge
464    *
465    * Use this only if your application starts before DBus does, and call it early in main()
466    * (before GetCurrentBridge() is called by anyone). GetCurrentBridge() will then return an
467    * instance of DummyBridge.
468    *
469    * When DBus is ready, call EnableAutoInit(). Please note that GetCurrentBridge() may still
470    * return an instance of DummyBridge if AT-SPI was disabled at compile time or using an
471    * environment variable, or if creating the real bridge failed.
472    *
473    * @see Dali::Accessibility::DummyBridge
474    * @see Dali::Accessibility::Bridge::EnableAutoInit
475    */
476   static void DisableAutoInit();
477
478   /**
479    * @brief Re-enables auto-initialization of AT-SPI bridge
480    *
481    * Normal applications do not have to call this function. GetCurrentBridge() tries to
482    * initialize the AT-SPI bridge when it is called for the first time.
483    *
484    * @see Dali::Accessibility::Bridge::DisableAutoInit
485    * @see Dali::Accessibility::Bridge::AddTopLevelWindow
486    * @see Dali::Accessibility::Bridge::SetApplicationName
487    */
488   static void EnableAutoInit();
489
490   /**
491    * @brief Encodes a widget ID as a usable bus name.
492    *
493    * @param widgetInstanceId The instance ID of a widget
494    * @return std::string Encoded bus name
495    *
496    * @see SetPreferredBusName
497    */
498   static std::string MakeBusNameForWidget(std::string_view widgetInstanceId);
499
500   static Signal<void()>& EnabledSignal()
501   {
502     return mEnabledSignal;
503   }
504
505   static Signal<void()>& DisabledSignal()
506   {
507     return mDisabledSignal;
508   }
509
510   static Signal<void()>& ScreenReaderEnabledSignal()
511   {
512     return mScreenReaderEnabledSignal;
513   }
514
515   static Signal<void()>& ScreenReaderDisabledSignal()
516   {
517     return mScreenReaderDisabledSignal;
518   }
519
520 protected:
521   struct Data
522   {
523     std::unordered_set<const Accessible*> mKnownObjects;
524     std::string                           mBusName;
525     Bridge*                               mBridge = nullptr;
526     Actor                                 mHighlightActor;
527     Actor                                 mCurrentlyHighlightedActor;
528     std::pair<std::int32_t, std::int32_t> mExtentsOffset{0, 0};
529   };
530   std::shared_ptr<Data> mData;
531   friend class Accessible;
532
533   enum class AutoInitState
534   {
535     DISABLED,
536     ENABLED
537   };
538
539   inline static AutoInitState mAutoInitState = AutoInitState::ENABLED;
540
541   inline static Signal<void()> mEnabledSignal;
542   inline static Signal<void()> mDisabledSignal;
543   inline static Signal<void()> mScreenReaderEnabledSignal;
544   inline static Signal<void()> mScreenReaderDisabledSignal;
545
546   /**
547    * @brief Registers accessible object to be known in bridge object.
548    *
549    * Bridge must known about all currently alive accessible objects, as some requst
550    * might come and object will be identified by number id (it's memory address).
551    * To avoid memory corruption number id is checked against set of known objects.
552    *
553    * @param[in] object The accessible object
554    **/
555   void RegisterOnBridge(const Accessible* object);
556
557   /**
558    * @brief Tells bridge, that given object is considered root (doesn't have any parents).
559    *
560    * All root objects will have the same parent - application object. Application object
561    * is controlled by bridge and private.
562    *
563    * @param[in] owner The accessible object
564    **/
565   void SetIsOnRootLevel(Accessible* owner);
566 };
567
568 /**
569  * @brief Checks if ATSPI is activated or not.
570  * @return True if ATSPI is activated.
571  */
572 inline bool IsUp()
573 {
574   if(Bridge::GetCurrentBridge() == nullptr)
575   {
576     return false;
577   }
578
579   if(Bridge::GetCurrentBridge()->IsEnabled() == false)
580   {
581     return false;
582   }
583
584   return Bridge::GetCurrentBridge()->IsUp();
585 }
586
587 } // namespace Accessibility
588 } // namespace Dali
589
590 #endif // DALI_ADAPTOR_ACCESSIBILITY_BRIDGE_H