10a16a1c5289b9fe5f92ea15710aa749ff69dff7
[platform/core/uifw/dali-adaptor.git] / dali / dali-bridge / src / BridgeBase.hpp
1 #ifndef BRIDGE_HPP
2 #define BRIDGE_HPP
3
4 #include "Common.hpp"
5 #include <memory>
6
7 class AppAccessible : public virtual Dali::Accessibility::Accessible, public virtual Dali::Accessibility::Collection
8 {
9 public:
10   Dali::Accessibility::EmptyAccessibleWithAddress parent;
11   std::vector< Dali::Accessibility::Accessible* > children;
12   std::string name;
13
14   std::string GetName() override
15   {
16     return name;
17   }
18   std::string GetDescription() override
19   {
20     return "";
21   }
22   Dali::Accessibility::Accessible* GetParent() override
23   {
24     return &parent;
25   }
26   size_t GetChildCount() override
27   {
28     return children.size();
29   }
30   Dali::Accessibility::Accessible* GetChildAtIndex( size_t index ) override
31   {
32     auto s = children.size();
33     if( index >= s )
34       throw Dali::Accessibility::AccessibleError{"invalid index " + std::to_string( index ) + " for object with " + std::to_string( s ) + " children"};
35     return children[index];
36   }
37   size_t GetIndexInParent() override
38   {
39     throw Dali::Accessibility::AccessibleError{"can't call GetIndexInParent on application object"};
40   }
41   Dali::Accessibility::Role GetRole() override
42   {
43     return Dali::Accessibility::Role::Application;
44   }
45   Dali::Accessibility::States GetStates() override
46   {
47     return {};
48   }
49   Dali::Accessibility::Attributes GetAttributes() override
50   {
51     return {};
52   }
53   Dali::Accessibility::Accessible* getActiveWindow()
54   {
55     return children.empty() ? nullptr : children[0];
56   }
57 };
58
59 class BridgeBase : public Dali::Accessibility::Bridge
60 {
61 public:
62   const std::string& GetBusName() const override;
63   void SetApplicationChild( Dali::Accessibility::Accessible* root ) override;
64   Dali::Accessibility::Accessible* GetApplication() const override
65   {
66     return &application;
67   }
68
69   template < typename SELF, typename... RET, typename... ARGS >
70   void AddFunctionToInterface(
71       DBus::DBusInterfaceDescription& desc, const std::string& funcName,
72       DBus::ValueOrError< RET... > ( SELF::*funcPtr )( ARGS... ) )
73   {
74     desc.addMethod< DBus::ValueOrError< RET... >( ARGS... ) >( funcName,
75                                                                [=]( ARGS... args ) -> DBus::ValueOrError< RET... > {
76                                                                  try
77                                                                  {
78                                                                    return ( dynamic_cast< SELF* >( this )->*funcPtr )( std::move( args )... );
79                                                                  }
80                                                                  catch( Dali::Accessibility::AccessibleError& e )
81                                                                  {
82                                                                    return DBus::Error{e.what()};
83                                                                  }
84                                                                } );
85   }
86   template < typename T, typename SELF >
87   void AddGetPropertyToInterface( DBus::DBusInterfaceDescription& desc,
88                                   const std::string& funcName, T ( SELF::*funcPtr )() )
89   {
90     desc.addProperty< T >( funcName,
91                            [=]() -> DBus::ValueOrError< T > {
92                              try
93                              {
94                                return ( dynamic_cast< SELF* >( this )->*funcPtr )();
95                              }
96                              catch( Dali::Accessibility::AccessibleError& e )
97                              {
98                                return DBus::Error{e.what()};
99                              }
100                            },
101                            {} );
102   }
103   template < typename T, typename SELF >
104   void AddSetPropertyToInterface( DBus::DBusInterfaceDescription& desc,
105                                   const std::string& funcName, void ( SELF::*funcPtr )( T ) )
106   {
107     desc.addProperty< T >( funcName, {},
108                            [=]( T t ) -> DBus::ValueOrError< void > {
109                              try
110                              {
111                                ( dynamic_cast< SELF* >( this )->*funcPtr )( std::move( t ) );
112                                return {};
113                              }
114                              catch( Dali::Accessibility::AccessibleError& e )
115                              {
116                                return DBus::Error{e.what()};
117                              }
118                            } );
119   }
120   template < typename T, typename T1, typename SELF >
121   void AddGetSetPropertyToInterface( DBus::DBusInterfaceDescription& desc,
122                                      const std::string& funcName, T1 ( SELF::*funcPtrGet )(), DBus::ValueOrError< void > ( SELF::*funcPtrSet )( T ) )
123   {
124     desc.addProperty< T >( funcName,
125                            [=]() -> DBus::ValueOrError< T > {
126                              try
127                              {
128                                return ( dynamic_cast< SELF* >( this )->*funcPtrGet )();
129                              }
130                              catch( Dali::Accessibility::AccessibleError& e )
131                              {
132                                return DBus::Error{e.what()};
133                              }
134                            },
135                            [=]( T t ) -> DBus::ValueOrError< void > {
136                              try
137                              {
138                                ( dynamic_cast< SELF* >( this )->*funcPtrSet )( std::move( t ) );
139                                return {};
140                              }
141                              catch( Dali::Accessibility::AccessibleError& e )
142                              {
143                                return DBus::Error{e.what()};
144                              }
145                            } );
146   }
147   template < typename T, typename T1, typename SELF >
148   void AddGetSetPropertyToInterface( DBus::DBusInterfaceDescription& desc,
149                                      const std::string& funcName, T1 ( SELF::*funcPtrGet )(), void ( SELF::*funcPtrSet )( T ) )
150   {
151     desc.addProperty< T >( funcName,
152                            [=]() -> DBus::ValueOrError< T > {
153                              try
154                              {
155                                return ( dynamic_cast< SELF* >( this )->*funcPtrGet )();
156                              }
157                              catch( Dali::Accessibility::AccessibleError& e )
158                              {
159                                return DBus::Error{e.what()};
160                              }
161                            },
162                            [=]( T t ) -> DBus::ValueOrError< void > {
163                              try
164                              {
165                                ( dynamic_cast< SELF* >( this )->*funcPtrSet )( std::move( t ) );
166                                return {};
167                              }
168                              catch( Dali::Accessibility::AccessibleError& e )
169                              {
170                                return DBus::Error{e.what()};
171                              }
172                            } );
173   }
174   static std::string StripPrefix( const std::string& path );
175
176   Dali::Accessibility::Accessible* Find( const std::string& path ) const;
177   Dali::Accessibility::Accessible* Find( const Dali::Accessibility::Address& ptr ) const;
178   Dali::Accessibility::Accessible* FindSelf() const;
179   Dali::Accessibility::Accessible* FindByPath( const std::string& name ) const override;
180   void SetApplicationName( std::string name ) override
181   {
182     application.name = std::move( name );
183   }
184
185 protected:
186   mutable AppAccessible application;
187
188 private:
189   void IdSet( int id );
190   int IdGet();
191
192   using CacheElementType = std::tuple<
193       Dali::Accessibility::Address, Dali::Accessibility::Address, Dali::Accessibility::Address,
194       std::vector< Dali::Accessibility::Address >,
195       std::vector< std::string >,
196       std::string,
197       Dali::Accessibility::Role,
198       std::string,
199       std::array< uint32_t, 2 > >;
200   DBus::ValueOrError< std::vector< CacheElementType > > GetItems();
201   CacheElementType CreateCacheElement( Dali::Accessibility::Accessible* item );
202
203 protected:
204   BridgeBase();
205   virtual ~BridgeBase() = default;
206
207   ForceUpResult ForceUp() override;
208   void ForceDown() override;
209
210   DBus::DBusServer dbusServer;
211   std::shared_ptr< DBus::EldbusConnection > con;
212   int id = 0;
213 };
214
215 #endif