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.
21 #include <dali/integration-api/debug.h>
23 #include <unordered_map>
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>
36 using namespace Dali::Accessibility;
38 class BridgeImpl : public virtual BridgeBase,
39 public BridgeAccessible,
41 public BridgeComponent,
42 public BridgeCollection,
46 public BridgeEditableText
48 DBus::DBusClient listenOnAtspiEnabledSignalClient;
49 DBus::DBusClient registryClient, directReadingClient;
50 bool screenReaderEnabled = false;
51 bool isEnabled = false;
53 std::unordered_map<int32_t, std::function<void(std::string)>> directReadingCallbacks;
54 Dali::Actor highlightedActor;
55 std::function<void(Dali::Actor)> highlightClearAction;
60 listenOnAtspiEnabledSignalClient = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
62 listenOnAtspiEnabledSignalClient.addPropertyChangedEvent<bool>("ScreenReaderEnabled", [this](bool res) {
63 screenReaderEnabled = res;
64 if(screenReaderEnabled || isEnabled)
74 listenOnAtspiEnabledSignalClient.addPropertyChangedEvent<bool>("IsEnabled", [this](bool res) {
76 if(screenReaderEnabled || isEnabled)
87 Consumed Emit(KeyEventType type, unsigned int keyCode, const std::string& keyName, unsigned int timeStamp, bool isText) override
94 unsigned int evType = 0;
98 case KeyEventType::KEY_PRESSED:
103 case KeyEventType::KEY_RELEASED:
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});
117 LOG() << result.getError().message;
120 return std::get<0>(result) ? Consumed::YES : Consumed::NO;
123 void Pause() override
130 directReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
133 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
139 void Resume() override
146 directReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
149 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
155 void StopReading(bool alsoNonDiscardable) override
162 directReadingClient.method<DBus::ValueOrError<void>(bool)>("StopReading").asyncCall([](DBus::ValueOrError<void> msg) {
165 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
171 void Say(const std::string& text, bool discardable, std::function<void(std::string)> callback) override
178 directReadingClient.method<DBus::ValueOrError<std::string, bool, int32_t>(std::string, bool)>("ReadCommand").asyncCall([=](DBus::ValueOrError<std::string, bool, int32_t> msg) {
181 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
185 directReadingCallbacks.emplace(std::get<2>(msg), callback);
192 void ForceDown() override
196 if(data->currentlyHighlightedActor && data->highlightActor)
198 data->currentlyHighlightedActor.Remove(data->highlightActor);
200 data->currentlyHighlightedActor = {};
201 data->highlightActor = {};
203 highlightedActor = {};
204 highlightClearAction = {};
205 BridgeAccessible::ForceDown();
207 directReadingClient = {};
208 directReadingCallbacks.clear();
211 void Terminate() override
215 data->currentlyHighlightedActor = {};
216 data->highlightActor = {};
219 listenOnAtspiEnabledSignalClient = {};
224 ForceUpResult ForceUp() override
226 if(BridgeAccessible::ForceUp() == ForceUpResult::ALREADY_UP)
228 return ForceUpResult::ALREADY_UP;
231 BridgeObject::RegisterInterfaces();
232 BridgeAccessible::RegisterInterfaces();
233 BridgeComponent::RegisterInterfaces();
234 BridgeCollection::RegisterInterfaces();
235 BridgeAction::RegisterInterfaces();
236 BridgeValue::RegisterInterfaces();
237 BridgeText::RegisterInterfaces();
238 BridgeEditableText::RegisterInterfaces();
240 RegisterOnBridge(&application);
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())
248 it->second(readingState);
249 if(readingState != "ReadingPaused" && readingState != "ReadingResumed" && readingState != "ReadingStarted")
250 directReadingCallbacks.erase(it);
254 auto proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, AtspiDbusInterfaceSocket, con};
255 Address root{"", "root"};
256 auto res = proxy.method<Address(Address)>("Embed").call(root);
259 LOG() << "Call to Embed failed: " << res.getError().message;
262 application.parent.SetAddress(std::move(std::get<0>(res)));
267 return ForceUpResult::JUST_STARTED;
272 auto win = application.getActiveWindow();
275 win->Emit(WindowEvent::ACTIVATE, 0);
279 void EmitDeactivate()
281 auto win = application.getActiveWindow();
284 win->Emit(WindowEvent::DEACTIVATE, 0);
288 void ApplicationHidden() override
290 if(isShown && IsUp())
297 void ApplicationShown() override
299 if(!isShown && IsUp())
306 void Initialize() override
308 auto req = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
309 auto p = req.property<bool>("ScreenReaderEnabled").get();
312 screenReaderEnabled = std::get<0>(p);
314 p = req.property<bool>("IsEnabled").get();
317 isEnabled = std::get<0>(p);
319 if(screenReaderEnabled || isEnabled)
325 bool GetScreenReaderEnabled()
327 return screenReaderEnabled;
336 static Bridge* CreateBridge()
340 return new BridgeImpl;
342 catch (const std::exception&)
344 DALI_LOG_ERROR("Failed to initialize AT-SPI bridge");
345 return Dali::Accessibility::DummyBridge::GetInstance();
349 Bridge* Bridge::GetCurrentBridge()
351 static Bridge* bridge = CreateBridge();