Update ATSPI code according to DALi coding rule
[platform/core/uifw/dali-adaptor.git] / dali / internal / accessibility / bridge / bridge-base.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/accessibility/bridge/bridge-base.h>
20
21 // EXTERNAL INCLUDES
22 #include <atomic>
23 #include <cstdlib>
24 #include <memory>
25
26 // INTERNAL INCLUDES
27 #include <dali/public-api/adaptor-framework/timer.h>
28
29 using namespace Dali::Accessibility;
30
31 static Dali::Timer tickTimer;
32
33 BridgeBase::~BridgeBase()
34 {
35 }
36
37 BridgeBase::BridgeBase()
38 {
39 }
40
41 void BridgeBase::addFilteredEvent(FilteredEvents kind, Dali::Accessibility::Accessible* obj, float delay, std::function<void()> functor)
42 {
43   if(delay < 0)
44   {
45     delay = 0;
46   }
47
48   auto it = filteredEvents.insert({{kind, obj}, {static_cast<unsigned int>(delay * 10), {}}});
49   if(it.second)
50   {
51     functor();
52   }
53   else
54   {
55     it.first->second.second = std::move(functor);
56   }
57
58   if(!tickTimer)
59   {
60     tickTimer = Dali::Timer::New(100);
61     tickTimer.TickSignal().Connect(this, &BridgeBase::tickFilteredEvents);
62   }
63 }
64
65 bool BridgeBase::tickFilteredEvents()
66 {
67   for(auto it = filteredEvents.begin(); it != filteredEvents.end();)
68   {
69     if(it->second.first)
70     {
71       --it->second.first;
72     }
73     else
74     {
75       if(it->second.second)
76       {
77         it->second.second();
78         it->second.second = {};
79       }
80       else
81       {
82         it = filteredEvents.erase(it);
83         continue;
84       }
85     }
86     ++it;
87   }
88   return !filteredEvents.empty();
89 }
90
91 void BridgeBase::RegisteredEventsUpdate()
92 {
93   using ReturnType = std::vector<std::tuple<std::string, std::string>>;
94   registry.method<DBus::ValueOrError<ReturnType>()>("GetRegisteredEvents").asyncCall([this](DBus::ValueOrError<ReturnType> msg) {
95     if(!msg)
96     {
97       LOG() << "Get registered events failed";
98       return;
99     }
100
101     allowObjectBoundsChangedEvent = false;
102
103     ReturnType values = std::get<ReturnType>(msg.getValues());
104     for(long unsigned int i = 0; i < values.size(); i++)
105     {
106       if(!std::get<1>(values[i]).compare("Object:BoundsChanged"))
107       {
108         allowObjectBoundsChangedEvent = true;
109       }
110     }
111   });
112 }
113
114 BridgeBase::ForceUpResult BridgeBase::ForceUp()
115 {
116   if(Bridge::ForceUp() == ForceUpResult::ALREADY_UP)
117   {
118     return ForceUpResult::ALREADY_UP;
119   }
120   auto proxy = DBus::DBusClient{dbusLocators::atspi::BUS, dbusLocators::atspi::OBJ_PATH, dbusLocators::atspi::BUS_INTERFACE, DBus::ConnectionType::SESSION};
121   auto addr  = proxy.method<std::string()>(dbusLocators::atspi::GET_ADDRESS).call();
122
123   if(!addr)
124   {
125     throw std::domain_error{std::string("failed at call '") + dbusLocators::atspi::GET_ADDRESS + "': " + addr.getError().message};
126   }
127
128   con           = DBusWrapper::Installed()->eldbus_address_connection_get_impl(std::get<0>(addr));
129   mData->mBusName = DBus::getConnectionName(con);
130   dbusServer    = {con};
131
132   {
133     DBus::DBusInterfaceDescription desc{"org.a11y.atspi.Cache"};
134     AddFunctionToInterface(desc, "GetItems", &BridgeBase::GetItems);
135     dbusServer.addInterface("/org/a11y/atspi/cache", desc);
136   }
137   {
138     DBus::DBusInterfaceDescription desc{"org.a11y.atspi.Application"};
139     AddGetSetPropertyToInterface(desc, "Id", &BridgeBase::IdGet, &BridgeBase::IdSet);
140     dbusServer.addInterface(AtspiPath, desc);
141   }
142
143   registry = {AtspiDbusNameRegistry, AtspiDbusPathRegistry, AtspiDbusInterfaceRegistry, con};
144
145   RegisteredEventsUpdate();
146
147   registry.addSignal<void(void)>("EventListenerRegistered", [this](void) {
148     RegisteredEventsUpdate();
149   });
150
151   registry.addSignal<void(void)>("EventListenerDeregistered", [this](void) {
152     RegisteredEventsUpdate();
153   });
154
155   return ForceUpResult::JUST_STARTED;
156 }
157
158 void BridgeBase::ForceDown()
159 {
160   Bridge::ForceDown();
161   registry   = {};
162   dbusServer = {};
163   con        = {};
164 }
165
166 const std::string& BridgeBase::GetBusName() const
167 {
168   static std::string empty;
169   return mData ? mData->mBusName : empty;
170 }
171
172 Accessible* BridgeBase::FindByPath(const std::string& name) const
173 {
174   try
175   {
176     return Find(name);
177   }
178   catch(std::domain_error&)
179   {
180     return nullptr;
181   }
182 }
183
184 void BridgeBase::AddPopup(Accessible* object)
185 {
186   if(std::find(popups.begin(), popups.end(), object) != popups.end())
187   {
188     return;
189   }
190   popups.push_back(object);
191   if(IsUp())
192   {
193     object->Emit(WindowEvent::ACTIVATE, 0);
194   }
195 }
196
197 void BridgeBase::RemovePopup(Accessible* object)
198 {
199   auto it = std::find(popups.begin(), popups.end(), object);
200   if(it == popups.end())
201   {
202     return;
203   }
204   popups.erase(it);
205   if(IsUp())
206   {
207     object->Emit(WindowEvent::DEACTIVATE, 0);
208     if(popups.empty())
209     {
210       application.children.back()->Emit(WindowEvent::ACTIVATE, 0);
211     }
212     else
213     {
214       popups.back()->Emit(WindowEvent::ACTIVATE, 0);
215     }
216   }
217 }
218
219 void BridgeBase::AddTopLevelWindow(Accessible* root)
220 {
221   application.children.push_back(root);
222   SetIsOnRootLevel(root);
223 }
224
225 void BridgeBase::RemoveTopLevelWindow(Accessible* root)
226 {
227   for(auto i = 0u; i < application.children.size(); ++i)
228   {
229     if(application.children[i] == root)
230     {
231       application.children.erase(application.children.begin() + i);
232       break;
233     }
234   }
235 }
236
237 std::string BridgeBase::StripPrefix(const std::string& path)
238 {
239   auto size = strlen(AtspiPath);
240   return path.substr(size + 1);
241 }
242
243 Accessible* BridgeBase::Find(const std::string& path) const
244 {
245   if(path == "root")
246   {
247     return &application;
248   }
249
250   void* accessible;
251   std::istringstream tmp{path};
252   if(!(tmp >> accessible))
253   {
254     throw std::domain_error{"invalid path '" + path + "'"};
255   }
256
257   auto it = mData->mKnownObjects.find(static_cast<Accessible*>(accessible));
258   if(it == mData->mKnownObjects.end())
259   {
260     throw std::domain_error{"unknown object '" + path + "'"};
261   }
262
263   return static_cast<Accessible*>(accessible);
264 }
265
266 Accessible* BridgeBase::Find(const Address& ptr) const
267 {
268   assert(ptr.GetBus() == mData->mBusName);
269   return Find(ptr.GetPath());
270 }
271
272 Accessible* BridgeBase::FindSelf() const
273 {
274   auto path  = DBus::DBusServer::getCurrentObjectPath();
275   auto size = strlen(AtspiPath);
276   if(path.size() <= size)
277   {
278     throw std::domain_error{"invalid path '" + path + "'"};
279   }
280   if(path.substr(0, size) != AtspiPath)
281   {
282     throw std::domain_error{"invalid path '" + path + "'"};
283   }
284   if(path[size] != '/')
285   {
286     throw std::domain_error{"invalid path '" + path + "'"};
287   }
288   return Find(StripPrefix(path));
289 }
290
291 void BridgeBase::IdSet(int id)
292 {
293   this->id = id;
294 }
295
296 int BridgeBase::IdGet()
297 {
298   return this->id;
299 }
300
301 auto BridgeBase::GetItems() -> DBus::ValueOrError<std::vector<CacheElementType>>
302 {
303   auto root = &application;
304
305   std::vector<CacheElementType> res;
306
307   std::function<void(Accessible*)> proc =
308     [&](Accessible* item) {
309       res.emplace_back(std::move(CreateCacheElement(root)));
310       for(auto i = 0u; i < item->GetChildCount(); ++i)
311       {
312         proc(item->GetChildAtIndex(i));
313       }
314     };
315
316   return res;
317 }
318
319 auto BridgeBase::CreateCacheElement(Accessible* item) -> CacheElementType
320 {
321   if(!item)
322   {
323     return {};
324   }
325
326   auto root   = &application;
327   auto parent = item->GetParent();
328
329   std::vector<Address> children;
330   for(auto i = 0u; i < item->GetChildCount(); ++i)
331   {
332     children.emplace_back(item->GetChildAtIndex(i)->GetAddress());
333   }
334
335   return std::make_tuple(
336     item->GetAddress(),
337     root->GetAddress(),
338     parent ? parent->GetAddress() : Address{},
339     children,
340     item->GetInterfaces(),
341     item->GetName(),
342     item->GetRole(),
343     item->GetDescription(),
344     item->GetStates().GetRawData());
345 }