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 Say(const std::string& text, bool discardable, std::function<void(std::string)> callback) override
164 directReadingClient.method<DBus::ValueOrError<std::string, bool, int32_t>(std::string, bool)>("ReadCommand").asyncCall([=](DBus::ValueOrError<std::string, bool, int32_t> msg) {
167 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
171 directReadingCallbacks.emplace(std::get<2>(msg), callback);
178 void ForceDown() override
182 if(data->currentlyHighlightedActor && data->highlightActor)
184 data->currentlyHighlightedActor.Remove(data->highlightActor);
186 data->currentlyHighlightedActor = {};
187 data->highlightActor = {};
189 highlightedActor = {};
190 highlightClearAction = {};
191 BridgeAccessible::ForceDown();
193 directReadingClient = {};
194 directReadingCallbacks.clear();
197 void Terminate() override
201 data->currentlyHighlightedActor = {};
202 data->highlightActor = {};
205 listenOnAtspiEnabledSignalClient = {};
210 ForceUpResult ForceUp() override
212 if(BridgeAccessible::ForceUp() == ForceUpResult::ALREADY_UP)
214 return ForceUpResult::ALREADY_UP;
217 BridgeObject::RegisterInterfaces();
218 BridgeAccessible::RegisterInterfaces();
219 BridgeComponent::RegisterInterfaces();
220 BridgeCollection::RegisterInterfaces();
221 BridgeAction::RegisterInterfaces();
222 BridgeValue::RegisterInterfaces();
223 BridgeText::RegisterInterfaces();
224 BridgeEditableText::RegisterInterfaces();
226 RegisterOnBridge(&application);
228 registryClient = {AtspiDbusNameRegistry, AtspiDbusPathDec, AtspiDbusInterfaceDec, con};
229 directReadingClient = DBus::DBusClient{DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, con};
230 directReadingClient.addSignal<void(int32_t, std::string)>("ReadingStateChanged", [=](int32_t id, std::string readingState) {
231 auto it = directReadingCallbacks.find(id);
232 if(it != directReadingCallbacks.end())
234 it->second(readingState);
235 if(readingState != "ReadingPaused" && readingState != "ReadingResumed" && readingState != "ReadingStarted")
236 directReadingCallbacks.erase(it);
240 auto proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, AtspiDbusInterfaceSocket, con};
241 Address root{"", "root"};
242 auto res = proxy.method<Address(Address)>("Embed").call(root);
245 LOG() << "Call to Embed failed: " << res.getError().message;
248 application.parent.SetAddress(std::move(std::get<0>(res)));
253 return ForceUpResult::JUST_STARTED;
258 auto win = application.getActiveWindow();
261 win->Emit(WindowEvent::ACTIVATE, 0);
265 void EmitDeactivate()
267 auto win = application.getActiveWindow();
270 win->Emit(WindowEvent::DEACTIVATE, 0);
274 void ApplicationHidden() override
276 if(isShown && IsUp())
283 void ApplicationShown() override
285 if(!isShown && IsUp())
292 void Initialize() override
294 auto req = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
295 auto p = req.property<bool>("ScreenReaderEnabled").get();
298 screenReaderEnabled = std::get<0>(p);
300 p = req.property<bool>("IsEnabled").get();
303 isEnabled = std::get<0>(p);
305 if(screenReaderEnabled || isEnabled)
311 bool GetScreenReaderEnabled()
313 return screenReaderEnabled;
322 static Bridge* CreateBridge()
326 /* check environment variable first */
327 const char *envAtspiDisabled = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_DISABLE_ATSPI);
328 if (envAtspiDisabled && std::atoi(envAtspiDisabled) != 0)
330 return Dali::Accessibility::DummyBridge::GetInstance();
333 return new BridgeImpl;
335 catch (const std::exception&)
337 DALI_LOG_ERROR("Failed to initialize AT-SPI bridge");
338 return Dali::Accessibility::DummyBridge::GetInstance();
342 Bridge* Bridge::GetCurrentBridge()
344 static Bridge* bridge = CreateBridge();