3a30b4b5ba30a74e887669464f1db2e838a5a525
[platform/core/uifw/dali-adaptor.git] / dali / internal / accessibility / bridge / accessible.cpp
1 /*
2  * Copyright (c) 2021 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
18 // CLASS HEADER
19
20 //INTERNAL INCLUDES
21 #include <dali/internal/accessibility/bridge/accessibility-common.h>
22 #include <third-party/libunibreak/linebreak.h>
23 #include <third-party/libunibreak/wordbreak.h>
24
25 using namespace Dali::Accessibility;
26
27 std::vector<std::string> Accessible::GetInterfaces()
28 {
29   std::vector<std::string> tmp;
30   tmp.push_back(AtspiDbusInterfaceAccessible);
31   if(dynamic_cast<Collection*>(this))
32   {
33     tmp.push_back(AtspiDbusInterfaceCollection);
34   }
35   if(dynamic_cast<Text*>(this))
36   {
37     tmp.push_back(AtspiDbusInterfaceText);
38   }
39   if(dynamic_cast<EditableText*>(this))
40   {
41     tmp.push_back(AtspiDbusInterfaceEditableText);
42   }
43   if(dynamic_cast<Value*>(this))
44   {
45     tmp.push_back(AtspiDbusInterfaceValue);
46   }
47   if(dynamic_cast<Component*>(this))
48   {
49     tmp.push_back(AtspiDbusInterfaceComponent);
50   }
51   if(auto d = dynamic_cast<Action*>(this))
52   {
53     if(d->GetActionCount() > 0)
54     {
55       tmp.push_back(AtspiDbusInterfaceAction);
56     }
57   }
58   return tmp;
59 }
60
61 Accessible::Accessible()
62 {
63 }
64
65 Accessible::~Accessible()
66 {
67   auto b = bridgeData.lock();
68   if(b)
69     b->knownObjects.erase(this);
70 }
71
72 void Accessible::EmitActiveDescendantChanged(Accessible* obj, Accessible* child)
73 {
74   if(auto b = GetBridgeData())
75   {
76     b->bridge->EmitActiveDescendantChanged(obj, child);
77   }
78 }
79
80 void Accessible::EmitStateChanged(State state, int newValue1, int newValue2)
81 {
82   if(auto b = GetBridgeData())
83   {
84     b->bridge->EmitStateChanged(this, state, newValue1, newValue2);
85   }
86 }
87
88 void Accessible::EmitShowing(bool showing)
89 {
90   if(auto b = GetBridgeData())
91   {
92     b->bridge->EmitStateChanged(this, State::SHOWING, showing ? 1 : 0, 0);
93   }
94 }
95
96 void Accessible::EmitVisible(bool visible)
97 {
98   if(auto b = GetBridgeData())
99   {
100     b->bridge->EmitStateChanged(this, State::VISIBLE, visible ? 1 : 0, 0);
101   }
102 }
103
104 void Accessible::EmitHighlighted(bool set)
105 {
106   if(auto b = GetBridgeData())
107   {
108     b->bridge->EmitStateChanged(this, State::HIGHLIGHTED, set ? 1 : 0, 0);
109   }
110 }
111
112 void Accessible::EmitFocused(bool set)
113 {
114   if(auto b = GetBridgeData())
115   {
116     b->bridge->EmitStateChanged(this, State::FOCUSED, set ? 1 : 0, 0);
117   }
118 }
119 void Accessible::EmitTextInserted(unsigned int position, unsigned int length, const std::string& content)
120 {
121   if(auto b = GetBridgeData())
122   {
123     b->bridge->EmitTextChanged(this, TextChangedState::INSERTED, position, length, content);
124   }
125 }
126 void Accessible::EmitTextDeleted(unsigned int position, unsigned int length, const std::string& content)
127 {
128   if(auto b = GetBridgeData())
129   {
130     b->bridge->EmitTextChanged(this, TextChangedState::DELETED, position, length, content);
131   }
132 }
133 void Accessible::EmitTextCaretMoved(unsigned int cursorPosition)
134 {
135   if(auto b = GetBridgeData())
136   {
137     b->bridge->EmitCaretMoved(this, cursorPosition);
138   }
139 }
140 void Accessible::Emit(WindowEvent we, unsigned int detail1)
141 {
142   if(auto b = GetBridgeData())
143   {
144     b->bridge->Emit(this, we, detail1);
145   }
146 }
147 void Accessible::Emit(ObjectPropertyChangeEvent ev)
148 {
149   if(auto b = GetBridgeData())
150   {
151     b->bridge->Emit(this, ev);
152   }
153 }
154
155 void Accessible::EmitBoundsChanged(Rect<> rect)
156 {
157   if(auto b = GetBridgeData())
158   {
159     b->bridge->EmitBoundsChanged(this, rect);
160   }
161 }
162
163 std::vector<Accessible*> Accessible::GetChildren()
164 {
165   std::vector<Accessible*> tmp(GetChildCount());
166   for(auto i = 0u; i < tmp.size(); ++i)
167   {
168     tmp[i] = GetChildAtIndex(i);
169   }
170   return tmp;
171 }
172
173 std::shared_ptr<Bridge::Data> Accessible::GetBridgeData()
174 {
175   auto b = bridgeData.lock();
176   if(!b)
177   {
178     auto p = Bridge::GetCurrentBridge();
179     b      = p->data;
180   }
181   return b;
182 }
183
184 Address Accessible::GetAddress()
185 {
186   auto b = bridgeData.lock();
187   if(!b)
188   {
189     b = GetBridgeData();
190     if(b)
191       b->bridge->RegisterOnBridge(this);
192   }
193   std::ostringstream tmp;
194   tmp << this;
195   return {b ? b->busName : "", tmp.str()};
196 }
197
198 void Bridge::RegisterOnBridge(Accessible* obj)
199 {
200   assert(!obj->bridgeData.lock() || obj->bridgeData.lock() == data);
201   if(!obj->bridgeData.lock())
202   {
203     assert(data);
204     data->knownObjects.insert(obj);
205     obj->bridgeData = data;
206   }
207 }
208
209 bool Accessible::IsProxy()
210 {
211   return false;
212 }
213
214 Accessible* Accessible::GetDefaultLabel()
215 {
216   return this;
217 }
218
219 void Accessible::NotifyAccessibilityStateChange(Dali::Accessibility::States states, bool doRecursive)
220 {
221   if(auto b = GetBridgeData())
222   {
223     auto s = GetStates() & states;
224     for(auto i = 0u; i < s.size(); i++)
225     {
226       auto index = static_cast<Dali::Accessibility::State>(i);
227       if(s[index])
228         b->bridge->EmitStateChanged(this, index, 1, 0);
229     }
230     if(doRecursive)
231     {
232       auto children = GetChildren();
233       for(auto c : children)
234         c->NotifyAccessibilityStateChange(states, doRecursive);
235     }
236   }
237 }
238
239 void Accessible::FindWordSeparationsUtf8(const utf8_t* s, size_t length, const char* language, char* breaks)
240 {
241   set_wordbreaks_utf8(s, length, language, breaks);
242 }
243
244 void Accessible::FindLineSeparationsUtf8(const utf8_t* s, size_t length, const char* language, char* breaks)
245 {
246   set_linebreaks_utf8(s, length, language, breaks);
247 }