VectorImageRenderer: Remove TizenVectorImageRenderer dependency
[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/dali-adaptor-version.h>
23 #include <dali/public-api/signals/connection-tracker.h>
24 #include <memory>
25
26 // INTERNAL INCLUDES
27 #include <dali/internal/accessibility/bridge/accessibility-common.h>
28
29 class AppAccessible : public virtual Dali::Accessibility::Accessible, public virtual Dali::Accessibility::Collection, public virtual Dali::Accessibility::Application
30 {
31 public:
32   Dali::Accessibility::EmptyAccessibleWithAddress parent;
33   std::vector<Dali::Accessibility::Accessible*>   children;
34   std::string                                     name;
35
36   std::string GetName() override
37   {
38     return name;
39   }
40
41   std::string GetDescription() override
42   {
43     return "";
44   }
45
46   Dali::Accessibility::Accessible* GetParent() override
47   {
48     return &parent;
49   }
50
51   size_t GetChildCount() override
52   {
53     return children.size();
54   }
55
56   Dali::Accessibility::Accessible* GetChildAtIndex(size_t index) override
57   {
58     auto s = children.size();
59     if(index >= s)
60       throw std::domain_error{"invalid index " + std::to_string(index) + " for object with " + std::to_string(s) + " children"};
61     return children[index];
62   }
63
64   size_t GetIndexInParent() override
65   {
66     throw std::domain_error{"can't call GetIndexInParent on application object"};
67   }
68
69   Dali::Accessibility::Role GetRole() override
70   {
71     return Dali::Accessibility::Role::APPLICATION;
72   }
73
74   Dali::Accessibility::States GetStates() override
75   {
76     return {};
77   }
78
79   Dali::Accessibility::Attributes GetAttributes() override
80   {
81     return {};
82   }
83
84   Dali::Accessibility::Accessible* getActiveWindow()
85   {
86     return children.empty() ? nullptr : children[0];
87   }
88
89   bool DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo) override
90   {
91     return false;
92   }
93
94   std::vector<Dali::Accessibility::Relation> GetRelationSet() override
95   {
96     return {};
97   }
98
99   Dali::Accessibility::Address GetAddress() override
100   {
101     return {"", "root"};
102   }
103
104   std::string GetToolkitName() override
105   {
106     return {"dali"};
107   }
108
109   std::string GetVersion() override
110   {
111     return std::to_string(Dali::ADAPTOR_MAJOR_VERSION) + "." + std::to_string(Dali::ADAPTOR_MINOR_VERSION);
112   }
113 };
114
115 enum class FilteredEvents
116 {
117   boundsChanged
118 };
119
120 namespace std
121 {
122 template<>
123 struct hash<std::pair<FilteredEvents, Dali::Accessibility::Accessible*>>
124 {
125   size_t operator()(std::pair<FilteredEvents, Dali::Accessibility::Accessible*> v) const
126   {
127     return (static_cast<size_t>(v.first) * 131) ^ reinterpret_cast<size_t>(v.second);
128   }
129 };
130 } // namespace std
131
132 class BridgeBase : public Dali::Accessibility::Bridge, public Dali::ConnectionTracker
133 {
134   std::unordered_map<std::pair<FilteredEvents, Dali::Accessibility::Accessible*>, std::pair<unsigned int, std::function<void()>>> filteredEvents;
135
136   bool tickFilteredEvents();
137
138 public:
139   void addFilteredEvent(FilteredEvents kind, Dali::Accessibility::Accessible* obj, float delay, std::function<void()> functor);
140
141   const std::string& GetBusName() const override;
142   void               AddTopLevelWindow(Dali::Accessibility::Accessible* window) override;
143   void               RemoveTopLevelWindow(Dali::Accessibility::Accessible* window) override;
144   void               AddPopup(Dali::Accessibility::Accessible* object) override;
145   void               RemovePopup(Dali::Accessibility::Accessible* object) override;
146
147   Dali::Accessibility::Accessible* GetApplication() const override
148   {
149     return &application;
150   }
151
152   template<typename SELF, typename... RET, typename... ARGS>
153   void AddFunctionToInterface(
154     DBus::DBusInterfaceDescription& desc, const std::string& funcName, DBus::ValueOrError<RET...> (SELF::*funcPtr)(ARGS...))
155   {
156     if(auto self = dynamic_cast<SELF*>(this))
157       desc.addMethod<DBus::ValueOrError<RET...>(ARGS...)>(
158         funcName,
159         [=](ARGS... args) -> DBus::ValueOrError<RET...> {
160           try
161           {
162             return (self->*funcPtr)(std::move(args)...);
163           }
164           catch(std::domain_error& e)
165           {
166             return DBus::Error{e.what()};
167           }
168         });
169   }
170
171   template<typename T, typename SELF>
172   void AddGetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
173                                  const std::string&              funcName,
174                                  T (SELF::*funcPtr)())
175   {
176     if(auto self = dynamic_cast<SELF*>(this))
177       desc.addProperty<T>(funcName,
178                           [=]() -> DBus::ValueOrError<T> {
179                             try
180                             {
181                               return (self->*funcPtr)();
182                             }
183                             catch(std::domain_error& e)
184                             {
185                               return DBus::Error{e.what()};
186                             }
187                           },
188                           {});
189   }
190
191   template<typename T, typename SELF>
192   void AddSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
193                                  const std::string&              funcName,
194                                  void (SELF::*funcPtr)(T))
195   {
196     if(auto self = dynamic_cast<SELF*>(this))
197       desc.addProperty<T>(funcName, {}, [=](T t) -> DBus::ValueOrError<void> {
198         try
199         {
200           (self->*funcPtr)(std::move(t));
201           return {};
202         }
203         catch(std::domain_error& e)
204         {
205           return DBus::Error{e.what()};
206         }
207       });
208   }
209
210   template<typename T, typename T1, typename SELF>
211   void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
212                                     const std::string&              funcName,
213                                     T1 (SELF::*funcPtrGet)(),
214                                     DBus::ValueOrError<void> (SELF::*funcPtrSet)(T))
215   {
216     if(auto self = dynamic_cast<SELF*>(this))
217       desc.addProperty<T>(
218         funcName,
219         [=]() -> DBus::ValueOrError<T> {
220           try
221           {
222             return (self->*funcPtrGet)();
223           }
224           catch(std::domain_error& e)
225           {
226             return DBus::Error{e.what()};
227           }
228         },
229         [=](T t) -> DBus::ValueOrError<void> {
230           try
231           {
232             (self->*funcPtrSet)(std::move(t));
233             return {};
234           }
235           catch(std::domain_error& e)
236           {
237             return DBus::Error{e.what()};
238           }
239         });
240   }
241   template<typename T, typename T1, typename SELF>
242   void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
243                                     const std::string&              funcName,
244                                     T1 (SELF::*funcPtrGet)(),
245                                     void (SELF::*funcPtrSet)(T))
246   {
247     if(auto self = dynamic_cast<SELF*>(this))
248       desc.addProperty<T>(
249         funcName,
250         [=]() -> DBus::ValueOrError<T> {
251           try
252           {
253             return (self->*funcPtrGet)();
254           }
255           catch(std::domain_error& e)
256           {
257             return DBus::Error{e.what()};
258           }
259         },
260         [=](T t) -> DBus::ValueOrError<void> {
261           try
262           {
263             (self->*funcPtrSet)(std::move(t));
264             return {};
265           }
266           catch(std::domain_error& e)
267           {
268             return DBus::Error{e.what()};
269           }
270         });
271   }
272   static std::string StripPrefix(const std::string& path);
273
274   Dali::Accessibility::Accessible* Find(const std::string& path) const;
275   Dali::Accessibility::Accessible* Find(const Dali::Accessibility::Address& ptr) const;
276   Dali::Accessibility::Accessible* FindSelf() const;
277   Dali::Accessibility::Accessible* FindByPath(const std::string& name) const override;
278   void                             SetApplicationName(std::string name) override
279   {
280     application.name = std::move(name);
281   }
282
283 protected:
284   mutable AppAccessible                         application;
285   std::vector<Dali::Accessibility::Accessible*> popups;
286
287 private:
288   void IdSet(int id);
289   int  IdGet();
290   void RegisteredEventsUpdate();
291
292   using CacheElementType = std::tuple<
293     Dali::Accessibility::Address,
294     Dali::Accessibility::Address,
295     Dali::Accessibility::Address,
296     std::vector<Dali::Accessibility::Address>,
297     std::vector<std::string>,
298     std::string,
299     Dali::Accessibility::Role,
300     std::string,
301     std::array<uint32_t, 2>>;
302   DBus::ValueOrError<std::vector<CacheElementType>> GetItems();
303   CacheElementType                                  CreateCacheElement(Dali::Accessibility::Accessible* item);
304
305 protected:
306   BridgeBase();
307   virtual ~BridgeBase();
308
309   ForceUpResult ForceUp() override;
310   void          ForceDown() override;
311
312   DBus::DBusServer           dbusServer;
313   DBusWrapper::ConnectionPtr con;
314   int                        id = 0;
315   DBus::DBusClient           registry;
316   bool                       allowObjectBoundsChangedEvent{false};
317 };
318
319 #endif // DALI_INTERNAL_ACCESSIBILITY_BRIDGE_BASE_H