Updated header files to new format too
[platform/core/uifw/dali-adaptor.git] / dali / internal / accessibility / bridge / accessibility-common.h
1 #ifndef DALI_INTERNAL_ATSPI_ACCESSIBILITY_COMMON_H
2 #define DALI_INTERNAL_ATSPI_ACCESSIBILITY_COMMON_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/integration-api/debug.h>
23 #include <iomanip>
24 #include <sstream>
25 #include <string>
26
27 // INTERNAL INCLUDES
28 #include <dali/devel-api/adaptor-framework/accessibility-impl.h>
29 #include <dali/internal/accessibility/bridge/dbus-locators.h>
30 #include <dali/internal/accessibility/bridge/dbus.h>
31 #include <dali/public-api/dali-adaptor-common.h>
32
33 #define A11yDbusName "org.a11y.Bus"
34 #define A11yDbusPath "/org/a11y/bus"
35 #define A11yDbusStatusInterface "org.a11y.Status"
36 #define AtspiDbusNameRegistry "org.a11y.atspi.Registry"
37 #define AtspiDbusPathRoot "/org/a11y/atspi/accessible/root"
38 #define AtspiDbusInterfaceSocket "org.a11y.atspi.Socket"
39 #define AtspiPath "/org/a11y/atspi/accessible"
40 #define AtspiDbusInterfaceAccessible "org.a11y.atspi.Accessible"
41 #define AtspiDbusInterfaceAction "org.a11y.atspi.Action"
42 #define AtspiDbusInterfaceApplication "org.a11y.atspi.Application"
43 #define AtspiDbusInterfaceCollection "org.a11y.atspi.Collection"
44 #define AtspiDbusInterfaceComponent "org.a11y.atspi.Component"
45 #define AtspiDbusInterfaceDocument "org.a11y.atspi.Document"
46 #define AtspiDbusInterfaceEditableText "org.a11y.atspi.EditableText"
47 #define AtspiDbusInterfaceEventKeyboard "org.a11y.atspi.Event.Keyboard"
48 #define AtspiDbusInterfaceEventMouse "org.a11y.atspi.Event.Mouse"
49 #define AtspiDbusInterfaceEventObject "org.a11y.atspi.Event.Object"
50 #define AtspiDbusInterfaceHyperlink "org.a11y.atspi.Hyperlink"
51 #define AtspiDbusInterfaceHypertext "org.a11y.atspi.Hypertext"
52 #define AtspiDbusInterfaceImage "org.a11y.atspi.Image"
53 #define AtspiDbusInterfaceSelection "org.a11y.atspi.Selection"
54 #define AtspiDbusInterfaceTable "org.a11y.atspi.Table"
55 #define AtspiDbusInterfaceTableCell "org.a11y.atspi.TableCell"
56 #define AtspiDbusInterfaceText "org.a11y.atspi.Text"
57 #define AtspiDbusInterfaceValue "org.a11y.atspi.Value"
58 #define AtspiDbusInterfaceSocket "org.a11y.atspi.Socket"
59 #define AtspiDbusInterfaceEventWindow "org.a11y.atspi.Event.Window"
60
61 #define AtspiDbusPathDec "/org/a11y/atspi/registry/deviceeventcontroller"
62 #define AtspiDbusInterfaceDec "org.a11y.atspi.DeviceEventController"
63 #define AtspiDbusInterfaceDeviceEventListener "org.a11y.atspi.DeviceEventListener"
64
65 #define DirectReadingDBusName "org.tizen.ScreenReader"
66 #define DirectReadingDBusPath "/org/tizen/DirectReading"
67 #define DirectReadingDBusInterface "org.tizen.DirectReading"
68
69 struct ObjectPath;
70
71 /**
72  * @brief Enumeration used for quering Accessibility objects
73  */
74 enum class MatchType : int32_t
75 {
76   INVALID,
77   ALL,
78   ANY,
79   NONE,
80   EMPTY
81 };
82
83 /**
84  * @brief Enumeration used for quering Accessibility objects
85  * SortOrder::Canonical uses breadth-first search and sort objects in order of indexes in parent
86  * SortOrder::ReverseCanonical uses SortOrder::Canonical and reverse collection
87  * The rest of orders is not supported.
88  */
89 enum class SortOrder : uint32_t
90 {
91   INVALID,
92   CANONICAL,
93   FLOW,
94   TAB,
95   REVERSE_CANONICAL,
96   REVERSE_FLOW,
97   REVERSE_TAB,
98   LAST_DEFINED
99 };
100
101 namespace DBus
102 {
103 class CurrentBridgePtr
104 {
105   static Dali::Accessibility::Bridge*& get()
106   {
107     static thread_local Dali::Accessibility::Bridge* b = nullptr;
108     return b;
109   }
110   Dali::Accessibility::Bridge* prev;
111   CurrentBridgePtr(const CurrentBridgePtr&) = delete;
112   CurrentBridgePtr(CurrentBridgePtr&&)      = delete;
113   CurrentBridgePtr& operator=(const CurrentBridgePtr&) = delete;
114   CurrentBridgePtr& operator=(CurrentBridgePtr&&) = delete;
115
116 public:
117   CurrentBridgePtr(Dali::Accessibility::Bridge* b)
118   : prev(get())
119   {
120     get() = b;
121   }
122
123   ~CurrentBridgePtr()
124   {
125     get() = prev;
126   }
127
128   static Dali::Accessibility::Bridge* current()
129   {
130     return get();
131   }
132 };
133
134 namespace detail
135 {
136 template<typename T>
137 struct signature_accessible_impl : signature_helper<signature_accessible_impl<T>>
138 {
139   using subtype = std::pair<std::string, ObjectPath>;
140
141   static constexpr auto name_v = concat("AtspiAccessiblePtr");
142   static constexpr auto sig_v  = concat("(so)");
143
144   /**
145    * @brief Marshals value v as marshalled type into message
146    */
147   static void set(const DBusWrapper::MessageIterPtr& iter, T* t)
148   {
149     if(t)
150     {
151       auto v = t->GetAddress();
152       signature<subtype>::set(iter, {v.GetBus(), ObjectPath{std::string{ATSPI_PREFIX_PATH} + v.GetPath()}});
153     }
154     else
155     {
156       signature<subtype>::set(iter, {"", ObjectPath{ATSPI_NULL_PATH}});
157     }
158   }
159
160   /**
161    * @brief Marshals value from marshalled type into variable v
162    */
163   static bool get(const DBusWrapper::MessageIterPtr& iter, T*& v)
164   {
165     subtype tmp;
166     if(!signature<subtype>::get(iter, tmp))
167     {
168       return false;
169     }
170
171     if(tmp.second.value == ATSPI_NULL_PATH)
172     {
173       v = nullptr;
174       return true;
175     }
176
177     if(tmp.second.value.substr(0, strlen(ATSPI_PREFIX_PATH)) != ATSPI_PREFIX_PATH)
178     {
179       return false;
180     }
181
182     auto b = CurrentBridgePtr::current();
183     if(b->GetBusName() != tmp.first)
184     {
185       return false;
186     }
187
188     v = b->FindByPath(tmp.second.value.substr(strlen(ATSPI_PREFIX_PATH)));
189     return v != nullptr;
190   }
191 };
192
193 template<>
194 struct signature<Dali::Accessibility::Accessible*> : public signature_accessible_impl<Dali::Accessibility::Accessible>
195 {
196 };
197
198 template<>
199 struct signature<Dali::Accessibility::Address> : signature_helper<signature<Dali::Accessibility::Address>>
200 {
201   using subtype = std::pair<std::string, ObjectPath>;
202
203   static constexpr auto name_v = concat("AtspiAccessiblePtr");
204   static constexpr auto sig_v  = concat("(so)");
205
206   /**
207    * @brief Marshals value v as marshalled type into message
208    */
209   static void set(const DBusWrapper::MessageIterPtr& iter, const Dali::Accessibility::Address& v)
210   {
211     if(v)
212     {
213       signature<subtype>::set(iter, {v.GetBus(), ObjectPath{std::string{ATSPI_PREFIX_PATH} + v.GetPath()}});
214     }
215     else
216     {
217       signature<subtype>::set(iter, {v.GetBus(), ObjectPath{ATSPI_NULL_PATH}});
218     }
219   }
220
221   /**
222    * @brief Marshals value from marshalled type into variable v
223    */
224   static bool get(const DBusWrapper::MessageIterPtr& iter, Dali::Accessibility::Address& v)
225   {
226     subtype tmp;
227     if(!signature<subtype>::get(iter, tmp))
228     {
229       return false;
230     }
231
232     if(tmp.second.value == ATSPI_NULL_PATH)
233     {
234       v = {};
235       return true;
236     }
237     if(tmp.second.value.substr(0, strlen(ATSPI_PREFIX_PATH)) != ATSPI_PREFIX_PATH)
238     {
239       return false;
240     }
241
242     v = {std::move(tmp.first), tmp.second.value.substr(strlen(ATSPI_PREFIX_PATH))};
243     return true;
244   }
245 };
246
247 template<>
248 struct signature<Dali::Accessibility::States> : signature_helper<signature<Dali::Accessibility::States>>
249 {
250   using subtype = std::array<uint32_t, 2>;
251
252   static constexpr auto name_v = signature<subtype>::name_v;
253   static constexpr auto sig_v  = signature<subtype>::sig_v;
254
255   /**
256    * @brief Marshals value v as marshalled type into message
257    */
258   static void set(const DBusWrapper::MessageIterPtr& iter, const Dali::Accessibility::States& v)
259   {
260     signature<subtype>::set(iter, v.GetRawData());
261   }
262
263   /**
264    * @brief Marshals value from marshalled type into variable v
265    */
266   static bool get(const DBusWrapper::MessageIterPtr& iter, Dali::Accessibility::States& v)
267   {
268     subtype tmp;
269     if(!signature<subtype>::get(iter, tmp))
270     {
271       return false;
272     }
273     v = Dali::Accessibility::States{tmp};
274     return true;
275   }
276 };
277 } // namespace detail
278 } // namespace DBus
279
280 struct _Logger
281 {
282   const char*        file;
283   int                line;
284   std::ostringstream tmp;
285
286   _Logger(const char* f, int l)
287   : file(f),
288     line(l)
289   {
290   }
291
292   ~_Logger()
293   {
294     Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, "%s:%d: %s", file, line, tmp.str().c_str());
295   }
296
297   template<typename T>
298   _Logger& operator<<(T&& t)
299   {
300     tmp << std::forward<T>(t);
301     return *this;
302   }
303 };
304
305 struct _LoggerEmpty
306 {
307   template<typename T>
308   _LoggerEmpty& operator<<(T&& t)
309   {
310     return *this;
311   }
312 };
313
314 struct _LoggerScope
315 {
316   const char* file;
317   int         line;
318
319   _LoggerScope(const char* f, int l)
320   : file(f),
321     line(l)
322   {
323     Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, "%s:%d: +", file, line);
324   }
325
326   ~_LoggerScope()
327   {
328     Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, "%s:%d: -", file, line);
329   }
330 };
331
332 #define LOG() _Logger(__FILE__, __LINE__)
333 #define SCOPE() _LoggerScope _l##__LINE__(__FILE__, __LINE__)
334
335 #endif // DALI_INTERNAL_ATSPI_ACCESSIBILITY_COMMON_H