Merge "Fix RenderTarget and RenderPass doesn't destroy issue" into devel/master
[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 <dali/devel-api/common/stage.h>
23 #include <atomic>
24 #include <cstdlib>
25 #include <memory>
26
27 // INTERNAL INCLUDES
28 #include <dali/public-api/adaptor-framework/timer.h>
29
30 using namespace Dali::Accessibility;
31
32 static Dali::Timer tickTimer;
33
34 BridgeBase::BridgeBase()
35 {
36 }
37
38 BridgeBase::~BridgeBase()
39 {
40   mApplication.mChildren.clear();
41   mApplication.mWindows.clear();
42 }
43
44 void BridgeBase::AddFilteredEvent(FilteredEvents kind, Dali::Accessibility::Accessible* obj, float delay, std::function<void()> functor)
45 {
46   if(delay < 0)
47   {
48     delay = 0;
49   }
50
51   auto it = mFilteredEvents.insert({{kind, obj}, {static_cast<unsigned int>(delay * 10), {}}});
52   if(it.second)
53   {
54     functor();
55   }
56   else
57   {
58     it.first->second.second = std::move(functor);
59   }
60
61   if(!tickTimer)
62   {
63     tickTimer = Dali::Timer::New(100);
64     tickTimer.TickSignal().Connect(this, &BridgeBase::TickFilteredEvents);
65   }
66 }
67
68 bool BridgeBase::TickFilteredEvents()
69 {
70   for(auto it = mFilteredEvents.begin(); it != mFilteredEvents.end();)
71   {
72     if(it->second.first)
73     {
74       --it->second.first;
75     }
76     else
77     {
78       if(it->second.second)
79       {
80         it->second.second();
81         it->second.second = {};
82       }
83       else
84       {
85         it = mFilteredEvents.erase(it);
86         continue;
87       }
88     }
89     ++it;
90   }
91   return !mFilteredEvents.empty();
92 }
93
94 void BridgeBase::UpdateRegisteredEvents()
95 {
96   using ReturnType = std::vector<std::tuple<std::string, std::string>>;
97   mRegistry.method<DBus::ValueOrError<ReturnType>()>("GetRegisteredEvents").asyncCall([this](DBus::ValueOrError<ReturnType> msg) {
98     if(!msg)
99     {
100       LOG() << "Get registered events failed";
101       return;
102     }
103
104     IsBoundsChangedEventAllowed = false;
105
106     ReturnType values = std::get<ReturnType>(msg.getValues());
107     for(long unsigned int i = 0; i < values.size(); i++)
108     {
109       if(!std::get<1>(values[i]).compare("Object:BoundsChanged"))
110       {
111         IsBoundsChangedEventAllowed = true;
112       }
113     }
114   });
115 }
116
117 BridgeBase::ForceUpResult BridgeBase::ForceUp()
118 {
119   if(Bridge::ForceUp() == ForceUpResult::ALREADY_UP)
120   {
121     return ForceUpResult::ALREADY_UP;
122   }
123   auto proxy = DBus::DBusClient{dbusLocators::atspi::BUS, dbusLocators::atspi::OBJ_PATH, dbusLocators::atspi::BUS_INTERFACE, DBus::ConnectionType::SESSION};
124   auto addr  = proxy.method<std::string()>(dbusLocators::atspi::GET_ADDRESS).call();
125
126   if(!addr)
127   {
128     throw std::domain_error{std::string("failed at call '") + dbusLocators::atspi::GET_ADDRESS + "': " + addr.getError().message};
129   }
130
131   mConnectionPtr  = DBusWrapper::Installed()->eldbus_address_connection_get_impl(std::get<0>(addr));
132   mData->mBusName = DBus::getConnectionName(mConnectionPtr);
133   mDbusServer     = {mConnectionPtr};
134
135   {
136     DBus::DBusInterfaceDescription desc{AtspiDbusInterfaceCache};
137     AddFunctionToInterface(desc, "GetItems", &BridgeBase::GetItems);
138     mDbusServer.addInterface(AtspiDbusPathCache, desc);
139   }
140   {
141     DBus::DBusInterfaceDescription desc{AtspiDbusInterfaceApplication};
142     AddGetSetPropertyToInterface(desc, "Id", &BridgeBase::GetId, &BridgeBase::SetId);
143     mDbusServer.addInterface(AtspiPath, desc);
144   }
145
146   mRegistry = {AtspiDbusNameRegistry, AtspiDbusPathRegistry, AtspiDbusInterfaceRegistry, mConnectionPtr};
147
148   UpdateRegisteredEvents();
149
150   mRegistry.addSignal<void(void)>("EventListenerRegistered", [this](void) {
151     UpdateRegisteredEvents();
152   });
153
154   mRegistry.addSignal<void(void)>("EventListenerDeregistered", [this](void) {
155     UpdateRegisteredEvents();
156   });
157
158   return ForceUpResult::JUST_STARTED;
159 }
160
161 void BridgeBase::ForceDown()
162 {
163   Bridge::ForceDown();
164   mRegistry      = {};
165   mDbusServer    = {};
166   mConnectionPtr = {};
167 }
168
169 const std::string& BridgeBase::GetBusName() const
170 {
171   static std::string empty;
172   return mData ? mData->mBusName : empty;
173 }
174
175 Accessible* BridgeBase::FindByPath(const std::string& name) const
176 {
177   try
178   {
179     return Find(name);
180   }
181   catch(std::domain_error&)
182   {
183     return nullptr;
184   }
185 }
186
187 void BridgeBase::OnWindowVisibilityChanged(Dali::Window window, bool visible)
188 {
189   if(visible)
190   {
191     // TODO : Should we check 'out of screen' here? -> Then, we need an actor of this change.
192     Dali::Accessibility::Bridge::GetCurrentBridge()->WindowShown(window); // Called when Window is shown.
193   }
194   else
195   {
196     Dali::Accessibility::Bridge::GetCurrentBridge()->WindowHidden(window); // Called when Window is hidden and iconified.
197   }
198 }
199
200 void BridgeBase::OnWindowFocusChanged(Dali::Window window, bool focusIn)
201 {
202   if(focusIn)
203   {
204     Dali::Accessibility::Bridge::GetCurrentBridge()->WindowFocused(window); // Called when Window is focused.
205   }
206   else
207   {
208     Dali::Accessibility::Bridge::GetCurrentBridge()->WindowUnfocused(window); // Called when Window is out of focus.
209   }
210 }
211
212 void BridgeBase::AddTopLevelWindow(Accessible* windowAccessible)
213 {
214   if(windowAccessible->GetInternalActor() == nullptr)
215   {
216     return;
217   }
218
219   // Prevent adding the default window twice.
220   if(!mApplication.mChildren.empty() &&
221      mApplication.mChildren[0]->GetInternalActor() == windowAccessible->GetInternalActor())
222   {
223     return;
224   }
225
226   // Adds Window to a list of Windows.
227   mApplication.mChildren.push_back(windowAccessible);
228   SetIsOnRootLevel(windowAccessible);
229
230   RegisterDefaultLabel(windowAccessible);
231
232   Dali::Window window = Dali::DevelWindow::Get(windowAccessible->GetInternalActor());
233   if(window)
234   {
235     mApplication.mWindows.push_back(window);
236     Dali::DevelWindow::VisibilityChangedSignal(window).Connect(this, &BridgeBase::OnWindowVisibilityChanged);
237     window.FocusChangeSignal().Connect(this, &BridgeBase::OnWindowFocusChanged);
238   }
239 }
240
241 void BridgeBase::RemoveTopLevelWindow(Accessible* windowAccessible)
242 {
243   for(auto i = 0u; i < mApplication.mWindows.size(); ++i)
244   {
245     if(windowAccessible->GetInternalActor() == mApplication.mWindows[i].GetRootLayer())
246     {
247       Dali::Accessibility::Bridge::GetCurrentBridge()->WindowHidden(mApplication.mWindows[i]);
248       Dali::DevelWindow::VisibilityChangedSignal(mApplication.mWindows[i]).Disconnect(this, &BridgeBase::OnWindowVisibilityChanged);
249       mApplication.mWindows[i].FocusChangeSignal().Disconnect(this, &BridgeBase::OnWindowFocusChanged);
250       mApplication.mWindows.erase(mApplication.mWindows.begin() + i);
251       break;
252     }
253   }
254
255   UnregisterDefaultLabel(windowAccessible);
256
257   for(auto i = 0u; i < mApplication.mChildren.size(); ++i)
258   {
259     if(mApplication.mChildren[i] == windowAccessible)
260     {
261       mApplication.mChildren.erase(mApplication.mChildren.begin() + i);
262       break;
263     }
264   }
265 }
266
267 void BridgeBase::RegisterDefaultLabel(Accessible* object)
268 {
269   if(std::find(mDefaultLabels.begin(), mDefaultLabels.end(), object) == mDefaultLabels.end())
270   {
271     mDefaultLabels.push_back(object);
272   }
273 }
274
275 void BridgeBase::UnregisterDefaultLabel(Accessible* object)
276 {
277   auto it = std::find(mDefaultLabels.begin(), mDefaultLabels.end(), object);
278   if(it != mDefaultLabels.end())
279   {
280     mDefaultLabels.erase(it);
281   }
282 }
283
284 std::string BridgeBase::StripPrefix(const std::string& path)
285 {
286   auto size = strlen(AtspiPath);
287   return path.substr(size + 1);
288 }
289
290 Accessible* BridgeBase::Find(const std::string& path) const
291 {
292   if(path == "root")
293   {
294     return &mApplication;
295   }
296
297   void*              accessible;
298   std::istringstream tmp{path};
299   if(!(tmp >> accessible))
300   {
301     throw std::domain_error{"invalid path '" + path + "'"};
302   }
303
304   auto it = mData->mKnownObjects.find(static_cast<Accessible*>(accessible));
305   if(it == mData->mKnownObjects.end())
306   {
307     throw std::domain_error{"unknown object '" + path + "'"};
308   }
309
310   return static_cast<Accessible*>(accessible);
311 }
312
313 Accessible* BridgeBase::Find(const Address& ptr) const
314 {
315   assert(ptr.GetBus() == mData->mBusName);
316   return Find(ptr.GetPath());
317 }
318
319 Accessible* BridgeBase::FindSelf() const
320 {
321   auto path = DBus::DBusServer::getCurrentObjectPath();
322   auto size = strlen(AtspiPath);
323   if(path.size() <= size)
324   {
325     throw std::domain_error{"invalid path '" + path + "'"};
326   }
327   if(path.substr(0, size) != AtspiPath)
328   {
329     throw std::domain_error{"invalid path '" + path + "'"};
330   }
331   if(path[size] != '/')
332   {
333     throw std::domain_error{"invalid path '" + path + "'"};
334   }
335   return Find(StripPrefix(path));
336 }
337
338 void BridgeBase::SetId(int id)
339 {
340   this->mId = id;
341 }
342
343 int BridgeBase::GetId()
344 {
345   return this->mId;
346 }
347
348 auto BridgeBase::GetItems() -> DBus::ValueOrError<std::vector<CacheElementType>>
349 {
350   auto root = &mApplication;
351
352   std::vector<CacheElementType> res;
353
354   std::function<void(Accessible*)> proc =
355     [&](Accessible* item) {
356       res.emplace_back(std::move(CreateCacheElement(root)));
357       for(auto i = 0u; i < item->GetChildCount(); ++i)
358       {
359         proc(item->GetChildAtIndex(i));
360       }
361     };
362
363   return res;
364 }
365
366 auto BridgeBase::CreateCacheElement(Accessible* item) -> CacheElementType
367 {
368   if(!item)
369   {
370     return {};
371   }
372
373   auto root   = &mApplication;
374   auto parent = item->GetParent();
375
376   std::vector<Address> children;
377   for(auto i = 0u; i < item->GetChildCount(); ++i)
378   {
379     children.emplace_back(item->GetChildAtIndex(i)->GetAddress());
380   }
381
382   return std::make_tuple(
383     item->GetAddress(),
384     root->GetAddress(),
385     parent ? parent->GetAddress() : Address{},
386     children,
387     item->GetInterfaces(),
388     item->GetName(),
389     item->GetRole(),
390     item->GetDescription(),
391     item->GetStates().GetRawData());
392 }