Revert "[Tizen] atspi: disable atspi using environment variable"
[platform/core/uifw/dali-adaptor.git] / dali / internal / accessibility / bridge / bridge-impl.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 // EXTERNAL INCLUDES
21 #include <dali/integration-api/debug.h>
22 #include <iostream>
23 #include <unordered_map>
24
25 // INTERNAL INCLUDES
26 #include <dali/internal/accessibility/bridge/bridge-accessible.h>
27 #include <dali/internal/accessibility/bridge/bridge-action.h>
28 #include <dali/internal/accessibility/bridge/bridge-collection.h>
29 #include <dali/internal/accessibility/bridge/bridge-component.h>
30 #include <dali/internal/accessibility/bridge/bridge-editable-text.h>
31 #include <dali/internal/accessibility/bridge/bridge-object.h>
32 #include <dali/internal/accessibility/bridge/bridge-text.h>
33 #include <dali/internal/accessibility/bridge/bridge-value.h>
34 #include <dali/internal/accessibility/bridge/dummy-atspi.h>
35
36 using namespace Dali::Accessibility;
37
38 class BridgeImpl : public virtual BridgeBase,
39                    public BridgeAccessible,
40                    public BridgeObject,
41                    public BridgeComponent,
42                    public BridgeCollection,
43                    public BridgeAction,
44                    public BridgeValue,
45                    public BridgeText,
46                    public BridgeEditableText
47 {
48   DBus::DBusClient                                              listenOnAtspiEnabledSignalClient;
49   DBus::DBusClient                                              registryClient, directReadingClient;
50   bool                                                          screenReaderEnabled = false;
51   bool                                                          isEnabled           = false;
52   bool                                                          isShown             = false;
53   std::unordered_map<int32_t, std::function<void(std::string)>> directReadingCallbacks;
54   Dali::Actor                                                   highlightedActor;
55   std::function<void(Dali::Actor)>                              highlightClearAction;
56
57 public:
58   BridgeImpl()
59   {
60     listenOnAtspiEnabledSignalClient = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
61
62     listenOnAtspiEnabledSignalClient.addPropertyChangedEvent<bool>("ScreenReaderEnabled", [this](bool res) {
63       screenReaderEnabled = res;
64       if(screenReaderEnabled || isEnabled)
65       {
66         ForceUp();
67       }
68       else
69       {
70         ForceDown();
71       }
72     });
73
74     listenOnAtspiEnabledSignalClient.addPropertyChangedEvent<bool>("IsEnabled", [this](bool res) {
75       isEnabled = res;
76       if(screenReaderEnabled || isEnabled)
77       {
78         ForceUp();
79       }
80       else
81       {
82         ForceDown();
83       }
84     });
85   }
86
87   Consumed Emit(KeyEventType type, unsigned int keyCode, const std::string& keyName, unsigned int timeStamp, bool isText) override
88   {
89     if(!IsUp())
90     {
91       return Consumed::NO;
92     }
93
94     unsigned int evType = 0;
95
96     switch(type)
97     {
98       case KeyEventType::KEY_PRESSED:
99       {
100         evType = 0;
101         break;
102       }
103       case KeyEventType::KEY_RELEASED:
104       {
105         evType = 1;
106         break;
107       }
108       default:
109       {
110         return Consumed::NO;
111       }
112     }
113     auto m      = registryClient.method<bool(std::tuple<uint32_t, int32_t, int32_t, int32_t, int32_t, std::string, bool>)>("NotifyListenersSync");
114     auto result = m.call(std::tuple<uint32_t, int32_t, int32_t, int32_t, int32_t, std::string, bool>{evType, 0, static_cast<int32_t>(keyCode), 0, static_cast<int32_t>(timeStamp), keyName, isText ? 1 : 0});
115     if(!result)
116     {
117       LOG() << result.getError().message;
118       return Consumed::NO;
119     }
120     return std::get<0>(result) ? Consumed::YES : Consumed::NO;
121   }
122
123   void Pause() override
124   {
125     if(!IsUp())
126     {
127       return;
128     }
129
130     directReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
131       if(!msg)
132       {
133         LOG() << "Direct reading command failed (" << msg.getError().message << ")";
134       }
135     },
136                                                                                         true);
137   }
138
139   void Resume() override
140   {
141     if(!IsUp())
142     {
143       return;
144     }
145
146     directReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
147       if(!msg)
148       {
149         LOG() << "Direct reading command failed (" << msg.getError().message << ")";
150       }
151     },
152                                                                                         false);
153   }
154
155   void Say(const std::string& text, bool discardable, std::function<void(std::string)> callback) override
156   {
157     if(!IsUp())
158     {
159       return;
160     }
161
162     directReadingClient.method<DBus::ValueOrError<std::string, bool, int32_t>(std::string, bool)>("ReadCommand").asyncCall([=](DBus::ValueOrError<std::string, bool, int32_t> msg) {
163       if(!msg)
164       {
165         LOG() << "Direct reading command failed (" << msg.getError().message << ")";
166       }
167       else if(callback)
168       {
169         directReadingCallbacks.emplace(std::get<2>(msg), callback);
170       }
171     },
172                                                                                                                            text,
173                                                                                                                            discardable);
174   }
175
176   void ForceDown() override
177   {
178     if(data)
179     {
180       if(data->currentlyHighlightedActor && data->highlightActor)
181       {
182         data->currentlyHighlightedActor.Remove(data->highlightActor);
183       }
184       data->currentlyHighlightedActor = {};
185       data->highlightActor            = {};
186     }
187     highlightedActor     = {};
188     highlightClearAction = {};
189     BridgeAccessible::ForceDown();
190     registryClient      = {};
191     directReadingClient = {};
192     directReadingCallbacks.clear();
193   }
194
195   void Terminate() override
196   {
197     if(data)
198     {
199       data->currentlyHighlightedActor = {};
200       data->highlightActor            = {};
201     }
202     ForceDown();
203     listenOnAtspiEnabledSignalClient = {};
204     dbusServer                       = {};
205     con                              = {};
206   }
207
208   ForceUpResult ForceUp() override
209   {
210     if(BridgeAccessible::ForceUp() == ForceUpResult::ALREADY_UP)
211     {
212       return ForceUpResult::ALREADY_UP;
213     }
214
215     BridgeObject::RegisterInterfaces();
216     BridgeAccessible::RegisterInterfaces();
217     BridgeComponent::RegisterInterfaces();
218     BridgeCollection::RegisterInterfaces();
219     BridgeAction::RegisterInterfaces();
220     BridgeValue::RegisterInterfaces();
221     BridgeText::RegisterInterfaces();
222     BridgeEditableText::RegisterInterfaces();
223
224     RegisterOnBridge(&application);
225
226     registryClient      = {AtspiDbusNameRegistry, AtspiDbusPathDec, AtspiDbusInterfaceDec, con};
227     directReadingClient = DBus::DBusClient{DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, con};
228     directReadingClient.addSignal<void(int32_t, std::string)>("ReadingStateChanged", [=](int32_t id, std::string readingState) {
229       auto it = directReadingCallbacks.find(id);
230       if(it != directReadingCallbacks.end())
231       {
232         it->second(readingState);
233         if(readingState != "ReadingPaused" && readingState != "ReadingResumed" && readingState != "ReadingStarted")
234           directReadingCallbacks.erase(it);
235       }
236     });
237
238     auto    proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, AtspiDbusInterfaceSocket, con};
239     Address root{"", "root"};
240     auto    res = proxy.method<Address(Address)>("Embed").call(root);
241     if(!res)
242     {
243       LOG() << "Call to Embed failed: " << res.getError().message;
244     }
245     assert(res);
246     application.parent.SetAddress(std::move(std::get<0>(res)));
247     if(isShown)
248     {
249       EmitActivate();
250     }
251     return ForceUpResult::JUST_STARTED;
252   }
253
254   void EmitActivate()
255   {
256     auto win = application.getActiveWindow();
257     if(win)
258     {
259       win->Emit(WindowEvent::ACTIVATE, 0);
260     }
261   }
262
263   void EmitDeactivate()
264   {
265     auto win = application.getActiveWindow();
266     if(win)
267     {
268       win->Emit(WindowEvent::DEACTIVATE, 0);
269     }
270   }
271
272   void ApplicationHidden() override
273   {
274     if(isShown && IsUp())
275     {
276       EmitDeactivate();
277     }
278     isShown = false;
279   }
280
281   void ApplicationShown() override
282   {
283     if(!isShown && IsUp())
284     {
285       EmitActivate();
286     }
287     isShown = true;
288   }
289
290   void Initialize() override
291   {
292     auto req = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
293     auto p   = req.property<bool>("ScreenReaderEnabled").get();
294     if(p)
295     {
296       screenReaderEnabled = std::get<0>(p);
297     }
298     p = req.property<bool>("IsEnabled").get();
299     if(p)
300     {
301       isEnabled = std::get<0>(p);
302     }
303     if(screenReaderEnabled || isEnabled)
304     {
305       ForceUp();
306     }
307   }
308
309   bool GetScreenReaderEnabled()
310   {
311     return screenReaderEnabled;
312   }
313
314   bool GetIsEnabled()
315   {
316     return isEnabled;
317   }
318 };
319
320 static Bridge* CreateBridge()
321 {
322   try
323   {
324     return new BridgeImpl;
325   }
326   catch (const std::exception&)
327   {
328     DALI_LOG_ERROR("Failed to initialize AT-SPI bridge");
329     return Dali::Accessibility::DummyBridge::GetInstance();
330   }
331 }
332
333 Bridge* Bridge::GetCurrentBridge()
334 {
335   static Bridge* bridge = CreateBridge();
336   return bridge;
337 }