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/devel-api/adaptor-framework/window-devel.h>
29 #include <dali/public-api/adaptor-framework/timer.h>
31 using namespace Dali::Accessibility;
33 static Dali::Timer tickTimer;
35 BridgeBase::BridgeBase()
39 BridgeBase::~BridgeBase()
41 mApplication.mChildren.clear();
44 void BridgeBase::AddCoalescableMessage(CoalescableMessages kind, Dali::Accessibility::Accessible* obj, float delay, std::function<void()> functor)
50 auto countdownBase = static_cast<unsigned int>(delay * 10);
52 auto it = mCoalescableMessages.insert({{kind, obj}, {countdownBase, countdownBase, {}}});
59 std::get<1>(it.first->second) = countdownBase;
60 std::get<2>(it.first->second) = std::move(functor);
65 tickTimer = Dali::Timer::New(100);
66 tickTimer.TickSignal().Connect(this, &BridgeBase::TickCoalescableMessages);
69 if(!tickTimer.IsRunning())
75 bool BridgeBase::TickCoalescableMessages()
77 for(auto it = mCoalescableMessages.begin(); it != mCoalescableMessages.end();)
79 auto& countdown = std::get<0>(it->second);
80 auto countdownBase = std::get<1>(it->second);
81 auto& functor = std::get<2>(it->second);
92 countdown = countdownBase;
96 it = mCoalescableMessages.erase(it);
102 return !mCoalescableMessages.empty();
105 void BridgeBase::UpdateRegisteredEvents()
107 using ReturnType = std::vector<std::tuple<std::string, std::string>>;
108 mRegistry.method<DBus::ValueOrError<ReturnType>()>("GetRegisteredEvents").asyncCall([this](DBus::ValueOrError<ReturnType> msg) {
111 LOG() << "Get registered events failed";
115 IsBoundsChangedEventAllowed = false;
117 ReturnType values = std::get<ReturnType>(msg.getValues());
118 for(long unsigned int i = 0; i < values.size(); i++)
120 if(!std::get<1>(values[i]).compare("Object:BoundsChanged"))
122 IsBoundsChangedEventAllowed = true;
128 BridgeBase::ForceUpResult BridgeBase::ForceUp()
130 //TODO: checking mBusName is enough? or a new variable to check bridge state?
131 if(Bridge::ForceUp() == ForceUpResult::ALREADY_UP && !GetBusName().empty())
133 return ForceUpResult::ALREADY_UP;
135 auto proxy = DBus::DBusClient{dbusLocators::atspi::BUS, dbusLocators::atspi::OBJ_PATH, dbusLocators::atspi::BUS_INTERFACE, DBus::ConnectionType::SESSION};
136 auto addr = proxy.method<std::string()>(dbusLocators::atspi::GET_ADDRESS).call();
140 DALI_LOG_ERROR("failed at call '%s': %s\n", dbusLocators::atspi::GET_ADDRESS, addr.getError().message.c_str());
141 return ForceUpResult::FAILED;
144 mConnectionPtr = DBusWrapper::Installed()->eldbus_address_connection_get_impl(std::get<0>(addr));
145 mData->mBusName = DBus::getConnectionName(mConnectionPtr);
146 mDbusServer = {mConnectionPtr};
149 DBus::DBusInterfaceDescription desc{Accessible::GetInterfaceName(AtspiInterface::CACHE)};
150 AddFunctionToInterface(desc, "GetItems", &BridgeBase::GetItems);
151 mDbusServer.addInterface(AtspiDbusPathCache, desc);
154 DBus::DBusInterfaceDescription desc{Accessible::GetInterfaceName(AtspiInterface::APPLICATION)};
155 AddGetSetPropertyToInterface(desc, "Id", &BridgeBase::GetId, &BridgeBase::SetId);
156 mDbusServer.addInterface(AtspiPath, desc);
159 mRegistry = {AtspiDbusNameRegistry, AtspiDbusPathRegistry, Accessible::GetInterfaceName(AtspiInterface::REGISTRY), mConnectionPtr};
161 UpdateRegisteredEvents();
163 mRegistry.addSignal<void(void)>("EventListenerRegistered", [this](void) {
164 UpdateRegisteredEvents();
167 mRegistry.addSignal<void(void)>("EventListenerDeregistered", [this](void) {
168 UpdateRegisteredEvents();
171 return ForceUpResult::JUST_STARTED;
174 void BridgeBase::ForceDown()
182 const std::string& BridgeBase::GetBusName() const
184 static std::string empty;
185 return mData ? mData->mBusName : empty;
188 Accessible* BridgeBase::FindByPath(const std::string& name) const
194 catch(std::domain_error&)
200 void BridgeBase::AddTopLevelWindow(Accessible* windowAccessible)
202 if(windowAccessible->GetInternalActor() == nullptr)
207 // Prevent adding the default window twice.
208 if(!mApplication.mChildren.empty() &&
209 mApplication.mChildren[0]->GetInternalActor() == windowAccessible->GetInternalActor())
214 // Adds Window to a list of Windows.
215 mApplication.mChildren.push_back(windowAccessible);
216 SetIsOnRootLevel(windowAccessible);
219 void BridgeBase::RemoveTopLevelWindow(Accessible* windowAccessible)
221 for(auto i = 0u; i < mApplication.mChildren.size(); ++i)
223 if(mApplication.mChildren[i] == windowAccessible)
225 mApplication.mChildren.erase(mApplication.mChildren.begin() + i);
231 void BridgeBase::CompressDefaultLabels()
233 // Remove entries for objects which no longer exist
234 mDefaultLabels.remove_if([](const DefaultLabelType& label) {
235 return !label.first.GetBaseHandle(); // Check window's weak handle
236 // TODO: Once Accessible becomes a handle type, check its weak handle here as well
240 void BridgeBase::RegisterDefaultLabel(Accessible* object)
242 CompressDefaultLabels();
244 Dali::WeakHandle<Dali::Window> window = GetWindow(object);
245 if(!window.GetBaseHandle()) // true also if `object` is null
247 DALI_LOG_ERROR("Cannot register default label: object does not belong to any window");
251 auto it = std::find_if(mDefaultLabels.begin(), mDefaultLabels.end(), [object](const DefaultLabelType& label) {
252 return object == label.second;
255 if(it == mDefaultLabels.end())
257 mDefaultLabels.push_back({window, object});
259 else if(it->first != window)
261 // TODO: Tentative implementation. It is yet to be specified what should happen
262 // when the same object is re-registered as a default label for another window.
263 *it = {window, object};
265 else // it->first == window && it->second == object
271 void BridgeBase::UnregisterDefaultLabel(Accessible* object)
273 CompressDefaultLabels();
275 mDefaultLabels.remove_if([object](const DefaultLabelType& label) {
276 return object == label.second;
280 Accessible* BridgeBase::GetDefaultLabel(Accessible* root) const
282 Dali::WeakHandle<Dali::Window> window = GetWindow(root);
283 if(!window.GetBaseHandle())
288 auto it = std::find_if(mDefaultLabels.rbegin(), mDefaultLabels.rend(), [&window](const DefaultLabelType& label) {
289 return window == label.first;
292 return (it == mDefaultLabels.rend()) ? root : it->second;
295 std::string BridgeBase::StripPrefix(const std::string& path)
297 auto size = strlen(AtspiPath);
298 return path.substr(size + 1);
301 Accessible* BridgeBase::Find(const std::string& path) const
305 return &mApplication;
309 std::istringstream tmp{path};
310 if(!(tmp >> accessible))
312 throw std::domain_error{"invalid path '" + path + "'"};
315 auto it = mData->mKnownObjects.find(static_cast<Accessible*>(accessible));
316 if(it == mData->mKnownObjects.end() || (*it)->IsHidden())
318 throw std::domain_error{"unknown object '" + path + "'"};
321 return static_cast<Accessible*>(accessible);
324 Accessible* BridgeBase::Find(const Address& ptr) const
326 assert(ptr.GetBus() == mData->mBusName);
327 return Find(ptr.GetPath());
330 Accessible* BridgeBase::FindCurrentObject() const
332 auto path = DBus::DBusServer::getCurrentObjectPath();
333 auto size = strlen(AtspiPath);
334 if(path.size() <= size)
336 throw std::domain_error{"invalid path '" + path + "'"};
338 if(path.substr(0, size) != AtspiPath)
340 throw std::domain_error{"invalid path '" + path + "'"};
342 if(path[size] != '/')
344 throw std::domain_error{"invalid path '" + path + "'"};
346 return Find(StripPrefix(path));
349 void BridgeBase::SetId(int id)
354 int BridgeBase::GetId()
359 auto BridgeBase::GetItems() -> DBus::ValueOrError<std::vector<CacheElementType>>
361 auto root = &mApplication;
363 std::vector<CacheElementType> res;
365 std::function<void(Accessible*)> proc =
366 [&](Accessible* item) {
367 res.emplace_back(std::move(CreateCacheElement(root)));
368 for(auto i = 0u; i < item->GetChildCount(); ++i)
370 proc(item->GetChildAtIndex(i));
377 auto BridgeBase::CreateCacheElement(Accessible* item) -> CacheElementType
384 auto root = &mApplication;
385 auto parent = item->GetParent();
387 std::vector<Address> children;
388 for(auto i = 0u; i < item->GetChildCount(); ++i)
390 children.emplace_back(item->GetChildAtIndex(i)->GetAddress());
393 return std::make_tuple(
396 parent ? parent->GetAddress() : Address{},
398 item->GetInterfacesAsStrings(),
401 item->GetDescription(),
402 item->GetStates().GetRawData());
405 Dali::WeakHandle<Dali::Window> BridgeBase::GetWindow(Dali::Accessibility::Accessible* accessible)
407 Dali::WeakHandle<Dali::Window> windowHandle;
408 Dali::Actor actor = accessible ? accessible->GetInternalActor() : Dali::Actor();
412 Dali::Window window = Dali::DevelWindow::Get(actor);
413 windowHandle = {window};