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>
35 #include <dali/internal/system/common/environment-variables.h>
36 #include <dali/devel-api/adaptor-framework/environment-variable.h>
38 using namespace Dali::Accessibility;
40 class BridgeImpl : public virtual BridgeBase,
41 public BridgeAccessible,
43 public BridgeComponent,
44 public BridgeCollection,
48 public BridgeEditableText
50 DBus::DBusClient listenOnAtspiEnabledSignalClient;
51 DBus::DBusClient registryClient, directReadingClient;
52 bool screenReaderEnabled = false;
53 bool isEnabled = false;
55 std::unordered_map<int32_t, std::function<void(std::string)>> directReadingCallbacks;
56 Dali::Actor highlightedActor;
57 std::function<void(Dali::Actor)> highlightClearAction;
62 listenOnAtspiEnabledSignalClient = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
64 listenOnAtspiEnabledSignalClient.addPropertyChangedEvent<bool>("ScreenReaderEnabled", [this](bool res) {
65 screenReaderEnabled = res;
66 if(screenReaderEnabled || isEnabled)
76 listenOnAtspiEnabledSignalClient.addPropertyChangedEvent<bool>("IsEnabled", [this](bool res) {
78 if(screenReaderEnabled || isEnabled)
89 Consumed Emit(KeyEventType type, unsigned int keyCode, const std::string& keyName, unsigned int timeStamp, bool isText) override
96 unsigned int evType = 0;
100 case KeyEventType::KEY_PRESSED:
105 case KeyEventType::KEY_RELEASED:
115 auto m = registryClient.method<bool(std::tuple<uint32_t, int32_t, int32_t, int32_t, int32_t, std::string, bool>)>("NotifyListenersSync");
116 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});
119 LOG() << result.getError().message;
122 return std::get<0>(result) ? Consumed::YES : Consumed::NO;
125 void Pause() override
132 directReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
135 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
141 void Resume() override
148 directReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
151 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
157 void StopReading(bool alsoNonDiscardable) override
164 directReadingClient.method<DBus::ValueOrError<void>(bool)>("StopReading").asyncCall([](DBus::ValueOrError<void> msg) {
167 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
173 void Say(const std::string& text, bool discardable, std::function<void(std::string)> callback) override
180 directReadingClient.method<DBus::ValueOrError<std::string, bool, int32_t>(std::string, bool)>("ReadCommand").asyncCall([=](DBus::ValueOrError<std::string, bool, int32_t> msg) {
183 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
187 directReadingCallbacks.emplace(std::get<2>(msg), callback);
194 void ForceDown() override
198 if(data->currentlyHighlightedActor && data->highlightActor)
200 data->currentlyHighlightedActor.Remove(data->highlightActor);
202 data->currentlyHighlightedActor = {};
203 data->highlightActor = {};
205 highlightedActor = {};
206 highlightClearAction = {};
207 BridgeAccessible::ForceDown();
209 directReadingClient = {};
210 directReadingCallbacks.clear();
213 void Terminate() override
217 data->currentlyHighlightedActor = {};
218 data->highlightActor = {};
221 listenOnAtspiEnabledSignalClient = {};
226 ForceUpResult ForceUp() override
228 if(BridgeAccessible::ForceUp() == ForceUpResult::ALREADY_UP)
230 return ForceUpResult::ALREADY_UP;
233 BridgeObject::RegisterInterfaces();
234 BridgeAccessible::RegisterInterfaces();
235 BridgeComponent::RegisterInterfaces();
236 BridgeCollection::RegisterInterfaces();
237 BridgeAction::RegisterInterfaces();
238 BridgeValue::RegisterInterfaces();
239 BridgeText::RegisterInterfaces();
240 BridgeEditableText::RegisterInterfaces();
242 RegisterOnBridge(&application);
244 registryClient = {AtspiDbusNameRegistry, AtspiDbusPathDec, AtspiDbusInterfaceDec, con};
245 directReadingClient = DBus::DBusClient{DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, con};
246 directReadingClient.addSignal<void(int32_t, std::string)>("ReadingStateChanged", [=](int32_t id, std::string readingState) {
247 auto it = directReadingCallbacks.find(id);
248 if(it != directReadingCallbacks.end())
250 it->second(readingState);
251 if(readingState != "ReadingPaused" && readingState != "ReadingResumed" && readingState != "ReadingStarted")
252 directReadingCallbacks.erase(it);
256 auto proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, AtspiDbusInterfaceSocket, con};
257 Address root{"", "root"};
258 auto res = proxy.method<Address(Address)>("Embed").call(root);
261 LOG() << "Call to Embed failed: " << res.getError().message;
264 application.parent.SetAddress(std::move(std::get<0>(res)));
269 return ForceUpResult::JUST_STARTED;
274 auto win = application.getActiveWindow();
277 win->Emit(WindowEvent::ACTIVATE, 0);
281 void EmitDeactivate()
283 auto win = application.getActiveWindow();
286 win->Emit(WindowEvent::DEACTIVATE, 0);
290 void ApplicationHidden() override
292 if(isShown && IsUp())
299 void ApplicationShown() override
301 if(!isShown && IsUp())
308 void Initialize() override
310 auto req = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
311 auto p = req.property<bool>("ScreenReaderEnabled").get();
314 screenReaderEnabled = std::get<0>(p);
316 p = req.property<bool>("IsEnabled").get();
319 isEnabled = std::get<0>(p);
321 if(screenReaderEnabled || isEnabled)
327 bool GetScreenReaderEnabled()
329 return screenReaderEnabled;
338 static Bridge* CreateBridge()
342 /* check environment variable first */
343 const char *envAtspiDisabled = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_DISABLE_ATSPI);
344 if (envAtspiDisabled && std::atoi(envAtspiDisabled) != 0)
346 return Dali::Accessibility::DummyBridge::GetInstance();
349 return new BridgeImpl;
351 catch (const std::exception&)
353 DALI_LOG_ERROR("Failed to initialize AT-SPI bridge");
354 return Dali::Accessibility::DummyBridge::GetInstance();
358 Bridge* Bridge::GetCurrentBridge()
360 static Bridge* bridge = CreateBridge();