Revert "Apply multi Instance for widget"
[platform/core/uifw/dali-adaptor.git] / dali / internal / accessibility / tizen-wayland / atspi / accessibility-common.h
1 #ifndef DALI_INTERNAL_ATSPI_ACCESSIBILITY_COMMON_H
2 #define DALI_INTERNAL_ATSPI_ACCESSIBILITY_COMMON_H
3
4 /*
5  * Copyright (c) 2019 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/public-api/dali-adaptor-common.h>
29 #include <dali/internal/accessibility/tizen-wayland/atspi/dbus.h>
30 #include <dali/internal/accessibility/tizen-wayland/atspi/dbus-locators.h>
31 #include <dali/internal/accessibility/tizen-wayland/atspi/accessibility-impl.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
104 class CurrentBridgePtr
105 {
106   static Dali::Accessibility::Bridge*& get()
107   {
108     static thread_local Dali::Accessibility::Bridge* b = nullptr;
109     return b;
110   }
111   Dali::Accessibility::Bridge* prev;
112   CurrentBridgePtr( const CurrentBridgePtr& ) = delete;
113   CurrentBridgePtr( CurrentBridgePtr&& ) = delete;
114   CurrentBridgePtr& operator=( const CurrentBridgePtr& ) = delete;
115   CurrentBridgePtr& operator=( CurrentBridgePtr&& ) = delete;
116
117 public:
118   CurrentBridgePtr( Dali::Accessibility::Bridge* b )
119   : prev( get() )
120   {
121     get() = b;
122   }
123
124   ~CurrentBridgePtr()
125   {
126     get() = prev;
127   }
128
129   static Dali::Accessibility::Bridge* current()
130   {
131     return get();
132   }
133 };
134
135 namespace detail
136 {
137
138 template < typename T >
139 struct signature_accessible_impl
140 {
141
142 using subtype = std::pair< std::string, ObjectPath >;
143
144   /**
145    * @brief Returns name of type marshalled, for informative purposes
146    */
147   static std::string name()
148   {
149     return "AtspiAccessiblePtr";
150   }
151
152   /**
153    * @brief Returns DBUS' signature of type marshalled
154    */
155   static std::string sig()
156   {
157     return "(so)";
158   }
159
160   /**
161    * @brief Marshals value v as marshalled type into message
162    */
163   static void set( const DBusWrapper::MessageIterPtr& iter, T* t )
164   {
165     if( t )
166     {
167       auto v = t->GetAddress();
168       signature< subtype >::set( iter, { v.GetBus(), ObjectPath{std::string{ ATSPI_PREFIX_PATH } +v.GetPath()} } );
169     }
170     else
171     {
172       signature< subtype >::set( iter, { "", ObjectPath{ ATSPI_NULL_PATH } } );
173     }
174   }
175
176   /**
177    * @brief Marshals value from marshalled type into variable v
178    */
179   static bool get( const DBusWrapper::MessageIterPtr& iter, T*& v )
180   {
181     subtype tmp;
182     if( !signature< subtype >::get( iter, tmp ) )
183     {
184       return false;
185     }
186
187     if( tmp.second.value == ATSPI_NULL_PATH )
188     {
189       v = nullptr;
190       return true;
191     }
192
193     if( tmp.second.value.substr( 0, strlen( ATSPI_PREFIX_PATH ) ) != ATSPI_PREFIX_PATH )
194     {
195       return false;
196     }
197
198     auto b = CurrentBridgePtr::current();
199     if( b->GetBusName() != tmp.first )
200     {
201       return false;
202     }
203
204     v = b->FindByPath( tmp.second.value.substr( strlen( ATSPI_PREFIX_PATH ) ) );
205     return v != nullptr;
206   }
207 };
208
209 template <>
210 struct signature< Dali::Accessibility::Accessible* > : public signature_accessible_impl< Dali::Accessibility::Accessible >
211 {
212 };
213
214 template <>
215 struct signature< Dali::Accessibility::Address >
216 {
217 using subtype = std::pair< std::string, ObjectPath >;
218
219   /**
220    * @brief Returns name of type marshalled, for informative purposes
221    */
222   static std::string name()
223   {
224     return "AtspiAccessiblePtr";
225   }
226
227   /**
228    * @brief Returns DBUS' signature of type marshalled
229    */
230   static std::string sig()
231   {
232     return "(so)";
233   }
234
235   /**
236    * @brief Marshals value v as marshalled type into message
237    */
238   static void set( const DBusWrapper::MessageIterPtr& iter, const Dali::Accessibility::Address& v )
239   {
240     if( v )
241     {
242       signature< subtype >::set( iter, { v.GetBus(), ObjectPath{ std::string{ ATSPI_PREFIX_PATH } + v.GetPath() } } );
243     }
244     else
245     {
246       signature< subtype >::set( iter, { v.GetBus(), ObjectPath{ ATSPI_NULL_PATH } } );
247     }
248   }
249
250   /**
251    * @brief Marshals value from marshalled type into variable v
252    */
253   static bool get( const DBusWrapper::MessageIterPtr& iter, Dali::Accessibility::Address& v )
254   {
255     subtype tmp;
256     if( !signature< subtype >::get( iter, tmp ) )
257     {
258       return false;
259     }
260
261     if( tmp.second.value == ATSPI_NULL_PATH )
262     {
263       v = {};
264       return true;
265     }
266     if( tmp.second.value.substr( 0, strlen( ATSPI_PREFIX_PATH ) ) != ATSPI_PREFIX_PATH )
267     {
268       return false;
269     }
270     
271   v = { std::move( tmp.first ), tmp.second.value.substr( strlen( ATSPI_PREFIX_PATH ) ) };
272   return true;
273   }
274 };
275
276 template <>
277 struct signature< Dali::Accessibility::States >
278 {
279 using subtype = std::array< uint32_t, 2 >;
280
281   /**
282    * @brief Returns name of type marshalled, for informative purposes
283    */
284   static std::string name()
285   {
286     return signature< subtype >::name();
287   }
288
289   /**
290    * @brief Returns DBUS' signature of type marshalled
291    */
292   static std::string sig()
293   {
294     return signature< subtype >::sig();
295   }
296
297   /**
298    * @brief Marshals value v as marshalled type into message
299    */
300   static void set( const DBusWrapper::MessageIterPtr& iter, const Dali::Accessibility::States& v )
301   {
302     signature< subtype >::set( iter, v.GetRawData() );
303   }
304
305   /**
306    * @brief Marshals value from marshalled type into variable v
307    */
308   static bool get( const DBusWrapper::MessageIterPtr& iter, Dali::Accessibility::States& v )
309   {
310     subtype tmp;
311     if( !signature< subtype >::get( iter, tmp ) )
312     {
313       return false;
314     }
315     v = Dali::Accessibility::States{ tmp };
316     return true;
317   }
318 };
319 }
320 }
321
322 struct _Logger
323 {
324   const char* file;
325   int line;
326   std::ostringstream tmp;
327
328   _Logger( const char* f, int l )
329   : file( f ),
330     line( l ){}
331
332   ~_Logger()
333   {
334     Dali::Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo, "%s:%d: %s", file, line, tmp.str().c_str() );
335   }
336
337   template < typename T >
338   _Logger& operator<<( T&& t )
339   {
340     tmp << std::forward< T >( t );
341     return *this;
342   }
343 };
344
345 struct _LoggerEmpty
346 {
347   template < typename T >
348   _LoggerEmpty& operator<<( T&& t )
349   {
350     return *this;
351   }
352 };
353
354 struct _LoggerScope
355 {
356   const char* file;
357   int line;
358
359   _LoggerScope( const char* f, int l )
360   : file( f ),
361     line( l )
362   {
363     Dali::Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo, "%s:%d: +", file, line );
364   }
365
366   ~_LoggerScope()
367   {
368     Dali::Integration::Log::LogMessage( Dali::Integration::Log::DebugInfo, "%s:%d: -", file, line );
369   }
370 };
371
372 #define LOG() _Logger( __FILE__, __LINE__ )
373 #define SCOPE() _LoggerScope _l##__LINE__( __FILE__, __LINE__ )
374
375 #endif // DALI_INTERNAL_ATSPI_ACCESSIBILITY_COMMON_H