1 #ifndef DALI_ADAPTOR_ACCESSIBILITY_BRIDGE_H
2 #define DALI_ADAPTOR_ACCESSIBILITY_BRIDGE_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/actor.h>
23 #include <dali/public-api/math/rect.h>
28 #include <unordered_set>
31 #include <dali/devel-api/adaptor-framework/accessibility.h>
32 #include <dali/public-api/adaptor-framework/window.h>
36 namespace Accessibility
39 class ProxyAccessible;
42 * @brief Base class for different accessibility bridges.
44 * Bridge is resposible for initializing and managing connection on accessibility bus.
45 * Accessibility clients will not get any information about UI without initialized and upraised bridge.
46 * Concrete implementation depends on the accessibility technology available on the platform.
48 * @note This class is singleton.
50 struct DALI_ADAPTOR_API Bridge
52 enum class ForceUpResult
62 virtual ~Bridge() = default;
65 * @brief Gets bus name which bridge is initialized on.
67 * @return The bus name
69 virtual const std::string& GetBusName() const = 0;
72 * @brief Registers top level window.
74 * Hierarchy of objects visible for accessibility clients is based on tree-like
75 * structure created from Actors objects. This method allows to connect chosen
76 * object as direct ancestor of application and therefore make it visible for
77 * accessibility clients.
79 * @param[in] object The accessible object
81 virtual void AddTopLevelWindow(Accessible* object) = 0;
84 * @brief Removes top level window.
86 * Hierarchy of objects visible for accessibility clients is based on tree-like
87 * structure created from Actors objects. This method removes previously added
88 * window from visible accessibility objects.
90 * @param[in] object The accessible object
92 virtual void RemoveTopLevelWindow(Accessible* object) = 0;
95 * @brief Adds object on the top of the stack of "default label" sourcing objects.
97 * @see GetDefaultLabel
99 * @param[in] object The accessible object
101 virtual void RegisterDefaultLabel(Accessible* object) = 0;
104 * @brief Removes object from the stack of "default label" sourcing objects.
106 * @see GetDefaultLabel
108 * @param[in] object The accessible object
110 virtual void UnregisterDefaultLabel(Accessible* object) = 0;
113 * @brief Gets the top-most object from the stack of "default label" sourcing objects.
115 * The "default label" is a reading material (text) derived from an accesibility object
116 * that could be read by screen-reader immediately after the navigation context has changed
117 * (window activates, popup shows up, tab changes) and before first UI element is highlighted.
119 * @return The handler to accessibility object
120 * @note This is a Tizen only feature not present in upstream ATSPI.
121 * Feature can be enabled/disabled for particular context root object
122 * by setting value of its accessibility attribute "default_label".
123 * Following strings are valid values for "default_label" attribute: "enabled", "disabled".
124 * Any other value will be interpreted as "enabled".
126 virtual Accessible* GetDefaultLabel() const = 0;
129 * @brief Sets name of current application which will be visible on accessibility bus.
131 * @param[in] name The application name
133 virtual void SetApplicationName(std::string name) = 0;
136 * @brief Sets the name of the GUI toolkit that AT-SPI clients can query.
138 * The default name is "dali".
140 * @param toolkitName The toolkit name
142 virtual void SetToolkitName(std::string_view toolkitName) = 0;
145 * @brief Gets object being root of accessibility tree.
147 * @return handler to accessibility object
149 virtual Accessible* GetApplication() const = 0;
152 * @brief Finds an object in accessibility tree.
154 * @param[in] path The path to object
156 * @return The handler to accessibility object
158 virtual Accessible* FindByPath(const std::string& path) const = 0;
161 * @brief Notifies accessibility dbus that window has just been shown.
163 * @param[in] window The window to be shown
165 virtual void WindowShown(Window window) = 0;
168 * @brief Notifies accessibility dbus that window has just been hidden.
170 * @param[in] window The window to be hidden
172 virtual void WindowHidden(Window window) = 0;
175 * @brief Notifies accessibility dbus that window has just been focused.
177 * @param[in] window The window to be focused
179 virtual void WindowFocused(Window window) = 0;
182 * @brief Notifies accessibility dbus that window has just been out of focus.
184 * @param[in] window The window to be out of focus
186 virtual void WindowUnfocused(Window window) = 0;
189 * @brief Initializes accessibility bus.
191 virtual void Initialize() = 0;
194 * @brief Terminates accessibility bus.
196 virtual void Terminate() = 0;
199 * @brief This method is called, when bridge is being activated.
201 virtual ForceUpResult ForceUp()
205 return ForceUpResult::ALREADY_UP;
207 mData = std::make_shared<Data>();
208 mData->mBridge = this;
209 return ForceUpResult::JUST_STARTED;
213 * @brief This method is called, when bridge is being deactivated.
215 virtual void ForceDown() = 0;
218 * @brief Checks if bridge is activated or not.
219 * @return True if brige is activated.
227 * @brief Emits cursor-moved event on at-spi bus.
229 * @param[in] obj The accessible object
230 * @param[in] cursorPosition The new cursor position
232 virtual void EmitCursorMoved(Accessible* obj, unsigned int cursorPosition) = 0;
235 * @brief Emits active-descendant-changed event on at-spi bus.
237 * @param[in] obj The accessible object
238 * @param[in] child The child of the object
240 virtual void EmitActiveDescendantChanged(Accessible* obj, Accessible* child) = 0;
243 * @brief Emits text-changed event on at-spi bus.
245 * @param[in] obj The accessible object
246 * @param[in] state The changed state for text, such as Inserted or Deleted
247 * @param[in] position The cursor position
248 * @param[in] length The text length
249 * @param[in] content The changed text
251 virtual void EmitTextChanged(Accessible* obj, TextChangedState state, unsigned int position, unsigned int length, const std::string& content) = 0;
254 * @brief Emits MoveOuted event on at-spi bus.
256 * @param[in] obj Accessible object
257 * @param[in] type Direction type when an Accessible object moves out of screen
259 virtual void EmitMovedOutOfScreen(Accessible* obj, ScreenRelativeMoveType type) = 0;
262 * @brief Emits "org.a11y.atspi.Socket.Available" event on AT-SPI bus.
264 * @param obj Accessible object
266 virtual void EmitSocketAvailable(Accessible* obj) = 0;
269 * @brief Emits state-changed event on at-spi bus.
271 * @param[in] obj The accessible object
272 * @param[in] state The accessibility state (SHOWING, HIGHLIGHTED, etc)
273 * @param[in] newValue Whether the state value is changed to new value or not.
274 * @param[in] reserved Reserved. (Currently, this argument is not implemented in dali)
276 virtual void EmitStateChanged(Accessible* obj, State state, int newValue, int reserved = 0) = 0;
279 * @brief Emits window event on at-spi bus.
281 * @param[in] obj The accessible object
282 * @param[in] event The enumerated window event
283 * @param[in] detail The additional parameter which interpretation depends on chosen event
285 virtual void Emit(Accessible* obj, WindowEvent event, unsigned int detail = 0) = 0;
288 * @brief Emits property-changed event on at-spi bus.
290 * @param[in] obj The accessible object
291 * @param[in] event Property changed event
293 virtual void Emit(Accessible* obj, ObjectPropertyChangeEvent event) = 0;
296 * @brief Emits bounds-changed event on at-spi bus.
298 * @param[in] obj The accessible object
299 * @param[in] rect The rectangle for changed bounds
301 virtual void EmitBoundsChanged(Accessible* obj, Rect<> rect) = 0;
304 * @brief Emits key event on at-spi bus.
306 * Screen-reader might receive this event and reply, that given keycode is consumed. In that case
307 * further processing of the keycode should be ignored.
309 * @param[in] type Key event type
310 * @param[in] keyCode Key code
311 * @param[in] keyName Key name
312 * @param[in] timeStamp Time stamp
313 * @param[in] isText Whether it's text or not
314 * @return Whether this event is consumed or not
316 virtual Consumed Emit(KeyEventType type, unsigned int keyCode, const std::string& keyName, unsigned int timeStamp, bool isText) = 0;
319 * @brief Reads given text by screen reader
321 * @param[in] text The text to read
322 * @param[in] discardable If TRUE, reading can be discarded by subsequent reading requests,
323 * if FALSE the reading must finish before next reading request can be started
324 * @param[in] callback the callback function that is called on reading signals emitted
325 * during processing of this reading request.
326 * Callback can be one of the following signals:
327 * ReadingCancelled, ReadingStopped, ReadingSkipped
329 virtual void Say(const std::string& text, bool discardable, std::function<void(std::string)> callback) = 0;
332 * @brief Force accessibility client to pause.
334 virtual void Pause() = 0;
337 * @brief Force accessibility client to resume.
339 virtual void Resume() = 0;
342 * @brief Cancels anything screen-reader is reading / has queued to read
344 * @param[in] alsoNonDiscardable whether to cancel non-discardable readings as well
346 virtual void StopReading(bool alsoNonDiscardable) = 0;
349 * @brief Suppresses reading of screen-reader
351 * @param[in] suppress whether to suppress reading of screen-reader
353 virtual void SuppressScreenReader(bool suppress) = 0;
356 * @brief Gets screen reader status.
358 * @return True if screen reader is enabled
360 virtual bool GetScreenReaderEnabled() = 0;
363 * @brief Gets ATSPI status.
365 * @return True if ATSPI is enabled
367 virtual bool IsEnabled() = 0;
370 * @brief Calls socket.Embed(plug) via D-Bus.
372 * @param[in] plug The plug
373 * @param[in] socket The socket
375 * @return Address returned by the D-Bus call.
377 * @note Remote object pointed to by 'socket' must implement 'org.a11y.atspi.Socket'.
378 * @see UnembedSocket()
380 virtual Address EmbedSocket(const Address& plug, const Address& socket) = 0;
383 * @brief Calls socket.Embedded(plug) via D-Bus.
385 * The "Embedded" D-Bus method is an ATK extension.
386 * See 'impl_Embedded' in AT_SPI2_ATK/atk-adaptor/adaptors/socket-adaptor.c for more information.
388 * @param[in] plug The plug
389 * @param[in] socket The socket
391 virtual void EmbedAtkSocket(const Address& plug, const Address& socket) = 0;
394 * @brief Calls socket.Unmbed(plug) via D-Bus.
396 * @param[in] plug The plug
397 * @param[in] socket The socket
399 * @note Remote object pointed to by 'socket' must implement 'org.a11y.atspi.Socket'.
402 virtual void UnembedSocket(const Address& plug, const Address& socket) = 0;
405 * @brief Calls socket.SetOffset(x, y) via D-Bus.
407 * The "SetOffset" D-Bus method is a DALi extension. It can be used to inform a DALi widget about
408 * its position on the screen.
410 * @param[in] socket The socket
411 * @param[in] x Horizontal offset
412 * @param[in] y Vertical offset
414 * @note Remote object pointed to by 'socket' must implement 'org.a11y.atspi.Socket'.
416 * @see SetExtentsOffset()
418 virtual void SetSocketOffset(ProxyAccessible* socket, std::int32_t x, std::int32_t y) = 0;
421 * @brief Sets the global extents offset.
423 * This offset will be added during serialization of GetExtents() return value to D-Bus.
424 * Local calls to GetExtents() are unaffected.
426 * @param[in] x Horizontal offset
427 * @param[in] y Vertical offset
429 * @see SetSocketOffset()
430 * @see Dali::Accessibility::Component::GetExtents()
432 virtual void SetExtentsOffset(std::int32_t x, std::int32_t y) = 0;
435 * @brief Sets the preferred bus name.
437 * If the Bridge is enabled, it will immediately release the previous name and request the new one.
439 * Otherwise, the Bridge will request this name on AT-SPI activation (and release it on deactivation).
440 * It is up to the caller to determine whether a given name will be available in the system.
442 * @param preferredBusName The preferred bus name
444 virtual void SetPreferredBusName(std::string_view preferredBusName) = 0;
447 * @brief Returns instance of bridge singleton object.
449 * @return The current bridge object
451 static std::shared_ptr<Bridge> GetCurrentBridge();
454 * @brief Blocks auto-initialization of AT-SPI bridge
456 * Use this only if your application starts before DBus does, and call it early in main()
457 * (before GetCurrentBridge() is called by anyone). GetCurrentBridge() will then return an
458 * instance of DummyBridge.
460 * When DBus is ready, call EnableAutoInit(). Please note that GetCurrentBridge() may still
461 * return an instance of DummyBridge if AT-SPI was disabled at compile time or using an
462 * environment variable, or if creating the real bridge failed.
464 * @see Dali::Accessibility::DummyBridge
465 * @see Dali::Accessibility::Bridge::EnableAutoInit
467 static void DisableAutoInit();
470 * @brief Re-enables auto-initialization of AT-SPI bridge
472 * Normal applications do not have to call this function. GetCurrentBridge() tries to
473 * initialize the AT-SPI bridge when it is called for the first time.
475 * @see Dali::Accessibility::Bridge::DisableAutoInit
476 * @see Dali::Accessibility::Bridge::AddTopLevelWindow
477 * @see Dali::Accessibility::Bridge::SetApplicationName
479 static void EnableAutoInit();
482 * @brief Encodes a widget ID as a usable bus name.
484 * @param widgetInstanceId The instance ID of a widget
485 * @return std::string Encoded bus name
487 * @see SetPreferredBusName
489 static std::string MakeBusNameForWidget(std::string_view widgetInstanceId);
491 static Signal<void()>& EnabledSignal()
493 return mEnabledSignal;
496 static Signal<void()>& DisabledSignal()
498 return mDisabledSignal;
501 static Signal<void()>& ScreenReaderEnabledSignal()
503 return mScreenReaderEnabledSignal;
506 static Signal<void()>& ScreenReaderDisabledSignal()
508 return mScreenReaderDisabledSignal;
514 std::unordered_set<const Accessible*> mKnownObjects;
515 std::string mBusName;
516 Bridge* mBridge = nullptr;
517 Actor mHighlightActor;
518 Actor mCurrentlyHighlightedActor;
519 std::pair<std::int32_t, std::int32_t> mExtentsOffset{0, 0};
521 std::shared_ptr<Data> mData;
522 friend class Accessible;
524 enum class AutoInitState
530 inline static AutoInitState mAutoInitState = AutoInitState::ENABLED;
532 inline static Signal<void()> mEnabledSignal;
533 inline static Signal<void()> mDisabledSignal;
534 inline static Signal<void()> mScreenReaderEnabledSignal;
535 inline static Signal<void()> mScreenReaderDisabledSignal;
538 * @brief Registers accessible object to be known in bridge object.
540 * Bridge must known about all currently alive accessible objects, as some requst
541 * might come and object will be identified by number id (it's memory address).
542 * To avoid memory corruption number id is checked against set of known objects.
544 * @param[in] object The accessible object
546 void RegisterOnBridge(const Accessible* object);
549 * @brief Tells bridge, that given object is considered root (doesn't have any parents).
551 * All root objects will have the same parent - application object. Application object
552 * is controlled by bridge and private.
554 * @param[in] owner The accessible object
556 void SetIsOnRootLevel(Accessible* owner);
560 * @brief Checks if ATSPI is activated or not.
561 * @return True if ATSPI is activated.
565 if(Bridge::GetCurrentBridge() == nullptr)
570 if(Bridge::GetCurrentBridge()->IsEnabled() == false)
575 return Bridge::GetCurrentBridge()->IsUp();
578 } // namespace Accessibility
581 #endif // DALI_ADAPTOR_ACCESSIBILITY_BRIDGE_H