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/devel-api/common/stage.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/actors/layer.h>
25 #include <unordered_map>
28 #include <dali/devel-api/adaptor-framework/environment-variable.h>
29 #include <dali/devel-api/adaptor-framework/window-devel.h>
30 #include <dali/internal/accessibility/bridge/bridge-accessible.h>
31 #include <dali/internal/accessibility/bridge/bridge-action.h>
32 #include <dali/internal/accessibility/bridge/bridge-collection.h>
33 #include <dali/internal/accessibility/bridge/bridge-component.h>
34 #include <dali/internal/accessibility/bridge/bridge-editable-text.h>
35 #include <dali/internal/accessibility/bridge/bridge-object.h>
36 #include <dali/internal/accessibility/bridge/bridge-selection.h>
37 #include <dali/internal/accessibility/bridge/bridge-text.h>
38 #include <dali/internal/accessibility/bridge/bridge-value.h>
39 #include <dali/internal/accessibility/bridge/dummy-atspi.h>
40 #include <dali/internal/adaptor/common/adaptor-impl.h>
41 #include <dali/internal/system/common/environment-variables.h>
43 using namespace Dali::Accessibility;
45 class BridgeImpl : public virtual BridgeBase,
46 public BridgeAccessible,
48 public BridgeComponent,
49 public BridgeCollection,
53 public BridgeEditableText,
54 public BridgeSelection
56 DBus::DBusClient listenOnAtspiEnabledSignalClient;
57 DBus::DBusClient registryClient, directReadingClient;
58 bool screenReaderEnabled = false;
59 bool isEnabled = false;
61 std::unordered_map<int32_t, std::function<void(std::string)>> directReadingCallbacks;
62 Dali::Actor highlightedActor;
63 std::function<void(Dali::Actor)> highlightClearAction;
68 listenOnAtspiEnabledSignalClient = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
70 listenOnAtspiEnabledSignalClient.addPropertyChangedEvent<bool>("ScreenReaderEnabled", [this](bool res) {
71 screenReaderEnabled = res;
72 if(screenReaderEnabled || isEnabled)
82 listenOnAtspiEnabledSignalClient.addPropertyChangedEvent<bool>("IsEnabled", [this](bool res) {
84 if(screenReaderEnabled || isEnabled)
95 Consumed Emit(KeyEventType type, unsigned int keyCode, const std::string& keyName, unsigned int timeStamp, bool isText) override
102 unsigned int evType = 0;
106 case KeyEventType::KEY_PRESSED:
111 case KeyEventType::KEY_RELEASED:
121 auto m = registryClient.method<bool(std::tuple<uint32_t, int32_t, int32_t, int32_t, int32_t, std::string, bool>)>("NotifyListenersSync");
122 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});
125 LOG() << result.getError().message;
128 return std::get<0>(result) ? Consumed::YES : Consumed::NO;
131 void Pause() override
138 directReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
141 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
147 void Resume() override
154 directReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
157 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
163 void StopReading(bool alsoNonDiscardable) override
170 directReadingClient.method<DBus::ValueOrError<void>(bool)>("StopReading").asyncCall([](DBus::ValueOrError<void> msg) {
173 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
179 void Say(const std::string& text, bool discardable, std::function<void(std::string)> callback) override
186 directReadingClient.method<DBus::ValueOrError<std::string, bool, int32_t>(std::string, bool)>("ReadCommand").asyncCall([=](DBus::ValueOrError<std::string, bool, int32_t> msg) {
189 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
193 directReadingCallbacks.emplace(std::get<2>(msg), callback);
200 void ForceDown() override
204 if(data->currentlyHighlightedActor && data->highlightActor)
206 data->currentlyHighlightedActor.Remove(data->highlightActor);
208 data->currentlyHighlightedActor = {};
209 data->highlightActor = {};
211 highlightedActor = {};
212 highlightClearAction = {};
213 BridgeAccessible::ForceDown();
215 directReadingClient = {};
216 directReadingCallbacks.clear();
219 void Terminate() override
223 data->currentlyHighlightedActor = {};
224 data->highlightActor = {};
227 listenOnAtspiEnabledSignalClient = {};
232 ForceUpResult ForceUp() override
234 if(BridgeAccessible::ForceUp() == ForceUpResult::ALREADY_UP)
236 return ForceUpResult::ALREADY_UP;
239 BridgeObject::RegisterInterfaces();
240 BridgeAccessible::RegisterInterfaces();
241 BridgeComponent::RegisterInterfaces();
242 BridgeCollection::RegisterInterfaces();
243 BridgeAction::RegisterInterfaces();
244 BridgeValue::RegisterInterfaces();
245 BridgeText::RegisterInterfaces();
246 BridgeEditableText::RegisterInterfaces();
247 BridgeSelection::RegisterInterfaces();
249 RegisterOnBridge(&application);
251 registryClient = {AtspiDbusNameRegistry, AtspiDbusPathDec, AtspiDbusInterfaceDec, con};
252 directReadingClient = DBus::DBusClient{DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, con};
253 directReadingClient.addSignal<void(int32_t, std::string)>("ReadingStateChanged", [=](int32_t id, std::string readingState) {
254 auto it = directReadingCallbacks.find(id);
255 if(it != directReadingCallbacks.end())
257 it->second(readingState);
258 if(readingState != "ReadingPaused" && readingState != "ReadingResumed" && readingState != "ReadingStarted")
259 directReadingCallbacks.erase(it);
263 auto proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, AtspiDbusInterfaceSocket, con};
264 Address root{"", "root"};
265 auto res = proxy.method<Address(Address)>("Embed").call(root);
268 LOG() << "Call to Embed failed: " << res.getError().message;
271 application.parent.SetAddress(std::move(std::get<0>(res)));
276 return ForceUpResult::JUST_STARTED;
281 auto win = application.getActiveWindow();
284 win->Emit(WindowEvent::ACTIVATE, 0);
288 void EmitDeactivate()
290 auto win = application.getActiveWindow();
293 win->Emit(WindowEvent::DEACTIVATE, 0);
297 void ApplicationHidden() override
299 if(isShown && IsUp())
306 void ApplicationShown() override
308 if(!isShown && IsUp())
315 void Initialize() override
317 auto req = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
318 auto p = req.property<bool>("ScreenReaderEnabled").get();
321 screenReaderEnabled = std::get<0>(p);
323 p = req.property<bool>("IsEnabled").get();
326 isEnabled = std::get<0>(p);
328 if(screenReaderEnabled || isEnabled)
334 bool GetScreenReaderEnabled()
336 return screenReaderEnabled;
345 static bool bridgeInitialized;
347 static Bridge* CreateBridge()
349 bridgeInitialized = true;
353 /* check environment variable first */
354 const char* envAtspiDisabled = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_DISABLE_ATSPI);
355 if(envAtspiDisabled && std::atoi(envAtspiDisabled) != 0)
357 return Dali::Accessibility::DummyBridge::GetInstance();
360 return new BridgeImpl;
362 catch(const std::exception&)
364 DALI_LOG_ERROR("Failed to initialize AT-SPI bridge");
365 return Dali::Accessibility::DummyBridge::GetInstance();
369 Bridge* Bridge::GetCurrentBridge()
371 static Bridge* bridge;
377 else if(autoInitState == AutoInitState::ENABLED)
379 bridge = CreateBridge();
381 /* check environment variable for suppressing screen-reader */
382 const char* envSuppressScreenReader = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_SUPPRESS_SCREEN_READER);
383 if(envSuppressScreenReader && std::atoi(envSuppressScreenReader) != 0)
385 bridge->SuppressScreenReader(true);
391 return Dali::Accessibility::DummyBridge::GetInstance();
394 void Bridge::DisableAutoInit()
396 if(bridgeInitialized)
398 DALI_LOG_ERROR("Bridge::DisableAutoInit() called after bridge auto-initialization");
401 autoInitState = AutoInitState::DISABLED;
404 void Bridge::EnableAutoInit()
406 autoInitState = AutoInitState::ENABLED;
408 if(bridgeInitialized)
413 auto rootLayer = Dali::Stage::GetCurrent().GetRootLayer();
414 auto window = Dali::DevelWindow::Get(rootLayer);
415 auto applicationName = Dali::Internal::Adaptor::Adaptor::GetApplicationPackageName();
417 auto* bridge = Bridge::GetCurrentBridge();
418 bridge->AddTopLevelWindow(Dali::Accessibility::Accessible::Get(rootLayer, true));
419 bridge->SetApplicationName(applicationName);
420 bridge->Initialize();
422 if(window && window.IsVisible())
424 bridge->ApplicationShown();