[AT-SPI] Introduce AtspiEvents
[platform/core/uifw/dali-adaptor.git] / dali / devel-api / atspi-interfaces / accessible.h
1 #ifndef DALI_ADAPTOR_ATSPI_ACCESSIBLE_H
2 #define DALI_ADAPTOR_ATSPI_ACCESSIBLE_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 // EXTERNAL INCLUDES
21 #include <dali/public-api/actors/actor.h>
22 #include <dali/public-api/math/rect.h>
23 #include <dali/public-api/object/object-registry.h>
24 #include <cstdint>
25 #include <string>
26 #include <vector>
27
28 // INTERNAL INCLUDES
29 #include <dali/devel-api/adaptor-framework/accessibility.h>
30 #include <dali/devel-api/adaptor-framework/accessibility-bridge.h>
31
32 namespace Dali::Accessibility
33 {
34 /**
35  * @brief Basic interface implemented by all accessibility objects.
36  */
37 class DALI_ADAPTOR_API Accessible
38 {
39 public:
40   virtual ~Accessible() noexcept;
41
42   using utf8_t = unsigned char;
43
44   /**
45    * @brief Calculates and finds word boundaries in given utf8 text.
46    *
47    * @param[in] string The source text to find
48    * @param[in] length The length of text to find
49    * @param[in] language The language to use
50    * @param[out] breaks The word boundaries in given text
51    *
52    * @note Word boundaries are returned as non-zero values in table breaks, which must be of size at least length.
53    */
54   static void FindWordSeparationsUtf8(const utf8_t* string, std::size_t length, const char* language, char* breaks);
55
56   /**
57    * @brief Calculates and finds line boundaries in given utf8 text.
58    *
59    * @param[in] string The source text to find
60    * @param[in] length The length of text to find
61    * @param[in] language The language to use
62    * @param[out] breaks The line boundaries in given text
63    *
64    * @note Line boundaries are returned as non-zero values in table breaks, which must be of size at least length.
65    */
66   static void FindLineSeparationsUtf8(const utf8_t* string, std::size_t length, const char* language, char* breaks);
67
68   /**
69    * @brief Helper function for emiting active-descendant-changed event.
70    *
71    * @param[in] child The child of the object
72    */
73   void EmitActiveDescendantChanged(Accessible* child);
74
75   /**
76    * @brief Helper function for emiting state-changed event.
77    *
78    * @param[in] state The accessibility state (SHOWING, HIGHLIGHTED, etc)
79    * @param[in] newValue Whether the state value is changed to new value or not.
80    * @param[in] reserved Reserved. (TODO : Currently, this argument is not implemented in dali)
81    *
82    * @note The second argument determines which value is depending on State.
83    * For instance, if the state is PRESSED, newValue means isPressed or isSelected.
84    * If the state is SHOWING, newValue means isShowing.
85    */
86   void EmitStateChanged(State state, int newValue, int reserved = 0);
87
88   /**
89    * @brief Helper function for emiting bounds-changed event.
90    *
91    * @param rect The rectangle for changed bounds
92    */
93   void EmitBoundsChanged(Rect<> rect);
94
95   /**
96    * @brief Emits "showing" event.
97    * The method informs accessibility clients about "showing" state.
98    *
99    * @param[in] isShowing The flag pointing if object is showing
100    */
101   void EmitShowing(bool isShowing);
102
103   /**
104    * @brief Emits "visible" event.
105    * The method informs accessibility clients about "visible" state.
106    *
107    * @param[in] isVisible The flag pointing if object is visible
108    */
109   void EmitVisible(bool isVisible);
110
111   /**
112    * @brief Emits "highlighted" event.
113    * The method informs accessibility clients about "highlighted" state.
114    *
115    * @param[in] isHighlighted The flag pointing if object is highlighted
116    */
117   void EmitHighlighted(bool isHighlighted);
118
119   /**
120    * @brief Emits "focused" event.
121    * The method informs accessibility clients about "focused" state.
122    *
123    * @param[in] isFocused The flag pointing if object is focused
124    */
125   void EmitFocused(bool isFocused);
126
127   /**
128    * @brief Emits "text inserted" event.
129    *
130    * @param[in] position The cursor position
131    * @param[in] length The text length
132    * @param[in] content The inserted text
133    */
134   void EmitTextInserted(unsigned int position, unsigned int length, const std::string& content);
135
136   /**
137    * @brief Emits "text deleted" event.
138    *
139    * @param[in] position The cursor position
140    * @param[in] length The text length
141    * @param[in] content The deleted text
142    */
143   void EmitTextDeleted(unsigned int position, unsigned int length, const std::string& content);
144
145   /**
146    * @brief Emits "cursor moved" event.
147    *
148    * @param[in] cursorPosition The new cursor position
149    */
150   void EmitTextCursorMoved(unsigned int cursorPosition);
151
152   /**
153    * @brief Emits "MoveOuted" event.
154    *
155    * @param[in] type moved out of screen type
156    */
157   void EmitMovedOutOfScreen(ScreenRelativeMoveType type);
158
159   /**
160    * @brief Emits "org.a11y.atspi.Socket.Available" signal.
161    */
162   // This belongs to Dali::Accessibility::Socket. However, all Emit*() helpers
163   // are here in Accessible, regardless of what interface they belong to (perhaps
164   // to spare a dynamic_cast if used like this: Accessible::Get()->Emit*(...)).
165   void EmitSocketAvailable();
166
167   /**
168    * @brief Emits "highlighted" event.
169    *
170    * @param[in] event The enumerated window event
171    * @param[in] detail The additional parameter which interpretation depends on chosen event
172    */
173   void Emit(WindowEvent event, unsigned int detail = 0);
174
175   /**
176    * @brief Emits property-changed event.
177    *
178    * @param[in] event Property changed event
179    **/
180   void Emit(ObjectPropertyChangeEvent event);
181
182   /**
183    * @brief Gets accessibility name.
184    *
185    * @return The string with name
186    */
187   virtual std::string GetName() const = 0;
188
189   /**
190    * @brief Gets accessibility description.
191    *
192    * @return The string with description
193    */
194   virtual std::string GetDescription() const = 0;
195
196   /**
197    * @brief Gets parent.
198    *
199    * @return The handler to accessibility object
200    */
201   virtual Accessible* GetParent() = 0;
202
203   /**
204    * @brief Gets the number of children.
205    *
206    * @return The number of children
207    */
208   virtual std::size_t GetChildCount() const = 0;
209
210   /**
211    * @brief Gets collection with all children.
212    *
213    * @return The collection of accessibility objects
214    */
215   virtual std::vector<Accessible*> GetChildren() = 0;
216
217   /**
218    * @brief Gets child of the index.
219    *
220    * @return The child object
221    */
222   virtual Accessible* GetChildAtIndex(std::size_t index) = 0;
223
224   /**
225    * @brief Gets index that current object has in its parent's children collection.
226    *
227    * @return The index of the current object
228    */
229   virtual std::size_t GetIndexInParent() = 0;
230
231   /**
232    * @brief Gets accessibility role.
233    *
234    * @return Role enumeration
235    *
236    * @see Dali::Accessibility::Role
237    */
238   virtual Role GetRole() const = 0;
239
240   /**
241    * @brief Gets name of accessibility role.
242    *
243    * @return The string with human readable role converted from enumeration
244    *
245    * @see Dali::Accessibility::Role
246    * @see Accessibility::Accessible::GetRole
247    */
248   virtual std::string GetRoleName() const;
249
250   /**
251    * @brief Gets localized name of accessibility role.
252    *
253    * @return The string with human readable role translated according to current
254    * translation domain
255    *
256    * @see Dali::Accessibility::Role
257    * @see Accessibility::Accessible::GetRole
258    * @see Accessibility::Accessible::GetRoleName
259    *
260    * @note translation is not supported in this version
261    */
262   virtual std::string GetLocalizedRoleName() const;
263
264   /**
265    * @brief Gets accessibility states.
266    *
267    * @return The collection of states
268    *
269    * @note States class is instatation of ArrayBitset template class
270    *
271    * @see Dali::Accessibility::State
272    * @see Dali::Accessibility::ArrayBitset
273    */
274   virtual States GetStates() = 0;
275
276   /**
277    * @brief Gets accessibility attributes.
278    *
279    * @return The map of attributes and their values
280    */
281   virtual Attributes GetAttributes() const = 0;
282
283   /**
284    * @brief Checks if this is hidden.
285    *
286    * @return True if this is hidden
287    *
288    * @note Hidden means not present in the AT-SPI tree.
289    */
290   virtual bool IsHidden() const;
291
292   /**
293    * @brief Checks if this is proxy.
294    *
295    * @return True if this is proxy
296    */
297   virtual bool IsProxy() const;
298
299   /**
300    * @brief Gets unique address on accessibility bus.
301    *
302    * @return The Address class containing address
303    *
304    * @see Dali::Accessibility::Address
305    */
306   virtual Address GetAddress() const;
307
308   /**
309    * @brief Deputes an object to perform provided gesture.
310    *
311    * @param[in] gestureInfo The structure describing the gesture
312    *
313    * @return true on success, false otherwise
314    *
315    * @see Dali::Accessibility::GestureInfo
316    */
317   virtual bool DoGesture(const GestureInfo& gestureInfo) = 0;
318
319   /**
320    * @brief Re-emits selected states of an Accessibility Object.
321    *
322    * @param[in] states The chosen states to re-emit
323    * @param[in] isRecursive If true, all children of the Accessibility object will also re-emit the states
324    */
325   void NotifyAccessibilityStateChange(Dali::Accessibility::States states, bool isRecursive);
326
327   /**
328    * @brief Gets information about current object and all relations that connects
329    * it with other accessibility objects.
330    *
331    * @return The iterable collection of Relation objects
332    *
333    * @see Dali::Accessibility::Relation
334    */
335   virtual std::vector<Relation> GetRelationSet() = 0;
336
337   /**
338    * @brief Gets the Actor associated with this Accessible (if there is one).
339    *
340    * @return The internal Actor
341    */
342   virtual Dali::Actor GetInternalActor() = 0;
343
344   /**
345    * @brief Gets all implemented interfaces.
346    *
347    * Override DoGetInterfaces() to customize the return value of this method.
348    *
349    * @return The collection of implemented interfaces
350    *
351    * @see DoGetInterfaces()
352    */
353   AtspiInterfaces GetInterfaces() const;
354
355   /**
356    * @brief Gets all implemented interfaces.
357    *
358    * Converts all interfaces returned by GetInterfaces() to their DBus names
359    * using GetInterfaceName().
360    *
361    * @return The collection of names of implemented interfaces
362    *
363    * @see GetInterfaces()
364    * @see GetInterfaceName()
365    */
366   std::vector<std::string> GetInterfacesAsStrings() const;
367
368   /**
369    * @brief Checks if object is on root level.
370    *
371    * @return Whether object is on root level or not
372    */
373   bool IsOnRootLevel() const
374   {
375     return mIsOnRootLevel;
376   }
377
378   /**
379    * @brief Gets all suppressed events.
380    *
381    * @return All suppressed events
382    */
383   AtspiEvents GetSuppressedEvents() const
384   {
385     return mSuppressedEvents;
386   }
387
388   /**
389    * @brief Gets all suppressed events.
390    *
391    * @return All suppressed events
392    */
393   AtspiEvents& GetSuppressedEvents()
394   {
395     return mSuppressedEvents;
396   }
397
398 protected:
399   Accessible();
400   Accessible(const Accessible&)         = delete;
401   Accessible(Accessible&&)              = delete;
402   Accessible&                   operator=(const Accessible&) = delete;
403   Accessible&                   operator=(Accessible&&) = delete;
404   std::shared_ptr<Bridge::Data> GetBridgeData() const;
405
406   /**
407    * @brief Returns the collection of AT-SPI interfaces implemented by this Accessible.
408    *
409    * This method is called only once and its return value is cached. The default implementation
410    * uses dynamic_cast to determine which interfaces are implemented. Override this if you
411    * conceptually provide fewer interfaces than dynamic_cast can see.
412    *
413    * @return The collection of implemented interfaces
414    *
415    * @see GetInterfaces()
416    * @see GetInterfaceName()
417    */
418   virtual AtspiInterfaces DoGetInterfaces() const;
419
420 public:
421   /**
422    * @brief Gets the highlight actor.
423    *
424    * This method is to get the highlight itself.
425    * @return The highlight actor
426    */
427   static Dali::Actor GetHighlightActor();
428
429   /**
430    * @brief Sets the highlight actor.
431    *
432    * This method is to set the highlight itself.
433    * @param[in] actor The highlight actor
434    */
435   static void SetHighlightActor(Dali::Actor actor);
436
437   /**
438    * @brief Gets the currently highlighted actor.
439    *
440    * @return The current highlighted actor
441    */
442   static Dali::Actor GetCurrentlyHighlightedActor();
443
444   /**
445    * @brief Sets currently highlighted actor.
446    *
447    * @param[in] actor The highlight actor
448    */
449   static void SetCurrentlyHighlightedActor(Dali::Actor actor);
450
451   /**
452    * @brief Sets ObjectRegistry.
453    *
454    * @param[in] registry ObjectRegistry instance
455    */
456   static void SetObjectRegistry(ObjectRegistry registry);
457
458   /**
459    * @brief The method registers functor resposible for converting Actor into Accessible.
460    * @param functor The returning Accessible handle from Actor object
461    */
462   static void RegisterExternalAccessibleGetter(std::function<Accessible*(Dali::Actor)> functor);
463
464   /**
465    * @brief Acquires Accessible object from Actor object.
466    *
467    * @param[in] actor Actor object
468    * @param[in] isRoot True, if it's top level object (window)
469    *
470    * @return The handle to Accessible object
471    */
472   static Accessible* Get(Dali::Actor actor, bool isRoot = false);
473
474   /**
475    * @brief Obtains the DBus interface name for the specified AT-SPI interface.
476    *
477    * @param interface AT-SPI interface identifier (e.g. AtspiInterface::ACCESSIBLE)
478    * @return AT-SPI interface name (e.g. "org.a11y.atspi.Accessible")
479    */
480   static std::string GetInterfaceName(AtspiInterface interface);
481
482   /**
483    * @brief Downcasts an Accessible pointer to an AT-SPI interface pointer.
484    *
485    * @tparam I Desired AT-SPI interface
486    *
487    * @param obj Object to cast.
488    *
489    * @return Pointer to an AT-SPI interface or null if the interface is not implemented.
490    */
491   template<AtspiInterface I>
492   static AtspiInterfaceType<I>* DownCast(Accessible* obj)
493   {
494     if(!obj || !obj->GetInterfaces()[I])
495     {
496       return nullptr;
497     }
498
499     return dynamic_cast<AtspiInterfaceType<I>*>(obj);
500   }
501
502 private:
503   friend class Bridge;
504
505   mutable std::weak_ptr<Bridge::Data> mBridgeData;
506   mutable AtspiInterfaces             mInterfaces;
507   AtspiEvents                         mSuppressedEvents;
508   bool                                mIsOnRootLevel = false;
509
510 }; // Accessible class
511
512 namespace Internal
513 {
514 template<>
515 struct AtspiInterfaceTypeHelper<AtspiInterface::ACCESSIBLE>
516 {
517   using Type = Accessible;
518 };
519 } // namespace Internal
520
521 } // namespace Dali::Accessibility
522
523 #endif // DALI_ADAPTOR_ATSPI_ACCESSIBLE_H