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 if(Bridge::ForceUp() == ForceUpResult::ALREADY_UP)
120 return ForceUpResult::ALREADY_UP;
122 auto proxy = DBus::DBusClient{dbusLocators::atspi::BUS, dbusLocators::atspi::OBJ_PATH, dbusLocators::atspi::BUS_INTERFACE, DBus::ConnectionType::SESSION};
123 auto addr = proxy.method<std::string()>(dbusLocators::atspi::GET_ADDRESS).call();
127 throw std::domain_error{std::string("failed at call '") + dbusLocators::atspi::GET_ADDRESS + "': " + addr.getError().message};
130 mConnectionPtr = DBusWrapper::Installed()->eldbus_address_connection_get_impl(std::get<0>(addr));
131 mData->mBusName = DBus::getConnectionName(mConnectionPtr);
132 mDbusServer = {mConnectionPtr};
135 DBus::DBusInterfaceDescription desc{AtspiDbusInterfaceCache};
136 AddFunctionToInterface(desc, "GetItems", &BridgeBase::GetItems);
137 mDbusServer.addInterface(AtspiDbusPathCache, desc);
140 DBus::DBusInterfaceDescription desc{AtspiDbusInterfaceApplication};
141 AddGetSetPropertyToInterface(desc, "Id", &BridgeBase::GetId, &BridgeBase::SetId);
142 mDbusServer.addInterface(AtspiPath, desc);
145 mRegistry = {AtspiDbusNameRegistry, AtspiDbusPathRegistry, AtspiDbusInterfaceRegistry, mConnectionPtr};
147 UpdateRegisteredEvents();
149 mRegistry.addSignal<void(void)>("EventListenerRegistered", [this](void) {
150 UpdateRegisteredEvents();
153 mRegistry.addSignal<void(void)>("EventListenerDeregistered", [this](void) {
154 UpdateRegisteredEvents();
157 return ForceUpResult::JUST_STARTED;
160 void BridgeBase::ForceDown()
168 const std::string& BridgeBase::GetBusName() const
170 static std::string empty;
171 return mData ? mData->mBusName : empty;
174 Accessible* BridgeBase::FindByPath(const std::string& name) const
180 catch(std::domain_error&)
186 void BridgeBase::AddPopup(Accessible* object)
188 if(std::find(mPopups.begin(), mPopups.end(), object) != mPopups.end())
192 mPopups.push_back(object);
195 object->Emit(WindowEvent::ACTIVATE, 0);
199 void BridgeBase::RemovePopup(Accessible* object)
201 auto it = std::find(mPopups.begin(), mPopups.end(), object);
202 if(it == mPopups.end())
210 object->Emit(WindowEvent::DEACTIVATE, 0);
213 mApplication.mChildren.back()->Emit(WindowEvent::ACTIVATE, 0);
217 mPopups.back()->Emit(WindowEvent::ACTIVATE, 0);
222 void BridgeBase::AddTopLevelWindow(Accessible* windowAccessible)
224 if(windowAccessible->GetInternalActor() == nullptr)
229 // Prevent adding the default window twice.
230 if(!mApplication.mChildren.empty() &&
231 mApplication.mChildren[0]->GetInternalActor() == windowAccessible->GetInternalActor())
236 // Adds Window to a list of Windows.
237 mApplication.mChildren.push_back(windowAccessible);
238 SetIsOnRootLevel(windowAccessible);
241 void BridgeBase::RemoveTopLevelWindow(Accessible* windowAccessible)
243 for(auto i = 0u; i < mApplication.mChildren.size(); ++i)
245 if(mApplication.mChildren[i] == windowAccessible)
247 mApplication.mChildren.erase(mApplication.mChildren.begin() + i);
253 std::string BridgeBase::StripPrefix(const std::string& path)
255 auto size = strlen(AtspiPath);
256 return path.substr(size + 1);
259 Accessible* BridgeBase::Find(const std::string& path) const
263 return &mApplication;
267 std::istringstream tmp{path};
268 if(!(tmp >> accessible))
270 throw std::domain_error{"invalid path '" + path + "'"};
273 auto it = mData->mKnownObjects.find(static_cast<Accessible*>(accessible));
274 if(it == mData->mKnownObjects.end())
276 throw std::domain_error{"unknown object '" + path + "'"};
279 return static_cast<Accessible*>(accessible);
282 Accessible* BridgeBase::Find(const Address& ptr) const
284 assert(ptr.GetBus() == mData->mBusName);
285 return Find(ptr.GetPath());
288 Accessible* BridgeBase::FindSelf() const
290 auto path = DBus::DBusServer::getCurrentObjectPath();
291 auto size = strlen(AtspiPath);
292 if(path.size() <= size)
294 throw std::domain_error{"invalid path '" + path + "'"};
296 if(path.substr(0, size) != AtspiPath)
298 throw std::domain_error{"invalid path '" + path + "'"};
300 if(path[size] != '/')
302 throw std::domain_error{"invalid path '" + path + "'"};
304 return Find(StripPrefix(path));
307 void BridgeBase::SetId(int id)
312 int BridgeBase::GetId()
317 auto BridgeBase::GetItems() -> DBus::ValueOrError<std::vector<CacheElementType>>
319 auto root = &mApplication;
321 std::vector<CacheElementType> res;
323 std::function<void(Accessible*)> proc =
324 [&](Accessible* item) {
325 res.emplace_back(std::move(CreateCacheElement(root)));
326 for(auto i = 0u; i < item->GetChildCount(); ++i)
328 proc(item->GetChildAtIndex(i));
335 auto BridgeBase::CreateCacheElement(Accessible* item) -> CacheElementType
342 auto root = &mApplication;
343 auto parent = item->GetParent();
345 std::vector<Address> children;
346 for(auto i = 0u; i < item->GetChildCount(); ++i)
348 children.emplace_back(item->GetChildAtIndex(i)->GetAddress());
351 return std::make_tuple(
354 parent ? parent->GetAddress() : Address{},
356 item->GetInterfaces(),
359 item->GetDescription(),
360 item->GetStates().GetRawData());