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>
29 #include <dali/devel-api/adaptor-framework/proxy-accessible.h>
30 #include <dali/devel-api/adaptor-framework/window-devel.h>
31 #include <dali/devel-api/atspi-interfaces/accessible.h>
32 #include <dali/devel-api/atspi-interfaces/application.h>
33 #include <dali/devel-api/atspi-interfaces/collection.h>
34 #include <dali/devel-api/atspi-interfaces/socket.h>
35 #include <dali/internal/accessibility/bridge/accessibility-common.h>
38 * @brief The ApplicationAccessible class is to define Accessibility Application.
40 class ApplicationAccessible : public virtual Dali::Accessibility::Accessible,
41 public virtual Dali::Accessibility::Application,
42 public virtual Dali::Accessibility::Collection,
43 public virtual Dali::Accessibility::Component,
44 public virtual Dali::Accessibility::Socket
47 Dali::Accessibility::ProxyAccessible mParent;
48 std::vector<Dali::Accessibility::Accessible*> mChildren;
50 std::string mToolkitName{"dali"};
51 bool mIsEmbedded{false};
53 std::string GetName() const override
58 std::string GetDescription() const override
63 Dali::Accessibility::Accessible* GetParent() override
68 size_t GetChildCount() const override
70 return mChildren.size();
73 std::vector<Dali::Accessibility::Accessible*> GetChildren() override
78 Dali::Accessibility::Accessible* GetChildAtIndex(size_t index) override
80 auto size = mChildren.size();
83 throw std::domain_error{"invalid index " + std::to_string(index) + " for object with " + std::to_string(size) + " children"};
85 return mChildren[index];
88 size_t GetIndexInParent() override
95 throw std::domain_error{"can't call GetIndexInParent on application object"};
98 Dali::Accessibility::Role GetRole() const override
100 return Dali::Accessibility::Role::APPLICATION;
103 Dali::Accessibility::States GetStates() override
105 Dali::Accessibility::States result;
107 for(auto* child : mChildren)
109 result = result | child->GetStates();
115 Dali::Accessibility::Attributes GetAttributes() const override
121 * @brief Gets the Accessible object from the window.
123 * @param[in] window The window to find
124 * @return Null if mChildren is empty, otherwise the Accessible object
125 * @note Currently, the default window would be returned when mChildren is not empty.
127 Dali::Accessibility::Accessible* GetWindowAccessible(Dali::Window window)
129 if(mChildren.empty())
134 Dali::Layer rootLayer = window.GetRootLayer();
136 // Find a child which is related to the window.
137 for(auto i = 0u; i < mChildren.size(); ++i)
139 if(rootLayer == mChildren[i]->GetInternalActor())
145 // If can't find its children, return the default window.
149 bool DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo) override
154 std::vector<Dali::Accessibility::Relation> GetRelationSet() override
159 Dali::Actor GetInternalActor() override
161 return Dali::Actor{};
164 Dali::Accessibility::Address GetAddress() const override
171 std::string GetToolkitName() const override
176 std::string GetVersion() const override
178 return std::to_string(Dali::ADAPTOR_MAJOR_VERSION) + "." + std::to_string(Dali::ADAPTOR_MINOR_VERSION);
183 Dali::Accessibility::Address Embed(Dali::Accessibility::Address plug) override
186 mParent.SetAddress(plug);
191 void Unembed(Dali::Accessibility::Address plug) override
193 if(mParent.GetAddress() == plug)
196 mParent.SetAddress({});
197 Dali::Accessibility::Bridge::GetCurrentBridge()->SetExtentsOffset(0, 0);
201 void SetOffset(std::int32_t x, std::int32_t y) override
208 Dali::Accessibility::Bridge::GetCurrentBridge()->SetExtentsOffset(x, y);
213 Dali::Rect<> GetExtents(Dali::Accessibility::CoordinateType type) const override
215 using limits = std::numeric_limits<float>;
217 float minX = limits::max();
218 float minY = limits::max();
219 float maxX = limits::min();
220 float maxY = limits::min();
222 for(Dali::Accessibility::Accessible* child : mChildren)
224 auto* component = Dali::Accessibility::Component::DownCast(child);
230 auto extents = component->GetExtents(type);
232 minX = std::min(minX, extents.x);
233 minY = std::min(minY, extents.y);
234 maxX = std::max(maxX, extents.x + extents.width);
235 maxY = std::max(maxY, extents.y + extents.height);
238 return {minX, minY, maxX - minX, maxY - minY};
241 Dali::Accessibility::ComponentLayer GetLayer() const override
243 return Dali::Accessibility::ComponentLayer::WINDOW;
246 std::int16_t GetMdiZOrder() const override
251 bool GrabFocus() override
256 double GetAlpha() const override
261 bool GrabHighlight() override
266 bool ClearHighlight() override
271 bool IsScrollable() const override
278 * @brief Enumeration for CoalescableMessages.
280 enum class CoalescableMessages
282 BOUNDS_CHANGED, ///< Bounds changed
283 SET_OFFSET, ///< Set offset
284 POST_RENDER, ///< Post render
287 // Custom specialization of std::hash
291 struct hash<std::pair<CoalescableMessages, Dali::Accessibility::Accessible*>>
293 size_t operator()(std::pair<CoalescableMessages, Dali::Accessibility::Accessible*> value) const
295 return (static_cast<size_t>(value.first) * 131) ^ reinterpret_cast<size_t>(value.second);
301 * @brief The BridgeBase class is basic class for Bridge functions.
303 class BridgeBase : public Dali::Accessibility::Bridge, public Dali::ConnectionTracker
305 std::unordered_map<std::pair<CoalescableMessages, Dali::Accessibility::Accessible*>, std::tuple<unsigned int, unsigned int, std::function<void()>>> mCoalescableMessages;
308 * @brief Removes all CoalescableMessages using Tick signal.
310 * @return False if mCoalescableMessages is empty, otherwise true.
312 bool TickCoalescableMessages();
316 * @brief Adds CoalescableMessages, Accessible, and delay time to mCoalescableMessages.
318 * @param[in] kind CoalescableMessages enum value
319 * @param[in] obj Accessible object
320 * @param[in] delay The delay time
321 * @param[in] functor The function to be called // NEED TO UPDATE!
323 void AddCoalescableMessage(CoalescableMessages kind, Dali::Accessibility::Accessible* obj, float delay, std::function<void()> functor);
326 * @brief Callback when the visibility of the window is changed.
328 * @param[in] window The window to be changed
329 * @param[in] visible The visibility of the window
331 void OnWindowVisibilityChanged(Dali::Window window, bool visible);
334 * @brief Callback when the window focus is changed.
336 * @param[in] window The window whose focus is changed
337 * @param[in] focusIn Whether the focus is in/out
339 void OnWindowFocusChanged(Dali::Window window, bool focusIn);
342 * @copydoc Dali::Accessibility::Bridge::GetBusName()
344 const std::string& GetBusName() const override;
347 * @copydoc Dali::Accessibility::Bridge::AddTopLevelWindow()
349 void AddTopLevelWindow(Dali::Accessibility::Accessible* windowAccessible) override;
352 * @copydoc Dali::Accessibility::Bridge::RemoveTopLevelWindow()
354 void RemoveTopLevelWindow(Dali::Accessibility::Accessible* windowAccessible) override;
357 * @copydoc Dali::Accessibility::Bridge::RegisterDefaultLabel()
359 void RegisterDefaultLabel(Dali::Accessibility::Accessible* object) override;
362 * @copydoc Dali::Accessibility::Bridge::UnregisterDefaultLabel()
364 void UnregisterDefaultLabel(Dali::Accessibility::Accessible* object) override;
367 * @copydoc Dali::Accessibility::Bridge::GetDefaultLabel()
369 Dali::Accessibility::Accessible* GetDefaultLabel(Dali::Accessibility::Accessible* root) const override;
372 * @copydoc Dali::Accessibility::Bridge::GetApplication()
374 Dali::Accessibility::Accessible* GetApplication() const override
376 return &mApplication;
380 * @brief Adds function to dbus interface.
382 template<typename SELF, typename... RET, typename... ARGS>
383 void AddFunctionToInterface(
384 DBus::DBusInterfaceDescription& desc, const std::string& funcName, DBus::ValueOrError<RET...> (SELF::*funcPtr)(ARGS...))
386 if(auto self = dynamic_cast<SELF*>(this))
387 desc.addMethod<DBus::ValueOrError<RET...>(ARGS...)>(
389 [=](ARGS... args) -> DBus::ValueOrError<RET...> {
392 return (self->*funcPtr)(std::move(args)...);
394 catch(std::domain_error& e)
396 return DBus::Error{e.what()};
402 * @brief Adds 'Get' property to dbus interface.
404 template<typename T, typename SELF>
405 void AddGetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
406 const std::string& funcName,
407 T (SELF::*funcPtr)())
409 if(auto self = dynamic_cast<SELF*>(this))
410 desc.addProperty<T>(funcName,
411 [=]() -> DBus::ValueOrError<T> {
414 return (self->*funcPtr)();
416 catch(std::domain_error& e)
418 return DBus::Error{e.what()};
425 * @brief Adds 'Set' property to dbus interface.
427 template<typename T, typename SELF>
428 void AddSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
429 const std::string& funcName,
430 void (SELF::*funcPtr)(T))
432 if(auto self = dynamic_cast<SELF*>(this))
433 desc.addProperty<T>(funcName, {}, [=](T t) -> DBus::ValueOrError<void> {
436 (self->*funcPtr)(std::move(t));
439 catch(std::domain_error& e)
441 return DBus::Error{e.what()};
447 * @brief Adds 'Set' and 'Get' properties to dbus interface.
449 template<typename T, typename T1, typename SELF>
450 void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
451 const std::string& funcName,
452 T1 (SELF::*funcPtrGet)(),
453 DBus::ValueOrError<void> (SELF::*funcPtrSet)(T))
455 if(auto self = dynamic_cast<SELF*>(this))
458 [=]() -> DBus::ValueOrError<T> {
461 return (self->*funcPtrGet)();
463 catch(std::domain_error& e)
465 return DBus::Error{e.what()};
468 [=](T t) -> DBus::ValueOrError<void> {
471 (self->*funcPtrSet)(std::move(t));
474 catch(std::domain_error& e)
476 return DBus::Error{e.what()};
482 * @brief Adds 'Get' and 'Set' properties to dbus interface.
484 template<typename T, typename T1, typename SELF>
485 void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
486 const std::string& funcName,
487 T1 (SELF::*funcPtrGet)(),
488 void (SELF::*funcPtrSet)(T))
490 if(auto self = dynamic_cast<SELF*>(this))
493 [=]() -> DBus::ValueOrError<T> {
496 return (self->*funcPtrGet)();
498 catch(std::domain_error& e)
500 return DBus::Error{e.what()};
503 [=](T t) -> DBus::ValueOrError<void> {
506 (self->*funcPtrSet)(std::move(t));
509 catch(std::domain_error& e)
511 return DBus::Error{e.what()};
517 * @brief Gets the string of the path excluding the specified prefix.
519 * @param path The path to get
520 * @return The string stripped of the specific prefix
522 static std::string StripPrefix(const std::string& path);
525 * @brief Finds the Accessible object according to the path.
527 * @param[in] path The path for Accessible object
528 * @return The Accessible object corresponding to the path
530 Dali::Accessibility::Accessible* Find(const std::string& path) const;
533 * @brief Finds the Accessible object with the given address.
535 * @param[in] ptr The unique Address of the object
536 * @return The Accessible object corresponding to the path
538 Dali::Accessibility::Accessible* Find(const Dali::Accessibility::Address& ptr) const;
541 * @brief Returns the target object of the currently executed DBus method call.
543 * @return The Accessible object
544 * @note When a DBus method is called on some object, this target object (`currentObject`) is temporarily saved by the bridge,
545 * because DBus handles the invocation target separately from the method arguments.
546 * We then use the saved object inside the 'glue' method (e.g. BridgeValue::GetMinimum)
547 * to call the equivalent method on the respective C++ object (this could be ScrollBar::AccessibleImpl::GetMinimum in the example given).
549 Dali::Accessibility::Accessible* FindCurrentObject() const;
552 * @brief Returns the target object of the currently executed DBus method call.
554 * This method tries to downcast the return value of FindCurrentObject() to the requested type,
555 * issuing an error reply to the DBus caller if the requested type is not implemented. Whether
556 * a given type is implemented is decided based on the return value of Accessible::GetInterfaces()
557 * for the current object.
559 * @tparam I The requested AT-SPI interface
560 * @return The Accessible object (cast to a more derived type)
562 * @see FindCurrentObject()
563 * @see Dali::Accessibility::AtspiInterface
564 * @see Dali::Accessibility::AtspiInterfaceType
565 * @see Dali::Accessibility::Accessible::GetInterfaces()
567 template<Dali::Accessibility::AtspiInterface I>
568 auto* FindCurrentObjectWithInterface() const
570 using Type = Dali::Accessibility::AtspiInterfaceType<I>;
573 auto* currentObject = FindCurrentObject();
574 DALI_ASSERT_DEBUG(currentObject); // FindCurrentObject() throws domain_error
576 if(!(result = Dali::Accessibility::Accessible::DownCast<I>(currentObject)))
580 s << "Object " << currentObject->GetAddress().ToString();
581 s << " does not implement ";
582 s << Dali::Accessibility::Accessible::GetInterfaceName(I);
584 throw std::domain_error{s.str()};
591 * @copydoc Dali::Accessibility::Bridge::FindByPath()
593 Dali::Accessibility::Accessible* FindByPath(const std::string& name) const override;
596 * @copydoc Dali::Accessibility::Bridge::SetApplicationName()
598 void SetApplicationName(std::string name) override
600 mApplication.mName = std::move(name);
604 * @copydoc Dali::Accessibility::Bridge::SetToolkitName()
606 void SetToolkitName(std::string_view toolkitName) override
608 mApplication.mToolkitName = std::string{toolkitName};
612 mutable ApplicationAccessible mApplication;
613 std::vector<Dali::Accessibility::Accessible*> mDefaultLabels;
614 bool mIsScreenReaderSuppressed = false;
619 * @param[in] id An ID (integer value)
624 * @brief Gets the ID.
625 * @return The ID to be set
630 * @brief Update registered events.
632 void UpdateRegisteredEvents();
634 using CacheElementType = std::tuple<
635 Dali::Accessibility::Address,
636 Dali::Accessibility::Address,
637 Dali::Accessibility::Address,
638 std::vector<Dali::Accessibility::Address>,
639 std::vector<std::string>,
641 Dali::Accessibility::Role,
643 std::array<uint32_t, 2>>;
646 * @brief Gets Items // NEED TO UPDATE!
650 DBus::ValueOrError<std::vector<CacheElementType>> GetItems();
653 * @brief Creates CacheElement.
655 * CreateCacheElement method works for GetItems which is a part of ATSPI protocol.
656 * ATSPI client library (libatspi from at-spi2-core) depending on cacheing policy configuration uses GetItems
657 * to pre-load entire accessible tree from application to its own cache in single dbus call.
658 * Otherwise the particular nodes in a tree are cached lazily when client library tries to access them.
659 * @param item Accessible to get information
660 * @return The elements to be cached
662 CacheElementType CreateCacheElement(Dali::Accessibility::Accessible* item);
666 virtual ~BridgeBase();
669 * @copydoc Dali::Accessibility::Bridge::ForceUp()
671 ForceUpResult ForceUp() override;
674 * @copydoc Dali::Accessibility::Bridge::ForceDown()
676 void ForceDown() override;
678 DBus::DBusServer mDbusServer;
679 DBusWrapper::ConnectionPtr mConnectionPtr;
681 DBus::DBusClient mRegistry;
682 bool IsBoundsChangedEventAllowed{false};
685 #endif // DALI_INTERNAL_ACCESSIBILITY_BRIDGE_BASE_H