2 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <dali/internal/accessibility/bridge/bridge-accessible.h>
25 //comment out 2 lines below to get more logs
27 #define LOG() _LoggerEmpty()
29 using namespace Dali::Accessibility;
31 #define GET_NAVIGABLE_AT_POINT_MAX_RECURSION_DEPTH 10000
35 bool SortVertically(Component* lhs, Component* rhs)
37 auto leftRect = lhs->GetExtents(CoordType::WINDOW);
38 auto rightRect = rhs->GetExtents(CoordType::WINDOW);
40 return leftRect.y < rightRect.y;
43 bool SortHorizontally(Component* lhs, Component* rhs)
45 auto leftRect = lhs->GetExtents(CoordType::WINDOW);
46 auto rightRect = rhs->GetExtents(CoordType::WINDOW);
48 return leftRect.x < rightRect.x;
51 std::vector<std::vector<Component*>> SplitLines(const std::vector<Component*>& children)
53 // Find first with non-zero area
54 auto first = std::find_if(children.begin(), children.end(), [](Component* component) -> bool {
55 auto extents = component->GetExtents(CoordType::WINDOW);
56 return extents.height != 0.0f && extents.width != 0.0f;
59 if(first == children.end())
64 std::vector<std::vector<Component*>> lines(1);
65 Dali::Rect<> lineRect = (*first)->GetExtents(CoordType::WINDOW);
69 for(auto it = first; it != children.end(); ++it)
73 rect = child->GetExtents(CoordType::WINDOW);
74 if(rect.height == 0.0f || rect.width == 0.0f)
80 if(lineRect.y + (0.25 * lineRect.height) >= rect.y)
83 lines.back().push_back(child);
90 lines.back().push_back(child);
97 } // anonymous namespace
99 BridgeAccessible::BridgeAccessible()
103 void BridgeAccessible::RegisterInterfaces()
105 DBus::DBusInterfaceDescription desc{AtspiDbusInterfaceAccessible};
106 AddGetPropertyToInterface(desc, "ChildCount", &BridgeAccessible::GetChildCount);
107 AddGetPropertyToInterface(desc, "Name", &BridgeAccessible::GetName);
108 AddGetPropertyToInterface(desc, "Description", &BridgeAccessible::GetDescription);
109 AddGetPropertyToInterface(desc, "Parent", &BridgeAccessible::GetParent);
110 AddFunctionToInterface(desc, "GetRole", &BridgeAccessible::GetRole);
111 AddFunctionToInterface(desc, "GetRoleName", &BridgeAccessible::GetRoleName);
112 AddFunctionToInterface(desc, "GetLocalizedRoleName", &BridgeAccessible::GetLocalizedRoleName);
113 AddFunctionToInterface(desc, "GetState", &BridgeAccessible::GetStates);
114 AddFunctionToInterface(desc, "GetAttributes", &BridgeAccessible::GetAttributes);
115 AddFunctionToInterface(desc, "GetInterfaces", &BridgeAccessible::GetInterfaces);
116 AddFunctionToInterface(desc, "GetChildAtIndex", &BridgeAccessible::GetChildAtIndex);
117 AddFunctionToInterface(desc, "GetChildren", &BridgeAccessible::GetChildren);
118 AddFunctionToInterface(desc, "GetIndexInParent", &BridgeAccessible::GetIndexInParent);
119 AddFunctionToInterface(desc, "GetNavigableAtPoint", &BridgeAccessible::GetNavigableAtPoint);
120 AddFunctionToInterface(desc, "GetNeighbor", &BridgeAccessible::GetNeighbor);
121 AddFunctionToInterface(desc, "GetDefaultLabelInfo", &BridgeAccessible::GetDefaultLabelInfo);
122 AddFunctionToInterface(desc, "DoGesture", &BridgeAccessible::DoGesture);
123 AddFunctionToInterface(desc, "GetReadingMaterial", &BridgeAccessible::GetReadingMaterial);
124 AddFunctionToInterface(desc, "GetRelationSet", &BridgeAccessible::GetRelationSet);
125 dbusServer.addInterface("/", desc, true);
128 static bool AcceptObjectCheckRelations(Component* obj)
130 auto r = obj->GetRelationSet();
132 for(const auto& it : r)
133 if(it.relationType == RelationType::CONTROLLED_BY)
139 static Component* GetScrollableParent(Accessible* obj)
143 obj = obj->GetParent();
144 auto comp = dynamic_cast<Component*>(obj);
145 if(comp && comp->IsScrollable())
151 static bool ObjectIsItem(Component* obj)
155 auto role = obj->GetRole();
156 return role == Role::LIST_ITEM || role == Role::MENU_ITEM;
159 static bool ObjectIsCollapsed(Component* obj)
163 const auto states = obj->GetStates();
164 return states[State::EXPANDABLE] && !states[State::EXPANDED];
167 static bool OobjectIsZeroSize(Component* obj)
171 auto extents = obj->GetExtents(CoordType::WINDOW);
172 return extents.height == 0 || extents.width == 0;
175 static bool AcceptObject(Component* obj)
179 const auto states = obj->GetStates();
180 if(!states[State::VISIBLE])
182 if(!AcceptObjectCheckRelations(obj))
184 if(!states[State::HIGHLIGHTABLE])
187 if(GetScrollableParent(obj) != nullptr)
189 auto parent = dynamic_cast<Component*>(obj->GetParent());
193 return !ObjectIsItem(obj) || !ObjectIsCollapsed(parent);
198 if(OobjectIsZeroSize(obj))
202 if(!states[State::SHOWING])
210 static bool AcceptObject(Accessible* obj)
212 auto c = dynamic_cast<Component*>(obj);
213 return AcceptObject(c);
216 static std::string objDump(Component* obj)
220 std::ostringstream o;
221 auto e = obj->GetExtents(CoordType::SCREEN);
222 o << "name: " << obj->GetName() << " extent: (" << e.x << ", "
223 << e.y << "), [" << e.width << ", " << e.height << "]";
227 Component* BridgeAccessible::GetObjectInRelation(Accessible* obj, RelationType ralationType)
231 for(auto& relation : obj->GetRelationSet())
233 if(relation.relationType == ralationType)
235 for(auto& address : relation.targets)
237 auto component = dynamic_cast<Component*>(Find(address));
246 static std::string makeIndent(unsigned int maxRecursionDepth)
248 return std::string(GET_NAVIGABLE_AT_POINT_MAX_RECURSION_DEPTH - maxRecursionDepth, ' ');
251 Component* BridgeAccessible::CalculateNavigableAccessibleAtPoint(Accessible* root, Point p, CoordType cType, unsigned int maxRecursionDepth)
253 if(!root || maxRecursionDepth == 0)
255 auto root_component = dynamic_cast<Component*>(root);
256 LOG() << "CalculateNavigableAccessibleAtPoint: checking: " << makeIndent(maxRecursionDepth) << objDump(root_component);
258 if(root_component && !root_component->Contains(p, cType))
261 auto children = root->GetChildren();
262 for(auto childIt = children.rbegin(); childIt != children.rend(); childIt++)
264 //check recursively all children first
265 auto result = CalculateNavigableAccessibleAtPoint(*childIt, p, cType, maxRecursionDepth - 1);
271 //Found a candidate, all its children are already checked
272 auto controledBy = GetObjectInRelation(root_component, RelationType::CONTROLLED_BY);
274 controledBy = root_component;
276 if(controledBy->IsProxy() || AcceptObject(controledBy))
278 LOG() << "CalculateNavigableAccessibleAtPoint: found: " << makeIndent(maxRecursionDepth) << objDump(root_component);
285 BridgeAccessible::ReadingMaterialType BridgeAccessible::GetReadingMaterial()
287 auto self = FindSelf();
288 auto attributes = self->GetAttributes();
289 auto name = self->GetName();
290 std::string labeledByName = "";
291 std::string textIfceName = "";
292 auto role = static_cast<uint32_t>(self->GetRole());
293 auto states = self->GetStates();
294 auto localizedName = self->GetLocalizedRoleName();
295 auto childCount = static_cast<int32_t>(self->GetChildCount());
297 double currentValue = 0.0;
298 double minimumIncrement = 0.0;
299 double maximumValue = 0.0;
300 double minimumValue = 0.0;
302 auto* value = dynamic_cast<Dali::Accessibility::Value*>(self);
305 currentValue = value->GetCurrent();
306 minimumIncrement = value->GetMinimumIncrement();
307 maximumValue = value->GetMaximum();
308 minimumValue = value->GetMinimum();
311 auto description = self->GetDescription();
312 auto indexInParent = static_cast<int32_t>(self->GetIndexInParent());
313 bool isSelectedInParent = false;
314 bool hasCheckBoxChild = false;
315 int32_t firstSelectedChildIndex = -1;
316 int32_t selectedChildCount = 0;
318 for(auto i = 0u; i < static_cast<size_t>(childCount); ++i)
320 auto q = self->GetChildAtIndex(i);
321 auto s = q->GetStates();
322 if(s[State::SELECTABLE])
324 if(s[State::SELECTED])
326 ++selectedChildCount;
327 if(firstSelectedChildIndex < 0)
328 firstSelectedChildIndex = static_cast<int32_t>(i);
331 if(q->GetRole() == Role::CHECK_BOX)
332 hasCheckBoxChild = true;
335 int32_t listChildrenCount = 0;
336 Accessible* parent = self->GetParent();
337 auto parentStateSet = parent ? parent->GetStates() : States{};
338 auto parentChildCount = parent ? static_cast<int32_t>(parent->GetChildCount()) : 0;
339 auto parentRole = static_cast<uint32_t>(parent ? parent->GetRole() : Role{});
340 Accessible* describedByObject = nullptr;
360 firstSelectedChildIndex,
369 void BridgeAccessible::SuppressScreenReader(bool suppress)
371 suppressScreenReader = suppress;
374 DBus::ValueOrError<bool> BridgeAccessible::DoGesture(Dali::Accessibility::Gesture type, int32_t xBeg, int32_t yBeg, int32_t xEnd, int32_t yEnd, Dali::Accessibility::GestureState state, uint32_t eventTime)
376 return FindSelf()->DoGesture(Dali::Accessibility::GestureInfo{type, xBeg, xEnd, yBeg, yEnd, state, eventTime});
379 DBus::ValueOrError<Accessible*, uint8_t, Accessible*> BridgeAccessible::GetNavigableAtPoint(int32_t x, int32_t y, uint32_t coordType)
381 Accessible* deputy = nullptr;
382 auto accessible = FindSelf();
383 auto cType = static_cast<CoordType>(coordType);
384 LOG() << "GetNavigableAtPoint: " << x << ", " << y << " type: " << coordType;
385 auto component = CalculateNavigableAccessibleAtPoint(accessible, {x, y}, cType, GET_NAVIGABLE_AT_POINT_MAX_RECURSION_DEPTH);
386 bool recurse = false;
389 recurse = component->IsProxy();
392 return {component, recurse, deputy};
395 static bool CheckChainEndWithAttribute(Accessible* obj, unsigned char forward)
399 auto attrs = obj->GetAttributes();
400 for(auto& attr : attrs)
402 if(attr.first == "relation_chain_end")
404 if((attr.second == "prev,end" && forward == 0) || (attr.second == "next,end" && forward == 1) || attr.second == "prev,next,end")
413 static Accessible* DeputyOfProxyInParentGet(Accessible* obj)
418 Accessible* BridgeAccessible::GetCurrentlyHighlighted()
420 //TODO: add currently highlighted object
424 std::vector<Accessible*> BridgeAccessible::ValidChildrenGet(const std::vector<Accessible*>& children, Accessible* start, Accessible* root)
426 std::vector<Component*> vec;
427 std::vector<Accessible*> ret;
429 for(auto child : children)
431 if(auto* component = dynamic_cast<Component*>(child); component)
433 vec.push_back(component);
437 std::sort(vec.begin(), vec.end(), &SortVertically);
439 for(auto& line : SplitLines(vec))
441 std::sort(line.begin(), line.end(), &SortHorizontally);
442 ret.insert(ret.end(), line.begin(), line.end());
448 static bool DeputyIs(Accessible* obj)
454 static Accessible* ProxyInParentGet(Accessible* obj)
458 auto children = obj->GetChildren();
459 for(auto& child : children)
467 static bool ObjectRoleIsAcceptableWhenNavigatingNextPrev(Accessible* obj)
471 auto role = obj->GetRole();
472 return role != Role::POPUP_MENU && role != Role::DIALOG;
476 struct CycleDetection
478 CycleDetection(const T value)
480 currentSearchSize(1),
484 bool check(const T value)
490 currentSearchSize <<= 1;
491 if(currentSearchSize == 0)
492 return true; // UNDEFINED BEHAVIOR
493 counter = currentSearchSize;
499 unsigned int currentSearchSize;
500 unsigned int counter;
503 static Accessible* FindNonDefunctChild(const std::vector<Accessible*>& children, unsigned int currentIndex, unsigned char forward)
505 unsigned int childrenCount = children.size();
506 for(; currentIndex < childrenCount; forward ? ++currentIndex : --currentIndex)
508 Accessible* n = children[currentIndex];
509 if(n && !n->GetStates()[State::DEFUNCT])
515 static Accessible* DirectionalDepthFirstSearchTryNonDefunctChild(Accessible* node, const std::vector<Accessible*>& children, unsigned char forward)
519 auto childrenCount = children.size();
520 if(childrenCount > 0)
522 const bool isShowing = GetScrollableParent(node) == nullptr ? node->GetStates()[State::SHOWING] : true;
525 return FindNonDefunctChild(children, forward ? 0 : childrenCount - 1, forward);
531 Accessible* BridgeAccessible::GetNextNonDefunctSibling(Accessible* obj, Accessible* start, Accessible* root, unsigned char forward)
535 auto parent = obj->GetParent();
539 auto children = ValidChildrenGet(parent->GetChildren(), start, root);
541 unsigned int children_count = children.size();
542 if(children_count == 0)
546 unsigned int current = 0;
547 for(; current < children_count && children[current] != obj; ++current)
549 if(current >= children_count)
553 forward ? ++current : --current;
554 auto ret = FindNonDefunctChild(children, current, forward);
558 Accessible* BridgeAccessible::DirectionalDepthFirstSearchTryNonDefunctSibling(bool& all_children_visited, Accessible* node, Accessible* start, Accessible* root, unsigned char forward)
562 Accessible* sibling = GetNextNonDefunctSibling(node, start, root, forward);
566 all_children_visited = false;
570 node = node->GetParent();
571 if(node == nullptr || node == root)
574 // in backward traversing stop the walk up on parent
581 Accessible* BridgeAccessible::CalculateNeighbor(Accessible* root, Accessible* start, unsigned char forward, BridgeAccessible::GetNeighborSearchMode search_mode)
583 if(start && CheckChainEndWithAttribute(start, forward))
585 if(root && root->GetStates()[State::DEFUNCT])
587 if(start && start->GetStates()[State::DEFUNCT])
593 if(search_mode == BridgeAccessible::GetNeighborSearchMode::recurseToOutside)
595 // This only works if we navigate backward, and it is not possible to
596 // find in embedded process. In this case the deputy should be used */
597 return DeputyOfProxyInParentGet(start);
600 Accessible* node = start ? start : root;
604 // initialization of all-children-visited flag for start node - we assume
605 // that when we begin at start node and we navigate backward, then all children
606 // are visited, so navigation will ignore start's children and go to
607 // previous sibling available.
608 // Regarding condtion (start != root):
609 // The last object can be found only if all_children_visited is false.
610 // The start is same with root, when looking for the last object.
611 bool all_children_visited = (start != root) && (search_mode != BridgeAccessible::GetNeighborSearchMode::recurseFromRoot && !forward);
612 // true, if starting element should be ignored. this is only used in rare case of
613 // recursive search failing to find an object.
614 // consider tree, where element A on bus BUS_A has child B on bus BUS_B. when going "next" from
615 // element A algorithm has to descend into BUS_B and search element B and its children. this is done
616 // by returning to our caller object B with special flag set (meaning - continue the search from B on bus BUS_B).
617 // if next object will be found there (on BUS_B), then search ends. but if not, then our caller will find it out
618 // and will call us again with object A and flag search_mode set to NEIGHBOR_SEARCH_MODE_CONTINUE_AFTER_FAILED_RECURSING.
619 // this flag means, that object A was already checked previously and we should skip it and its children.
620 bool force_next = (search_mode == BridgeAccessible::GetNeighborSearchMode::continueAfterFailedRecursion);
622 CycleDetection<Accessible*> cycleDetection(node);
625 if(node->GetStates()[State::DEFUNCT])
628 // always accept proxy object from different world
629 if(!force_next && node->IsProxy())
632 auto children = node->GetChildren();
633 children = ValidChildrenGet(children, start, root);
637 // 2. parent after all children in backward traversing
638 // 3. Nodes with roles: ATSPI_ROLE_PAGE_TAB, ATSPI_ROLE_POPUP_MENU and ATSPI_ROLE_DIALOG, only when looking for first or last element.
639 // Objects with those roles shouldnt be reachable, when navigating next / prev.
640 bool all_children_visited_or_moving_forward = (children.size() == 0 || forward || all_children_visited);
641 if(!force_next && node != start && all_children_visited_or_moving_forward && AcceptObject(node))
643 if(start == NULL || ObjectRoleIsAcceptableWhenNavigatingNextPrev(node))
647 Accessible* next_related_in_direction = !force_next ? GetObjectInRelation(node, forward ? RelationType::FLOWS_TO : RelationType::FLOWS_FROM) : nullptr;
648 // force_next means that the search_mode is NEIGHBOR_SEARCH_MODE_CONTINUE_AFTER_FAILED_RECURSING
649 // in this case the node is elm_layout which is parent of proxy object.
650 // There is an access object working for the proxy object, and the access
651 // object could have relation information. This relation information should
652 // be checked first before using the elm_layout as a node.
653 if(force_next && forward)
655 auto deputy = DeputyOfProxyInParentGet(node);
656 next_related_in_direction =
657 GetObjectInRelation(deputy, RelationType::FLOWS_TO);
660 if(next_related_in_direction && start && start->GetStates()[State::DEFUNCT])
662 next_related_in_direction = NULL;
665 unsigned char want_cycle_detection = 0;
666 if(next_related_in_direction)
668 // Check next_related_in_direction is deputy object
672 // If the prev object is deputy, then go to inside of its proxy first
673 if(DeputyIs(next_related_in_direction))
675 parent = next_related_in_direction->GetParent();
676 next_related_in_direction = ProxyInParentGet(parent);
681 // If current object is deputy, and it has relation next object,
682 // then do not use the relation next object, and use proxy first
685 parent = node->GetParent();
686 next_related_in_direction = ProxyInParentGet(parent);
689 node = next_related_in_direction;
690 want_cycle_detection = 1;
694 auto child = !force_next && !all_children_visited ? DirectionalDepthFirstSearchTryNonDefunctChild(node, children, forward) : nullptr;
697 want_cycle_detection = 1;
701 if(!force_next && node == root)
703 all_children_visited = true;
704 child = DirectionalDepthFirstSearchTryNonDefunctSibling(all_children_visited, node, start, root, forward);
709 if(want_cycle_detection && cycleDetection.check(node))
717 DBus::ValueOrError<Accessible*, uint8_t> BridgeAccessible::GetNeighbor(std::string rootPath, int32_t direction, int32_t search_mode)
719 auto start = FindSelf();
720 rootPath = StripPrefix(rootPath);
721 auto root = !rootPath.empty() ? Find(rootPath) : nullptr;
722 auto accessible = CalculateNeighbor(root, start, direction == 1, static_cast<GetNeighborSearchMode>(search_mode));
723 unsigned char recurse = 0;
726 recurse = accessible->IsProxy();
728 return {accessible, recurse};
731 Accessible* BridgeAccessible::GetParent()
733 // NOTE: currently bridge supports single application root element.
734 // only element set as application root might return nullptr from GetParent
735 // if you want more, then you need to change setApplicationRoot to
736 // add/remove ApplicationRoot and make roots a vector.
737 auto p = FindSelf()->GetParent();
741 DBus::ValueOrError<std::vector<Accessible*>> BridgeAccessible::GetChildren()
743 return FindSelf()->GetChildren();
745 std::string BridgeAccessible::GetDescription()
747 return FindSelf()->GetDescription();
749 DBus::ValueOrError<uint32_t> BridgeAccessible::GetRole()
751 return static_cast<unsigned int>(FindSelf()->GetRole());
753 DBus::ValueOrError<std::string> BridgeAccessible::GetRoleName()
755 return FindSelf()->GetRoleName();
757 DBus::ValueOrError<std::string> BridgeAccessible::GetLocalizedRoleName()
759 return FindSelf()->GetLocalizedRoleName();
761 DBus::ValueOrError<int32_t> BridgeAccessible::GetIndexInParent()
763 return FindSelf()->GetIndexInParent();
765 DBus::ValueOrError<std::array<uint32_t, 2>> BridgeAccessible::GetStates()
767 return FindSelf()->GetStates().GetRawData();
769 DBus::ValueOrError<std::unordered_map<std::string, std::string>> BridgeAccessible::GetAttributes()
771 std::unordered_map<std::string, std::string> attributes = FindSelf()->GetAttributes();
772 if(suppressScreenReader)
774 attributes.insert({"suppress-screen-reader", "true"});
779 DBus::ValueOrError<std::vector<std::string>> BridgeAccessible::GetInterfaces()
781 return FindSelf()->GetInterfaces();
783 int BridgeAccessible::GetChildCount()
785 return FindSelf()->GetChildCount();
787 DBus::ValueOrError<Accessible*> BridgeAccessible::GetChildAtIndex(int index)
790 throw std::domain_error{"negative index (" + std::to_string(index) + ")"};
791 return FindSelf()->GetChildAtIndex(static_cast<size_t>(index));
794 std::string BridgeAccessible::GetName()
796 return FindSelf()->GetName();
799 DBus::ValueOrError<Accessible*, uint32_t, std::unordered_map<std::string, std::string>> BridgeAccessible::GetDefaultLabelInfo()
801 auto defaultLabel = FindSelf()->GetDefaultLabel();
802 return {defaultLabel, static_cast<uint32_t>(defaultLabel->GetRole()), defaultLabel->GetAttributes()};
805 DBus::ValueOrError<std::vector<BridgeAccessible::Relation>> BridgeAccessible::GetRelationSet()
807 auto relations = FindSelf()->GetRelationSet();
808 std::vector<BridgeAccessible::Relation> ret;
810 for(auto& it : relations)
811 ret.emplace_back(Relation{static_cast<uint32_t>(it.relationType), it.targets});