2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/internal/accessibility/bridge/bridge-base.h>
22 #include <dali/devel-api/common/stage.h>
28 #include <dali/public-api/adaptor-framework/timer.h>
30 using namespace Dali::Accessibility;
32 static Dali::Timer tickTimer;
34 BridgeBase::BridgeBase()
38 BridgeBase::~BridgeBase()
40 mApplication.mChildren.clear();
41 mApplication.mWindows.clear();
44 void BridgeBase::AddFilteredEvent(FilteredEvents kind, Dali::Accessibility::Accessible* obj, float delay, std::function<void()> functor)
51 auto it = mFilteredEvents.insert({{kind, obj}, {static_cast<unsigned int>(delay * 10), {}}});
58 it.first->second.second = std::move(functor);
63 tickTimer = Dali::Timer::New(100);
64 tickTimer.TickSignal().Connect(this, &BridgeBase::TickFilteredEvents);
68 bool BridgeBase::TickFilteredEvents()
70 for(auto it = mFilteredEvents.begin(); it != mFilteredEvents.end();)
81 it->second.second = {};
85 it = mFilteredEvents.erase(it);
91 return !mFilteredEvents.empty();
94 void BridgeBase::UpdateRegisteredEvents()
96 using ReturnType = std::vector<std::tuple<std::string, std::string>>;
97 mRegistry.method<DBus::ValueOrError<ReturnType>()>("GetRegisteredEvents").asyncCall([this](DBus::ValueOrError<ReturnType> msg) {
100 LOG() << "Get registered events failed";
104 IsBoundsChangedEventAllowed = false;
106 ReturnType values = std::get<ReturnType>(msg.getValues());
107 for(long unsigned int i = 0; i < values.size(); i++)
109 if(!std::get<1>(values[i]).compare("Object:BoundsChanged"))
111 IsBoundsChangedEventAllowed = true;
117 BridgeBase::ForceUpResult BridgeBase::ForceUp()
119 //TODO: checking mBusName is enough? or a new variable to check bridge state?
120 if(Bridge::ForceUp() == ForceUpResult::ALREADY_UP && !GetBusName().empty())
122 return ForceUpResult::ALREADY_UP;
124 auto proxy = DBus::DBusClient{dbusLocators::atspi::BUS, dbusLocators::atspi::OBJ_PATH, dbusLocators::atspi::BUS_INTERFACE, DBus::ConnectionType::SESSION};
125 auto addr = proxy.method<std::string()>(dbusLocators::atspi::GET_ADDRESS).call();
129 DALI_LOG_ERROR("failed at call '%s': %s\n", dbusLocators::atspi::GET_ADDRESS, addr.getError().message.c_str());
130 return ForceUpResult::FAILED;
133 mConnectionPtr = DBusWrapper::Installed()->eldbus_address_connection_get_impl(std::get<0>(addr));
134 mData->mBusName = DBus::getConnectionName(mConnectionPtr);
135 mDbusServer = {mConnectionPtr};
138 DBus::DBusInterfaceDescription desc{AtspiDbusInterfaceCache};
139 AddFunctionToInterface(desc, "GetItems", &BridgeBase::GetItems);
140 mDbusServer.addInterface(AtspiDbusPathCache, desc);
143 DBus::DBusInterfaceDescription desc{AtspiDbusInterfaceApplication};
144 AddGetSetPropertyToInterface(desc, "Id", &BridgeBase::GetId, &BridgeBase::SetId);
145 mDbusServer.addInterface(AtspiPath, desc);
148 mRegistry = {AtspiDbusNameRegistry, AtspiDbusPathRegistry, AtspiDbusInterfaceRegistry, mConnectionPtr};
150 UpdateRegisteredEvents();
152 mRegistry.addSignal<void(void)>("EventListenerRegistered", [this](void) {
153 UpdateRegisteredEvents();
156 mRegistry.addSignal<void(void)>("EventListenerDeregistered", [this](void) {
157 UpdateRegisteredEvents();
160 return ForceUpResult::JUST_STARTED;
163 void BridgeBase::ForceDown()
171 const std::string& BridgeBase::GetBusName() const
173 static std::string empty;
174 return mData ? mData->mBusName : empty;
177 Accessible* BridgeBase::FindByPath(const std::string& name) const
183 catch(std::domain_error&)
189 void BridgeBase::OnWindowVisibilityChanged(Dali::Window window, bool visible)
193 // TODO : Should we check 'out of screen' here? -> Then, we need an actor of this change.
194 Dali::Accessibility::Bridge::GetCurrentBridge()->WindowShown(window); // Called when Window is shown.
198 Dali::Accessibility::Bridge::GetCurrentBridge()->WindowHidden(window); // Called when Window is hidden and iconified.
202 void BridgeBase::OnWindowFocusChanged(Dali::Window window, bool focusIn)
206 Dali::Accessibility::Bridge::GetCurrentBridge()->WindowFocused(window); // Called when Window is focused.
210 Dali::Accessibility::Bridge::GetCurrentBridge()->WindowUnfocused(window); // Called when Window is out of focus.
214 void BridgeBase::AddTopLevelWindow(Accessible* windowAccessible)
216 if(windowAccessible->GetInternalActor() == nullptr)
221 // Prevent adding the default window twice.
222 if(!mApplication.mChildren.empty() &&
223 mApplication.mChildren[0]->GetInternalActor() == windowAccessible->GetInternalActor())
228 // Adds Window to a list of Windows.
229 mApplication.mChildren.push_back(windowAccessible);
230 SetIsOnRootLevel(windowAccessible);
232 RegisterDefaultLabel(windowAccessible);
234 Dali::Window window = Dali::DevelWindow::Get(windowAccessible->GetInternalActor());
237 mApplication.mWindows.push_back(window);
238 Dali::DevelWindow::VisibilityChangedSignal(window).Connect(this, &BridgeBase::OnWindowVisibilityChanged);
239 window.FocusChangeSignal().Connect(this, &BridgeBase::OnWindowFocusChanged);
243 void BridgeBase::RemoveTopLevelWindow(Accessible* windowAccessible)
245 for(auto i = 0u; i < mApplication.mWindows.size(); ++i)
247 if(windowAccessible->GetInternalActor() == mApplication.mWindows[i].GetRootLayer())
249 Dali::DevelWindow::VisibilityChangedSignal(mApplication.mWindows[i]).Disconnect(this, &BridgeBase::OnWindowVisibilityChanged);
250 mApplication.mWindows[i].FocusChangeSignal().Disconnect(this, &BridgeBase::OnWindowFocusChanged);
251 mApplication.mWindows.erase(mApplication.mWindows.begin() + i);
256 UnregisterDefaultLabel(windowAccessible);
258 for(auto i = 0u; i < mApplication.mChildren.size(); ++i)
260 if(mApplication.mChildren[i] == windowAccessible)
262 mApplication.mChildren.erase(mApplication.mChildren.begin() + i);
268 void BridgeBase::RegisterDefaultLabel(Accessible* object)
270 if(std::find(mDefaultLabels.begin(), mDefaultLabels.end(), object) == mDefaultLabels.end())
272 mDefaultLabels.push_back(object);
276 void BridgeBase::UnregisterDefaultLabel(Accessible* object)
278 auto it = std::find(mDefaultLabels.begin(), mDefaultLabels.end(), object);
279 if(it != mDefaultLabels.end())
281 mDefaultLabels.erase(it);
285 std::string BridgeBase::StripPrefix(const std::string& path)
287 auto size = strlen(AtspiPath);
288 return path.substr(size + 1);
291 Accessible* BridgeBase::Find(const std::string& path) const
295 return &mApplication;
299 std::istringstream tmp{path};
300 if(!(tmp >> accessible))
302 throw std::domain_error{"invalid path '" + path + "'"};
305 auto it = mData->mKnownObjects.find(static_cast<Accessible*>(accessible));
306 if(it == mData->mKnownObjects.end() || (*it)->IsHidden())
308 throw std::domain_error{"unknown object '" + path + "'"};
311 return static_cast<Accessible*>(accessible);
314 Accessible* BridgeBase::Find(const Address& ptr) const
316 assert(ptr.GetBus() == mData->mBusName);
317 return Find(ptr.GetPath());
320 Accessible* BridgeBase::FindSelf() const
322 auto path = DBus::DBusServer::getCurrentObjectPath();
323 auto size = strlen(AtspiPath);
324 if(path.size() <= size)
326 throw std::domain_error{"invalid path '" + path + "'"};
328 if(path.substr(0, size) != AtspiPath)
330 throw std::domain_error{"invalid path '" + path + "'"};
332 if(path[size] != '/')
334 throw std::domain_error{"invalid path '" + path + "'"};
336 return Find(StripPrefix(path));
339 void BridgeBase::SetId(int id)
344 int BridgeBase::GetId()
349 auto BridgeBase::GetItems() -> DBus::ValueOrError<std::vector<CacheElementType>>
351 auto root = &mApplication;
353 std::vector<CacheElementType> res;
355 std::function<void(Accessible*)> proc =
356 [&](Accessible* item) {
357 res.emplace_back(std::move(CreateCacheElement(root)));
358 for(auto i = 0u; i < item->GetChildCount(); ++i)
360 proc(item->GetChildAtIndex(i));
367 auto BridgeBase::CreateCacheElement(Accessible* item) -> CacheElementType
374 auto root = &mApplication;
375 auto parent = item->GetParent();
377 std::vector<Address> children;
378 for(auto i = 0u; i < item->GetChildCount(); ++i)
380 children.emplace_back(item->GetChildAtIndex(i)->GetAddress());
383 return std::make_tuple(
386 parent ? parent->GetAddress() : Address{},
388 item->GetInterfaces(),
391 item->GetDescription(),
392 item->GetStates().GetRawData());