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>
27 #include <dali/public-api/adaptor-framework/timer.h>
29 using namespace Dali::Accessibility;
31 static Dali::Timer tickTimer;
33 BridgeBase::~BridgeBase()
37 BridgeBase::BridgeBase()
41 void BridgeBase::AddFilteredEvent(FilteredEvents kind, Dali::Accessibility::Accessible* obj, float delay, std::function<void()> functor)
48 auto it = mFilteredEvents.insert({{kind, obj}, {static_cast<unsigned int>(delay * 10), {}}});
55 it.first->second.second = std::move(functor);
60 tickTimer = Dali::Timer::New(100);
61 tickTimer.TickSignal().Connect(this, &BridgeBase::TickFilteredEvents);
65 bool BridgeBase::TickFilteredEvents()
67 for(auto it = mFilteredEvents.begin(); it != mFilteredEvents.end();)
78 it->second.second = {};
82 it = mFilteredEvents.erase(it);
88 return !mFilteredEvents.empty();
91 void BridgeBase::UpdateRegisteredEvents()
93 using ReturnType = std::vector<std::tuple<std::string, std::string>>;
94 mRegistry.method<DBus::ValueOrError<ReturnType>()>("GetRegisteredEvents").asyncCall([this](DBus::ValueOrError<ReturnType> msg) {
97 LOG() << "Get registered events failed";
101 IsBoundsChangedEventAllowed = false;
103 ReturnType values = std::get<ReturnType>(msg.getValues());
104 for(long unsigned int i = 0; i < values.size(); i++)
106 if(!std::get<1>(values[i]).compare("Object:BoundsChanged"))
108 IsBoundsChangedEventAllowed = true;
114 BridgeBase::ForceUpResult BridgeBase::ForceUp()
116 if(Bridge::ForceUp() == ForceUpResult::ALREADY_UP)
118 return ForceUpResult::ALREADY_UP;
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();
125 throw std::domain_error{std::string("failed at call '") + dbusLocators::atspi::GET_ADDRESS + "': " + addr.getError().message};
128 mConnectionPtr = DBusWrapper::Installed()->eldbus_address_connection_get_impl(std::get<0>(addr));
129 mData->mBusName = DBus::getConnectionName(mConnectionPtr);
130 mDbusServer = {mConnectionPtr};
133 DBus::DBusInterfaceDescription desc{AtspiDbusInterfaceCache};
134 AddFunctionToInterface(desc, "GetItems", &BridgeBase::GetItems);
135 mDbusServer.addInterface(AtspiDbusPathCache, desc);
138 DBus::DBusInterfaceDescription desc{AtspiDbusInterfaceApplication};
139 AddGetSetPropertyToInterface(desc, "Id", &BridgeBase::GetId, &BridgeBase::SetId);
140 mDbusServer.addInterface(AtspiPath, desc);
143 mRegistry = {AtspiDbusNameRegistry, AtspiDbusPathRegistry, AtspiDbusInterfaceRegistry, mConnectionPtr};
145 UpdateRegisteredEvents();
147 mRegistry.addSignal<void(void)>("EventListenerRegistered", [this](void) {
148 UpdateRegisteredEvents();
151 mRegistry.addSignal<void(void)>("EventListenerDeregistered", [this](void) {
152 UpdateRegisteredEvents();
155 return ForceUpResult::JUST_STARTED;
158 void BridgeBase::ForceDown()
166 const std::string& BridgeBase::GetBusName() const
168 static std::string empty;
169 return mData ? mData->mBusName : empty;
172 Accessible* BridgeBase::FindByPath(const std::string& name) const
178 catch(std::domain_error&)
184 void BridgeBase::AddPopup(Accessible* object)
186 if(std::find(mPopups.begin(), mPopups.end(), object) != mPopups.end())
190 mPopups.push_back(object);
193 object->Emit(WindowEvent::ACTIVATE, 0);
197 void BridgeBase::RemovePopup(Accessible* object)
199 auto it = std::find(mPopups.begin(), mPopups.end(), object);
200 if(it == mPopups.end())
208 object->Emit(WindowEvent::DEACTIVATE, 0);
211 mApplication.mChildren.back()->Emit(WindowEvent::ACTIVATE, 0);
215 mPopups.back()->Emit(WindowEvent::ACTIVATE, 0);
220 void BridgeBase::AddTopLevelWindow(Accessible* root)
222 mApplication.mChildren.push_back(root);
223 SetIsOnRootLevel(root);
226 void BridgeBase::RemoveTopLevelWindow(Accessible* root)
228 for(auto i = 0u; i < mApplication.mChildren.size(); ++i)
230 if(mApplication.mChildren[i] == root)
232 mApplication.mChildren.erase(mApplication.mChildren.begin() + i);
238 std::string BridgeBase::StripPrefix(const std::string& path)
240 auto size = strlen(AtspiPath);
241 return path.substr(size + 1);
244 Accessible* BridgeBase::Find(const std::string& path) const
248 return &mApplication;
252 std::istringstream tmp{path};
253 if(!(tmp >> accessible))
255 throw std::domain_error{"invalid path '" + path + "'"};
258 auto it = mData->mKnownObjects.find(static_cast<Accessible*>(accessible));
259 if(it == mData->mKnownObjects.end())
261 throw std::domain_error{"unknown object '" + path + "'"};
264 return static_cast<Accessible*>(accessible);
267 Accessible* BridgeBase::Find(const Address& ptr) const
269 assert(ptr.GetBus() == mData->mBusName);
270 return Find(ptr.GetPath());
273 Accessible* BridgeBase::FindSelf() const
275 auto path = DBus::DBusServer::getCurrentObjectPath();
276 auto size = strlen(AtspiPath);
277 if(path.size() <= size)
279 throw std::domain_error{"invalid path '" + path + "'"};
281 if(path.substr(0, size) != AtspiPath)
283 throw std::domain_error{"invalid path '" + path + "'"};
285 if(path[size] != '/')
287 throw std::domain_error{"invalid path '" + path + "'"};
289 return Find(StripPrefix(path));
292 void BridgeBase::SetId(int id)
297 int BridgeBase::GetId()
302 auto BridgeBase::GetItems() -> DBus::ValueOrError<std::vector<CacheElementType>>
304 auto root = &mApplication;
306 std::vector<CacheElementType> res;
308 std::function<void(Accessible*)> proc =
309 [&](Accessible* item) {
310 res.emplace_back(std::move(CreateCacheElement(root)));
311 for(auto i = 0u; i < item->GetChildCount(); ++i)
313 proc(item->GetChildAtIndex(i));
320 auto BridgeBase::CreateCacheElement(Accessible* item) -> CacheElementType
327 auto root = &mApplication;
328 auto parent = item->GetParent();
330 std::vector<Address> children;
331 for(auto i = 0u; i < item->GetChildCount(); ++i)
333 children.emplace_back(item->GetChildAtIndex(i)->GetAddress());
336 return std::make_tuple(
339 parent ? parent->GetAddress() : Address{},
341 item->GetInterfaces(),
344 item->GetDescription(),
345 item->GetStates().GetRawData());