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/devel-api/atspi-interfaces/socket.h>
34 #include <dali/internal/accessibility/bridge/accessibility-common.h>
37 * @brief The ApplicationAccessible class is to define Accessibility Application.
39 class ApplicationAccessible : public virtual Dali::Accessibility::Accessible,
40 public virtual Dali::Accessibility::Application,
41 public virtual Dali::Accessibility::Collection,
42 public virtual Dali::Accessibility::Component,
43 public virtual Dali::Accessibility::Socket
46 Dali::Accessibility::ProxyAccessible mParent;
47 std::vector<Dali::Accessibility::Accessible*> mChildren;
49 std::string mToolkitName{"dali"};
50 bool mIsEmbedded{false};
52 std::string GetName() const override
57 std::string GetDescription() const override
62 Dali::Accessibility::Accessible* GetParent() override
67 size_t GetChildCount() const override
69 return mChildren.size();
72 std::vector<Dali::Accessibility::Accessible*> GetChildren() override
77 Dali::Accessibility::Accessible* GetChildAtIndex(size_t index) override
79 auto size = mChildren.size();
82 throw std::domain_error{"invalid index " + std::to_string(index) + " for object with " + std::to_string(size) + " children"};
84 return mChildren[index];
87 size_t GetIndexInParent() override
94 throw std::domain_error{"can't call GetIndexInParent on application object"};
97 Dali::Accessibility::Role GetRole() const override
99 return Dali::Accessibility::Role::APPLICATION;
102 Dali::Accessibility::States GetStates() override
107 Dali::Accessibility::Attributes GetAttributes() const override
113 * @brief Gets the Accessible object from the window.
115 * @param[in] window The window to find
116 * @return Null if mChildren is empty, otherwise the Accessible object
117 * @note Currently, the default window would be returned when mChildren is not empty.
119 Dali::Accessibility::Accessible* GetWindowAccessible(Dali::Window window)
121 if(mChildren.empty())
126 Dali::Layer rootLayer = window.GetRootLayer();
128 // Find a child which is related to the window.
129 for(auto i = 0u; i < mChildren.size(); ++i)
131 if(rootLayer == mChildren[i]->GetInternalActor())
137 // If can't find its children, return the default window.
141 bool DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo) override
146 std::vector<Dali::Accessibility::Relation> GetRelationSet() override
151 Dali::Actor GetInternalActor() override
153 return Dali::Actor{};
156 Dali::Accessibility::Address GetAddress() const override
163 std::string GetToolkitName() const override
168 std::string GetVersion() const override
170 return std::to_string(Dali::ADAPTOR_MAJOR_VERSION) + "." + std::to_string(Dali::ADAPTOR_MINOR_VERSION);
175 Dali::Accessibility::Address Embed(Dali::Accessibility::Address plug) override
178 mParent.SetAddress(plug);
183 void Unembed(Dali::Accessibility::Address plug) override
185 if(mParent.GetAddress() == plug)
188 mParent.SetAddress({});
194 Dali::Rect<> GetExtents(Dali::Accessibility::CoordinateType type) const override
196 using limits = std::numeric_limits<float>;
198 float minX = limits::max();
199 float minY = limits::max();
200 float maxX = limits::min();
201 float maxY = limits::min();
203 for(Dali::Accessibility::Accessible* child : mChildren)
205 auto* component = Dali::Accessibility::Component::DownCast(child);
211 auto extents = component->GetExtents(type);
213 minX = std::min(minX, extents.x);
214 minY = std::min(minY, extents.y);
215 maxX = std::max(maxX, extents.x + extents.width);
216 maxY = std::max(maxY, extents.y + extents.height);
219 return {minX, minY, maxX - minX, maxY - minY};
222 Dali::Accessibility::ComponentLayer GetLayer() const override
224 return Dali::Accessibility::ComponentLayer::WINDOW;
227 std::int16_t GetMdiZOrder() const override
232 bool GrabFocus() override
237 double GetAlpha() const override
242 bool GrabHighlight() override
247 bool ClearHighlight() override
252 bool IsScrollable() const override
259 * @brief Enumeration for FilteredEvents.
261 enum class FilteredEvents
263 BOUNDS_CHANGED ///< Bounds changed
266 // Custom specialization of std::hash
270 struct hash<std::pair<FilteredEvents, Dali::Accessibility::Accessible*>>
272 size_t operator()(std::pair<FilteredEvents, Dali::Accessibility::Accessible*> value) const
274 return (static_cast<size_t>(value.first) * 131) ^ reinterpret_cast<size_t>(value.second);
280 * @brief The BridgeBase class is basic class for Bridge functions.
282 class BridgeBase : public Dali::Accessibility::Bridge, public Dali::ConnectionTracker
284 std::unordered_map<std::pair<FilteredEvents, Dali::Accessibility::Accessible*>, std::pair<unsigned int, std::function<void()>>> mFilteredEvents;
287 * @brief Removes all FilteredEvents using Tick signal.
289 * @return False if mFilteredEvents is empty, otherwise true.
291 bool TickFilteredEvents();
295 * @brief Adds FilteredEvents, Accessible, and delay time to mFilteredEvents.
297 * @param[in] kind FilteredEvents enum value
298 * @param[in] obj Accessible object
299 * @param[in] delay The delay time
300 * @param[in] functor The function to be called // NEED TO UPDATE!
302 void AddFilteredEvent(FilteredEvents kind, Dali::Accessibility::Accessible* obj, float delay, std::function<void()> functor);
305 * @brief Callback when the visibility of the window is changed.
307 * @param[in] window The window to be changed
308 * @param[in] visible The visibility of the window
310 void OnWindowVisibilityChanged(Dali::Window window, bool visible);
313 * @brief Callback when the window focus is changed.
315 * @param[in] window The window whose focus is changed
316 * @param[in] focusIn Whether the focus is in/out
318 void OnWindowFocusChanged(Dali::Window window, bool focusIn);
321 * @copydoc Dali::Accessibility::Bridge::GetBusName()
323 const std::string& GetBusName() const override;
326 * @copydoc Dali::Accessibility::Bridge::AddTopLevelWindow()
328 void AddTopLevelWindow(Dali::Accessibility::Accessible* windowAccessible) override;
331 * @copydoc Dali::Accessibility::Bridge::RemoveTopLevelWindow()
333 void RemoveTopLevelWindow(Dali::Accessibility::Accessible* windowAccessible) override;
336 * @copydoc Dali::Accessibility::Bridge::RegisterDefaultLabel()
338 void RegisterDefaultLabel(Dali::Accessibility::Accessible* object) override;
341 * @copydoc Dali::Accessibility::Bridge::UnregisterDefaultLabel()
343 void UnregisterDefaultLabel(Dali::Accessibility::Accessible* object) override;
346 * @copydoc Dali::Accessibility::Bridge::GetDefaultLabel()
348 Dali::Accessibility::Accessible* GetDefaultLabel() const override
350 return mDefaultLabels.empty() ? nullptr : mDefaultLabels.back();
354 * @copydoc Dali::Accessibility::Bridge::GetApplication()
356 Dali::Accessibility::Accessible* GetApplication() const override
358 return &mApplication;
362 * @brief Adds function to dbus interface.
364 template<typename SELF, typename... RET, typename... ARGS>
365 void AddFunctionToInterface(
366 DBus::DBusInterfaceDescription& desc, const std::string& funcName, DBus::ValueOrError<RET...> (SELF::*funcPtr)(ARGS...))
368 if(auto self = dynamic_cast<SELF*>(this))
369 desc.addMethod<DBus::ValueOrError<RET...>(ARGS...)>(
371 [=](ARGS... args) -> DBus::ValueOrError<RET...> {
374 return (self->*funcPtr)(std::move(args)...);
376 catch(std::domain_error& e)
378 return DBus::Error{e.what()};
384 * @brief Adds 'Get' property to dbus interface.
386 template<typename T, typename SELF>
387 void AddGetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
388 const std::string& funcName,
389 T (SELF::*funcPtr)())
391 if(auto self = dynamic_cast<SELF*>(this))
392 desc.addProperty<T>(funcName,
393 [=]() -> DBus::ValueOrError<T> {
396 return (self->*funcPtr)();
398 catch(std::domain_error& e)
400 return DBus::Error{e.what()};
407 * @brief Adds 'Set' property to dbus interface.
409 template<typename T, typename SELF>
410 void AddSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
411 const std::string& funcName,
412 void (SELF::*funcPtr)(T))
414 if(auto self = dynamic_cast<SELF*>(this))
415 desc.addProperty<T>(funcName, {}, [=](T t) -> DBus::ValueOrError<void> {
418 (self->*funcPtr)(std::move(t));
421 catch(std::domain_error& e)
423 return DBus::Error{e.what()};
429 * @brief Adds 'Set' and 'Get' properties to dbus interface.
431 template<typename T, typename T1, typename SELF>
432 void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
433 const std::string& funcName,
434 T1 (SELF::*funcPtrGet)(),
435 DBus::ValueOrError<void> (SELF::*funcPtrSet)(T))
437 if(auto self = dynamic_cast<SELF*>(this))
440 [=]() -> DBus::ValueOrError<T> {
443 return (self->*funcPtrGet)();
445 catch(std::domain_error& e)
447 return DBus::Error{e.what()};
450 [=](T t) -> DBus::ValueOrError<void> {
453 (self->*funcPtrSet)(std::move(t));
456 catch(std::domain_error& e)
458 return DBus::Error{e.what()};
464 * @brief Adds 'Get' and 'Set' properties to dbus interface.
466 template<typename T, typename T1, typename SELF>
467 void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
468 const std::string& funcName,
469 T1 (SELF::*funcPtrGet)(),
470 void (SELF::*funcPtrSet)(T))
472 if(auto self = dynamic_cast<SELF*>(this))
475 [=]() -> DBus::ValueOrError<T> {
478 return (self->*funcPtrGet)();
480 catch(std::domain_error& e)
482 return DBus::Error{e.what()};
485 [=](T t) -> DBus::ValueOrError<void> {
488 (self->*funcPtrSet)(std::move(t));
491 catch(std::domain_error& e)
493 return DBus::Error{e.what()};
499 * @brief Gets the string of the path excluding the specified prefix.
501 * @param path The path to get
502 * @return The string stripped of the specific prefix
504 static std::string StripPrefix(const std::string& path);
507 * @brief Finds the Accessible object according to the path.
509 * @param[in] path The path for Accessible object
510 * @return The Accessible object corresponding to the path
512 Dali::Accessibility::Accessible* Find(const std::string& path) const;
515 * @brief Finds the Accessible object with the given address.
517 * @param[in] ptr The unique Address of the object
518 * @return The Accessible object corresponding to the path
520 Dali::Accessibility::Accessible* Find(const Dali::Accessibility::Address& ptr) const;
523 * @brief Returns the target object of the currently executed DBus method call.
525 * @return The Accessible object
526 * @note When a DBus method is called on some object, this target object (`currentObject`) is temporarily saved by the bridge,
527 * because DBus handles the invocation target separately from the method arguments.
528 * We then use the saved object inside the 'glue' method (e.g. BridgeValue::GetMinimum)
529 * to call the equivalent method on the respective C++ object (this could be ScrollBar::AccessibleImpl::GetMinimum in the example given).
531 Dali::Accessibility::Accessible* FindCurrentObject() const;
534 * @brief Returns the target object of the currently executed DBus method call.
536 * This method tries to downcast the return value of FindCurrentObject() to the requested type,
537 * issuing an error reply to the DBus caller if the requested type is not implemented. Whether
538 * a given type is implemented is decided based on the return value of Accessible::GetInterfaces()
539 * for the current object.
541 * @tparam I The requested AT-SPI interface
542 * @return The Accessible object (cast to a more derived type)
544 * @see FindCurrentObject()
545 * @see Dali::Accessibility::AtspiInterface
546 * @see Dali::Accessibility::AtspiInterfaceType
547 * @see Dali::Accessibility::Accessible::GetInterfaces()
549 template<Dali::Accessibility::AtspiInterface I>
550 auto* FindCurrentObjectWithInterface() const
552 using Type = Dali::Accessibility::AtspiInterfaceType<I>;
555 auto* currentObject = FindCurrentObject();
556 DALI_ASSERT_DEBUG(currentObject); // FindCurrentObject() throws domain_error
558 if(!(result = Dali::Accessibility::Accessible::DownCast<I>(currentObject)))
562 s << "Object " << currentObject->GetAddress().ToString();
563 s << " does not implement ";
564 s << Dali::Accessibility::Accessible::GetInterfaceName(I);
566 throw std::domain_error{s.str()};
573 * @copydoc Dali::Accessibility::Bridge::FindByPath()
575 Dali::Accessibility::Accessible* FindByPath(const std::string& name) const override;
578 * @copydoc Dali::Accessibility::Bridge::SetApplicationName()
580 void SetApplicationName(std::string name) override
582 mApplication.mName = std::move(name);
586 * @copydoc Dali::Accessibility::Bridge::SetToolkitName()
588 void SetToolkitName(std::string_view toolkitName) override
590 mApplication.mToolkitName = std::string{toolkitName};
594 mutable ApplicationAccessible mApplication;
595 std::vector<Dali::Accessibility::Accessible*> mDefaultLabels;
596 bool mIsScreenReaderSuppressed = false;
601 * @param[in] id An ID (integer value)
606 * @brief Gets the ID.
607 * @return The ID to be set
612 * @brief Update registered events.
614 void UpdateRegisteredEvents();
616 using CacheElementType = std::tuple<
617 Dali::Accessibility::Address,
618 Dali::Accessibility::Address,
619 Dali::Accessibility::Address,
620 std::vector<Dali::Accessibility::Address>,
621 std::vector<std::string>,
623 Dali::Accessibility::Role,
625 std::array<uint32_t, 2>>;
628 * @brief Gets Items // NEED TO UPDATE!
632 DBus::ValueOrError<std::vector<CacheElementType>> GetItems();
635 * @brief Creates CacheElement.
637 * CreateCacheElement method works for GetItems which is a part of ATSPI protocol.
638 * ATSPI client library (libatspi from at-spi2-core) depending on cacheing policy configuration uses GetItems
639 * to pre-load entire accessible tree from application to its own cache in single dbus call.
640 * Otherwise the particular nodes in a tree are cached lazily when client library tries to access them.
641 * @param item Accessible to get information
642 * @return The elements to be cached
644 CacheElementType CreateCacheElement(Dali::Accessibility::Accessible* item);
648 virtual ~BridgeBase();
651 * @copydoc Dali::Accessibility::Bridge::ForceUp()
653 ForceUpResult ForceUp() override;
656 * @copydoc Dali::Accessibility::Bridge::ForceDown()
658 void ForceDown() override;
660 DBus::DBusServer mDbusServer;
661 DBusWrapper::ConnectionPtr mConnectionPtr;
663 DBus::DBusClient mRegistry;
664 bool IsBoundsChangedEventAllowed{false};
667 #endif // DALI_INTERNAL_ACCESSIBILITY_BRIDGE_BASE_H