Merge "Remove the replacing surface code" into devel/master
[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 StopReading(bool alsoNonDiscardable) override
156   {
157     if(!IsUp())
158     {
159       return;
160     }
161
162     directReadingClient.method<DBus::ValueOrError<void>(bool)>("StopReading").asyncCall([](DBus::ValueOrError<void> msg) {
163       if(!msg)
164       {
165         LOG() << "Direct reading command failed (" << msg.getError().message << ")";
166       }
167     },
168                                                                                         alsoNonDiscardable);
169   }
170
171   void Say(const std::string& text, bool discardable, std::function<void(std::string)> callback) override
172   {
173     if(!IsUp())
174     {
175       return;
176     }
177
178     directReadingClient.method<DBus::ValueOrError<std::string, bool, int32_t>(std::string, bool)>("ReadCommand").asyncCall([=](DBus::ValueOrError<std::string, bool, int32_t> msg) {
179       if(!msg)
180       {
181         LOG() << "Direct reading command failed (" << msg.getError().message << ")";
182       }
183       else if(callback)
184       {
185         directReadingCallbacks.emplace(std::get<2>(msg), callback);
186       }
187     },
188                                                                                                                            text,
189                                                                                                                            discardable);
190   }
191
192   void ForceDown() override
193   {
194     if(data)
195     {
196       if(data->currentlyHighlightedActor && data->highlightActor)
197       {
198         data->currentlyHighlightedActor.Remove(data->highlightActor);
199       }
200       data->currentlyHighlightedActor = {};
201       data->highlightActor            = {};
202     }
203     highlightedActor     = {};
204     highlightClearAction = {};
205     BridgeAccessible::ForceDown();
206     registryClient      = {};
207     directReadingClient = {};
208     directReadingCallbacks.clear();
209   }
210
211   void Terminate() override
212   {
213     if(data)
214     {
215       data->currentlyHighlightedActor = {};
216       data->highlightActor            = {};
217     }
218     ForceDown();
219     listenOnAtspiEnabledSignalClient = {};
220     dbusServer                       = {};
221     con                              = {};
222   }
223
224   ForceUpResult ForceUp() override
225   {
226     if(BridgeAccessible::ForceUp() == ForceUpResult::ALREADY_UP)
227     {
228       return ForceUpResult::ALREADY_UP;
229     }
230
231     BridgeObject::RegisterInterfaces();
232     BridgeAccessible::RegisterInterfaces();
233     BridgeComponent::RegisterInterfaces();
234     BridgeCollection::RegisterInterfaces();
235     BridgeAction::RegisterInterfaces();
236     BridgeValue::RegisterInterfaces();
237     BridgeText::RegisterInterfaces();
238     BridgeEditableText::RegisterInterfaces();
239
240     RegisterOnBridge(&application);
241
242     registryClient      = {AtspiDbusNameRegistry, AtspiDbusPathDec, AtspiDbusInterfaceDec, con};
243     directReadingClient = DBus::DBusClient{DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, con};
244     directReadingClient.addSignal<void(int32_t, std::string)>("ReadingStateChanged", [=](int32_t id, std::string readingState) {
245       auto it = directReadingCallbacks.find(id);
246       if(it != directReadingCallbacks.end())
247       {
248         it->second(readingState);
249         if(readingState != "ReadingPaused" && readingState != "ReadingResumed" && readingState != "ReadingStarted")
250           directReadingCallbacks.erase(it);
251       }
252     });
253
254     auto    proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, AtspiDbusInterfaceSocket, con};
255     Address root{"", "root"};
256     auto    res = proxy.method<Address(Address)>("Embed").call(root);
257     if(!res)
258     {
259       LOG() << "Call to Embed failed: " << res.getError().message;
260     }
261     assert(res);
262     application.parent.SetAddress(std::move(std::get<0>(res)));
263     if(isShown)
264     {
265       EmitActivate();
266     }
267     return ForceUpResult::JUST_STARTED;
268   }
269
270   void EmitActivate()
271   {
272     auto win = application.getActiveWindow();
273     if(win)
274     {
275       win->Emit(WindowEvent::ACTIVATE, 0);
276     }
277   }
278
279   void EmitDeactivate()
280   {
281     auto win = application.getActiveWindow();
282     if(win)
283     {
284       win->Emit(WindowEvent::DEACTIVATE, 0);
285     }
286   }
287
288   void ApplicationHidden() override
289   {
290     if(isShown && IsUp())
291     {
292       EmitDeactivate();
293     }
294     isShown = false;
295   }
296
297   void ApplicationShown() override
298   {
299     if(!isShown && IsUp())
300     {
301       EmitActivate();
302     }
303     isShown = true;
304   }
305
306   void Initialize() override
307   {
308     auto req = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
309     auto p   = req.property<bool>("ScreenReaderEnabled").get();
310     if(p)
311     {
312       screenReaderEnabled = std::get<0>(p);
313     }
314     p = req.property<bool>("IsEnabled").get();
315     if(p)
316     {
317       isEnabled = std::get<0>(p);
318     }
319     if(screenReaderEnabled || isEnabled)
320     {
321       ForceUp();
322     }
323   }
324
325   bool GetScreenReaderEnabled()
326   {
327     return screenReaderEnabled;
328   }
329
330   bool GetIsEnabled()
331   {
332     return isEnabled;
333   }
334 };
335
336 static Bridge* CreateBridge()
337 {
338   try
339   {
340     return new BridgeImpl;
341   }
342   catch (const std::exception&)
343   {
344     DALI_LOG_ERROR("Failed to initialize AT-SPI bridge");
345     return Dali::Accessibility::DummyBridge::GetInstance();
346   }
347 }
348
349 Bridge* Bridge::GetCurrentBridge()
350 {
351   static Bridge* bridge = CreateBridge();
352   return bridge;
353 }