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();
112 // The Application object should never have the SENSITIVE state
113 result[Dali::Accessibility::State::SENSITIVE] = false;
118 Dali::Accessibility::Attributes GetAttributes() const override
124 * @brief Gets the Accessible object from the window.
126 * @param[in] window The window to find
127 * @return Null if mChildren is empty, otherwise the Accessible object
128 * @note Currently, the default window would be returned when mChildren is not empty.
130 Dali::Accessibility::Accessible* GetWindowAccessible(Dali::Window window)
132 if(mChildren.empty())
137 Dali::Layer rootLayer = window.GetRootLayer();
139 // Find a child which is related to the window.
140 for(auto i = 0u; i < mChildren.size(); ++i)
142 if(rootLayer == mChildren[i]->GetInternalActor())
148 // If can't find its children, return the default window.
152 bool DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo) override
157 std::vector<Dali::Accessibility::Relation> GetRelationSet() override
162 Dali::Actor GetInternalActor() override
164 return Dali::Actor{};
167 Dali::Accessibility::Address GetAddress() const override
174 std::string GetToolkitName() const override
179 std::string GetVersion() const override
181 return std::to_string(Dali::ADAPTOR_MAJOR_VERSION) + "." + std::to_string(Dali::ADAPTOR_MINOR_VERSION);
186 Dali::Accessibility::Address Embed(Dali::Accessibility::Address plug) override
189 mParent.SetAddress(plug);
194 void Unembed(Dali::Accessibility::Address plug) override
196 if(mParent.GetAddress() == plug)
199 mParent.SetAddress({});
200 Dali::Accessibility::Bridge::GetCurrentBridge()->SetExtentsOffset(0, 0);
204 void SetOffset(std::int32_t x, std::int32_t y) override
211 Dali::Accessibility::Bridge::GetCurrentBridge()->SetExtentsOffset(x, y);
216 Dali::Rect<> GetExtents(Dali::Accessibility::CoordinateType type) const override
218 using limits = std::numeric_limits<float>;
220 float minX = limits::max();
221 float minY = limits::max();
222 float maxX = limits::min();
223 float maxY = limits::min();
225 for(Dali::Accessibility::Accessible* child : mChildren)
227 auto* component = Dali::Accessibility::Component::DownCast(child);
233 auto extents = component->GetExtents(type);
235 minX = std::min(minX, extents.x);
236 minY = std::min(minY, extents.y);
237 maxX = std::max(maxX, extents.x + extents.width);
238 maxY = std::max(maxY, extents.y + extents.height);
241 return {minX, minY, maxX - minX, maxY - minY};
244 Dali::Accessibility::ComponentLayer GetLayer() const override
246 return Dali::Accessibility::ComponentLayer::WINDOW;
249 std::int16_t GetMdiZOrder() const override
254 bool GrabFocus() override
259 double GetAlpha() const override
264 bool GrabHighlight() override
269 bool ClearHighlight() override
274 bool IsScrollable() const override
281 * @brief Enumeration for CoalescableMessages.
283 enum class CoalescableMessages
285 BOUNDS_CHANGED, ///< Bounds changed
286 SET_OFFSET, ///< Set offset
289 // Custom specialization of std::hash
293 struct hash<std::pair<CoalescableMessages, Dali::Accessibility::Accessible*>>
295 size_t operator()(std::pair<CoalescableMessages, Dali::Accessibility::Accessible*> value) const
297 return (static_cast<size_t>(value.first) * 131) ^ reinterpret_cast<size_t>(value.second);
303 * @brief The BridgeBase class is basic class for Bridge functions.
305 class BridgeBase : public Dali::Accessibility::Bridge, public Dali::ConnectionTracker
307 std::unordered_map<std::pair<CoalescableMessages, Dali::Accessibility::Accessible*>, std::tuple<unsigned int, unsigned int, std::function<void()>>> mCoalescableMessages;
310 * @brief Removes all CoalescableMessages using Tick signal.
312 * @return False if mCoalescableMessages is empty, otherwise true.
314 bool TickCoalescableMessages();
318 * @brief Adds CoalescableMessages, Accessible, and delay time to mCoalescableMessages.
320 * @param[in] kind CoalescableMessages enum value
321 * @param[in] obj Accessible object
322 * @param[in] delay The delay time
323 * @param[in] functor The function to be called // NEED TO UPDATE!
325 void AddCoalescableMessage(CoalescableMessages kind, Dali::Accessibility::Accessible* obj, float delay, std::function<void()> functor);
328 * @brief Callback when the visibility of the window is changed.
330 * @param[in] window The window to be changed
331 * @param[in] visible The visibility of the window
333 void OnWindowVisibilityChanged(Dali::Window window, bool visible);
336 * @brief Callback when the window focus is changed.
338 * @param[in] window The window whose focus is changed
339 * @param[in] focusIn Whether the focus is in/out
341 void OnWindowFocusChanged(Dali::Window window, bool focusIn);
344 * @copydoc Dali::Accessibility::Bridge::GetBusName()
346 const std::string& GetBusName() const override;
349 * @copydoc Dali::Accessibility::Bridge::AddTopLevelWindow()
351 void AddTopLevelWindow(Dali::Accessibility::Accessible* windowAccessible) override;
354 * @copydoc Dali::Accessibility::Bridge::RemoveTopLevelWindow()
356 void RemoveTopLevelWindow(Dali::Accessibility::Accessible* windowAccessible) override;
359 * @copydoc Dali::Accessibility::Bridge::RegisterDefaultLabel()
361 void RegisterDefaultLabel(Dali::Accessibility::Accessible* object) override;
364 * @copydoc Dali::Accessibility::Bridge::UnregisterDefaultLabel()
366 void UnregisterDefaultLabel(Dali::Accessibility::Accessible* object) override;
369 * @copydoc Dali::Accessibility::Bridge::GetDefaultLabel()
371 Dali::Accessibility::Accessible* GetDefaultLabel(Dali::Accessibility::Accessible* root) const override;
374 * @copydoc Dali::Accessibility::Bridge::GetApplication()
376 Dali::Accessibility::Accessible* GetApplication() const override
378 return &mApplication;
382 * @brief Adds function to dbus interface.
384 template<typename SELF, typename... RET, typename... ARGS>
385 void AddFunctionToInterface(
386 DBus::DBusInterfaceDescription& desc, const std::string& funcName, DBus::ValueOrError<RET...> (SELF::*funcPtr)(ARGS...))
388 if(auto self = dynamic_cast<SELF*>(this))
389 desc.addMethod<DBus::ValueOrError<RET...>(ARGS...)>(
391 [=](ARGS... args) -> DBus::ValueOrError<RET...> {
394 return (self->*funcPtr)(std::move(args)...);
396 catch(std::domain_error& e)
398 return DBus::Error{e.what()};
404 * @brief Adds 'Get' property to dbus interface.
406 template<typename T, typename SELF>
407 void AddGetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
408 const std::string& funcName,
409 T (SELF::*funcPtr)())
411 if(auto self = dynamic_cast<SELF*>(this))
412 desc.addProperty<T>(funcName,
413 [=]() -> DBus::ValueOrError<T> {
416 return (self->*funcPtr)();
418 catch(std::domain_error& e)
420 return DBus::Error{e.what()};
427 * @brief Adds 'Set' property to dbus interface.
429 template<typename T, typename SELF>
430 void AddSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
431 const std::string& funcName,
432 void (SELF::*funcPtr)(T))
434 if(auto self = dynamic_cast<SELF*>(this))
435 desc.addProperty<T>(funcName, {}, [=](T t) -> DBus::ValueOrError<void> {
438 (self->*funcPtr)(std::move(t));
441 catch(std::domain_error& e)
443 return DBus::Error{e.what()};
449 * @brief Adds 'Set' and 'Get' properties to dbus interface.
451 template<typename T, typename T1, typename SELF>
452 void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
453 const std::string& funcName,
454 T1 (SELF::*funcPtrGet)(),
455 DBus::ValueOrError<void> (SELF::*funcPtrSet)(T))
457 if(auto self = dynamic_cast<SELF*>(this))
460 [=]() -> DBus::ValueOrError<T> {
463 return (self->*funcPtrGet)();
465 catch(std::domain_error& e)
467 return DBus::Error{e.what()};
470 [=](T t) -> DBus::ValueOrError<void> {
473 (self->*funcPtrSet)(std::move(t));
476 catch(std::domain_error& e)
478 return DBus::Error{e.what()};
484 * @brief Adds 'Get' and 'Set' properties to dbus interface.
486 template<typename T, typename T1, typename SELF>
487 void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
488 const std::string& funcName,
489 T1 (SELF::*funcPtrGet)(),
490 void (SELF::*funcPtrSet)(T))
492 if(auto self = dynamic_cast<SELF*>(this))
495 [=]() -> DBus::ValueOrError<T> {
498 return (self->*funcPtrGet)();
500 catch(std::domain_error& e)
502 return DBus::Error{e.what()};
505 [=](T t) -> DBus::ValueOrError<void> {
508 (self->*funcPtrSet)(std::move(t));
511 catch(std::domain_error& e)
513 return DBus::Error{e.what()};
519 * @brief Gets the string of the path excluding the specified prefix.
521 * @param path The path to get
522 * @return The string stripped of the specific prefix
524 static std::string StripPrefix(const std::string& path);
527 * @brief Finds the Accessible object according to the path.
529 * @param[in] path The path for Accessible object
530 * @return The Accessible object corresponding to the path
532 Dali::Accessibility::Accessible* Find(const std::string& path) const;
535 * @brief Finds the Accessible object with the given address.
537 * @param[in] ptr The unique Address of the object
538 * @return The Accessible object corresponding to the path
540 Dali::Accessibility::Accessible* Find(const Dali::Accessibility::Address& ptr) const;
543 * @brief Returns the target object of the currently executed DBus method call.
545 * @return The Accessible object
546 * @note When a DBus method is called on some object, this target object (`currentObject`) is temporarily saved by the bridge,
547 * because DBus handles the invocation target separately from the method arguments.
548 * We then use the saved object inside the 'glue' method (e.g. BridgeValue::GetMinimum)
549 * to call the equivalent method on the respective C++ object (this could be ScrollBar::AccessibleImpl::GetMinimum in the example given).
551 Dali::Accessibility::Accessible* FindCurrentObject() const;
554 * @brief Returns the target object of the currently executed DBus method call.
556 * This method tries to downcast the return value of FindCurrentObject() to the requested type,
557 * issuing an error reply to the DBus caller if the requested type is not implemented. Whether
558 * a given type is implemented is decided based on the return value of Accessible::GetInterfaces()
559 * for the current object.
561 * @tparam I The requested AT-SPI interface
562 * @return The Accessible object (cast to a more derived type)
564 * @see FindCurrentObject()
565 * @see Dali::Accessibility::AtspiInterface
566 * @see Dali::Accessibility::AtspiInterfaceType
567 * @see Dali::Accessibility::Accessible::GetInterfaces()
569 template<Dali::Accessibility::AtspiInterface I>
570 auto* FindCurrentObjectWithInterface() const
572 using Type = Dali::Accessibility::AtspiInterfaceType<I>;
575 auto* currentObject = FindCurrentObject();
576 DALI_ASSERT_DEBUG(currentObject); // FindCurrentObject() throws domain_error
578 if(!(result = Dali::Accessibility::Accessible::DownCast<I>(currentObject)))
582 s << "Object " << currentObject->GetAddress().ToString();
583 s << " does not implement ";
584 s << Dali::Accessibility::Accessible::GetInterfaceName(I);
586 throw std::domain_error{s.str()};
593 * @copydoc Dali::Accessibility::Bridge::FindByPath()
595 Dali::Accessibility::Accessible* FindByPath(const std::string& name) const override;
598 * @copydoc Dali::Accessibility::Bridge::SetApplicationName()
600 void SetApplicationName(std::string name) override
602 mApplication.mName = std::move(name);
606 * @copydoc Dali::Accessibility::Bridge::SetToolkitName()
608 void SetToolkitName(std::string_view toolkitName) override
610 mApplication.mToolkitName = std::string{toolkitName};
614 mutable ApplicationAccessible mApplication;
615 std::vector<Dali::Accessibility::Accessible*> mDefaultLabels;
616 bool mIsScreenReaderSuppressed = false;
621 * @param[in] id An ID (integer value)
626 * @brief Gets the ID.
627 * @return The ID to be set
632 * @brief Update registered events.
634 void UpdateRegisteredEvents();
636 using CacheElementType = std::tuple<
637 Dali::Accessibility::Address,
638 Dali::Accessibility::Address,
639 Dali::Accessibility::Address,
640 std::vector<Dali::Accessibility::Address>,
641 std::vector<std::string>,
643 Dali::Accessibility::Role,
645 std::array<uint32_t, 2>>;
648 * @brief Gets Items // NEED TO UPDATE!
652 DBus::ValueOrError<std::vector<CacheElementType>> GetItems();
655 * @brief Creates CacheElement.
657 * CreateCacheElement method works for GetItems which is a part of ATSPI protocol.
658 * ATSPI client library (libatspi from at-spi2-core) depending on cacheing policy configuration uses GetItems
659 * to pre-load entire accessible tree from application to its own cache in single dbus call.
660 * Otherwise the particular nodes in a tree are cached lazily when client library tries to access them.
661 * @param item Accessible to get information
662 * @return The elements to be cached
664 CacheElementType CreateCacheElement(Dali::Accessibility::Accessible* item);
668 virtual ~BridgeBase();
671 * @copydoc Dali::Accessibility::Bridge::ForceUp()
673 ForceUpResult ForceUp() override;
676 * @copydoc Dali::Accessibility::Bridge::ForceDown()
678 void ForceDown() override;
680 DBus::DBusServer mDbusServer;
681 DBusWrapper::ConnectionPtr mConnectionPtr;
683 DBus::DBusClient mRegistry;
684 bool IsBoundsChangedEventAllowed{false};
687 #endif // DALI_INTERNAL_ACCESSIBILITY_BRIDGE_BASE_H