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 Dali::Accessibility::Accessible* GetChildAtIndex(size_t index) override
68 auto size = mChildren.size();
71 throw std::domain_error{"invalid index " + std::to_string(index) + " for object with " + std::to_string(size) + " children"};
73 return mChildren[index];
76 size_t GetIndexInParent() override
78 throw std::domain_error{"can't call GetIndexInParent on application object"};
81 Dali::Accessibility::Role GetRole() const override
83 return Dali::Accessibility::Role::APPLICATION;
86 Dali::Accessibility::States GetStates() override
91 Dali::Accessibility::Attributes GetAttributes() const override
97 * @brief Gets the Accessible object from the window.
99 * @param[in] window The window to find
100 * @return Null if mChildren is empty, otherwise the Accessible object
101 * @note Currently, the default window would be returned when mChildren is not empty.
103 Dali::Accessibility::Accessible* GetWindowAccessible(Dali::Window window)
105 if(mChildren.empty())
110 Dali::Layer rootLayer = window.GetRootLayer();
112 // Find a child which is related to the window.
113 for(auto i = 0u; i < mChildren.size(); ++i)
115 if(rootLayer == mChildren[i]->GetInternalActor())
121 // If can't find its children, return the default window.
125 bool DoGesture(const Dali::Accessibility::GestureInfo& gestureInfo) override
130 std::vector<Dali::Accessibility::Relation> GetRelationSet() override
135 Dali::Actor GetInternalActor() override
137 return Dali::Actor{};
140 Dali::Accessibility::Address GetAddress() const override
145 std::string GetToolkitName() const override
150 std::string GetVersion() const override
152 return std::to_string(Dali::ADAPTOR_MAJOR_VERSION) + "." + std::to_string(Dali::ADAPTOR_MINOR_VERSION);
157 * @brief Enumeration for FilteredEvents.
159 enum class FilteredEvents
161 BOUNDS_CHANGED ///< Bounds changed
164 // Custom specialization of std::hash
168 struct hash<std::pair<FilteredEvents, Dali::Accessibility::Accessible*>>
170 size_t operator()(std::pair<FilteredEvents, Dali::Accessibility::Accessible*> value) const
172 return (static_cast<size_t>(value.first) * 131) ^ reinterpret_cast<size_t>(value.second);
178 * @brief The BridgeBase class is basic class for Bridge functions.
180 class BridgeBase : public Dali::Accessibility::Bridge, public Dali::ConnectionTracker
182 std::unordered_map<std::pair<FilteredEvents, Dali::Accessibility::Accessible*>, std::pair<unsigned int, std::function<void()>>> mFilteredEvents;
185 * @brief Removes all FilteredEvents using Tick signal.
187 * @return False if mFilteredEvents is empty, otherwise true.
189 bool TickFilteredEvents();
193 * @brief Adds FilteredEvents, Accessible, and delay time to mFilteredEvents.
195 * @param[in] kind FilteredEvents enum value
196 * @param[in] obj Accessible object
197 * @param[in] delay The delay time
198 * @param[in] functor The function to be called // NEED TO UPDATE!
200 void AddFilteredEvent(FilteredEvents kind, Dali::Accessibility::Accessible* obj, float delay, std::function<void()> functor);
203 * @brief Callback when the visibility of the window is changed.
205 * @param[in] window The window to be changed
206 * @param[in] visible The visibility of the window
208 void OnWindowVisibilityChanged(Dali::Window window, bool visible);
211 * @brief Callback when the window focus is changed.
213 * @param[in] window The window whose focus is changed
214 * @param[in] focusIn Whether the focus is in/out
216 void OnWindowFocusChanged(Dali::Window window, bool focusIn);
219 * @copydoc Dali::Accessibility::Bridge::GetBusName()
221 const std::string& GetBusName() const override;
224 * @copydoc Dali::Accessibility::Bridge::AddTopLevelWindow()
226 void AddTopLevelWindow(Dali::Accessibility::Accessible* windowAccessible) override;
229 * @copydoc Dali::Accessibility::Bridge::RemoveTopLevelWindow()
231 void RemoveTopLevelWindow(Dali::Accessibility::Accessible* windowAccessible) override;
234 * @copydoc Dali::Accessibility::Bridge::RegisterDefaultLabel()
236 void RegisterDefaultLabel(Dali::Accessibility::Accessible* object) override;
239 * @copydoc Dali::Accessibility::Bridge::UnregisterDefaultLabel()
241 void UnregisterDefaultLabel(Dali::Accessibility::Accessible* object) override;
244 * @copydoc Dali::Accessibility::Bridge::GetDefaultLabel()
246 Dali::Accessibility::Accessible* GetDefaultLabel() const override
248 return mDefaultLabels.empty() ? nullptr : mDefaultLabels.back();
252 * @copydoc Dali::Accessibility::Bridge::GetApplication()
254 Dali::Accessibility::Accessible* GetApplication() const override
256 return &mApplication;
260 * @brief Adds function to dbus interface.
262 template<typename SELF, typename... RET, typename... ARGS>
263 void AddFunctionToInterface(
264 DBus::DBusInterfaceDescription& desc, const std::string& funcName, DBus::ValueOrError<RET...> (SELF::*funcPtr)(ARGS...))
266 if(auto self = dynamic_cast<SELF*>(this))
267 desc.addMethod<DBus::ValueOrError<RET...>(ARGS...)>(
269 [=](ARGS... args) -> DBus::ValueOrError<RET...> {
272 return (self->*funcPtr)(std::move(args)...);
274 catch(std::domain_error& e)
276 return DBus::Error{e.what()};
282 * @brief Adds 'Get' property to dbus interface.
284 template<typename T, typename SELF>
285 void AddGetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
286 const std::string& funcName,
287 T (SELF::*funcPtr)())
289 if(auto self = dynamic_cast<SELF*>(this))
290 desc.addProperty<T>(funcName,
291 [=]() -> DBus::ValueOrError<T> {
294 return (self->*funcPtr)();
296 catch(std::domain_error& e)
298 return DBus::Error{e.what()};
305 * @brief Adds 'Set' property to dbus interface.
307 template<typename T, typename SELF>
308 void AddSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
309 const std::string& funcName,
310 void (SELF::*funcPtr)(T))
312 if(auto self = dynamic_cast<SELF*>(this))
313 desc.addProperty<T>(funcName, {}, [=](T t) -> DBus::ValueOrError<void> {
316 (self->*funcPtr)(std::move(t));
319 catch(std::domain_error& e)
321 return DBus::Error{e.what()};
327 * @brief Adds 'Set' and 'Get' properties to dbus interface.
329 template<typename T, typename T1, typename SELF>
330 void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
331 const std::string& funcName,
332 T1 (SELF::*funcPtrGet)(),
333 DBus::ValueOrError<void> (SELF::*funcPtrSet)(T))
335 if(auto self = dynamic_cast<SELF*>(this))
338 [=]() -> DBus::ValueOrError<T> {
341 return (self->*funcPtrGet)();
343 catch(std::domain_error& e)
345 return DBus::Error{e.what()};
348 [=](T t) -> DBus::ValueOrError<void> {
351 (self->*funcPtrSet)(std::move(t));
354 catch(std::domain_error& e)
356 return DBus::Error{e.what()};
362 * @brief Adds 'Get' and 'Set' properties to dbus interface.
364 template<typename T, typename T1, typename SELF>
365 void AddGetSetPropertyToInterface(DBus::DBusInterfaceDescription& desc,
366 const std::string& funcName,
367 T1 (SELF::*funcPtrGet)(),
368 void (SELF::*funcPtrSet)(T))
370 if(auto self = dynamic_cast<SELF*>(this))
373 [=]() -> DBus::ValueOrError<T> {
376 return (self->*funcPtrGet)();
378 catch(std::domain_error& e)
380 return DBus::Error{e.what()};
383 [=](T t) -> DBus::ValueOrError<void> {
386 (self->*funcPtrSet)(std::move(t));
389 catch(std::domain_error& e)
391 return DBus::Error{e.what()};
397 * @brief Gets the string of the path excluding the specified prefix.
399 * @param path The path to get
400 * @return The string stripped of the specific prefix
402 static std::string StripPrefix(const std::string& path);
405 * @brief Finds the Accessible object according to the path.
407 * @param[in] path The path for Accessible object
408 * @return The Accessible object corresponding to the path
410 Dali::Accessibility::Accessible* Find(const std::string& path) const;
413 * @brief Finds the Accessible object with the given address.
415 * @param[in] ptr The unique Address of the object
416 * @return The Accessible object corresponding to the path
418 Dali::Accessibility::Accessible* Find(const Dali::Accessibility::Address& ptr) const;
421 * @brief Returns the target object of the currently executed DBus method call.
423 * And any subclasses redefine `FindSelf` with a different return type as a convenient wrapper around dynamic_cast.
424 * @return The Accessible object
425 * @note When a DBus method is called on some object, this target object (`currentObject`) is temporarily saved by the bridge,
426 * because DBus handles the invocation target separately from the method arguments.
427 * We then use the saved object inside the 'glue' method (e.g. BridgeValue::GetMinimum)
428 * to call the equivalent method on the respective C++ object (this could be ScrollBar::AccessibleImpl::GetMinimum in the example given).
430 Dali::Accessibility::Accessible* FindSelf() const;
433 * @copydoc Dali::Accessibility::Bridge::FindByPath()
435 Dali::Accessibility::Accessible* FindByPath(const std::string& name) const override;
438 * @copydoc Dali::Accessibility::Bridge::SetApplicationName()
440 void SetApplicationName(std::string name) override
442 mApplication.mName = std::move(name);
446 mutable ApplicationAccessible mApplication;
447 std::vector<Dali::Accessibility::Accessible*> mDefaultLabels;
448 bool mIsScreenReaderSuppressed = false;
453 * @param[in] id An ID (integer value)
458 * @brief Gets the ID.
459 * @return The ID to be set
464 * @brief Update registered events.
466 void UpdateRegisteredEvents();
468 using CacheElementType = std::tuple<
469 Dali::Accessibility::Address,
470 Dali::Accessibility::Address,
471 Dali::Accessibility::Address,
472 std::vector<Dali::Accessibility::Address>,
473 std::vector<std::string>,
475 Dali::Accessibility::Role,
477 std::array<uint32_t, 2>>;
480 * @brief Gets Items // NEED TO UPDATE!
484 DBus::ValueOrError<std::vector<CacheElementType>> GetItems();
487 * @brief Creates CacheElement.
489 * CreateCacheElement method works for GetItems which is a part of ATSPI protocol.
490 * ATSPI client library (libatspi from at-spi2-core) depending on cacheing policy configuration uses GetItems
491 * to pre-load entire accessible tree from application to its own cache in single dbus call.
492 * Otherwise the particular nodes in a tree are cached lazily when client library tries to access them.
493 * @param item Accessible to get information
494 * @return The elements to be cached
496 CacheElementType CreateCacheElement(Dali::Accessibility::Accessible* item);
500 virtual ~BridgeBase();
503 * @copydoc Dali::Accessibility::Bridge::ForceUp()
505 ForceUpResult ForceUp() override;
508 * @copydoc Dali::Accessibility::Bridge::ForceDown()
510 void ForceDown() override;
512 DBus::DBusServer mDbusServer;
513 DBusWrapper::ConnectionPtr mConnectionPtr;
515 DBus::DBusClient mRegistry;
516 bool IsBoundsChangedEventAllowed{false};
519 #endif // DALI_INTERNAL_ACCESSIBILITY_BRIDGE_BASE_H