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/bridge-application.h>
40 #include <dali/internal/accessibility/bridge/dummy-atspi.h>
41 #include <dali/internal/adaptor/common/adaptor-impl.h>
42 #include <dali/internal/system/common/environment-variables.h>
44 using namespace Dali::Accessibility;
47 * @brief The BridgeImpl class is to implement some Bridge functions.
49 class BridgeImpl : public virtual BridgeBase,
50 public BridgeAccessible,
52 public BridgeComponent,
53 public BridgeCollection,
57 public BridgeEditableText,
58 public BridgeSelection,
59 public BridgeApplication
61 DBus::DBusClient mAccessibilityStatusClient;
62 DBus::DBusClient mRegistryClient;
63 DBus::DBusClient mDirectReadingClient;
64 bool mIsScreenReaderEnabled = false;
65 bool mIsEnabled = false;
66 bool mIsShown = false;
67 std::unordered_map<int32_t, std::function<void(std::string)>> mDirectReadingCallbacks;
68 Dali::Actor mHighlightedActor;
69 std::function<void(Dali::Actor)> mHighlightClearAction;
70 Dali::CallbackBase* mIdleCallback = NULL;
78 * @copydoc Dali::Accessibility::Bridge::Emit()
80 Consumed Emit(KeyEventType type, unsigned int keyCode, const std::string& keyName, unsigned int timeStamp, bool isText) override
87 unsigned int keyType = 0;
91 case KeyEventType::KEY_PRESSED:
96 case KeyEventType::KEY_RELEASED:
107 auto methodObject = mRegistryClient.method<bool(std::tuple<uint32_t, int32_t, int32_t, int32_t, int32_t, std::string, bool>)>("NotifyListenersSync");
108 auto result = methodObject.call(std::tuple<uint32_t, int32_t, int32_t, int32_t, int32_t, std::string, bool>{keyType, 0, static_cast<int32_t>(keyCode), 0, static_cast<int32_t>(timeStamp), keyName, isText ? 1 : 0});
111 LOG() << result.getError().message;
114 return std::get<0>(result) ? Consumed::YES : Consumed::NO;
118 * @copydoc Dali::Accessibility::Bridge::Pause()
120 void Pause() override
127 mDirectReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
130 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
137 * @copydoc Dali::Accessibility::Bridge::Resume()
139 void Resume() override
146 mDirectReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
149 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
156 * @copydoc Dali::Accessibility::Bridge::StopReading()
158 void StopReading(bool alsoNonDiscardable) override
165 mDirectReadingClient.method<DBus::ValueOrError<void>(bool)>("StopReading").asyncCall([](DBus::ValueOrError<void> msg) {
168 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
175 * @copydoc Dali::Accessibility::Bridge::Say()
177 void Say(const std::string& text, bool discardable, std::function<void(std::string)> callback) override
184 mDirectReadingClient.method<DBus::ValueOrError<std::string, bool, int32_t>(std::string, bool)>("ReadCommand").asyncCall([=](DBus::ValueOrError<std::string, bool, int32_t> msg) {
187 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
191 mDirectReadingCallbacks.emplace(std::get<2>(msg), callback);
199 * @copydoc Dali::Accessibility::Bridge::ForceDown()
201 void ForceDown() override
205 if(mData->mCurrentlyHighlightedActor && mData->mHighlightActor)
207 mData->mCurrentlyHighlightedActor.Remove(mData->mHighlightActor);
209 mData->mCurrentlyHighlightedActor = {};
210 mData->mHighlightActor = {};
212 mHighlightedActor = {};
213 mHighlightClearAction = {};
214 BridgeAccessible::ForceDown();
215 mRegistryClient = {};
216 mDirectReadingClient = {};
217 mDirectReadingCallbacks.clear();
218 mApplication.mChildren.clear();
219 mApplication.mWindows.clear();
223 * @copydoc Dali::Accessibility::Bridge::Terminate()
225 void Terminate() override
229 mData->mCurrentlyHighlightedActor = {};
230 mData->mHighlightActor = {};
233 if((NULL != mIdleCallback) && Dali::Adaptor::IsAvailable())
235 Dali::Adaptor::Get().RemoveIdle(mIdleCallback);
237 mAccessibilityStatusClient = {};
243 * @copydoc Dali::Accessibility::Bridge::ForceUp()
245 ForceUpResult ForceUp() override
247 if(BridgeAccessible::ForceUp() == ForceUpResult::ALREADY_UP)
249 return ForceUpResult::ALREADY_UP;
252 BridgeObject::RegisterInterfaces();
253 BridgeAccessible::RegisterInterfaces();
254 BridgeComponent::RegisterInterfaces();
255 BridgeCollection::RegisterInterfaces();
256 BridgeAction::RegisterInterfaces();
257 BridgeValue::RegisterInterfaces();
258 BridgeText::RegisterInterfaces();
259 BridgeEditableText::RegisterInterfaces();
260 BridgeSelection::RegisterInterfaces();
261 BridgeApplication::RegisterInterfaces();
263 RegisterOnBridge(&mApplication);
265 mRegistryClient = {AtspiDbusNameRegistry, AtspiDbusPathDec, AtspiDbusInterfaceDec, mConnectionPtr};
266 mDirectReadingClient = DBus::DBusClient{DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, mConnectionPtr};
268 mDirectReadingClient.addSignal<void(int32_t, std::string)>("ReadingStateChanged", [=](int32_t id, std::string readingState) {
269 auto it = mDirectReadingCallbacks.find(id);
270 if(it != mDirectReadingCallbacks.end())
272 it->second(readingState);
273 if(readingState != "ReadingPaused" && readingState != "ReadingResumed" && readingState != "ReadingStarted")
275 mDirectReadingCallbacks.erase(it);
280 auto proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, AtspiDbusInterfaceSocket, mConnectionPtr};
281 Address root{"", "root"};
282 auto res = proxy.method<Address(Address)>("Embed").call(root);
285 LOG() << "Call to Embed failed: " << res.getError().message;
289 mApplication.mParent.SetAddress(std::move(std::get<0>(res)));
293 auto rootLayer = Dali::Stage::GetCurrent().GetRootLayer();
294 auto window = Dali::DevelWindow::Get(rootLayer);
295 EmitActivate(window); // Currently, sends a signal that the default window is activated here.
297 return ForceUpResult::JUST_STARTED;
301 * @brief Sends a signal to dbus that the window is shown.
303 * @param[in] window The window to be shown
304 * @see Accessible::EmitShowing() and BridgeObject::EmitStateChanged()
306 void EmitShown(Dali::Window window)
308 auto windowAccessible = mApplication.GetWindowAccessible(window);
311 windowAccessible->EmitShowing(true);
316 * @brief Sends a signal to dbus that the window is hidden.
318 * @param[in] window The window to be hidden
319 * @see Accessible::EmitShowing() and BridgeObject::EmitStateChanged()
321 void EmitHidden(Dali::Window window)
323 auto windowAccessible = mApplication.GetWindowAccessible(window);
326 windowAccessible->EmitShowing(false);
331 * @brief Sends a signal to dbus that the window is activated.
333 * @param[in] window The window to be activated
334 * @see BridgeObject::Emit()
336 void EmitActivate(Dali::Window window)
338 auto windowAccessible = mApplication.GetWindowAccessible(window);
341 windowAccessible->Emit(WindowEvent::ACTIVATE, 0);
346 * @brief Sends a signal to dbus that the window is deactivated.
348 * @param[in] window The window to be deactivated
349 * @see BridgeObject::Emit()
351 void EmitDeactivate(Dali::Window window)
353 auto windowAccessible = mApplication.GetWindowAccessible(window);
356 windowAccessible->Emit(WindowEvent::DEACTIVATE, 0);
361 * @copydoc Dali::Accessibility::Bridge::WindowShown()
363 void WindowShown(Dali::Window window) override
365 if(!mIsShown && IsUp())
373 * @copydoc Dali::Accessibility::Bridge::WindowHidden()
375 void WindowHidden(Dali::Window window) override
377 if(mIsShown && IsUp())
385 * @copydoc Dali::Accessibility::Bridge::WindowFocused()
387 void WindowFocused(Dali::Window window) override
389 if(mIsShown && IsUp())
391 EmitActivate(window);
396 * @copydoc Dali::Accessibility::Bridge::WindowUnfocused()
398 void WindowUnfocused(Dali::Window window) override
400 if(mIsShown && IsUp())
402 EmitDeactivate(window);
406 void ReadIsEnabledProperty()
408 mAccessibilityStatusClient.property<bool>("IsEnabled").asyncGet([this](DBus::ValueOrError<bool> msg) {
411 DALI_LOG_ERROR("Get IsEnabled property error: %s\n", msg.getError().message.c_str());
412 if(msg.getError().errorType == DBus::ErrorType::INVALID_REPLY)
414 ReadIsEnabledProperty();
418 mIsEnabled = std::get<0>(msg);
426 void ListenIsEnabledProperty()
428 mAccessibilityStatusClient.addPropertyChangedEvent<bool>("IsEnabled", [this](bool res) {
430 if(mIsScreenReaderEnabled || mIsEnabled)
441 void ReadScreenReaderEnabledProperty()
443 mAccessibilityStatusClient.property<bool>("ScreenReaderEnabled").asyncGet([this](DBus::ValueOrError<bool> msg) {
446 DALI_LOG_ERROR("Get ScreenReaderEnabled property error: %s\n", msg.getError().message.c_str());
447 if(msg.getError().errorType == DBus::ErrorType::INVALID_REPLY)
449 ReadScreenReaderEnabledProperty();
453 mIsScreenReaderEnabled = std::get<0>(msg);
454 if(mIsScreenReaderEnabled)
461 void ListenScreenReaderEnabledProperty()
463 mAccessibilityStatusClient.addPropertyChangedEvent<bool>("ScreenReaderEnabled", [this](bool res) {
464 mIsScreenReaderEnabled = res;
465 if(mIsScreenReaderEnabled || mIsEnabled)
476 void ReadAndListenProperties()
478 ReadIsEnabledProperty();
479 ListenIsEnabledProperty();
481 ReadScreenReaderEnabledProperty();
482 ListenScreenReaderEnabledProperty();
485 bool InitializeAccessibilityStatusClient()
487 mAccessibilityStatusClient = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
489 if (!mAccessibilityStatusClient)
491 DALI_LOG_ERROR("Accessibility Status DbusClient is not ready\n");
500 if ( InitializeAccessibilityStatusClient() )
502 ReadAndListenProperties();
503 mIdleCallback = NULL;
511 * @copydoc Dali::Accessibility::Bridge::Initialize()
513 void Initialize() override
515 if ( InitializeAccessibilityStatusClient() )
517 ReadAndListenProperties();
521 // Initialize failed. Try it again on Idle
522 if( Dali::Adaptor::IsAvailable() )
524 Dali::Adaptor& adaptor = Dali::Adaptor::Get();
525 if( NULL == mIdleCallback )
527 mIdleCallback = MakeCallback( this, &BridgeImpl::OnIdleSignal );
528 adaptor.AddIdle( mIdleCallback, true );
534 * @copydoc Dali::Accessibility::Bridge::GetScreenReaderEnabled()
536 bool GetScreenReaderEnabled() override
538 return mIsScreenReaderEnabled;
542 * @copydoc Dali::Accessibility::Bridge::IsEnabled()
544 bool IsEnabled() override
550 namespace // unnamed namespace
553 bool INITIALIZED_BRIDGE = false;
556 * @brief Creates BridgeImpl instance.
558 * @return The BridgeImpl instance
559 * @note This method is to check environment variable first. If ATSPI is disable using env, it returns dummy bridge instance.
561 Bridge* CreateBridge()
563 INITIALIZED_BRIDGE = true;
567 /* check environment variable first */
568 const char* envAtspiDisabled = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_DISABLE_ATSPI);
569 if(envAtspiDisabled && std::atoi(envAtspiDisabled) != 0)
571 return Dali::Accessibility::DummyBridge::GetInstance();
574 return new BridgeImpl;
576 catch(const std::exception&)
578 DALI_LOG_ERROR("Failed to initialize AT-SPI bridge");
579 return Dali::Accessibility::DummyBridge::GetInstance();
583 } // unnamed namespace
585 // Dali::Accessibility::Bridge class implementation
587 Bridge* Bridge::GetCurrentBridge()
589 static Bridge* bridge;
595 else if(mAutoInitState == AutoInitState::ENABLED)
597 bridge = CreateBridge();
599 /* check environment variable for suppressing screen-reader */
600 const char* envSuppressScreenReader = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_SUPPRESS_SCREEN_READER);
601 if(envSuppressScreenReader && std::atoi(envSuppressScreenReader) != 0)
603 bridge->SuppressScreenReader(true);
609 return Dali::Accessibility::DummyBridge::GetInstance();
612 void Bridge::DisableAutoInit()
614 if(INITIALIZED_BRIDGE)
616 DALI_LOG_ERROR("Bridge::DisableAutoInit() called after bridge auto-initialization");
619 mAutoInitState = AutoInitState::DISABLED;
622 void Bridge::EnableAutoInit()
624 mAutoInitState = AutoInitState::ENABLED;
626 if(INITIALIZED_BRIDGE)
631 auto rootLayer = Dali::Stage::GetCurrent().GetRootLayer(); // A root layer of the default window.
632 auto window = Dali::DevelWindow::Get(rootLayer);
633 auto applicationName = Dali::Internal::Adaptor::Adaptor::GetApplicationPackageName();
635 auto accessible = Accessibility::Accessible::Get(rootLayer, true);
637 auto* bridge = Bridge::GetCurrentBridge();
638 bridge->AddTopLevelWindow(accessible);
639 bridge->SetApplicationName(applicationName);
640 bridge->Initialize();
642 if(window && window.IsVisible())
644 bridge->WindowShown(window);