[Tizen][AT-SPI] Remove State::SENSITIVE from ApplicationAccessible
[platform/core/uifw/dali-adaptor.git] / dali / internal / accessibility / bridge / bridge-base.h
1 #ifndef DALI_INTERNAL_ACCESSIBILITY_BRIDGE_BASE_H
2 #define DALI_INTERNAL_ACCESSIBILITY_BRIDGE_BASE_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/layer.h>
23 #include <dali/public-api/dali-adaptor-version.h>
24 #include <dali/public-api/signals/connection-tracker.h>
25 #include <memory>
26 #include <tuple>
27
28 // INTERNAL INCLUDES
29 #include <dali/devel-api/adaptor-framework/proxy-accessible.h>
30 #include <dali/devel-api/adaptor-framework/window-devel.h>
31 #include <dali/devel-api/atspi-interfaces/accessible.h>
32 #include <dali/devel-api/atspi-interfaces/application.h>
33 #include <dali/devel-api/atspi-interfaces/collection.h>
34 #include <dali/devel-api/atspi-interfaces/socket.h>
35 #include <dali/internal/accessibility/bridge/accessibility-common.h>
36
37 /**
38  * @brief The ApplicationAccessible class is to define Accessibility Application.
39  */
40 class ApplicationAccessible : public virtual Dali::Accessibility::Accessible,
41                               public virtual Dali::Accessibility::Application,
42                               public virtual Dali::Accessibility::Collection,
43                               public virtual Dali::Accessibility::Component,
44                               public virtual Dali::Accessibility::Socket
45 {
46 public:
47   Dali::Accessibility::ProxyAccessible          mParent;
48   std::vector<Dali::Accessibility::Accessible*> mChildren;
49   std::string                                   mName;
50   std::string                                   mToolkitName{"dali"};
51   bool                                          mIsEmbedded{false};
52
53   std::string GetName() const override
54   {
55     return mName;
56   }
57
58   std::string GetDescription() const override
59   {
60     return "";
61   }
62
63   Dali::Accessibility::Accessible* GetParent() override
64   {
65     return &mParent;
66   }
67
68   size_t GetChildCount() const override
69   {
70     return mChildren.size();
71   }
72
73   std::vector<Dali::Accessibility::Accessible*> GetChildren() override
74   {
75     return mChildren;
76   }
77
78   Dali::Accessibility::Accessible* GetChildAtIndex(size_t index) override
79   {
80     auto size = mChildren.size();
81     if(index >= size)
82     {
83       throw std::domain_error{"invalid index " + std::to_string(index) + " for object with " + std::to_string(size) + " children"};
84     }
85     return mChildren[index];
86   }
87
88   size_t GetIndexInParent() override
89   {
90     if(mIsEmbedded)
91     {
92       return 0u;
93     }
94
95     throw std::domain_error{"can't call GetIndexInParent on application object"};
96   }
97
98   Dali::Accessibility::Role GetRole() const override
99   {
100     return Dali::Accessibility::Role::APPLICATION;
101   }
102
103   Dali::Accessibility::States GetStates() override
104   {
105     Dali::Accessibility::States result;
106
107     for(auto* child : mChildren)
108     {
109       result = result | child->GetStates();
110     }
111
112     // The Application object should never have the SENSITIVE state
113     result[Dali::Accessibility::State::SENSITIVE] = false;
114
115     return result;
116   }
117
118   Dali::Accessibility::Attributes GetAttributes() const override
119   {
120     return {};
121   }
122
123   /**
124    * @brief Gets the Accessible object from the window.
125    *
126    * @param[in] window The window to find
127    * @return Null if mChildren is empty, otherwise the Accessible object
128    * @note Currently, the default window would be returned when mChildren is not empty.
129    */
130   Dali::Accessibility::Accessible* GetWindowAccessible(Dali::Window window)
131   {
132     if(mChildren.empty())
133     {
134       return nullptr;
135     }
136
137     Dali::Layer rootLayer = window.GetRootLayer();
138
139     // Find a child which is related to the window.
140     for(auto i = 0u; i < mChildren.size(); ++i)
141     {
142       if(rootLayer == mChildren[i]->GetInternalActor())
143       {
144         return mChildren[i];
145       }
146     }
147
148     // If can't find its children, return the default window.
149     return mChildren[0];
150   }
151
152   bool DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo) override
153   {
154     return false;
155   }
156
157   std::vector<Dali::Accessibility::Relation> GetRelationSet() override
158   {
159     return {};
160   }
161
162   Dali::Actor GetInternalActor() override
163   {
164     return Dali::Actor{};
165   }
166
167   Dali::Accessibility::Address GetAddress() const override
168   {
169     return {"", "root"};
170   }
171
172   // Application
173
174   std::string GetToolkitName() const override
175   {
176     return mToolkitName;
177   }
178
179   std::string GetVersion() const override
180   {
181     return std::to_string(Dali::ADAPTOR_MAJOR_VERSION) + "." + std::to_string(Dali::ADAPTOR_MINOR_VERSION);
182   }
183
184   // Socket
185
186   Dali::Accessibility::Address Embed(Dali::Accessibility::Address plug) override
187   {
188     mIsEmbedded = true;
189     mParent.SetAddress(plug);
190
191     return GetAddress();
192   }
193
194   void Unembed(Dali::Accessibility::Address plug) override
195   {
196     if(mParent.GetAddress() == plug)
197     {
198       mIsEmbedded = false;
199       mParent.SetAddress({});
200       Dali::Accessibility::Bridge::GetCurrentBridge()->SetExtentsOffset(0, 0);
201     }
202   }
203
204   void SetOffset(std::int32_t x, std::int32_t y) override
205   {
206     if(!mIsEmbedded)
207     {
208       return;
209     }
210
211     Dali::Accessibility::Bridge::GetCurrentBridge()->SetExtentsOffset(x, y);
212   }
213
214   // Component
215
216   Dali::Rect<> GetExtents(Dali::Accessibility::CoordinateType type) const override
217   {
218     using limits = std::numeric_limits<float>;
219
220     float minX = limits::max();
221     float minY = limits::max();
222     float maxX = limits::min();
223     float maxY = limits::min();
224
225     for(Dali::Accessibility::Accessible* child : mChildren)
226     {
227       auto* component = Dali::Accessibility::Component::DownCast(child);
228       if(!component)
229       {
230         continue;
231       }
232
233       auto extents = component->GetExtents(type);
234
235       minX = std::min(minX, extents.x);
236       minY = std::min(minY, extents.y);
237       maxX = std::max(maxX, extents.x + extents.width);
238       maxY = std::max(maxY, extents.y + extents.height);
239     }
240
241     return {minX, minY, maxX - minX, maxY - minY};
242   }
243
244   Dali::Accessibility::ComponentLayer GetLayer() const override
245   {
246     return Dali::Accessibility::ComponentLayer::WINDOW;
247   }
248
249   std::int16_t GetMdiZOrder() const override
250   {
251     return 0;
252   }
253
254   bool GrabFocus() override
255   {
256     return false;
257   }
258
259   double GetAlpha() const override
260   {
261     return 0.0;
262   }
263
264   bool GrabHighlight() override
265   {
266     return false;
267   }
268
269   bool ClearHighlight() override
270   {
271     return false;
272   }
273
274   bool IsScrollable() const override
275   {
276     return false;
277   }
278 };
279
280 /**
281  * @brief Enumeration for CoalescableMessages.
282  */
283 enum class CoalescableMessages
284 {
285   BOUNDS_CHANGED, ///< Bounds changed
286   SET_OFFSET, ///< Set offset
287 };
288
289 // Custom specialization of std::hash
290 namespace std
291 {
292 template<>
293 struct hash<std::pair<CoalescableMessages, Dali::Accessibility::Accessible*>>
294 {
295   size_t operator()(std::pair<CoalescableMessages, Dali::Accessibility::Accessible*> value) const
296   {
297     return (static_cast<size_t>(value.first) * 131) ^ reinterpret_cast<size_t>(value.second);
298   }
299 };
300 } // namespace std
301
302 /**
303  * @brief The BridgeBase class is basic class for Bridge functions.
304  */
305 class BridgeBase : public Dali::Accessibility::Bridge, public Dali::ConnectionTracker
306 {
307   std::unordered_map<std::pair<CoalescableMessages, Dali::Accessibility::Accessible*>, std::tuple<unsigned int, unsigned int, std::function<void()>>> mCoalescableMessages;
308
309   /**
310    * @brief Removes all CoalescableMessages using Tick signal.
311    *
312    * @return False if mCoalescableMessages is empty, otherwise true.
313    */
314   bool TickCoalescableMessages();
315
316 public:
317   /**
318    * @brief Adds CoalescableMessages, Accessible, and delay time to mCoalescableMessages.
319    *
320    * @param[in] kind CoalescableMessages enum value
321    * @param[in] obj Accessible object
322    * @param[in] delay The delay time
323    * @param[in] functor The function to be called // NEED TO UPDATE!
324    */
325   void AddCoalescableMessage(CoalescableMessages kind, Dali::Accessibility::Accessible* obj, float delay, std::function<void()> functor);
326
327   /**
328    * @brief Callback when the visibility of the window is changed.
329    *
330    * @param[in] window The window to be changed
331    * @param[in] visible The visibility of the window
332    */
333   void OnWindowVisibilityChanged(Dali::Window window, bool visible);
334
335   /**
336    * @brief Callback when the window focus is changed.
337    *
338    * @param[in] window The window whose focus is changed
339    * @param[in] focusIn Whether the focus is in/out
340    */
341   void OnWindowFocusChanged(Dali::Window window, bool focusIn);
342
343   /**
344    * @copydoc Dali::Accessibility::Bridge::GetBusName()
345    */
346   const std::string& GetBusName() const override;
347
348   /**
349    * @copydoc Dali::Accessibility::Bridge::AddTopLevelWindow()
350    */
351   void AddTopLevelWindow(Dali::Accessibility::Accessible* windowAccessible) override;
352
353   /**
354    * @copydoc Dali::Accessibility::Bridge::RemoveTopLevelWindow()
355    */
356   void RemoveTopLevelWindow(Dali::Accessibility::Accessible* windowAccessible) override;
357
358   /**
359    * @copydoc Dali::Accessibility::Bridge::RegisterDefaultLabel()
360    */
361   void RegisterDefaultLabel(Dali::Accessibility::Accessible* object) override;
362
363   /**
364    * @copydoc Dali::Accessibility::Bridge::UnregisterDefaultLabel()
365    */
366   void UnregisterDefaultLabel(Dali::Accessibility::Accessible* object) override;
367
368   /**
369    * @copydoc Dali::Accessibility::Bridge::GetDefaultLabel()
370    */
371   Dali::Accessibility::Accessible* GetDefaultLabel(Dali::Accessibility::Accessible* root) const override;
372
373   /**
374    * @copydoc Dali::Accessibility::Bridge::GetApplication()
375    */
376   Dali::Accessibility::Accessible* GetApplication() const override
377   {
378     return &mApplication;
379   }
380
381   /**
382    * @brief Adds function to dbus interface.
383    */
384   template<typename SELF, typename... RET, typename... ARGS>
385   void AddFunctionToInterface(
386     DBus::DBusInterfaceDescription& desc, const std::string& funcName, DBus::ValueOrError<RET...> (SELF::*funcPtr)(ARGS...))
387   {
388     if(auto self = dynamic_cast<SELF*>(this))
389       desc.addMethod<DBus::ValueOrError<RET...>(ARGS...)>(
390         funcName,
391         [=](ARGS... args) -> DBus::ValueOrError<RET...> {
392           try
393           {
394             return (self->*funcPtr)(std::move(args)...);
395           }
396           catch(std::domain_error& e)
397           {
398             return DBus::Error{e.what()};
399           }
400         });
401   }
402
403   /**
404    * @brief Adds 'Get' property to dbus interface.
405    */
406   template<typename T, typename SELF>
407   void AddGetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
408                                  const std::string&              funcName,
409                                  T (SELF::*funcPtr)())
410   {
411     if(auto self = dynamic_cast<SELF*>(this))
412       desc.addProperty<T>(funcName,
413                           [=]() -> DBus::ValueOrError<T> {
414                             try
415                             {
416                               return (self->*funcPtr)();
417                             }
418                             catch(std::domain_error& e)
419                             {
420                               return DBus::Error{e.what()};
421                             }
422                           },
423                           {});
424   }
425
426   /**
427    * @brief Adds 'Set' property to dbus interface.
428    */
429   template<typename T, typename SELF>
430   void AddSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
431                                  const std::string&              funcName,
432                                  void (SELF::*funcPtr)(T))
433   {
434     if(auto self = dynamic_cast<SELF*>(this))
435       desc.addProperty<T>(funcName, {}, [=](T t) -> DBus::ValueOrError<void> {
436         try
437         {
438           (self->*funcPtr)(std::move(t));
439           return {};
440         }
441         catch(std::domain_error& e)
442         {
443           return DBus::Error{e.what()};
444         }
445       });
446   }
447
448   /**
449    * @brief Adds 'Set' and 'Get' properties to dbus interface.
450    */
451   template<typename T, typename T1, typename SELF>
452   void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
453                                     const std::string&              funcName,
454                                     T1 (SELF::*funcPtrGet)(),
455                                     DBus::ValueOrError<void> (SELF::*funcPtrSet)(T))
456   {
457     if(auto self = dynamic_cast<SELF*>(this))
458       desc.addProperty<T>(
459         funcName,
460         [=]() -> DBus::ValueOrError<T> {
461           try
462           {
463             return (self->*funcPtrGet)();
464           }
465           catch(std::domain_error& e)
466           {
467             return DBus::Error{e.what()};
468           }
469         },
470         [=](T t) -> DBus::ValueOrError<void> {
471           try
472           {
473             (self->*funcPtrSet)(std::move(t));
474             return {};
475           }
476           catch(std::domain_error& e)
477           {
478             return DBus::Error{e.what()};
479           }
480         });
481   }
482
483   /**
484    * @brief Adds 'Get' and 'Set' properties to dbus interface.
485    */
486   template<typename T, typename T1, typename SELF>
487   void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
488                                     const std::string&              funcName,
489                                     T1 (SELF::*funcPtrGet)(),
490                                     void (SELF::*funcPtrSet)(T))
491   {
492     if(auto self = dynamic_cast<SELF*>(this))
493       desc.addProperty<T>(
494         funcName,
495         [=]() -> DBus::ValueOrError<T> {
496           try
497           {
498             return (self->*funcPtrGet)();
499           }
500           catch(std::domain_error& e)
501           {
502             return DBus::Error{e.what()};
503           }
504         },
505         [=](T t) -> DBus::ValueOrError<void> {
506           try
507           {
508             (self->*funcPtrSet)(std::move(t));
509             return {};
510           }
511           catch(std::domain_error& e)
512           {
513             return DBus::Error{e.what()};
514           }
515         });
516   }
517
518   /**
519    * @brief Gets the string of the path excluding the specified prefix.
520    *
521    * @param path The path to get
522    * @return The string stripped of the specific prefix
523    */
524   static std::string StripPrefix(const std::string& path);
525
526   /**
527    * @brief Finds the Accessible object according to the path.
528    *
529    * @param[in] path The path for Accessible object
530    * @return The Accessible object corresponding to the path
531    */
532   Dali::Accessibility::Accessible* Find(const std::string& path) const;
533
534   /**
535    * @brief Finds the Accessible object with the given address.
536    *
537    * @param[in] ptr The unique Address of the object
538    * @return The Accessible object corresponding to the path
539    */
540   Dali::Accessibility::Accessible* Find(const Dali::Accessibility::Address& ptr) const;
541
542   /**
543    * @brief Returns the target object of the currently executed DBus method call.
544    *
545    * @return The Accessible object
546    * @note When a DBus method is called on some object, this target object (`currentObject`) is temporarily saved by the bridge,
547    * because DBus handles the invocation target separately from the method arguments.
548    * We then use the saved object inside the 'glue' method (e.g. BridgeValue::GetMinimum)
549    * to call the equivalent method on the respective C++ object (this could be ScrollBar::AccessibleImpl::GetMinimum in the example given).
550    */
551   Dali::Accessibility::Accessible* FindCurrentObject() const;
552
553   /**
554    * @brief Returns the target object of the currently executed DBus method call.
555    *
556    * This method tries to downcast the return value of FindCurrentObject() to the requested type,
557    * issuing an error reply to the DBus caller if the requested type is not implemented. Whether
558    * a given type is implemented is decided based on the return value of Accessible::GetInterfaces()
559    * for the current object.
560    *
561    * @tparam I The requested AT-SPI interface
562    * @return The Accessible object (cast to a more derived type)
563    *
564    * @see FindCurrentObject()
565    * @see Dali::Accessibility::AtspiInterface
566    * @see Dali::Accessibility::AtspiInterfaceType
567    * @see Dali::Accessibility::Accessible::GetInterfaces()
568    */
569   template<Dali::Accessibility::AtspiInterface I>
570   auto* FindCurrentObjectWithInterface() const
571   {
572     using Type = Dali::Accessibility::AtspiInterfaceType<I>;
573
574     Type* result;
575     auto* currentObject = FindCurrentObject();
576     DALI_ASSERT_DEBUG(currentObject); // FindCurrentObject() throws domain_error
577
578     if(!(result = Dali::Accessibility::Accessible::DownCast<I>(currentObject)))
579     {
580       std::stringstream s;
581
582       s << "Object " << currentObject->GetAddress().ToString();
583       s << " does not implement ";
584       s << Dali::Accessibility::Accessible::GetInterfaceName(I);
585
586       throw std::domain_error{s.str()};
587     }
588
589     return result;
590   }
591
592   /**
593    * @copydoc Dali::Accessibility::Bridge::FindByPath()
594    */
595   Dali::Accessibility::Accessible* FindByPath(const std::string& name) const override;
596
597   /**
598    * @copydoc Dali::Accessibility::Bridge::SetApplicationName()
599    */
600   void SetApplicationName(std::string name) override
601   {
602     mApplication.mName = std::move(name);
603   }
604
605   /**
606    * @copydoc Dali::Accessibility::Bridge::SetToolkitName()
607    */
608   void SetToolkitName(std::string_view toolkitName) override
609   {
610     mApplication.mToolkitName = std::string{toolkitName};
611   }
612
613 protected:
614   mutable ApplicationAccessible                 mApplication;
615   std::vector<Dali::Accessibility::Accessible*> mDefaultLabels;
616   bool                                          mIsScreenReaderSuppressed = false;
617
618 private:
619   /**
620    * @brief Sets an ID.
621    * @param[in] id An ID (integer value)
622    */
623   void SetId(int id);
624
625   /**
626    * @brief Gets the ID.
627    * @return The ID to be set
628    */
629   int GetId();
630
631   /**
632    * @brief Update registered events.
633    */
634   void UpdateRegisteredEvents();
635
636   using CacheElementType = std::tuple<
637     Dali::Accessibility::Address,
638     Dali::Accessibility::Address,
639     Dali::Accessibility::Address,
640     std::vector<Dali::Accessibility::Address>,
641     std::vector<std::string>,
642     std::string,
643     Dali::Accessibility::Role,
644     std::string,
645     std::array<uint32_t, 2>>;
646
647   /**
648    * @brief Gets Items  // NEED TO UPDATE!
649    *
650    * @return
651    */
652   DBus::ValueOrError<std::vector<CacheElementType>> GetItems();
653
654   /**
655    * @brief Creates CacheElement.
656    *
657    * CreateCacheElement method works for GetItems which is a part of ATSPI protocol.
658    * ATSPI client library (libatspi from at-spi2-core) depending on cacheing policy configuration uses GetItems
659    * to pre-load entire accessible tree from application to its own cache in single dbus call.
660    * Otherwise the particular nodes in a tree are cached lazily when client library tries to access them.
661    * @param item Accessible to get information
662    * @return The elements to be cached
663    */
664   CacheElementType CreateCacheElement(Dali::Accessibility::Accessible* item);
665
666 protected:
667   BridgeBase();
668   virtual ~BridgeBase();
669
670   /**
671    * @copydoc Dali::Accessibility::Bridge::ForceUp()
672    */
673   ForceUpResult ForceUp() override;
674
675   /**
676    * @copydoc Dali::Accessibility::Bridge::ForceDown()
677    */
678   void ForceDown() override;
679
680   DBus::DBusServer           mDbusServer;
681   DBusWrapper::ConnectionPtr mConnectionPtr;
682   int                        mId = 0;
683   DBus::DBusClient           mRegistry;
684   bool                       IsBoundsChangedEventAllowed{false};
685 };
686
687 #endif // DALI_INTERNAL_ACCESSIBILITY_BRIDGE_BASE_H