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();
230 UnembedSocket(mApplication.GetAddress(), {AtspiDbusNameRegistry, "root"});
233 mHighlightedActor = {};
234 mHighlightClearAction = {};
235 BridgeAccessible::ForceDown();
236 mRegistryClient = {};
237 mDirectReadingClient = {};
238 mDirectReadingCallbacks.clear();
239 mApplication.mChildren.clear();
247 mInitializeTimer.Stop();
248 mInitializeTimer.Reset();
251 if(mReadIsEnabledTimer)
253 mReadIsEnabledTimer.Stop();
254 mReadIsEnabledTimer.Reset();
257 if(mReadScreenReaderEnabledTimer)
259 mReadScreenReaderEnabledTimer.Stop();
260 mReadScreenReaderEnabledTimer.Reset();
265 mForceUpTimer.Stop();
266 mForceUpTimer.Reset();
271 * @copydoc Dali::Accessibility::Bridge::Terminate()
273 void Terminate() override
277 // The ~Window() after this point cannot emit DESTROY, because Bridge is not available. So emit DESTROY here.
278 for(auto windowAccessible : mApplication.mChildren)
280 BridgeObject::Emit(windowAccessible, WindowEvent::DESTROY);
282 mData->mCurrentlyHighlightedActor = {};
283 mData->mHighlightActor = {};
286 if((NULL != mIdleCallback) && Dali::Adaptor::IsAvailable())
288 Dali::Adaptor::Get().RemoveIdle(mIdleCallback);
290 mAccessibilityStatusClient = {};
295 bool ForceUpTimerCallback()
297 if(ForceUp() != ForceUpResult::FAILED)
305 * @copydoc Dali::Accessibility::Bridge::ForceUp()
307 ForceUpResult ForceUp() override
309 auto forceUpResult = BridgeAccessible::ForceUp();
310 if(forceUpResult == ForceUpResult::ALREADY_UP)
312 return forceUpResult;
314 else if(forceUpResult == ForceUpResult::FAILED)
318 mForceUpTimer = Dali::Timer::New(RETRY_INTERVAL);
319 mForceUpTimer.TickSignal().Connect(this, &BridgeImpl::ForceUpTimerCallback);
320 mForceUpTimer.Start();
322 return forceUpResult;
325 BridgeObject::RegisterInterfaces();
326 BridgeAccessible::RegisterInterfaces();
327 BridgeComponent::RegisterInterfaces();
328 BridgeCollection::RegisterInterfaces();
329 BridgeAction::RegisterInterfaces();
330 BridgeValue::RegisterInterfaces();
331 BridgeText::RegisterInterfaces();
332 BridgeEditableText::RegisterInterfaces();
333 BridgeSelection::RegisterInterfaces();
334 BridgeApplication::RegisterInterfaces();
335 BridgeHypertext::RegisterInterfaces();
336 BridgeHyperlink::RegisterInterfaces();
337 BridgeSocket::RegisterInterfaces();
339 RegisterOnBridge(&mApplication);
341 mRegistryClient = {AtspiDbusNameRegistry, AtspiDbusPathDec, Accessible::GetInterfaceName(AtspiInterface::DEVICE_EVENT_CONTROLLER), mConnectionPtr};
342 mDirectReadingClient = DBus::DBusClient{DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, mConnectionPtr};
344 mDirectReadingClient.addSignal<void(int32_t, std::string)>("ReadingStateChanged", [=](int32_t id, std::string readingState) {
345 auto it = mDirectReadingCallbacks.find(id);
346 if(it != mDirectReadingCallbacks.end())
348 it->second(readingState);
349 if(readingState != "ReadingPaused" && readingState != "ReadingResumed" && readingState != "ReadingStarted")
351 mDirectReadingCallbacks.erase(it);
356 auto parentAddress = EmbedSocket(mApplication.GetAddress(), {AtspiDbusNameRegistry, "root"});
357 mApplication.mParent.SetAddress(std::move(parentAddress));
358 mEnabledSignal.Emit();
360 return ForceUpResult::JUST_STARTED;
364 * @brief Sends a signal to dbus that the window is shown.
366 * @param[in] window The window to be shown
367 * @see Accessible::EmitShowing() and BridgeObject::EmitStateChanged()
369 void EmitShown(Dali::Window window)
371 auto windowAccessible = mApplication.GetWindowAccessible(window);
374 windowAccessible->EmitShowing(true);
379 * @brief Sends a signal to dbus that the window is hidden.
381 * @param[in] window The window to be hidden
382 * @see Accessible::EmitShowing() and BridgeObject::EmitStateChanged()
384 void EmitHidden(Dali::Window window)
386 auto windowAccessible = mApplication.GetWindowAccessible(window);
389 windowAccessible->EmitShowing(false);
394 * @brief Sends a signal to dbus that the window is activated.
396 * @param[in] window The window to be activated
397 * @see BridgeObject::Emit()
399 void EmitActivate(Dali::Window window)
401 auto windowAccessible = mApplication.GetWindowAccessible(window);
404 windowAccessible->Emit(WindowEvent::ACTIVATE, 0);
409 * @brief Sends a signal to dbus that the window is deactivated.
411 * @param[in] window The window to be deactivated
412 * @see BridgeObject::Emit()
414 void EmitDeactivate(Dali::Window window)
416 auto windowAccessible = mApplication.GetWindowAccessible(window);
419 windowAccessible->Emit(WindowEvent::DEACTIVATE, 0);
424 * @copydoc Dali::Accessibility::Bridge::WindowShown()
426 void WindowShown(Dali::Window window) override
428 if(!mIsShown && IsUp())
436 * @copydoc Dali::Accessibility::Bridge::WindowHidden()
438 void WindowHidden(Dali::Window window) override
440 if(mIsShown && IsUp())
448 * @copydoc Dali::Accessibility::Bridge::WindowFocused()
450 void WindowFocused(Dali::Window window) override
452 if(mIsShown && IsUp())
454 EmitActivate(window);
459 * @copydoc Dali::Accessibility::Bridge::WindowUnfocused()
461 void WindowUnfocused(Dali::Window window) override
463 if(mIsShown && IsUp())
465 EmitDeactivate(window);
470 * @copydoc Dali::Accessibility::Bridge::SuppressScreenReader()
472 void SuppressScreenReader(bool suppress) override
474 if(mIsScreenReaderSuppressed == suppress)
478 mIsScreenReaderSuppressed = suppress;
479 ReadScreenReaderEnabledProperty();
484 if((!mIsScreenReaderSuppressed && mIsScreenReaderEnabled) || mIsEnabled)
494 bool ReadIsEnabledTimerCallback()
496 ReadIsEnabledProperty();
500 void ReadIsEnabledProperty()
502 mAccessibilityStatusClient.property<bool>("IsEnabled").asyncGet([this](DBus::ValueOrError<bool> msg) {
505 DALI_LOG_ERROR("Get IsEnabled property error: %s\n", msg.getError().message.c_str());
506 if(msg.getError().errorType == DBus::ErrorType::INVALID_REPLY)
508 if(!mReadIsEnabledTimer)
510 mReadIsEnabledTimer = Dali::Timer::New(RETRY_INTERVAL);
511 mReadIsEnabledTimer.TickSignal().Connect(this, &BridgeImpl::ReadIsEnabledTimerCallback);
513 mReadIsEnabledTimer.Start();
518 if(mReadIsEnabledTimer)
520 mReadIsEnabledTimer.Stop();
521 mReadIsEnabledTimer.Reset();
524 mIsEnabled = std::get<0>(msg);
529 void ListenIsEnabledProperty()
531 mAccessibilityStatusClient.addPropertyChangedEvent<bool>("IsEnabled", [this](bool res) {
537 bool ReadScreenReaderEnabledTimerCallback()
539 ReadScreenReaderEnabledProperty();
543 void ReadScreenReaderEnabledProperty()
545 // can be true because of SuppressScreenReader before init
546 if (!mAccessibilityStatusClient)
551 mAccessibilityStatusClient.property<bool>("ScreenReaderEnabled").asyncGet([this](DBus::ValueOrError<bool> msg) {
554 DALI_LOG_ERROR("Get ScreenReaderEnabled property error: %s\n", msg.getError().message.c_str());
555 if(msg.getError().errorType == DBus::ErrorType::INVALID_REPLY)
557 if(!mReadScreenReaderEnabledTimer)
559 mReadScreenReaderEnabledTimer = Dali::Timer::New(RETRY_INTERVAL);
560 mReadScreenReaderEnabledTimer.TickSignal().Connect(this, &BridgeImpl::ReadScreenReaderEnabledTimerCallback);
562 mReadScreenReaderEnabledTimer.Start();
567 if(mReadScreenReaderEnabledTimer)
569 mReadScreenReaderEnabledTimer.Stop();
570 mReadScreenReaderEnabledTimer.Reset();
573 mIsScreenReaderEnabled = std::get<0>(msg);
578 void EmitScreenReaderEnabledSignal()
580 if (mIsScreenReaderEnabled)
582 mScreenReaderEnabledSignal.Emit();
586 mScreenReaderDisabledSignal.Emit();
590 void ListenScreenReaderEnabledProperty()
592 mAccessibilityStatusClient.addPropertyChangedEvent<bool>("ScreenReaderEnabled", [this](bool res) {
593 mIsScreenReaderEnabled = res;
594 EmitScreenReaderEnabledSignal();
599 void ReadAndListenProperties()
601 ReadIsEnabledProperty();
602 ListenIsEnabledProperty();
604 ReadScreenReaderEnabledProperty();
605 ListenScreenReaderEnabledProperty();
608 bool InitializeAccessibilityStatusClient()
610 mAccessibilityStatusClient = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
612 if (!mAccessibilityStatusClient)
614 DALI_LOG_ERROR("Accessibility Status DbusClient is not ready\n");
621 bool InitializeTimerCallback()
623 if ( InitializeAccessibilityStatusClient() )
625 ReadAndListenProperties();
633 if ( InitializeAccessibilityStatusClient() )
635 ReadAndListenProperties();
636 mIdleCallback = NULL;
640 if(!mInitializeTimer)
642 mInitializeTimer = Dali::Timer::New(RETRY_INTERVAL);
643 mInitializeTimer.TickSignal().Connect(this, &BridgeImpl::InitializeTimerCallback);
645 mInitializeTimer.Start();
647 mIdleCallback = NULL;
652 * @copydoc Dali::Accessibility::Bridge::Initialize()
654 void Initialize() override
656 if ( InitializeAccessibilityStatusClient() )
658 ReadAndListenProperties();
662 // Initialize failed. Try it again on Idle
663 if( Dali::Adaptor::IsAvailable() )
665 Dali::Adaptor& adaptor = Dali::Adaptor::Get();
666 if( NULL == mIdleCallback )
668 mIdleCallback = MakeCallback( this, &BridgeImpl::OnIdleSignal );
669 adaptor.AddIdle( mIdleCallback, true );
675 * @copydoc Dali::Accessibility::Bridge::GetScreenReaderEnabled()
677 bool GetScreenReaderEnabled() override
679 return mIsScreenReaderEnabled;
683 * @copydoc Dali::Accessibility::Bridge::IsEnabled()
685 bool IsEnabled() override
690 Address EmbedSocket(const Address& plug, const Address& socket) override
692 auto client = CreateSocketClient(socket);
693 auto reply = client.method<Address(Address)>("Embed").call(plug);
697 DALI_LOG_ERROR("Failed to embed socket %s: %s", socket.ToString().c_str(), reply.getError().message.c_str());
701 return std::get<0>(reply.getValues());
704 void EmbedAtkSocket(const Address& plug, const Address& socket) override
706 auto client = CreateSocketClient(socket);
708 client.method<void(std::string)>("Embedded").call(ATSPI_PREFIX_PATH + plug.GetPath());
711 void UnembedSocket(const Address& plug, const Address& socket) override
713 auto client = CreateSocketClient(socket);
715 client.method<void(Address)>("Unembed").call(plug);
719 DBus::DBusClient CreateSocketClient(const Address& socket)
721 return {socket.GetBus(), ATSPI_PREFIX_PATH + socket.GetPath(), Accessible::GetInterfaceName(AtspiInterface::SOCKET), mConnectionPtr};
725 namespace // unnamed namespace
728 bool INITIALIZED_BRIDGE = false;
731 * @brief Creates BridgeImpl instance.
733 * @return The BridgeImpl instance
734 * @note This method is to check environment variable first. If ATSPI is disable using env, it returns dummy bridge instance.
736 std::shared_ptr<Bridge> CreateBridge()
738 INITIALIZED_BRIDGE = true;
742 /* check environment variable first */
743 const char* envAtspiDisabled = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_DISABLE_ATSPI);
744 if(envAtspiDisabled && std::atoi(envAtspiDisabled) != 0)
746 return Dali::Accessibility::DummyBridge::GetInstance();
749 return std::make_shared<BridgeImpl>();
751 catch(const std::exception&)
753 DALI_LOG_ERROR("Failed to initialize AT-SPI bridge");
754 return Dali::Accessibility::DummyBridge::GetInstance();
758 } // unnamed namespace
760 // Dali::Accessibility::Bridge class implementation
762 std::shared_ptr<Bridge> Bridge::GetCurrentBridge()
764 static std::shared_ptr<Bridge> bridge;
770 else if(mAutoInitState == AutoInitState::ENABLED)
772 bridge = CreateBridge();
774 /* check environment variable for suppressing screen-reader */
775 const char* envSuppressScreenReader = Dali::EnvironmentVariable::GetEnvironmentVariable(DALI_ENV_SUPPRESS_SCREEN_READER);
776 if(envSuppressScreenReader && std::atoi(envSuppressScreenReader) != 0)
778 bridge->SuppressScreenReader(true);
784 return Dali::Accessibility::DummyBridge::GetInstance();
787 void Bridge::DisableAutoInit()
789 if(INITIALIZED_BRIDGE)
791 DALI_LOG_ERROR("Bridge::DisableAutoInit() called after bridge auto-initialization");
794 mAutoInitState = AutoInitState::DISABLED;
797 void Bridge::EnableAutoInit()
799 mAutoInitState = AutoInitState::ENABLED;
801 if(INITIALIZED_BRIDGE)
806 auto rootLayer = Dali::Stage::GetCurrent().GetRootLayer(); // A root layer of the default window.
807 auto window = Dali::DevelWindow::Get(rootLayer);
808 auto applicationName = Dali::Internal::Adaptor::Adaptor::GetApplicationPackageName();
810 auto accessible = Accessibility::Accessible::Get(rootLayer);
812 auto bridge = Bridge::GetCurrentBridge();
813 bridge->AddTopLevelWindow(accessible);
814 bridge->SetApplicationName(applicationName);
815 bridge->Initialize();
817 if(window && window.IsVisible())
819 bridge->WindowShown(window);