Add window minimize/restore/maximize event for DALi
[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) 2022 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-bridge.h>
29 #include <dali/internal/accessibility/bridge/dbus/dbus-locators.h>
30 #include <dali/internal/accessibility/bridge/dbus/dbus.h>
31 #include <dali/public-api/dali-adaptor-common.h>
32
33 // DBus names
34
35 #define A11yDbusName "org.a11y.Bus"
36 #define A11yDbusStatusInterface "org.a11y.Status"
37 #define AtspiDbusNameRegistry "org.a11y.atspi.Registry"
38 #define DirectReadingDBusName "org.tizen.ScreenReader"
39 #define DirectReadingDBusInterface "org.tizen.DirectReading"
40
41 // DBus paths
42
43 #define A11yDbusPath "/org/a11y/bus"
44 #define AtspiDbusPathCache "/org/a11y/atspi/cache"
45 #define AtspiDbusPathDec "/org/a11y/atspi/registry/deviceeventcontroller"
46 #define AtspiDbusPathRegistry "/org/a11y/atspi/registry"
47 #define AtspiDbusPathRoot "/org/a11y/atspi/accessible/root"
48 #define AtspiPath "/org/a11y/atspi/accessible"
49 #define DirectReadingDBusPath "/org/tizen/DirectReading"
50
51 struct ObjectPath;
52
53 /**
54  * @brief Enumeration used for quering Accessibility objects
55  */
56 enum class MatchType : int32_t
57 {
58   INVALID,
59   ALL,
60   ANY,
61   NONE,
62   EMPTY
63 };
64
65 /**
66  * @brief Enumeration used for quering Accessibility objects
67  * SortOrder::Canonical uses breadth-first search and sort objects in order of indexes in parent
68  * SortOrder::ReverseCanonical uses SortOrder::Canonical and reverse collection
69  * The rest of orders is not supported.
70  */
71 enum class SortOrder : uint32_t
72 {
73   INVALID,
74   CANONICAL,
75   FLOW,
76   TAB,
77   REVERSE_CANONICAL,
78   REVERSE_FLOW,
79   REVERSE_TAB,
80   LAST_DEFINED
81 };
82
83 namespace DBus
84 {
85
86 /**
87  * @brief The CurrentBridgePtr class is to save the current Accessibility Bridge.
88  */
89 class CurrentBridgePtr
90 {
91   static Dali::Accessibility::Bridge*& Get()
92   {
93     static thread_local Dali::Accessibility::Bridge* bridge = nullptr;
94     return bridge;
95   }
96   Dali::Accessibility::Bridge* mPrev;
97   CurrentBridgePtr(const CurrentBridgePtr&) = delete;
98   CurrentBridgePtr(CurrentBridgePtr&&)      = delete;
99   CurrentBridgePtr& operator=(const CurrentBridgePtr&) = delete;
100   CurrentBridgePtr& operator=(CurrentBridgePtr&&) = delete;
101
102 public:
103   CurrentBridgePtr(Dali::Accessibility::Bridge* bridge)
104   : mPrev(Get())
105   {
106     Get() = bridge;
107   }
108
109   ~CurrentBridgePtr()
110   {
111     Get() = mPrev;
112   }
113
114   static Dali::Accessibility::Bridge* GetCurrentBridge()
115   {
116     return Get();
117   }
118 }; // CurrentBridgePtr
119
120
121 // Templates for setting and getting Accessible values
122 namespace detail
123 {
124 template<>
125 struct signature<Dali::Accessibility::Address> : signature_helper<signature<Dali::Accessibility::Address>>
126 {
127   using subtype = std::pair<std::string, ObjectPath>;
128
129   static constexpr auto name_v = concat("AtspiAccessiblePtr");
130   static constexpr auto sig_v  = signature<subtype>::sig_v; // "(so)"
131
132   /**
133    * @brief Marshals value address as marshalled type into message
134    */
135   static void set(const DBusWrapper::MessageIterPtr& iter, const Dali::Accessibility::Address& address)
136   {
137     if(address)
138     {
139       signature<subtype>::set(iter, {address.GetBus(), ObjectPath{std::string{ATSPI_PREFIX_PATH} + address.GetPath()}});
140     }
141     else
142     {
143       signature<subtype>::set(iter, {address.GetBus(), ObjectPath{ATSPI_NULL_PATH}});
144     }
145   }
146
147   /**
148    * @brief Marshals value from marshalled type into variable address
149    */
150   static bool get(const DBusWrapper::MessageIterPtr& iter, Dali::Accessibility::Address& address)
151   {
152     subtype tmp;
153     if(!signature<subtype>::get(iter, tmp))
154     {
155       return false;
156     }
157
158     if(tmp.second.value == ATSPI_NULL_PATH)
159     {
160       address = {};
161       return true;
162     }
163     if(tmp.second.value.substr(0, strlen(ATSPI_PREFIX_PATH)) != ATSPI_PREFIX_PATH)
164     {
165       return false;
166     }
167
168     address = {std::move(tmp.first), tmp.second.value.substr(strlen(ATSPI_PREFIX_PATH))};
169     return true;
170   }
171 };
172
173 template<typename T>
174 struct SignatureAccessibleImpl : signature_helper<SignatureAccessibleImpl<T>>
175 {
176   using subtype = Dali::Accessibility::Address;
177
178   static constexpr auto name_v = signature<subtype>::name_v;
179   static constexpr auto sig_v  = signature<subtype>::sig_v;
180
181   /**
182    * @brief Marshals value address as marshalled type into message
183    */
184   static void set(const DBusWrapper::MessageIterPtr& iter, T* accessible)
185   {
186     signature<subtype>::set(iter, accessible ? accessible->GetAddress() : subtype{});
187   }
188
189   /**
190    * @brief Marshals value from marshalled type into variable path
191    */
192   static bool get(const DBusWrapper::MessageIterPtr& iter, T*& path)
193   {
194     subtype address;
195
196     signature<subtype>::get(iter, address);
197
198     auto currentBridge = CurrentBridgePtr::GetCurrentBridge();
199     if(currentBridge->GetBusName() != address.GetBus())
200     {
201       return false;
202     }
203
204     path = currentBridge->FindByPath(address.GetPath());
205     return path != nullptr;
206   }
207 };
208
209 template<>
210 struct signature<Dali::Accessibility::Accessible*> : public SignatureAccessibleImpl<Dali::Accessibility::Accessible>
211 {
212 };
213
214 template<>
215 struct signature<Dali::Accessibility::States> : signature_helper<signature<Dali::Accessibility::States>>
216 {
217   using subtype = std::array<uint32_t, 2>;
218
219   static constexpr auto name_v = signature<subtype>::name_v;
220   static constexpr auto sig_v  = signature<subtype>::sig_v;
221
222   /**
223    * @brief Marshals value state as marshalled type into message
224    */
225   static void set(const DBusWrapper::MessageIterPtr& iter, const Dali::Accessibility::States& states)
226   {
227     signature<subtype>::set(iter, states.GetRawData());
228   }
229
230   /**
231    * @brief Marshals value from marshalled type into variable state
232    */
233   static bool get(const DBusWrapper::MessageIterPtr& iter, Dali::Accessibility::States& state)
234   {
235     subtype tmp;
236     if(!signature<subtype>::get(iter, tmp))
237     {
238       return false;
239     }
240     state = Dali::Accessibility::States{tmp};
241     return true;
242   }
243 };
244 } // namespace detail
245 } // namespace DBus
246
247 struct _Logger
248 {
249   const char*        mFile;
250   int                mLine;
251   std::ostringstream mTmp;
252
253   _Logger(const char* file, int line)
254   : mFile(file),
255     mLine(line)
256   {
257   }
258
259   ~_Logger()
260   {
261     Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, "%s:%d: %s", mFile, mLine, mTmp.str().c_str());
262   }
263
264   template<typename T>
265   _Logger& operator<<(T&& t)
266   {
267     mTmp << std::forward<T>(t);
268     return *this;
269   }
270 };
271
272 struct _LoggerEmpty
273 {
274   template<typename T>
275   _LoggerEmpty& operator<<(T&& t)
276   {
277     return *this;
278   }
279 };
280
281 struct _LoggerScope
282 {
283   const char* mFile;
284   int         mLine;
285
286   _LoggerScope(const char* file, int line)
287   : mFile(file),
288     mLine(line)
289   {
290     Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, "%s:%d: +", mFile, mLine);
291   }
292
293   ~_LoggerScope()
294   {
295     Dali::Integration::Log::LogMessage(Dali::Integration::Log::DebugInfo, "%s:%d: -", mFile, mLine);
296   }
297 };
298
299 #define LOG() _Logger(__FILE__, __LINE__)
300 #define SCOPE() _LoggerScope _l##__LINE__(__FILE__, __LINE__)
301
302 #endif // DALI_INTERNAL_ATSPI_ACCESSIBILITY_COMMON_H