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;
46 namespace // unnamed namespace
49 const int RETRY_INTERVAL = 1000;
51 } // unnamed namespace
54 * @brief The BridgeImpl class is to implement some Bridge functions.
56 class BridgeImpl : public virtual BridgeBase,
57 public BridgeAccessible,
59 public BridgeComponent,
60 public BridgeCollection,
64 public BridgeEditableText,
65 public BridgeSelection,
66 public BridgeApplication
68 DBus::DBusClient mAccessibilityStatusClient;
69 DBus::DBusClient mRegistryClient;
70 DBus::DBusClient mDirectReadingClient;
71 bool mIsScreenReaderEnabled = false;
72 bool mIsEnabled = false;
73 bool mIsShown = false;
74 std::unordered_map<int32_t, std::function<void(std::string)>> mDirectReadingCallbacks;
75 Dali::Actor mHighlightedActor;
76 std::function<void(Dali::Actor)> mHighlightClearAction;
77 Dali::CallbackBase* mIdleCallback = NULL;
78 Dali::Timer mInitializeTimer;
79 Dali::Timer mReadIsEnabledTimer;
80 Dali::Timer mReadScreenReaderEnabledTimer;
88 * @copydoc Dali::Accessibility::Bridge::Emit()
90 Consumed Emit(KeyEventType type, unsigned int keyCode, const std::string& keyName, unsigned int timeStamp, bool isText) override
97 unsigned int keyType = 0;
101 case KeyEventType::KEY_PRESSED:
106 case KeyEventType::KEY_RELEASED:
117 auto methodObject = mRegistryClient.method<bool(std::tuple<uint32_t, int32_t, int32_t, int32_t, int32_t, std::string, bool>)>("NotifyListenersSync");
118 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});
121 LOG() << result.getError().message;
124 return std::get<0>(result) ? Consumed::YES : Consumed::NO;
128 * @copydoc Dali::Accessibility::Bridge::Pause()
130 void Pause() override
137 mDirectReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
140 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
147 * @copydoc Dali::Accessibility::Bridge::Resume()
149 void Resume() override
156 mDirectReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
159 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
166 * @copydoc Dali::Accessibility::Bridge::StopReading()
168 void StopReading(bool alsoNonDiscardable) override
175 mDirectReadingClient.method<DBus::ValueOrError<void>(bool)>("StopReading").asyncCall([](DBus::ValueOrError<void> msg) {
178 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
185 * @copydoc Dali::Accessibility::Bridge::Say()
187 void Say(const std::string& text, bool discardable, std::function<void(std::string)> callback) override
194 mDirectReadingClient.method<DBus::ValueOrError<std::string, bool, int32_t>(std::string, bool)>("ReadCommand").asyncCall([=](DBus::ValueOrError<std::string, bool, int32_t> msg) {
197 LOG() << "Direct reading command failed (" << msg.getError().message << ")";
201 mDirectReadingCallbacks.emplace(std::get<2>(msg), callback);
209 * @copydoc Dali::Accessibility::Bridge::ForceDown()
211 void ForceDown() override
215 if(mData->mCurrentlyHighlightedActor && mData->mHighlightActor)
217 mData->mCurrentlyHighlightedActor.Remove(mData->mHighlightActor);
219 mData->mCurrentlyHighlightedActor = {};
220 mData->mHighlightActor = {};
222 mDisabledSignal.Emit();
224 mHighlightedActor = {};
225 mHighlightClearAction = {};
226 BridgeAccessible::ForceDown();
227 mRegistryClient = {};
228 mDirectReadingClient = {};
229 mDirectReadingCallbacks.clear();
230 mApplication.mChildren.clear();
237 mInitializeTimer.Stop();
240 if(mReadIsEnabledTimer)
242 mReadIsEnabledTimer.Stop();
245 if(mReadScreenReaderEnabledTimer)
247 mReadScreenReaderEnabledTimer.Stop();
252 * @copydoc Dali::Accessibility::Bridge::Terminate()
254 void Terminate() override
258 mData->mCurrentlyHighlightedActor = {};
259 mData->mHighlightActor = {};
263 if((NULL != mIdleCallback) && Dali::Adaptor::IsAvailable())
265 Dali::Adaptor::Get().RemoveIdle(mIdleCallback);
267 mAccessibilityStatusClient = {};
273 * @copydoc Dali::Accessibility::Bridge::ForceUp()
275 ForceUpResult ForceUp() override
277 if(BridgeAccessible::ForceUp() == ForceUpResult::ALREADY_UP)
279 return ForceUpResult::ALREADY_UP;
282 BridgeObject::RegisterInterfaces();
283 BridgeAccessible::RegisterInterfaces();
284 BridgeComponent::RegisterInterfaces();
285 BridgeCollection::RegisterInterfaces();
286 BridgeAction::RegisterInterfaces();
287 BridgeValue::RegisterInterfaces();
288 BridgeText::RegisterInterfaces();
289 BridgeEditableText::RegisterInterfaces();
290 BridgeSelection::RegisterInterfaces();
291 BridgeApplication::RegisterInterfaces();
293 RegisterOnBridge(&mApplication);
295 mRegistryClient = {AtspiDbusNameRegistry, AtspiDbusPathDec, AtspiDbusInterfaceDec, mConnectionPtr};
296 mDirectReadingClient = DBus::DBusClient{DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, mConnectionPtr};
298 mDirectReadingClient.addSignal<void(int32_t, std::string)>("ReadingStateChanged", [=](int32_t id, std::string readingState) {
299 auto it = mDirectReadingCallbacks.find(id);
300 if(it != mDirectReadingCallbacks.end())
302 it->second(readingState);
303 if(readingState != "ReadingPaused" && readingState != "ReadingResumed" && readingState != "ReadingStarted")
305 mDirectReadingCallbacks.erase(it);
310 auto proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, AtspiDbusInterfaceSocket, mConnectionPtr};
311 Address root{"", "root"};
312 auto res = proxy.method<Address(Address)>("Embed").call(root);
315 LOG() << "Call to Embed failed: " << res.getError().message;
319 mApplication.mParent.SetAddress(std::move(std::get<0>(res)));
321 mEnabledSignal.Emit();
323 return ForceUpResult::JUST_STARTED;
327 * @brief Sends a signal to dbus that the window is shown.
329 * @param[in] window The window to be shown
330 * @see Accessible::EmitShowing() and BridgeObject::EmitStateChanged()
332 void EmitShown(Dali::Window window)
334 auto windowAccessible = mApplication.GetWindowAccessible(window);
337 windowAccessible->EmitShowing(true);
342 * @brief Sends a signal to dbus that the window is hidden.
344 * @param[in] window The window to be hidden
345 * @see Accessible::EmitShowing() and BridgeObject::EmitStateChanged()
347 void EmitHidden(Dali::Window window)
349 auto windowAccessible = mApplication.GetWindowAccessible(window);
352 windowAccessible->EmitShowing(false);
357 * @brief Sends a signal to dbus that the window is activated.
359 * @param[in] window The window to be activated
360 * @see BridgeObject::Emit()
362 void EmitActivate(Dali::Window window)
364 auto windowAccessible = mApplication.GetWindowAccessible(window);
367 windowAccessible->Emit(WindowEvent::ACTIVATE, 0);
372 * @brief Sends a signal to dbus that the window is deactivated.
374 * @param[in] window The window to be deactivated
375 * @see BridgeObject::Emit()
377 void EmitDeactivate(Dali::Window window)
379 auto windowAccessible = mApplication.GetWindowAccessible(window);
382 windowAccessible->Emit(WindowEvent::DEACTIVATE, 0);
387 * @copydoc Dali::Accessibility::Bridge::WindowShown()
389 void WindowShown(Dali::Window window) override
391 if(!mIsShown && IsUp())
399 * @copydoc Dali::Accessibility::Bridge::WindowHidden()
401 void WindowHidden(Dali::Window window) override
403 if(mIsShown && IsUp())
411 * @copydoc Dali::Accessibility::Bridge::WindowFocused()
413 void WindowFocused(Dali::Window window) override
415 if(mIsShown && IsUp())
417 EmitActivate(window);
422 * @copydoc Dali::Accessibility::Bridge::WindowUnfocused()
424 void WindowUnfocused(Dali::Window window) override
426 if(mIsShown && IsUp())
428 EmitDeactivate(window);
433 * @copydoc Dali::Accessibility::Bridge::SuppressScreenReader()
435 void SuppressScreenReader(bool suppress) override
437 if(mIsScreenReaderSuppressed == suppress)
441 mIsScreenReaderSuppressed = suppress;
442 ReadScreenReaderEnabledProperty();
445 bool ReadIsEnabledTimerCallback()
447 ReadIsEnabledProperty();
451 void ReadIsEnabledProperty()
453 mAccessibilityStatusClient.property<bool>("IsEnabled").asyncGet([this](DBus::ValueOrError<bool> msg) {
456 DALI_LOG_ERROR("Get IsEnabled property error: %s\n", msg.getError().message.c_str());
457 if(msg.getError().errorType == DBus::ErrorType::INVALID_REPLY)
459 if(!mReadIsEnabledTimer)
461 mReadIsEnabledTimer = Dali::Timer::New(RETRY_INTERVAL);
462 mReadIsEnabledTimer.TickSignal().Connect(this, &BridgeImpl::ReadIsEnabledTimerCallback);
464 mReadIsEnabledTimer.Start();
468 mIsEnabled = std::get<0>(msg);
469 if((!mIsScreenReaderSuppressed && mIsScreenReaderEnabled) || mIsEnabled)
480 void ListenIsEnabledProperty()
482 mAccessibilityStatusClient.addPropertyChangedEvent<bool>("IsEnabled", [this](bool res) {
484 if(mIsScreenReaderEnabled || mIsEnabled)
495 bool ReadScreenReaderEnabledTimerCallback()
497 ReadScreenReaderEnabledProperty();
501 void ReadScreenReaderEnabledProperty()
503 // can be true because of SuppressScreenReader before init
504 if (!mAccessibilityStatusClient)
509 mAccessibilityStatusClient.property<bool>("ScreenReaderEnabled").asyncGet([this](DBus::ValueOrError<bool> msg) {
512 DALI_LOG_ERROR("Get ScreenReaderEnabled property error: %s\n", msg.getError().message.c_str());
513 if(msg.getError().errorType == DBus::ErrorType::INVALID_REPLY)
515 if(!mReadScreenReaderEnabledTimer)
517 mReadScreenReaderEnabledTimer = Dali::Timer::New(RETRY_INTERVAL);
518 mReadScreenReaderEnabledTimer.TickSignal().Connect(this, &BridgeImpl::ReadScreenReaderEnabledTimerCallback);
520 mReadScreenReaderEnabledTimer.Start();
524 mIsScreenReaderEnabled = std::get<0>(msg);
525 if((!mIsScreenReaderSuppressed && mIsScreenReaderEnabled) || mIsEnabled)
536 void ListenScreenReaderEnabledProperty()
538 mAccessibilityStatusClient.addPropertyChangedEvent<bool>("ScreenReaderEnabled", [this](bool res) {
539 mIsScreenReaderEnabled = res;
540 if((!mIsScreenReaderSuppressed && mIsScreenReaderEnabled) || mIsEnabled)
551 void ReadAndListenProperties()
553 ReadIsEnabledProperty();
554 ListenIsEnabledProperty();
556 ReadScreenReaderEnabledProperty();
557 ListenScreenReaderEnabledProperty();
560 bool InitializeAccessibilityStatusClient()
562 mAccessibilityStatusClient = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
564 if (!mAccessibilityStatusClient)
566 DALI_LOG_ERROR("Accessibility Status DbusClient is not ready\n");
573 bool InitializeTimerCallback()
575 if ( InitializeAccessibilityStatusClient() )
577 ReadAndListenProperties();
585 if ( InitializeAccessibilityStatusClient() )
587 ReadAndListenProperties();
588 mIdleCallback = NULL;
592 if(!mInitializeTimer)
594 mInitializeTimer = Dali::Timer::New(RETRY_INTERVAL);
595 mInitializeTimer.TickSignal().Connect(this, &BridgeImpl::InitializeTimerCallback);
597 mInitializeTimer.Start();
599 mIdleCallback = NULL;
604 * @copydoc Dali::Accessibility::Bridge::Initialize()
606 void Initialize() override
608 if ( InitializeAccessibilityStatusClient() )
610 ReadAndListenProperties();
614 // Initialize failed. Try it again on Idle
615 if( Dali::Adaptor::IsAvailable() )
617 Dali::Adaptor& adaptor = Dali::Adaptor::Get();
618 if( NULL == mIdleCallback )
620 mIdleCallback = MakeCallback( this, &BridgeImpl::OnIdleSignal );
621 adaptor.AddIdle( mIdleCallback, true );
627 * @copydoc Dali::Accessibility::Bridge::GetScreenReaderEnabled()
629 bool GetScreenReaderEnabled() override
631 return mIsScreenReaderEnabled;
635 * @copydoc Dali::Accessibility::Bridge::IsEnabled()
637 bool IsEnabled() override
643 namespace // unnamed namespace
646 bool INITIALIZED_BRIDGE = false;
649 * @brief Creates BridgeImpl instance.
651 * @return The BridgeImpl instance
652 * @note This method is to check environment variable first. If ATSPI is disable using env, it returns dummy bridge instance.
654 Bridge* CreateBridge()
656 INITIALIZED_BRIDGE = true;
660 /* check environment variable first */
661 const char* envAtspiDisabled = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_DISABLE_ATSPI);
662 if(envAtspiDisabled && std::atoi(envAtspiDisabled) != 0)
664 return Dali::Accessibility::DummyBridge::GetInstance();
667 return new BridgeImpl;
669 catch(const std::exception&)
671 DALI_LOG_ERROR("Failed to initialize AT-SPI bridge");
672 return Dali::Accessibility::DummyBridge::GetInstance();
676 } // unnamed namespace
678 // Dali::Accessibility::Bridge class implementation
680 Bridge* Bridge::GetCurrentBridge()
682 static Bridge* bridge;
688 else if(mAutoInitState == AutoInitState::ENABLED)
690 bridge = CreateBridge();
692 /* check environment variable for suppressing screen-reader */
693 const char* envSuppressScreenReader = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_SUPPRESS_SCREEN_READER);
694 if(envSuppressScreenReader && std::atoi(envSuppressScreenReader) != 0)
696 bridge->SuppressScreenReader(true);
702 return Dali::Accessibility::DummyBridge::GetInstance();
705 void Bridge::DisableAutoInit()
707 if(INITIALIZED_BRIDGE)
709 DALI_LOG_ERROR("Bridge::DisableAutoInit() called after bridge auto-initialization");
712 mAutoInitState = AutoInitState::DISABLED;
715 void Bridge::EnableAutoInit()
717 mAutoInitState = AutoInitState::ENABLED;
719 if(INITIALIZED_BRIDGE)
724 auto rootLayer = Dali::Stage::GetCurrent().GetRootLayer(); // A root layer of the default window.
725 auto window = Dali::DevelWindow::Get(rootLayer);
726 auto applicationName = Dali::Internal::Adaptor::Adaptor::GetApplicationPackageName();
728 auto accessible = Accessibility::Accessible::Get(rootLayer, true);
730 auto* bridge = Bridge::GetCurrentBridge();
731 bridge->AddTopLevelWindow(accessible);
732 bridge->SetApplicationName(applicationName);
733 bridge->Initialize();
735 if(window && window.IsVisible())
737 bridge->WindowShown(window);