1 #ifndef DALI_INTERNAL_ACCESSIBILITY_BRIDGE_BASE_H
2 #define DALI_INTERNAL_ACCESSIBILITY_BRIDGE_BASE_H
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 #include <dali/public-api/actors/layer.h>
23 #include <dali/public-api/dali-adaptor-version.h>
24 #include <dali/public-api/signals/connection-tracker.h>
28 #include <dali/devel-api/adaptor-framework/proxy-accessible.h>
29 #include <dali/devel-api/adaptor-framework/window-devel.h>
30 #include <dali/devel-api/atspi-interfaces/accessible.h>
31 #include <dali/devel-api/atspi-interfaces/application.h>
32 #include <dali/devel-api/atspi-interfaces/collection.h>
33 #include <dali/internal/accessibility/bridge/accessibility-common.h>
36 * @brief The ApplicationAccessible class is to define Accessibility Application.
38 class ApplicationAccessible : public virtual Dali::Accessibility::Accessible, public virtual Dali::Accessibility::Collection, public virtual Dali::Accessibility::Application
41 Dali::Accessibility::ProxyAccessible mParent;
42 std::vector<Dali::Accessibility::Accessible*> mChildren;
45 std::string GetName() const override
50 std::string GetDescription() const override
55 Dali::Accessibility::Accessible* GetParent() override
60 size_t GetChildCount() const override
62 return mChildren.size();
65 std::vector<Dali::Accessibility::Accessible*> GetChildren() override
70 Dali::Accessibility::Accessible* GetChildAtIndex(size_t index) override
72 auto size = mChildren.size();
75 throw std::domain_error{"invalid index " + std::to_string(index) + " for object with " + std::to_string(size) + " children"};
77 return mChildren[index];
80 size_t GetIndexInParent() override
82 throw std::domain_error{"can't call GetIndexInParent on application object"};
85 Dali::Accessibility::Role GetRole() const override
87 return Dali::Accessibility::Role::APPLICATION;
90 Dali::Accessibility::States GetStates() override
95 Dali::Accessibility::Attributes GetAttributes() const override
101 * @brief Gets the Accessible object from the window.
103 * @param[in] window The window to find
104 * @return Null if mChildren is empty, otherwise the Accessible object
105 * @note Currently, the default window would be returned when mChildren is not empty.
107 Dali::Accessibility::Accessible* GetWindowAccessible(Dali::Window window)
109 if(mChildren.empty())
114 Dali::Layer rootLayer = window.GetRootLayer();
116 // Find a child which is related to the window.
117 for(auto i = 0u; i < mChildren.size(); ++i)
119 if(rootLayer == mChildren[i]->GetInternalActor())
125 // If can't find its children, return the default window.
129 bool DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo) override
134 std::vector<Dali::Accessibility::Relation> GetRelationSet() override
139 Dali::Actor GetInternalActor() override
141 return Dali::Actor{};
144 Dali::Accessibility::Address GetAddress() const override
149 std::string GetToolkitName() const override
154 std::string GetVersion() const override
156 return std::to_string(Dali::ADAPTOR_MAJOR_VERSION) + "." + std::to_string(Dali::ADAPTOR_MINOR_VERSION);
161 * @brief Enumeration for FilteredEvents.
163 enum class FilteredEvents
165 BOUNDS_CHANGED ///< Bounds changed
168 // Custom specialization of std::hash
172 struct hash<std::pair<FilteredEvents, Dali::Accessibility::Accessible*>>
174 size_t operator()(std::pair<FilteredEvents, Dali::Accessibility::Accessible*> value) const
176 return (static_cast<size_t>(value.first) * 131) ^ reinterpret_cast<size_t>(value.second);
182 * @brief The BridgeBase class is basic class for Bridge functions.
184 class BridgeBase : public Dali::Accessibility::Bridge, public Dali::ConnectionTracker
186 std::unordered_map<std::pair<FilteredEvents, Dali::Accessibility::Accessible*>, std::pair<unsigned int, std::function<void()>>> mFilteredEvents;
189 * @brief Removes all FilteredEvents using Tick signal.
191 * @return False if mFilteredEvents is empty, otherwise true.
193 bool TickFilteredEvents();
197 * @brief Adds FilteredEvents, Accessible, and delay time to mFilteredEvents.
199 * @param[in] kind FilteredEvents enum value
200 * @param[in] obj Accessible object
201 * @param[in] delay The delay time
202 * @param[in] functor The function to be called // NEED TO UPDATE!
204 void AddFilteredEvent(FilteredEvents kind, Dali::Accessibility::Accessible* obj, float delay, std::function<void()> functor);
207 * @brief Callback when the visibility of the window is changed.
209 * @param[in] window The window to be changed
210 * @param[in] visible The visibility of the window
212 void OnWindowVisibilityChanged(Dali::Window window, bool visible);
215 * @brief Callback when the window focus is changed.
217 * @param[in] window The window whose focus is changed
218 * @param[in] focusIn Whether the focus is in/out
220 void OnWindowFocusChanged(Dali::Window window, bool focusIn);
223 * @copydoc Dali::Accessibility::Bridge::GetBusName()
225 const std::string& GetBusName() const override;
228 * @copydoc Dali::Accessibility::Bridge::AddTopLevelWindow()
230 void AddTopLevelWindow(Dali::Accessibility::Accessible* windowAccessible) override;
233 * @copydoc Dali::Accessibility::Bridge::RemoveTopLevelWindow()
235 void RemoveTopLevelWindow(Dali::Accessibility::Accessible* windowAccessible) override;
238 * @copydoc Dali::Accessibility::Bridge::RegisterDefaultLabel()
240 void RegisterDefaultLabel(Dali::Accessibility::Accessible* object) override;
243 * @copydoc Dali::Accessibility::Bridge::UnregisterDefaultLabel()
245 void UnregisterDefaultLabel(Dali::Accessibility::Accessible* object) override;
248 * @copydoc Dali::Accessibility::Bridge::GetDefaultLabel()
250 Dali::Accessibility::Accessible* GetDefaultLabel() const override
252 return mDefaultLabels.empty() ? nullptr : mDefaultLabels.back();
256 * @copydoc Dali::Accessibility::Bridge::GetApplication()
258 Dali::Accessibility::Accessible* GetApplication() const override
260 return &mApplication;
264 * @brief Adds function to dbus interface.
266 template<typename SELF, typename... RET, typename... ARGS>
267 void AddFunctionToInterface(
268 DBus::DBusInterfaceDescription& desc, const std::string& funcName, DBus::ValueOrError<RET...> (SELF::*funcPtr)(ARGS...))
270 if(auto self = dynamic_cast<SELF*>(this))
271 desc.addMethod<DBus::ValueOrError<RET...>(ARGS...)>(
273 [=](ARGS... args) -> DBus::ValueOrError<RET...> {
276 return (self->*funcPtr)(std::move(args)...);
278 catch(std::domain_error& e)
280 return DBus::Error{e.what()};
286 * @brief Adds 'Get' property to dbus interface.
288 template<typename T, typename SELF>
289 void AddGetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
290 const std::string& funcName,
291 T (SELF::*funcPtr)())
293 if(auto self = dynamic_cast<SELF*>(this))
294 desc.addProperty<T>(funcName,
295 [=]() -> DBus::ValueOrError<T> {
298 return (self->*funcPtr)();
300 catch(std::domain_error& e)
302 return DBus::Error{e.what()};
309 * @brief Adds 'Set' property to dbus interface.
311 template<typename T, typename SELF>
312 void AddSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
313 const std::string& funcName,
314 void (SELF::*funcPtr)(T))
316 if(auto self = dynamic_cast<SELF*>(this))
317 desc.addProperty<T>(funcName, {}, [=](T t) -> DBus::ValueOrError<void> {
320 (self->*funcPtr)(std::move(t));
323 catch(std::domain_error& e)
325 return DBus::Error{e.what()};
331 * @brief Adds 'Set' and 'Get' properties to dbus interface.
333 template<typename T, typename T1, typename SELF>
334 void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
335 const std::string& funcName,
336 T1 (SELF::*funcPtrGet)(),
337 DBus::ValueOrError<void> (SELF::*funcPtrSet)(T))
339 if(auto self = dynamic_cast<SELF*>(this))
342 [=]() -> DBus::ValueOrError<T> {
345 return (self->*funcPtrGet)();
347 catch(std::domain_error& e)
349 return DBus::Error{e.what()};
352 [=](T t) -> DBus::ValueOrError<void> {
355 (self->*funcPtrSet)(std::move(t));
358 catch(std::domain_error& e)
360 return DBus::Error{e.what()};
366 * @brief Adds 'Get' and 'Set' properties to dbus interface.
368 template<typename T, typename T1, typename SELF>
369 void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
370 const std::string& funcName,
371 T1 (SELF::*funcPtrGet)(),
372 void (SELF::*funcPtrSet)(T))
374 if(auto self = dynamic_cast<SELF*>(this))
377 [=]() -> DBus::ValueOrError<T> {
380 return (self->*funcPtrGet)();
382 catch(std::domain_error& e)
384 return DBus::Error{e.what()};
387 [=](T t) -> DBus::ValueOrError<void> {
390 (self->*funcPtrSet)(std::move(t));
393 catch(std::domain_error& e)
395 return DBus::Error{e.what()};
401 * @brief Gets the string of the path excluding the specified prefix.
403 * @param path The path to get
404 * @return The string stripped of the specific prefix
406 static std::string StripPrefix(const std::string& path);
409 * @brief Finds the Accessible object according to the path.
411 * @param[in] path The path for Accessible object
412 * @return The Accessible object corresponding to the path
414 Dali::Accessibility::Accessible* Find(const std::string& path) const;
417 * @brief Finds the Accessible object with the given address.
419 * @param[in] ptr The unique Address of the object
420 * @return The Accessible object corresponding to the path
422 Dali::Accessibility::Accessible* Find(const Dali::Accessibility::Address& ptr) const;
425 * @brief Returns the target object of the currently executed DBus method call.
427 * @return The Accessible object
428 * @note When a DBus method is called on some object, this target object (`currentObject`) is temporarily saved by the bridge,
429 * because DBus handles the invocation target separately from the method arguments.
430 * We then use the saved object inside the 'glue' method (e.g. BridgeValue::GetMinimum)
431 * to call the equivalent method on the respective C++ object (this could be ScrollBar::AccessibleImpl::GetMinimum in the example given).
433 Dali::Accessibility::Accessible* FindCurrentObject() const;
436 * @brief Returns the target object of the currently executed DBus method call.
438 * This method tries to downcast the return value of FindCurrentObject() to the requested type,
439 * issuing an error reply to the DBus caller if the requested type is not implemented. Whether
440 * a given type is implemented is decided based on the return value of Accessible::GetInterfaces()
441 * for the current object.
443 * @tparam I The requested AT-SPI interface
444 * @return The Accessible object (cast to a more derived type)
446 * @see FindCurrentObject()
447 * @see Dali::Accessibility::AtspiInterface
448 * @see Dali::Accessibility::AtspiInterfaceType
449 * @see Dali::Accessibility::Accessible::GetInterfaces()
451 template<Dali::Accessibility::AtspiInterface I>
452 auto* FindCurrentObjectWithInterface() const
454 using Type = Dali::Accessibility::AtspiInterfaceType<I>;
457 auto* currentObject = FindCurrentObject();
458 DALI_ASSERT_DEBUG(currentObject); // FindCurrentObject() throws domain_error
460 if(!(result = Dali::Accessibility::Accessible::DownCast<I>(currentObject)))
464 s << "Object " << currentObject->GetAddress().ToString();
465 s << " does not implement ";
466 s << Dali::Accessibility::Accessible::GetInterfaceName(I);
468 throw std::domain_error{s.str()};
475 * @copydoc Dali::Accessibility::Bridge::FindByPath()
477 Dali::Accessibility::Accessible* FindByPath(const std::string& name) const override;
480 * @copydoc Dali::Accessibility::Bridge::SetApplicationName()
482 void SetApplicationName(std::string name) override
484 mApplication.mName = std::move(name);
488 mutable ApplicationAccessible mApplication;
489 std::vector<Dali::Accessibility::Accessible*> mDefaultLabels;
490 bool mIsScreenReaderSuppressed = false;
495 * @param[in] id An ID (integer value)
500 * @brief Gets the ID.
501 * @return The ID to be set
506 * @brief Update registered events.
508 void UpdateRegisteredEvents();
510 using CacheElementType = std::tuple<
511 Dali::Accessibility::Address,
512 Dali::Accessibility::Address,
513 Dali::Accessibility::Address,
514 std::vector<Dali::Accessibility::Address>,
515 std::vector<std::string>,
517 Dali::Accessibility::Role,
519 std::array<uint32_t, 2>>;
522 * @brief Gets Items // NEED TO UPDATE!
526 DBus::ValueOrError<std::vector<CacheElementType>> GetItems();
529 * @brief Creates CacheElement.
531 * CreateCacheElement method works for GetItems which is a part of ATSPI protocol.
532 * ATSPI client library (libatspi from at-spi2-core) depending on cacheing policy configuration uses GetItems
533 * to pre-load entire accessible tree from application to its own cache in single dbus call.
534 * Otherwise the particular nodes in a tree are cached lazily when client library tries to access them.
535 * @param item Accessible to get information
536 * @return The elements to be cached
538 CacheElementType CreateCacheElement(Dali::Accessibility::Accessible* item);
542 virtual ~BridgeBase();
545 * @copydoc Dali::Accessibility::Bridge::ForceUp()
547 ForceUpResult ForceUp() override;
550 * @copydoc Dali::Accessibility::Bridge::ForceDown()
552 void ForceDown() override;
554 DBus::DBusServer mDbusServer;
555 DBusWrapper::ConnectionPtr mConnectionPtr;
557 DBus::DBusClient mRegistry;
558 bool IsBoundsChangedEventAllowed{false};
561 #endif // DALI_INTERNAL_ACCESSIBILITY_BRIDGE_BASE_H