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;
43 std::vector<Dali::Window> mWindows;
46 std::string GetName() const override
51 std::string GetDescription() const override
56 Dali::Accessibility::Accessible* GetParent() override
61 size_t GetChildCount() const override
63 return mChildren.size();
66 std::vector<Dali::Accessibility::Accessible*> GetChildren() override
71 Dali::Accessibility::Accessible* GetChildAtIndex(size_t index) override
73 auto size = mChildren.size();
76 throw std::domain_error{"invalid index " + std::to_string(index) + " for object with " + std::to_string(size) + " children"};
78 return mChildren[index];
81 size_t GetIndexInParent() override
83 throw std::domain_error{"can't call GetIndexInParent on application object"};
86 Dali::Accessibility::Role GetRole() const override
88 return Dali::Accessibility::Role::APPLICATION;
91 Dali::Accessibility::States GetStates() override
96 Dali::Accessibility::Attributes GetAttributes() const override
102 * @brief Gets the Accessible object from the window.
104 * @param[in] window The window to find
105 * @return Null if mChildren is empty, otherwise the Accessible object
106 * @note Currently, the default window would be returned when mChildren is not empty.
108 Dali::Accessibility::Accessible* GetWindowAccessible(Dali::Window window)
110 if(mChildren.empty())
115 Dali::Layer rootLayer = window.GetRootLayer();
117 // Find a child which is related to the window.
118 for(auto i = 0u; i < mChildren.size(); ++i)
120 if(rootLayer == mChildren[i]->GetInternalActor())
126 // If can't find its children, return the default window.
130 bool DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo) override
135 std::vector<Dali::Accessibility::Relation> GetRelationSet() override
140 Dali::Actor GetInternalActor() override
142 return Dali::Actor{};
145 Dali::Accessibility::Address GetAddress() const override
150 std::string GetToolkitName() const override
155 std::string GetVersion() const override
157 return std::to_string(Dali::ADAPTOR_MAJOR_VERSION) + "." + std::to_string(Dali::ADAPTOR_MINOR_VERSION);
162 * @brief Enumeration for FilteredEvents.
164 enum class FilteredEvents
166 BOUNDS_CHANGED ///< Bounds changed
169 // Custom specialization of std::hash
173 struct hash<std::pair<FilteredEvents, Dali::Accessibility::Accessible*>>
175 size_t operator()(std::pair<FilteredEvents, Dali::Accessibility::Accessible*> value) const
177 return (static_cast<size_t>(value.first) * 131) ^ reinterpret_cast<size_t>(value.second);
183 * @brief The BridgeBase class is basic class for Bridge functions.
185 class BridgeBase : public Dali::Accessibility::Bridge, public Dali::ConnectionTracker
187 std::unordered_map<std::pair<FilteredEvents, Dali::Accessibility::Accessible*>, std::pair<unsigned int, std::function<void()>>> mFilteredEvents;
190 * @brief Removes all FilteredEvents using Tick signal.
192 * @return False if mFilteredEvents is empty, otherwise true.
194 bool TickFilteredEvents();
198 * @brief Adds FilteredEvents, Accessible, and delay time to mFilteredEvents.
200 * @param[in] kind FilteredEvents enum value
201 * @param[in] obj Accessible object
202 * @param[in] delay The delay time
203 * @param[in] functor The function to be called // NEED TO UPDATE!
205 void AddFilteredEvent(FilteredEvents kind, Dali::Accessibility::Accessible* obj, float delay, std::function<void()> functor);
208 * @brief Callback when the visibility of the window is changed.
210 * @param[in] window The window to be changed
211 * @param[in] visible The visibility of the window
213 void OnWindowVisibilityChanged(Dali::Window window, bool visible);
216 * @brief Callback when the window focus is changed.
218 * @param[in] window The window whose focus is changed
219 * @param[in] focusIn Whether the focus is in/out
221 void OnWindowFocusChanged(Dali::Window window, bool focusIn);
224 * @copydoc Dali::Accessibility::Bridge::GetBusName()
226 const std::string& GetBusName() const override;
229 * @copydoc Dali::Accessibility::Bridge::AddTopLevelWindow()
231 void AddTopLevelWindow(Dali::Accessibility::Accessible* windowAccessible) override;
234 * @copydoc Dali::Accessibility::Bridge::RemoveTopLevelWindow()
236 void RemoveTopLevelWindow(Dali::Accessibility::Accessible* windowAccessible) override;
239 * @copydoc Dali::Accessibility::Bridge::RegisterDefaultLabel()
241 void RegisterDefaultLabel(Dali::Accessibility::Accessible* object) override;
244 * @copydoc Dali::Accessibility::Bridge::UnregisterDefaultLabel()
246 void UnregisterDefaultLabel(Dali::Accessibility::Accessible* object) override;
249 * @copydoc Dali::Accessibility::Bridge::GetDefaultLabel()
251 Dali::Accessibility::Accessible* GetDefaultLabel() const override
253 return mDefaultLabels.empty() ? nullptr : mDefaultLabels.back();
257 * @copydoc Dali::Accessibility::Bridge::GetApplication()
259 Dali::Accessibility::Accessible* GetApplication() const override
261 return &mApplication;
265 * @brief Adds function to dbus interface.
267 template<typename SELF, typename... RET, typename... ARGS>
268 void AddFunctionToInterface(
269 DBus::DBusInterfaceDescription& desc, const std::string& funcName, DBus::ValueOrError<RET...> (SELF::*funcPtr)(ARGS...))
271 if(auto self = dynamic_cast<SELF*>(this))
272 desc.addMethod<DBus::ValueOrError<RET...>(ARGS...)>(
274 [=](ARGS... args) -> DBus::ValueOrError<RET...> {
277 return (self->*funcPtr)(std::move(args)...);
279 catch(std::domain_error& e)
281 return DBus::Error{e.what()};
287 * @brief Adds 'Get' property to dbus interface.
289 template<typename T, typename SELF>
290 void AddGetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
291 const std::string& funcName,
292 T (SELF::*funcPtr)())
294 if(auto self = dynamic_cast<SELF*>(this))
295 desc.addProperty<T>(funcName,
296 [=]() -> DBus::ValueOrError<T> {
299 return (self->*funcPtr)();
301 catch(std::domain_error& e)
303 return DBus::Error{e.what()};
310 * @brief Adds 'Set' property to dbus interface.
312 template<typename T, typename SELF>
313 void AddSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
314 const std::string& funcName,
315 void (SELF::*funcPtr)(T))
317 if(auto self = dynamic_cast<SELF*>(this))
318 desc.addProperty<T>(funcName, {}, [=](T t) -> DBus::ValueOrError<void> {
321 (self->*funcPtr)(std::move(t));
324 catch(std::domain_error& e)
326 return DBus::Error{e.what()};
332 * @brief Adds 'Set' and 'Get' properties to dbus interface.
334 template<typename T, typename T1, typename SELF>
335 void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
336 const std::string& funcName,
337 T1 (SELF::*funcPtrGet)(),
338 DBus::ValueOrError<void> (SELF::*funcPtrSet)(T))
340 if(auto self = dynamic_cast<SELF*>(this))
343 [=]() -> DBus::ValueOrError<T> {
346 return (self->*funcPtrGet)();
348 catch(std::domain_error& e)
350 return DBus::Error{e.what()};
353 [=](T t) -> DBus::ValueOrError<void> {
356 (self->*funcPtrSet)(std::move(t));
359 catch(std::domain_error& e)
361 return DBus::Error{e.what()};
367 * @brief Adds 'Get' and 'Set' properties to dbus interface.
369 template<typename T, typename T1, typename SELF>
370 void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
371 const std::string& funcName,
372 T1 (SELF::*funcPtrGet)(),
373 void (SELF::*funcPtrSet)(T))
375 if(auto self = dynamic_cast<SELF*>(this))
378 [=]() -> DBus::ValueOrError<T> {
381 return (self->*funcPtrGet)();
383 catch(std::domain_error& e)
385 return DBus::Error{e.what()};
388 [=](T t) -> DBus::ValueOrError<void> {
391 (self->*funcPtrSet)(std::move(t));
394 catch(std::domain_error& e)
396 return DBus::Error{e.what()};
402 * @brief Gets the string of the path excluding the specified prefix.
404 * @param path The path to get
405 * @return The string stripped of the specific prefix
407 static std::string StripPrefix(const std::string& path);
410 * @brief Finds the Accessible object according to the path.
412 * @param[in] path The path for Accessible object
413 * @return The Accessible object corresponding to the path
415 Dali::Accessibility::Accessible* Find(const std::string& path) const;
418 * @brief Finds the Accessible object with the given address.
420 * @param[in] ptr The unique Address of the object
421 * @return The Accessible object corresponding to the path
423 Dali::Accessibility::Accessible* Find(const Dali::Accessibility::Address& ptr) const;
426 * @brief Returns the target object of the currently executed DBus method call.
428 * @return The Accessible object
429 * @note When a DBus method is called on some object, this target object (`currentObject`) is temporarily saved by the bridge,
430 * because DBus handles the invocation target separately from the method arguments.
431 * We then use the saved object inside the 'glue' method (e.g. BridgeValue::GetMinimum)
432 * to call the equivalent method on the respective C++ object (this could be ScrollBar::AccessibleImpl::GetMinimum in the example given).
434 Dali::Accessibility::Accessible* FindCurrentObject() const;
437 * @brief Returns the target object of the currently executed DBus method call.
439 * This method tries to downcast the return value of FindCurrentObject() to the requested type,
440 * issuing an error reply to the DBus caller if the requested type is not implemented. Whether
441 * a given type is implemented is decided based on the return value of Accessible::GetInterfaces()
442 * for the current object.
444 * @tparam I The requested AT-SPI interface
445 * @return The Accessible object (cast to a more derived type)
447 * @see FindCurrentObject()
448 * @see Dali::Accessibility::AtspiInterface
449 * @see Dali::Accessibility::AtspiInterfaceType
450 * @see Dali::Accessibility::Accessible::GetInterfaces()
452 template<Dali::Accessibility::AtspiInterface I>
453 auto* FindCurrentObjectWithInterface() const
455 using Type = Dali::Accessibility::AtspiInterfaceType<I>;
458 auto* currentObject = FindCurrentObject();
459 DALI_ASSERT_DEBUG(currentObject); // FindCurrentObject() throws domain_error
461 if(!(result = Dali::Accessibility::Accessible::DownCast<I>(currentObject)))
465 s << "Object " << currentObject->GetAddress().ToString();
466 s << " does not implement ";
467 s << Dali::Accessibility::Accessible::GetInterfaceName(I);
469 throw std::domain_error{s.str()};
476 * @copydoc Dali::Accessibility::Bridge::FindByPath()
478 Dali::Accessibility::Accessible* FindByPath(const std::string& name) const override;
481 * @copydoc Dali::Accessibility::Bridge::SetApplicationName()
483 void SetApplicationName(std::string name) override
485 mApplication.mName = std::move(name);
489 mutable ApplicationAccessible mApplication;
490 std::vector<Dali::Accessibility::Accessible*> mDefaultLabels;
491 bool mIsScreenReaderSuppressed = false;
496 * @param[in] id An ID (integer value)
501 * @brief Gets the ID.
502 * @return The ID to be set
507 * @brief Update registered events.
509 void UpdateRegisteredEvents();
511 using CacheElementType = std::tuple<
512 Dali::Accessibility::Address,
513 Dali::Accessibility::Address,
514 Dali::Accessibility::Address,
515 std::vector<Dali::Accessibility::Address>,
516 std::vector<std::string>,
518 Dali::Accessibility::Role,
520 std::array<uint32_t, 2>>;
523 * @brief Gets Items // NEED TO UPDATE!
527 DBus::ValueOrError<std::vector<CacheElementType>> GetItems();
530 * @brief Creates CacheElement.
532 * CreateCacheElement method works for GetItems which is a part of ATSPI protocol.
533 * ATSPI client library (libatspi from at-spi2-core) depending on cacheing policy configuration uses GetItems
534 * to pre-load entire accessible tree from application to its own cache in single dbus call.
535 * Otherwise the particular nodes in a tree are cached lazily when client library tries to access them.
536 * @param item Accessible to get information
537 * @return The elements to be cached
539 CacheElementType CreateCacheElement(Dali::Accessibility::Accessible* item);
543 virtual ~BridgeBase();
546 * @copydoc Dali::Accessibility::Bridge::ForceUp()
548 ForceUpResult ForceUp() override;
551 * @copydoc Dali::Accessibility::Bridge::ForceDown()
553 void ForceDown() override;
555 DBus::DBusServer mDbusServer;
556 DBusWrapper::ConnectionPtr mConnectionPtr;
558 DBus::DBusClient mRegistry;
559 bool IsBoundsChangedEventAllowed{false};
562 #endif // DALI_INTERNAL_ACCESSIBILITY_BRIDGE_BASE_H