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-hypertext.h>
36 #include <dali/internal/accessibility/bridge/bridge-hyperlink.h>
37 #include <dali/internal/accessibility/bridge/bridge-object.h>
38 #include <dali/internal/accessibility/bridge/bridge-selection.h>
39 #include <dali/internal/accessibility/bridge/bridge-socket.h>
40 #include <dali/internal/accessibility/bridge/bridge-text.h>
41 #include <dali/internal/accessibility/bridge/bridge-value.h>
42 #include <dali/internal/accessibility/bridge/bridge-application.h>
43 #include <dali/internal/accessibility/bridge/dummy-atspi.h>
44 #include <dali/internal/adaptor/common/adaptor-impl.h>
45 #include <dali/internal/system/common/environment-variables.h>
47 using namespace Dali::Accessibility;
49 namespace // unnamed namespace
52 const int RETRY_INTERVAL = 1000;
54 } // unnamed namespace
57 * @brief The BridgeImpl class is to implement some Bridge functions.
59 class BridgeImpl : public virtual BridgeBase,
60 public BridgeAccessible,
62 public BridgeComponent,
63 public BridgeCollection,
67 public BridgeEditableText,
68 public BridgeSelection,
69 public BridgeApplication,
70 public BridgeHypertext,
71 public BridgeHyperlink,
74 DBus::DBusClient mAccessibilityStatusClient;
75 DBus::DBusClient mRegistryClient;
76 DBus::DBusClient mDirectReadingClient;
77 bool mIsScreenReaderEnabled = false;
78 bool mIsEnabled = false;
79 bool mIsShown = false;
80 std::unordered_map<int32_t, std::function<void(std::string)>> mDirectReadingCallbacks;
81 Dali::Actor mHighlightedActor;
82 std::function<void(Dali::Actor)> mHighlightClearAction;
83 Dali::CallbackBase* mIdleCallback = NULL;
84 Dali::Timer mInitializeTimer;
85 Dali::Timer mReadIsEnabledTimer;
86 Dali::Timer mReadScreenReaderEnabledTimer;
87 Dali::Timer mForceUpTimer;
95 * @copydoc Dali::Accessibility::Bridge::Emit()
97 Consumed Emit(KeyEventType type, unsigned int keyCode, const std::string& keyName, unsigned int timeStamp, bool isText) override
104 unsigned int keyType = 0;
108 case KeyEventType::KEY_PRESSED:
113 case KeyEventType::KEY_RELEASED:
124 auto methodObject = mRegistryClient.method<bool(std::tuple<uint32_t, int32_t, int32_t, int32_t, int32_t, std::string, bool>)>("NotifyListenersSync");
125 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});
128 LOG() << result.getError().message;
131 return std::get<0>(result) ? Consumed::YES : Consumed::NO;
135 * @copydoc Dali::Accessibility::Bridge::Pause()
137 void Pause() override
144 mDirectReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
147 LOG() << "Direct reading command failed (" << msg.getError().message << ")\n";
154 * @copydoc Dali::Accessibility::Bridge::Resume()
156 void Resume() override
163 mDirectReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
166 LOG() << "Direct reading command failed (" << msg.getError().message << ")\n";
173 * @copydoc Dali::Accessibility::Bridge::StopReading()
175 void StopReading(bool alsoNonDiscardable) override
182 mDirectReadingClient.method<DBus::ValueOrError<void>(bool)>("StopReading").asyncCall([](DBus::ValueOrError<void> msg) {
185 LOG() << "Direct reading command failed (" << msg.getError().message << ")\n";
192 * @copydoc Dali::Accessibility::Bridge::Say()
194 void Say(const std::string& text, bool discardable, std::function<void(std::string)> callback) override
201 mDirectReadingClient.method<DBus::ValueOrError<std::string, bool, int32_t>(std::string, bool)>("ReadCommand").asyncCall([=](DBus::ValueOrError<std::string, bool, int32_t> msg) {
204 LOG() << "Direct reading command failed (" << msg.getError().message << ")\n";
208 mDirectReadingCallbacks.emplace(std::get<2>(msg), callback);
216 * @copydoc Dali::Accessibility::Bridge::ForceDown()
218 void ForceDown() override
222 if(mData->mCurrentlyHighlightedActor && mData->mHighlightActor)
224 mData->mCurrentlyHighlightedActor.Remove(mData->mHighlightActor);
226 mData->mCurrentlyHighlightedActor = {};
227 mData->mHighlightActor = {};
229 mDisabledSignal.Emit();
231 mHighlightedActor = {};
232 mHighlightClearAction = {};
233 BridgeAccessible::ForceDown();
234 mRegistryClient = {};
235 mDirectReadingClient = {};
236 mDirectReadingCallbacks.clear();
237 mApplication.mChildren.clear();
238 mApplication.mWindows.clear();
246 mInitializeTimer.Stop();
247 mInitializeTimer.Reset();
250 if(mReadIsEnabledTimer)
252 mReadIsEnabledTimer.Stop();
253 mReadIsEnabledTimer.Reset();
256 if(mReadScreenReaderEnabledTimer)
258 mReadScreenReaderEnabledTimer.Stop();
259 mReadScreenReaderEnabledTimer.Reset();
264 mForceUpTimer.Stop();
265 mForceUpTimer.Reset();
270 * @copydoc Dali::Accessibility::Bridge::Terminate()
272 void Terminate() override
276 mData->mCurrentlyHighlightedActor = {};
277 mData->mHighlightActor = {};
280 if((NULL != mIdleCallback) && Dali::Adaptor::IsAvailable())
282 Dali::Adaptor::Get().RemoveIdle(mIdleCallback);
284 mAccessibilityStatusClient = {};
289 bool ForceUpTimerCallback()
291 if(ForceUp() != ForceUpResult::FAILED)
299 * @copydoc Dali::Accessibility::Bridge::ForceUp()
301 ForceUpResult ForceUp() override
303 auto forceUpResult = BridgeAccessible::ForceUp();
304 if(forceUpResult == ForceUpResult::ALREADY_UP)
306 return forceUpResult;
308 else if(forceUpResult == ForceUpResult::FAILED)
312 mForceUpTimer = Dali::Timer::New(RETRY_INTERVAL);
313 mForceUpTimer.TickSignal().Connect(this, &BridgeImpl::ForceUpTimerCallback);
314 mForceUpTimer.Start();
316 return forceUpResult;
319 BridgeObject::RegisterInterfaces();
320 BridgeAccessible::RegisterInterfaces();
321 BridgeComponent::RegisterInterfaces();
322 BridgeCollection::RegisterInterfaces();
323 BridgeAction::RegisterInterfaces();
324 BridgeValue::RegisterInterfaces();
325 BridgeText::RegisterInterfaces();
326 BridgeEditableText::RegisterInterfaces();
327 BridgeSelection::RegisterInterfaces();
328 BridgeApplication::RegisterInterfaces();
329 BridgeHypertext::RegisterInterfaces();
330 BridgeHyperlink::RegisterInterfaces();
331 BridgeSocket::RegisterInterfaces();
333 RegisterOnBridge(&mApplication);
335 mRegistryClient = {AtspiDbusNameRegistry, AtspiDbusPathDec, Accessible::GetInterfaceName(AtspiInterface::DEVICE_EVENT_CONTROLLER), mConnectionPtr};
336 mDirectReadingClient = DBus::DBusClient{DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, mConnectionPtr};
338 mDirectReadingClient.addSignal<void(int32_t, std::string)>("ReadingStateChanged", [=](int32_t id, std::string readingState) {
339 auto it = mDirectReadingCallbacks.find(id);
340 if(it != mDirectReadingCallbacks.end())
342 it->second(readingState);
343 if(readingState != "ReadingPaused" && readingState != "ReadingResumed" && readingState != "ReadingStarted")
345 mDirectReadingCallbacks.erase(it);
350 auto proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, Accessible::GetInterfaceName(AtspiInterface::SOCKET), mConnectionPtr};
351 Address root{"", "root"};
352 auto res = proxy.method<Address(Address)>("Embed").call(root);
355 LOG() << "Call to Embed failed: " << res.getError().message;
359 mApplication.mParent.SetAddress(std::move(std::get<0>(res)));
361 mEnabledSignal.Emit();
365 auto rootLayer = Dali::Stage::GetCurrent().GetRootLayer();
366 auto window = Dali::DevelWindow::Get(rootLayer);
367 EmitActivate(window); // Currently, sends a signal that the default window is activated here.
370 return ForceUpResult::JUST_STARTED;
374 * @brief Sends a signal to dbus that the window is shown.
376 * @param[in] window The window to be shown
377 * @see Accessible::EmitShowing() and BridgeObject::EmitStateChanged()
379 void EmitShown(Dali::Window window)
381 auto windowAccessible = mApplication.GetWindowAccessible(window);
384 windowAccessible->EmitShowing(true);
389 * @brief Sends a signal to dbus that the window is hidden.
391 * @param[in] window The window to be hidden
392 * @see Accessible::EmitShowing() and BridgeObject::EmitStateChanged()
394 void EmitHidden(Dali::Window window)
396 auto windowAccessible = mApplication.GetWindowAccessible(window);
399 windowAccessible->EmitShowing(false);
404 * @brief Sends a signal to dbus that the window is activated.
406 * @param[in] window The window to be activated
407 * @see BridgeObject::Emit()
409 void EmitActivate(Dali::Window window)
411 auto windowAccessible = mApplication.GetWindowAccessible(window);
414 windowAccessible->Emit(WindowEvent::ACTIVATE, 0);
419 * @brief Sends a signal to dbus that the window is deactivated.
421 * @param[in] window The window to be deactivated
422 * @see BridgeObject::Emit()
424 void EmitDeactivate(Dali::Window window)
426 auto windowAccessible = mApplication.GetWindowAccessible(window);
429 windowAccessible->Emit(WindowEvent::DEACTIVATE, 0);
434 * @copydoc Dali::Accessibility::Bridge::WindowShown()
436 void WindowShown(Dali::Window window) override
438 if(!mIsShown && IsUp())
446 * @copydoc Dali::Accessibility::Bridge::WindowHidden()
448 void WindowHidden(Dali::Window window) override
450 if(mIsShown && IsUp())
458 * @copydoc Dali::Accessibility::Bridge::WindowFocused()
460 void WindowFocused(Dali::Window window) override
462 if(mIsShown && IsUp())
464 EmitActivate(window);
469 * @copydoc Dali::Accessibility::Bridge::WindowUnfocused()
471 void WindowUnfocused(Dali::Window window) override
473 if(mIsShown && IsUp())
475 EmitDeactivate(window);
480 * @copydoc Dali::Accessibility::Bridge::SuppressScreenReader()
482 void SuppressScreenReader(bool suppress) override
484 if(mIsScreenReaderSuppressed == suppress)
488 mIsScreenReaderSuppressed = suppress;
489 ReadScreenReaderEnabledProperty();
494 if((!mIsScreenReaderSuppressed && mIsScreenReaderEnabled) || mIsEnabled)
504 bool ReadIsEnabledTimerCallback()
506 ReadIsEnabledProperty();
510 void ReadIsEnabledProperty()
512 mAccessibilityStatusClient.property<bool>("IsEnabled").asyncGet([this](DBus::ValueOrError<bool> msg) {
515 DALI_LOG_ERROR("Get IsEnabled property error: %s\n", msg.getError().message.c_str());
516 if(msg.getError().errorType == DBus::ErrorType::INVALID_REPLY)
518 if(!mReadIsEnabledTimer)
520 mReadIsEnabledTimer = Dali::Timer::New(RETRY_INTERVAL);
521 mReadIsEnabledTimer.TickSignal().Connect(this, &BridgeImpl::ReadIsEnabledTimerCallback);
523 mReadIsEnabledTimer.Start();
528 if(mReadIsEnabledTimer)
530 mReadIsEnabledTimer.Stop();
531 mReadIsEnabledTimer.Reset();
534 mIsEnabled = std::get<0>(msg);
539 void ListenIsEnabledProperty()
541 mAccessibilityStatusClient.addPropertyChangedEvent<bool>("IsEnabled", [this](bool res) {
547 bool ReadScreenReaderEnabledTimerCallback()
549 ReadScreenReaderEnabledProperty();
553 void ReadScreenReaderEnabledProperty()
555 // can be true because of SuppressScreenReader before init
556 if (!mAccessibilityStatusClient)
561 mAccessibilityStatusClient.property<bool>("ScreenReaderEnabled").asyncGet([this](DBus::ValueOrError<bool> msg) {
564 DALI_LOG_ERROR("Get ScreenReaderEnabled property error: %s\n", msg.getError().message.c_str());
565 if(msg.getError().errorType == DBus::ErrorType::INVALID_REPLY)
567 if(!mReadScreenReaderEnabledTimer)
569 mReadScreenReaderEnabledTimer = Dali::Timer::New(RETRY_INTERVAL);
570 mReadScreenReaderEnabledTimer.TickSignal().Connect(this, &BridgeImpl::ReadScreenReaderEnabledTimerCallback);
572 mReadScreenReaderEnabledTimer.Start();
577 if(mReadScreenReaderEnabledTimer)
579 mReadScreenReaderEnabledTimer.Stop();
580 mReadScreenReaderEnabledTimer.Reset();
583 mIsScreenReaderEnabled = std::get<0>(msg);
588 void ListenScreenReaderEnabledProperty()
590 mAccessibilityStatusClient.addPropertyChangedEvent<bool>("ScreenReaderEnabled", [this](bool res) {
591 mIsScreenReaderEnabled = res;
596 void ReadAndListenProperties()
598 ReadIsEnabledProperty();
599 ListenIsEnabledProperty();
601 ReadScreenReaderEnabledProperty();
602 ListenScreenReaderEnabledProperty();
605 bool InitializeAccessibilityStatusClient()
607 mAccessibilityStatusClient = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
609 if (!mAccessibilityStatusClient)
611 DALI_LOG_ERROR("Accessibility Status DbusClient is not ready\n");
618 bool InitializeTimerCallback()
620 if ( InitializeAccessibilityStatusClient() )
622 ReadAndListenProperties();
630 if ( InitializeAccessibilityStatusClient() )
632 ReadAndListenProperties();
633 mIdleCallback = NULL;
637 if(!mInitializeTimer)
639 mInitializeTimer = Dali::Timer::New(RETRY_INTERVAL);
640 mInitializeTimer.TickSignal().Connect(this, &BridgeImpl::InitializeTimerCallback);
642 mInitializeTimer.Start();
644 mIdleCallback = NULL;
649 * @copydoc Dali::Accessibility::Bridge::Initialize()
651 void Initialize() override
653 if ( InitializeAccessibilityStatusClient() )
655 ReadAndListenProperties();
659 // Initialize failed. Try it again on Idle
660 if( Dali::Adaptor::IsAvailable() )
662 Dali::Adaptor& adaptor = Dali::Adaptor::Get();
663 if( NULL == mIdleCallback )
665 mIdleCallback = MakeCallback( this, &BridgeImpl::OnIdleSignal );
666 adaptor.AddIdle( mIdleCallback, true );
672 * @copydoc Dali::Accessibility::Bridge::GetScreenReaderEnabled()
674 bool GetScreenReaderEnabled() override
676 return mIsScreenReaderEnabled;
680 * @copydoc Dali::Accessibility::Bridge::IsEnabled()
682 bool IsEnabled() override
688 namespace // unnamed namespace
691 bool INITIALIZED_BRIDGE = false;
694 * @brief Creates BridgeImpl instance.
696 * @return The BridgeImpl instance
697 * @note This method is to check environment variable first. If ATSPI is disable using env, it returns dummy bridge instance.
699 std::shared_ptr<Bridge> CreateBridge()
701 INITIALIZED_BRIDGE = true;
705 /* check environment variable first */
706 const char* envAtspiDisabled = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_DISABLE_ATSPI);
707 if(envAtspiDisabled && std::atoi(envAtspiDisabled) != 0)
709 return Dali::Accessibility::DummyBridge::GetInstance();
712 return std::make_shared<BridgeImpl>();
714 catch(const std::exception&)
716 DALI_LOG_ERROR("Failed to initialize AT-SPI bridge");
717 return Dali::Accessibility::DummyBridge::GetInstance();
721 } // unnamed namespace
723 // Dali::Accessibility::Bridge class implementation
725 std::shared_ptr<Bridge> Bridge::GetCurrentBridge()
727 static std::shared_ptr<Bridge> bridge;
733 else if(mAutoInitState == AutoInitState::ENABLED)
735 bridge = CreateBridge();
737 /* check environment variable for suppressing screen-reader */
738 const char* envSuppressScreenReader = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_SUPPRESS_SCREEN_READER);
739 if(envSuppressScreenReader && std::atoi(envSuppressScreenReader) != 0)
741 bridge->SuppressScreenReader(true);
747 return Dali::Accessibility::DummyBridge::GetInstance();
750 void Bridge::DisableAutoInit()
752 if(INITIALIZED_BRIDGE)
754 DALI_LOG_ERROR("Bridge::DisableAutoInit() called after bridge auto-initialization");
757 mAutoInitState = AutoInitState::DISABLED;
760 void Bridge::EnableAutoInit()
762 mAutoInitState = AutoInitState::ENABLED;
764 if(INITIALIZED_BRIDGE)
769 auto rootLayer = Dali::Stage::GetCurrent().GetRootLayer(); // A root layer of the default window.
770 auto window = Dali::DevelWindow::Get(rootLayer);
771 auto applicationName = Dali::Internal::Adaptor::Adaptor::GetApplicationPackageName();
773 auto accessible = Accessibility::Accessible::Get(rootLayer, true);
775 auto bridge = Bridge::GetCurrentBridge();
776 bridge->AddTopLevelWindow(accessible);
777 bridge->SetApplicationName(applicationName);
778 bridge->Initialize();
780 if(window && window.IsVisible())
782 bridge->WindowShown(window);