196ab605c72fa1e39508851058254286745c2d2d
[platform/core/uifw/dali-adaptor.git] / dali / devel-api / adaptor-framework / accessibility.cpp
1 /*
2  * Copyright 2020  Samsung Electronics Co., Ltd
3  *
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
7  *
8  *  http://www.apache.org/licenses/LICENSE-2.0
9
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.
15  */
16
17 // EXTERNAL INCLUDES
18 #include <dali/integration-api/debug.h>
19 #include <dali/public-api/actors/actor.h>
20 #include <dali/public-api/actors/layer.h>
21 #include <dali/public-api/object/base-object.h>
22 #include <dali/public-api/object/object-registry.h>
23 #include <dali/public-api/object/type-info.h>
24 #include <dali/public-api/object/type-registry-helper.h>
25 #include <string_view>
26 #include <unordered_map>
27
28 // INTERNAL INCLUDES
29 #include <dali/devel-api/adaptor-framework/accessibility-bridge.h>
30 #include <dali/devel-api/adaptor-framework/actor-accessible.h>
31 #include <dali/devel-api/adaptor-framework/proxy-accessible.h>
32 #include <dali/devel-api/adaptor-framework/window-devel.h>
33 #include <dali/devel-api/atspi-interfaces/accessible.h>
34 #include <dali/devel-api/atspi-interfaces/action.h>
35 #include <dali/devel-api/atspi-interfaces/application.h>
36 #include <dali/devel-api/atspi-interfaces/collection.h>
37 #include <dali/devel-api/atspi-interfaces/component.h>
38 #include <dali/devel-api/atspi-interfaces/editable-text.h>
39 #include <dali/devel-api/atspi-interfaces/hyperlink.h>
40 #include <dali/devel-api/atspi-interfaces/hypertext.h>
41 #include <dali/devel-api/atspi-interfaces/selection.h>
42 #include <dali/devel-api/atspi-interfaces/text.h>
43 #include <dali/devel-api/atspi-interfaces/value.h>
44 #include <dali/internal/adaptor/common/adaptor-impl.h>
45 #include <dali/public-api/dali-adaptor-common.h>
46
47 using namespace Dali::Accessibility;
48 using namespace Dali;
49
50 const std::string& Dali::Accessibility::Address::GetBus() const
51 {
52   return mBus.empty() && Bridge::GetCurrentBridge() ? Bridge::GetCurrentBridge()->GetBusName() : mBus;
53 }
54
55 std::string Accessible::GetLocalizedRoleName() const
56 {
57   return GetRoleName();
58 }
59
60 std::string Accessible::GetRoleName() const
61 {
62   static const std::unordered_map<Role, std::string_view> roleMap{
63     {Role::INVALID, "invalid"},
64     {Role::ACCELERATOR_LABEL, "accelerator label"},
65     {Role::ALERT, "alert"},
66     {Role::ANIMATION, "animation"},
67     {Role::ARROW, "arrow"},
68     {Role::CALENDAR, "calendar"},
69     {Role::CANVAS, "canvas"},
70     {Role::CHECK_BOX, "check box"},
71     {Role::CHECK_MENU_ITEM, "check menu item"},
72     {Role::COLOR_CHOOSER, "color chooser"},
73     {Role::COLUMN_HEADER, "column header"},
74     {Role::COMBO_BOX, "combo box"},
75     {Role::DATE_EDITOR, "date editor"},
76     {Role::DESKTOP_ICON, "desktop icon"},
77     {Role::DESKTOP_FRAME, "desktop frame"},
78     {Role::DIAL, "dial"},
79     {Role::DIALOG, "dialog"},
80     {Role::DIRECTORY_PANE, "directory pane"},
81     {Role::DRAWING_AREA, "drawing area"},
82     {Role::FILE_CHOOSER, "file chooser"},
83     {Role::FILLER, "filler"},
84     {Role::FOCUS_TRAVERSABLE, "focus traversable"},
85     {Role::FONT_CHOOSER, "font chooser"},
86     {Role::FRAME, "frame"},
87     {Role::GLASS_PANE, "glass pane"},
88     {Role::HTML_CONTAINER, "html container"},
89     {Role::ICON, "icon"},
90     {Role::IMAGE, "image"},
91     {Role::INTERNAL_FRAME, "internal frame"},
92     {Role::LABEL, "label"},
93     {Role::LAYERED_PANE, "layered pane"},
94     {Role::LIST, "list"},
95     {Role::LIST_ITEM, "list item"},
96     {Role::MENU, "menu"},
97     {Role::MENU_BAR, "menu bar"},
98     {Role::MENU_ITEM, "menu item"},
99     {Role::OPTION_PANE, "option pane"},
100     {Role::PAGE_TAB, "page tab"},
101     {Role::PAGE_TAB_LIST, "page tab list"},
102     {Role::PANEL, "panel"},
103     {Role::PASSWORD_TEXT, "password text"},
104     {Role::POPUP_MENU, "popup menu"},
105     {Role::PROGRESS_BAR, "progress bar"},
106     {Role::PUSH_BUTTON, "push button"},
107     {Role::RADIO_BUTTON, "radio button"},
108     {Role::RADIO_MENU_ITEM, "radio menu item"},
109     {Role::ROOT_PANE, "root pane"},
110     {Role::ROW_HEADER, "row header"},
111     {Role::SCROLL_BAR, "scroll bar"},
112     {Role::SCROLL_PANE, "scroll pane"},
113     {Role::SEPARATOR, "separator"},
114     {Role::SLIDER, "slider"},
115     {Role::SPIN_BUTTON, "spin button"},
116     {Role::SPLIT_PANE, "split pane"},
117     {Role::STATUS_BAR, "status bar"},
118     {Role::TABLE, "table"},
119     {Role::TABLE_CELL, "table cell"},
120     {Role::TABLE_COLUMN_HEADER, "table column header"},
121     {Role::TABLE_ROW_HEADER, "table row header"},
122     {Role::TEAROFF_MENU_ITEM, "tearoff menu item"},
123     {Role::TERMINAL, "terminal"},
124     {Role::TEXT, "text"},
125     {Role::TOGGLE_BUTTON, "toggle button"},
126     {Role::TOOL_BAR, "tool bar"},
127     {Role::TOOL_TIP, "tool tip"},
128     {Role::TREE, "tree"},
129     {Role::TREE_TABLE, "tree table"},
130     {Role::UNKNOWN, "unknown"},
131     {Role::VIEWPORT, "viewport"},
132     {Role::WINDOW, "window"},
133     {Role::EXTENDED, "extended"},
134     {Role::HEADER, "header"},
135     {Role::FOOTER, "footer"},
136     {Role::PARAGRAPH, "paragraph"},
137     {Role::RULER, "ruler"},
138     {Role::APPLICATION, "application"},
139     {Role::AUTOCOMPLETE, "autocomplete"},
140     {Role::EDITBAR, "edit bar"},
141     {Role::EMBEDDED, "embedded"},
142     {Role::ENTRY, "entry"},
143     {Role::CHART, "chart"},
144     {Role::CAPTION, "caution"},
145     {Role::DOCUMENT_FRAME, "document frame"},
146     {Role::HEADING, "heading"},
147     {Role::PAGE, "page"},
148     {Role::SECTION, "section"},
149     {Role::REDUNDANT_OBJECT, "redundant object"},
150     {Role::FORM, "form"},
151     {Role::LINK, "link"},
152     {Role::INPUT_METHOD_WINDOW, "input method window"},
153     {Role::TABLE_ROW, "table row"},
154     {Role::TREE_ITEM, "tree item"},
155     {Role::DOCUMENT_SPREADSHEET, "document spreadsheet"},
156     {Role::DOCUMENT_PRESENTATION, "document presentation"},
157     {Role::DOCUMENT_TEXT, "document text"},
158     {Role::DOCUMENT_WEB, "document web"},
159     {Role::DOCUMENT_EMAIL, "document email"},
160     {Role::COMMENT, "comment"},
161     {Role::LIST_BOX, "list box"},
162     {Role::GROUPING, "grouping"},
163     {Role::IMAGE_MAP, "image map"},
164     {Role::NOTIFICATION, "notification"},
165     {Role::INFO_BAR, "info bar"},
166     {Role::LEVEL_BAR, "level bar"},
167     {Role::TITLE_BAR, "title bar"},
168     {Role::BLOCK_QUOTE, "block quote"},
169     {Role::AUDIO, "audio"},
170     {Role::VIDEO, "video"},
171     {Role::DEFINITION, "definition"},
172     {Role::ARTICLE, "article"},
173     {Role::LANDMARK, "landmark"},
174     {Role::LOG, "log"},
175     {Role::MARQUEE, "marquee"},
176     {Role::MATH, "math"},
177     {Role::RATING, "rating"},
178     {Role::TIMER, "timer"},
179     {Role::STATIC, "static"},
180     {Role::MATH_FRACTION, "math fraction"},
181     {Role::MATH_ROOT, "math root"},
182     {Role::SUBSCRIPT, "subscript"},
183     {Role::SUPERSCRIPT, "superscript"},
184   };
185
186   auto it = roleMap.find(GetRole());
187
188   if(it == roleMap.end())
189   {
190     return {};
191   }
192
193   return std::string{it->second};
194 }
195
196 AtspiInterfaces Accessible::GetInterfaces() const
197 {
198   if(!mInterfaces)
199   {
200     mInterfaces = DoGetInterfaces();
201     DALI_ASSERT_DEBUG(mInterfaces); // There has to be at least AtspiInterface::ACCESSIBLE
202   }
203
204   return mInterfaces;
205 }
206
207 std::vector<std::string> Accessible::GetInterfacesAsStrings() const
208 {
209   std::vector<std::string> ret;
210   AtspiInterfaces          interfaces = GetInterfaces();
211
212   for(std::size_t i = 0u; i < static_cast<std::size_t>(AtspiInterface::MAX_COUNT); ++i)
213   {
214     auto interface = static_cast<AtspiInterface>(i);
215
216     if(interfaces[interface])
217     {
218       auto name = GetInterfaceName(interface);
219
220       DALI_ASSERT_DEBUG(!name.empty());
221       ret.emplace_back(std::move(name));
222     }
223   }
224
225   return ret;
226 }
227
228 AtspiInterfaces Accessible::DoGetInterfaces() const
229 {
230   AtspiInterfaces interfaces;
231
232   interfaces[AtspiInterface::ACCESSIBLE]    = true;
233   interfaces[AtspiInterface::ACTION]        = dynamic_cast<const Action*>(this);
234   interfaces[AtspiInterface::APPLICATION]   = dynamic_cast<const Application*>(this);
235   interfaces[AtspiInterface::COLLECTION]    = dynamic_cast<const Collection*>(this);
236   interfaces[AtspiInterface::COMPONENT]     = dynamic_cast<const Component*>(this);
237   interfaces[AtspiInterface::EDITABLE_TEXT] = dynamic_cast<const EditableText*>(this);
238   interfaces[AtspiInterface::HYPERLINK]     = dynamic_cast<const Hyperlink*>(this);
239   interfaces[AtspiInterface::HYPERTEXT]     = dynamic_cast<const Hypertext*>(this);
240   interfaces[AtspiInterface::SELECTION]     = dynamic_cast<const Selection*>(this);
241   interfaces[AtspiInterface::TEXT]          = dynamic_cast<const Text*>(this);
242   interfaces[AtspiInterface::VALUE]         = dynamic_cast<const Value*>(this);
243
244   return interfaces;
245 }
246
247 std::string Accessible::GetInterfaceName(AtspiInterface interface)
248 {
249   static const std::unordered_map<AtspiInterface, std::string_view> interfaceMap{
250     {AtspiInterface::ACCESSIBLE, "org.a11y.atspi.Accessible"},
251     {AtspiInterface::ACTION, "org.a11y.atspi.Action"},
252     {AtspiInterface::APPLICATION, "org.a11y.atspi.Application"},
253     {AtspiInterface::CACHE, "org.a11y.atspi.Cache"},
254     {AtspiInterface::COLLECTION, "org.a11y.atspi.Collection"},
255     {AtspiInterface::COMPONENT, "org.a11y.atspi.Component"},
256     {AtspiInterface::DEVICE_EVENT_CONTROLLER, "org.a11y.atspi.DeviceEventController"},
257     {AtspiInterface::DEVICE_EVENT_LISTENER, "org.a11y.atspi.DeviceEventListener"},
258     {AtspiInterface::DOCUMENT, "org.a11y.atspi.Document"},
259     {AtspiInterface::EDITABLE_TEXT, "org.a11y.atspi.EditableText"},
260     {AtspiInterface::EVENT_DOCUMENT, "org.a11y.atspi.Event.Document"},
261     {AtspiInterface::EVENT_FOCUS, "org.a11y.atspi.Event.Focus"},
262     {AtspiInterface::EVENT_KEYBOARD, "org.a11y.atspi.Event.Keyboard"},
263     {AtspiInterface::EVENT_MOUSE, "org.a11y.atspi.Event.Mouse"},
264     {AtspiInterface::EVENT_OBJECT, "org.a11y.atspi.Event.Object"},
265     {AtspiInterface::EVENT_TERMINAL, "org.a11y.atspi.Event.Terminal"},
266     {AtspiInterface::EVENT_WINDOW, "org.a11y.atspi.Event.Window"},
267     {AtspiInterface::HYPERLINK, "org.a11y.atspi.Hyperlink"},
268     {AtspiInterface::HYPERTEXT, "org.a11y.atspi.Hypertext"},
269     {AtspiInterface::IMAGE, "org.a11y.atspi.Image"},
270     {AtspiInterface::REGISTRY, "org.a11y.atspi.Registry"},
271     {AtspiInterface::SELECTION, "org.a11y.atspi.Selection"},
272     {AtspiInterface::SOCKET, "org.a11y.atspi.Socket"},
273     {AtspiInterface::TABLE, "org.a11y.atspi.Table"},
274     {AtspiInterface::TABLE_CELL, "org.a11y.atspi.TableCell"},
275     {AtspiInterface::TEXT, "org.a11y.atspi.Text"},
276     {AtspiInterface::VALUE, "org.a11y.atspi.Value"},
277   };
278
279   auto it = interfaceMap.find(interface);
280
281   if(it == interfaceMap.end())
282   {
283     return {};
284   }
285
286   return std::string{it->second};
287 }
288
289 Dali::Actor Accessible::GetCurrentlyHighlightedActor()
290 {
291   return IsUp() ? Bridge::GetCurrentBridge()->mData->mCurrentlyHighlightedActor : Dali::Actor{};
292 }
293
294 void Accessible::SetCurrentlyHighlightedActor(Dali::Actor actor)
295 {
296   if(IsUp())
297   {
298     Bridge::GetCurrentBridge()->mData->mCurrentlyHighlightedActor = actor;
299   }
300 }
301
302 Dali::Actor Accessible::GetHighlightActor()
303 {
304   return IsUp() ? Bridge::GetCurrentBridge()->mData->mHighlightActor : Dali::Actor{};
305 }
306
307 void Accessible::SetHighlightActor(Dali::Actor actor)
308 {
309   if(IsUp())
310   {
311     Bridge::GetCurrentBridge()->mData->mHighlightActor = actor;
312   }
313 }
314
315 void Bridge::ForceDown()
316 {
317   auto highlighted = Accessible::GetCurrentlyHighlightedActor();
318   if(highlighted)
319   {
320     auto component = dynamic_cast<Component*>(Accessible::Get(highlighted));
321     if(component)
322     {
323       component->ClearHighlight();
324     }
325   }
326   mData = {};
327 }
328
329 void Bridge::SetIsOnRootLevel(Accessible* owner)
330 {
331   owner->mIsOnRootLevel = true;
332 }
333
334 namespace
335 {
336 class AdaptorAccessible : public ActorAccessible
337 {
338 protected:
339   bool mRoot = false;
340
341 public:
342   AdaptorAccessible(Dali::Actor actor, bool isRoot)
343   : ActorAccessible(actor),
344     mRoot(isRoot)
345   {
346   }
347
348   bool GrabFocus() override
349   {
350     return false;
351   }
352
353   bool GrabHighlight() override
354   {
355     return false;
356   }
357
358   bool ClearHighlight() override
359   {
360     return false;
361   }
362
363   Role GetRole() const override
364   {
365     return mRoot ? Role::WINDOW : Role::REDUNDANT_OBJECT;
366   }
367
368   States GetStates() override
369   {
370     States state;
371     if(mRoot)
372     {
373       auto window             = Dali::DevelWindow::Get(Self());
374       auto visible            = window.IsVisible();
375       state[State::ENABLED]   = true;
376       state[State::SENSITIVE] = true;
377       state[State::SHOWING]   = visible;
378       state[State::VISIBLE]   = true;
379       state[State::ACTIVE]    = visible;
380     }
381     else
382     {
383       auto parentState      = GetParent()->GetStates();
384       state[State::SHOWING] = parentState[State::SHOWING];
385       state[State::VISIBLE] = parentState[State::VISIBLE];
386     }
387     return state;
388   }
389
390   Attributes GetAttributes() const override
391   {
392     Dali::TypeInfo type;
393     Self().GetTypeInfo(type);
394     return {
395       {"class", type.GetName()},
396     };
397   }
398
399   bool DoGesture(const GestureInfo& gestureInfo) override
400   {
401     return false;
402   }
403
404   std::vector<Relation> GetRelationSet() override
405   {
406     return {};
407   }
408 }; // AdaptorAccessible
409
410 using AdaptorAccessiblesType = std::unordered_map<const Dali::RefObject*, std::unique_ptr<AdaptorAccessible> >;
411
412 // Save RefObject from an Actor in Accessible::Get()
413 AdaptorAccessiblesType gAdaptorAccessibles;
414
415 std::function<Accessible*(Dali::Actor)> convertingFunctor = [](Dali::Actor) -> Accessible* {
416   return nullptr;
417 };
418
419 ObjectRegistry objectRegistry;
420 } // namespace
421
422 void Accessible::SetObjectRegistry(ObjectRegistry registry)
423 {
424   objectRegistry = registry;
425   objectRegistry.ObjectDestroyedSignal().Connect([](const Dali::RefObject* obj) {
426     gAdaptorAccessibles.erase(obj);
427   });
428 }
429
430 void Accessible::RegisterExternalAccessibleGetter(std::function<Accessible*(Dali::Actor)> functor)
431 {
432   convertingFunctor = functor;
433 }
434
435 Accessible* Accessible::Get(Dali::Actor actor, bool isRoot)
436 {
437   if(!actor)
438   {
439     return nullptr;
440   }
441
442   auto accessible = convertingFunctor(actor);
443   if(!accessible)
444   {
445     auto pair = gAdaptorAccessibles.emplace(&actor.GetBaseObject(), nullptr);
446     if(pair.second)
447     {
448       pair.first->second.reset(new AdaptorAccessible(actor, isRoot));
449     }
450     accessible = pair.first->second.get();
451   }
452
453   return accessible;
454 }