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();
43 void BridgeBase::AddFilteredEvent(FilteredEvents kind, Dali::Accessibility::Accessible* obj, float delay, std::function<void()> functor)
50 auto it = mFilteredEvents.insert({{kind, obj}, {static_cast<unsigned int>(delay * 10), {}}});
57 it.first->second.second = std::move(functor);
62 tickTimer = Dali::Timer::New(100);
63 tickTimer.TickSignal().Connect(this, &BridgeBase::TickFilteredEvents);
67 bool BridgeBase::TickFilteredEvents()
69 for(auto it = mFilteredEvents.begin(); it != mFilteredEvents.end();)
80 it->second.second = {};
84 it = mFilteredEvents.erase(it);
90 return !mFilteredEvents.empty();
93 void BridgeBase::UpdateRegisteredEvents()
95 using ReturnType = std::vector<std::tuple<std::string, std::string>>;
96 mRegistry.method<DBus::ValueOrError<ReturnType>()>("GetRegisteredEvents").asyncCall([this](DBus::ValueOrError<ReturnType> msg) {
99 LOG() << "Get registered events failed";
103 IsBoundsChangedEventAllowed = false;
105 ReturnType values = std::get<ReturnType>(msg.getValues());
106 for(long unsigned int i = 0; i < values.size(); i++)
108 if(!std::get<1>(values[i]).compare("Object:BoundsChanged"))
110 IsBoundsChangedEventAllowed = true;
116 BridgeBase::ForceUpResult BridgeBase::ForceUp()
118 //TODO: checking mBusName is enough? or a new variable to check bridge state?
119 if(Bridge::ForceUp() == ForceUpResult::ALREADY_UP && !GetBusName().empty())
121 return ForceUpResult::ALREADY_UP;
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();
128 DALI_LOG_ERROR("failed at call '%s': %s\n", dbusLocators::atspi::GET_ADDRESS, addr.getError().message.c_str());
129 return ForceUpResult::FAILED;
132 mConnectionPtr = DBusWrapper::Installed()->eldbus_address_connection_get_impl(std::get<0>(addr));
133 mData->mBusName = DBus::getConnectionName(mConnectionPtr);
134 mDbusServer = {mConnectionPtr};
137 DBus::DBusInterfaceDescription desc{Accessible::GetInterfaceName(AtspiInterface::CACHE)};
138 AddFunctionToInterface(desc, "GetItems", &BridgeBase::GetItems);
139 mDbusServer.addInterface(AtspiDbusPathCache, desc);
142 DBus::DBusInterfaceDescription desc{Accessible::GetInterfaceName(AtspiInterface::APPLICATION)};
143 AddGetSetPropertyToInterface(desc, "Id", &BridgeBase::GetId, &BridgeBase::SetId);
144 mDbusServer.addInterface(AtspiPath, desc);
147 mRegistry = {AtspiDbusNameRegistry, AtspiDbusPathRegistry, Accessible::GetInterfaceName(AtspiInterface::REGISTRY), mConnectionPtr};
149 UpdateRegisteredEvents();
151 mRegistry.addSignal<void(void)>("EventListenerRegistered", [this](void) {
152 UpdateRegisteredEvents();
155 mRegistry.addSignal<void(void)>("EventListenerDeregistered", [this](void) {
156 UpdateRegisteredEvents();
159 return ForceUpResult::JUST_STARTED;
162 void BridgeBase::ForceDown()
170 const std::string& BridgeBase::GetBusName() const
172 static std::string empty;
173 return mData ? mData->mBusName : empty;
176 Accessible* BridgeBase::FindByPath(const std::string& name) const
182 catch(std::domain_error&)
188 void BridgeBase::AddTopLevelWindow(Accessible* windowAccessible)
190 if(windowAccessible->GetInternalActor() == nullptr)
195 // Prevent adding the default window twice.
196 if(!mApplication.mChildren.empty() &&
197 mApplication.mChildren[0]->GetInternalActor() == windowAccessible->GetInternalActor())
202 // Adds Window to a list of Windows.
203 mApplication.mChildren.push_back(windowAccessible);
204 SetIsOnRootLevel(windowAccessible);
206 RegisterDefaultLabel(windowAccessible);
209 void BridgeBase::RemoveTopLevelWindow(Accessible* windowAccessible)
211 UnregisterDefaultLabel(windowAccessible);
213 for(auto i = 0u; i < mApplication.mChildren.size(); ++i)
215 if(mApplication.mChildren[i] == windowAccessible)
217 mApplication.mChildren.erase(mApplication.mChildren.begin() + i);
223 void BridgeBase::RegisterDefaultLabel(Accessible* object)
225 if(std::find(mDefaultLabels.begin(), mDefaultLabels.end(), object) == mDefaultLabels.end())
227 mDefaultLabels.push_back(object);
231 void BridgeBase::UnregisterDefaultLabel(Accessible* object)
233 auto it = std::find(mDefaultLabels.begin(), mDefaultLabels.end(), object);
234 if(it != mDefaultLabels.end())
236 mDefaultLabels.erase(it);
240 std::string BridgeBase::StripPrefix(const std::string& path)
242 auto size = strlen(AtspiPath);
243 return path.substr(size + 1);
246 Accessible* BridgeBase::Find(const std::string& path) const
250 return &mApplication;
254 std::istringstream tmp{path};
255 if(!(tmp >> accessible))
257 throw std::domain_error{"invalid path '" + path + "'"};
260 auto it = mData->mKnownObjects.find(static_cast<Accessible*>(accessible));
261 if(it == mData->mKnownObjects.end() || (*it)->IsHidden())
263 throw std::domain_error{"unknown object '" + path + "'"};
266 return static_cast<Accessible*>(accessible);
269 Accessible* BridgeBase::Find(const Address& ptr) const
271 assert(ptr.GetBus() == mData->mBusName);
272 return Find(ptr.GetPath());
275 Accessible* BridgeBase::FindCurrentObject() const
277 auto path = DBus::DBusServer::getCurrentObjectPath();
278 auto size = strlen(AtspiPath);
279 if(path.size() <= size)
281 throw std::domain_error{"invalid path '" + path + "'"};
283 if(path.substr(0, size) != AtspiPath)
285 throw std::domain_error{"invalid path '" + path + "'"};
287 if(path[size] != '/')
289 throw std::domain_error{"invalid path '" + path + "'"};
291 return Find(StripPrefix(path));
294 void BridgeBase::SetId(int id)
299 int BridgeBase::GetId()
304 auto BridgeBase::GetItems() -> DBus::ValueOrError<std::vector<CacheElementType>>
306 auto root = &mApplication;
308 std::vector<CacheElementType> res;
310 std::function<void(Accessible*)> proc =
311 [&](Accessible* item) {
312 res.emplace_back(std::move(CreateCacheElement(root)));
313 for(auto i = 0u; i < item->GetChildCount(); ++i)
315 proc(item->GetChildAtIndex(i));
322 auto BridgeBase::CreateCacheElement(Accessible* item) -> CacheElementType
329 auto root = &mApplication;
330 auto parent = item->GetParent();
332 std::vector<Address> children;
333 for(auto i = 0u; i < item->GetChildCount(); ++i)
335 children.emplace_back(item->GetChildAtIndex(i)->GetAddress());
338 return std::make_tuple(
341 parent ? parent->GetAddress() : Address{},
343 item->GetInterfacesAsStrings(),
346 item->GetDescription(),
347 item->GetStates().GetRawData());