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