Merge "[AT-SPI] Make ToolkitName customizable" into devel/master
[platform/core/uifw/dali-adaptor.git] / dali / internal / accessibility / bridge / bridge-impl.cpp
index a4144ab..c48240c 100644 (file)
 #include <dali/internal/accessibility/bridge/bridge-collection.h>
 #include <dali/internal/accessibility/bridge/bridge-component.h>
 #include <dali/internal/accessibility/bridge/bridge-editable-text.h>
+#include <dali/internal/accessibility/bridge/bridge-hypertext.h>
+#include <dali/internal/accessibility/bridge/bridge-hyperlink.h>
 #include <dali/internal/accessibility/bridge/bridge-object.h>
 #include <dali/internal/accessibility/bridge/bridge-selection.h>
+#include <dali/internal/accessibility/bridge/bridge-socket.h>
 #include <dali/internal/accessibility/bridge/bridge-text.h>
 #include <dali/internal/accessibility/bridge/bridge-value.h>
 #include <dali/internal/accessibility/bridge/bridge-application.h>
 
 using namespace Dali::Accessibility;
 
+namespace // unnamed namespace
+{
+
+const int RETRY_INTERVAL = 1000;
+
+} // unnamed namespace
+
 /**
  * @brief The BridgeImpl class is to implement some Bridge functions.
  */
@@ -56,7 +66,10 @@ class BridgeImpl : public virtual BridgeBase,
                    public BridgeText,
                    public BridgeEditableText,
                    public BridgeSelection,
-                   public BridgeApplication
+                   public BridgeApplication,
+                   public BridgeHypertext,
+                   public BridgeHyperlink,
+                   public BridgeSocket
 {
   DBus::DBusClient                                              mAccessibilityStatusClient;
   DBus::DBusClient                                              mRegistryClient;
@@ -68,6 +81,10 @@ class BridgeImpl : public virtual BridgeBase,
   Dali::Actor                                                   mHighlightedActor;
   std::function<void(Dali::Actor)>                              mHighlightClearAction;
   Dali::CallbackBase*                                           mIdleCallback          = NULL;
+  Dali::Timer                                                   mInitializeTimer;
+  Dali::Timer                                                   mReadIsEnabledTimer;
+  Dali::Timer                                                   mReadScreenReaderEnabledTimer;
+  Dali::Timer                                                   mForceUpTimer;
 
 public:
   BridgeImpl()
@@ -127,7 +144,7 @@ public:
     mDirectReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
       if(!msg)
       {
-        LOG() << "Direct reading command failed (" << msg.getError().message << ")";
+        LOG() << "Direct reading command failed (" << msg.getError().message << ")\n";
       }
     },
                                                                                         true);
@@ -146,7 +163,7 @@ public:
     mDirectReadingClient.method<DBus::ValueOrError<void>(bool)>("PauseResume").asyncCall([](DBus::ValueOrError<void> msg) {
       if(!msg)
       {
-        LOG() << "Direct reading command failed (" << msg.getError().message << ")";
+        LOG() << "Direct reading command failed (" << msg.getError().message << ")\n";
       }
     },
                                                                                         false);
@@ -165,7 +182,7 @@ public:
     mDirectReadingClient.method<DBus::ValueOrError<void>(bool)>("StopReading").asyncCall([](DBus::ValueOrError<void> msg) {
       if(!msg)
       {
-        LOG() << "Direct reading command failed (" << msg.getError().message << ")";
+        LOG() << "Direct reading command failed (" << msg.getError().message << ")\n";
       }
     },
                                                                                         alsoNonDiscardable);
@@ -184,7 +201,7 @@ public:
     mDirectReadingClient.method<DBus::ValueOrError<std::string, bool, int32_t>(std::string, bool)>("ReadCommand").asyncCall([=](DBus::ValueOrError<std::string, bool, int32_t> msg) {
       if(!msg)
       {
-        LOG() << "Direct reading command failed (" << msg.getError().message << ")";
+        LOG() << "Direct reading command failed (" << msg.getError().message << ")\n";
       }
       else if(callback)
       {
@@ -208,6 +225,8 @@ public:
       }
       mData->mCurrentlyHighlightedActor = {};
       mData->mHighlightActor            = {};
+
+      mDisabledSignal.Emit();
     }
     mHighlightedActor     = {};
     mHighlightClearAction = {};
@@ -215,6 +234,35 @@ public:
     mRegistryClient       = {};
     mDirectReadingClient  = {};
     mDirectReadingCallbacks.clear();
+    mApplication.mChildren.clear();
+    ClearTimer();
+  }
+
+  void ClearTimer()
+  {
+    if(mInitializeTimer)
+    {
+      mInitializeTimer.Stop();
+      mInitializeTimer.Reset();
+    }
+
+    if(mReadIsEnabledTimer)
+    {
+      mReadIsEnabledTimer.Stop();
+      mReadIsEnabledTimer.Reset();
+    }
+
+    if(mReadScreenReaderEnabledTimer)
+    {
+      mReadScreenReaderEnabledTimer.Stop();
+      mReadScreenReaderEnabledTimer.Reset();
+    }
+
+    if(mForceUpTimer)
+    {
+      mForceUpTimer.Stop();
+      mForceUpTimer.Reset();
+    }
   }
 
   /**
@@ -224,6 +272,11 @@ public:
   {
     if(mData)
     {
+      // The ~Window() after this point cannot emit DESTROY, because Bridge is not available. So emit DESTROY here.
+      for(auto windowAccessible : mApplication.mChildren)
+      {
+        BridgeObject::Emit(windowAccessible, WindowEvent::DESTROY);
+      }
       mData->mCurrentlyHighlightedActor = {};
       mData->mHighlightActor            = {};
     }
@@ -237,14 +290,34 @@ public:
     mConnectionPtr                    = {};
   }
 
+  bool ForceUpTimerCallback()
+  {
+    if(ForceUp() != ForceUpResult::FAILED)
+    {
+      return false;
+    }
+    return true;
+  }
+
   /**
    * @copydoc Dali::Accessibility::Bridge::ForceUp()
    */
   ForceUpResult ForceUp() override
   {
-    if(BridgeAccessible::ForceUp() == ForceUpResult::ALREADY_UP)
+    auto forceUpResult = BridgeAccessible::ForceUp();
+    if(forceUpResult == ForceUpResult::ALREADY_UP)
+    {
+      return forceUpResult;
+    }
+    else if(forceUpResult == ForceUpResult::FAILED)
     {
-      return ForceUpResult::ALREADY_UP;
+      if(!mForceUpTimer)
+      {
+        mForceUpTimer = Dali::Timer::New(RETRY_INTERVAL);
+        mForceUpTimer.TickSignal().Connect(this, &BridgeImpl::ForceUpTimerCallback);
+        mForceUpTimer.Start();
+      }
+      return forceUpResult;
     }
 
     BridgeObject::RegisterInterfaces();
@@ -257,10 +330,13 @@ public:
     BridgeEditableText::RegisterInterfaces();
     BridgeSelection::RegisterInterfaces();
     BridgeApplication::RegisterInterfaces();
+    BridgeHypertext::RegisterInterfaces();
+    BridgeHyperlink::RegisterInterfaces();
+    BridgeSocket::RegisterInterfaces();
 
     RegisterOnBridge(&mApplication);
 
-    mRegistryClient      = {AtspiDbusNameRegistry, AtspiDbusPathDec, AtspiDbusInterfaceDec, mConnectionPtr};
+    mRegistryClient      = {AtspiDbusNameRegistry, AtspiDbusPathDec, Accessible::GetInterfaceName(AtspiInterface::DEVICE_EVENT_CONTROLLER), mConnectionPtr};
     mDirectReadingClient = DBus::DBusClient{DirectReadingDBusName, DirectReadingDBusPath, DirectReadingDBusInterface, mConnectionPtr};
 
     mDirectReadingClient.addSignal<void(int32_t, std::string)>("ReadingStateChanged", [=](int32_t id, std::string readingState) {
@@ -275,7 +351,7 @@ public:
       }
     });
 
-    auto    proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, AtspiDbusInterfaceSocket, mConnectionPtr};
+    auto    proxy = DBus::DBusClient{AtspiDbusNameRegistry, AtspiDbusPathRoot, Accessible::GetInterfaceName(AtspiInterface::SOCKET), mConnectionPtr};
     Address root{"", "root"};
     auto    res = proxy.method<Address(Address)>("Embed").call(root);
     if(!res)
@@ -285,113 +361,257 @@ public:
     assert(res);
 
     mApplication.mParent.SetAddress(std::move(std::get<0>(res)));
-    if(mIsShown)
+
+    mEnabledSignal.Emit();
+
+    return ForceUpResult::JUST_STARTED;
+  }
+
+  /**
+   * @brief Sends a signal to dbus that the window is shown.
+   *
+   * @param[in] window The window to be shown
+   * @see Accessible::EmitShowing() and BridgeObject::EmitStateChanged()
+   */
+  void EmitShown(Dali::Window window)
+  {
+    auto windowAccessible = mApplication.GetWindowAccessible(window);
+    if(windowAccessible)
     {
-      EmitActivate();
+      windowAccessible->EmitShowing(true);
     }
-    return ForceUpResult::JUST_STARTED;
   }
 
   /**
-   * @brief Sends a signal to dbus that the default window is activated.
+   * @brief Sends a signal to dbus that the window is hidden.
    *
-   * TODO : This is subject to change if/when we implement multi-window support.
+   * @param[in] window The window to be hidden
+   * @see Accessible::EmitShowing() and BridgeObject::EmitStateChanged()
+   */
+  void EmitHidden(Dali::Window window)
+  {
+    auto windowAccessible = mApplication.GetWindowAccessible(window);
+    if(windowAccessible)
+    {
+      windowAccessible->EmitShowing(false);
+    }
+  }
+
+  /**
+   * @brief Sends a signal to dbus that the window is activated.
+   *
+   * @param[in] window The window to be activated
    * @see BridgeObject::Emit()
    */
-  void EmitActivate()
+  void EmitActivate(Dali::Window window)
   {
-    auto win = mApplication.GetActiveWindow();
-    if(win)
+    auto windowAccessible = mApplication.GetWindowAccessible(window);
+    if(windowAccessible)
     {
-      win->Emit(WindowEvent::ACTIVATE, 0);
+      windowAccessible->Emit(WindowEvent::ACTIVATE, 0);
     }
   }
 
   /**
-   * @brief Sends a signal to dbus that the default window is deactivated.
+   * @brief Sends a signal to dbus that the window is deactivated.
    *
-   * TODO : This is subject to change if/when we implement multi-window support.
+   * @param[in] window The window to be deactivated
    * @see BridgeObject::Emit()
    */
-  void EmitDeactivate()
+  void EmitDeactivate(Dali::Window window)
   {
-    auto win = mApplication.GetActiveWindow();
-    if(win)
+    auto windowAccessible = mApplication.GetWindowAccessible(window);
+    if(windowAccessible)
     {
-      win->Emit(WindowEvent::DEACTIVATE, 0);
+      windowAccessible->Emit(WindowEvent::DEACTIVATE, 0);
     }
   }
 
   /**
+   * @copydoc Dali::Accessibility::Bridge::WindowShown()
+   */
+  void WindowShown(Dali::Window window) override
+  {
+    if(!mIsShown && IsUp())
+    {
+      EmitShown(window);
+    }
+    mIsShown = true;
+  }
+
+  /**
    * @copydoc Dali::Accessibility::Bridge::WindowHidden()
    */
-  void WindowHidden() override
+  void WindowHidden(Dali::Window window) override
   {
     if(mIsShown && IsUp())
     {
-      EmitDeactivate();
+      EmitHidden(window);
     }
     mIsShown = false;
   }
 
   /**
-   * @copydoc Dali::Accessibility::Bridge::WindowShown()
+   * @copydoc Dali::Accessibility::Bridge::WindowFocused()
    */
-  void WindowShown() override
+  void WindowFocused(Dali::Window window) override
   {
-    if(!mIsShown && IsUp())
+    if(mIsShown && IsUp())
     {
-      EmitActivate();
+      EmitActivate(window);
     }
-    mIsShown = true;
   }
 
-  void ReadAndListenProperty()
+  /**
+   * @copydoc Dali::Accessibility::Bridge::WindowUnfocused()
+   */
+  void WindowUnfocused(Dali::Window window) override
   {
-    // read property
-    auto enabled = mAccessibilityStatusClient.property<bool>("ScreenReaderEnabled").get();
-    if(enabled)
+    if(mIsShown && IsUp())
     {
-      mIsScreenReaderEnabled = std::get<0>(enabled);
+      EmitDeactivate(window);
     }
+  }
 
-    enabled = mAccessibilityStatusClient.property<bool>("IsEnabled").get();
-    if(enabled)
+  /**
+   * @copydoc Dali::Accessibility::Bridge::SuppressScreenReader()
+   */
+  void SuppressScreenReader(bool suppress) override
+  {
+    if(mIsScreenReaderSuppressed == suppress)
     {
-      mIsEnabled = std::get<0>(enabled);
+      return;
     }
+    mIsScreenReaderSuppressed = suppress;
+    ReadScreenReaderEnabledProperty();
+  }
 
-    if(mIsScreenReaderEnabled || mIsEnabled)
+  void SwitchBridge()
+  {
+    if((!mIsScreenReaderSuppressed && mIsScreenReaderEnabled) || mIsEnabled)
     {
       ForceUp();
     }
+    else
+    {
+      ForceDown();
+    }
+  }
 
-    // listen property change
-    mAccessibilityStatusClient.addPropertyChangedEvent<bool>("ScreenReaderEnabled", [this](bool res) {
-      mIsScreenReaderEnabled = res;
-      if(mIsScreenReaderEnabled || mIsEnabled)
+  bool ReadIsEnabledTimerCallback()
+  {
+    ReadIsEnabledProperty();
+    return false;
+  }
+
+  void ReadIsEnabledProperty()
+  {
+    mAccessibilityStatusClient.property<bool>("IsEnabled").asyncGet([this](DBus::ValueOrError<bool> msg) {
+      if(!msg)
       {
-        ForceUp();
+        DALI_LOG_ERROR("Get IsEnabled property error: %s\n", msg.getError().message.c_str());
+        if(msg.getError().errorType == DBus::ErrorType::INVALID_REPLY)
+        {
+          if(!mReadIsEnabledTimer)
+          {
+            mReadIsEnabledTimer = Dali::Timer::New(RETRY_INTERVAL);
+            mReadIsEnabledTimer.TickSignal().Connect(this, &BridgeImpl::ReadIsEnabledTimerCallback);
+          }
+          mReadIsEnabledTimer.Start();
+        }
+        return;
       }
-      else
+
+      if(mReadIsEnabledTimer)
       {
-        ForceDown();
+        mReadIsEnabledTimer.Stop();
+        mReadIsEnabledTimer.Reset();
       }
+
+      mIsEnabled = std::get<0>(msg);
+      SwitchBridge();
     });
+  }
 
+  void ListenIsEnabledProperty()
+  {
     mAccessibilityStatusClient.addPropertyChangedEvent<bool>("IsEnabled", [this](bool res) {
       mIsEnabled = res;
-      if(mIsScreenReaderEnabled || mIsEnabled)
+      SwitchBridge();
+    });
+  }
+
+  bool ReadScreenReaderEnabledTimerCallback()
+  {
+    ReadScreenReaderEnabledProperty();
+    return false;
+  }
+
+  void ReadScreenReaderEnabledProperty()
+  {
+    // can be true because of SuppressScreenReader before init
+    if (!mAccessibilityStatusClient)
+    {
+      return;
+    }
+
+    mAccessibilityStatusClient.property<bool>("ScreenReaderEnabled").asyncGet([this](DBus::ValueOrError<bool> msg) {
+      if(!msg)
       {
-        ForceUp();
+        DALI_LOG_ERROR("Get ScreenReaderEnabled property error: %s\n", msg.getError().message.c_str());
+        if(msg.getError().errorType == DBus::ErrorType::INVALID_REPLY)
+        {
+          if(!mReadScreenReaderEnabledTimer)
+          {
+            mReadScreenReaderEnabledTimer = Dali::Timer::New(RETRY_INTERVAL);
+            mReadScreenReaderEnabledTimer.TickSignal().Connect(this, &BridgeImpl::ReadScreenReaderEnabledTimerCallback);
+          }
+          mReadScreenReaderEnabledTimer.Start();
+        }
+        return;
       }
-      else
+
+      if(mReadScreenReaderEnabledTimer)
       {
-        ForceDown();
+        mReadScreenReaderEnabledTimer.Stop();
+        mReadScreenReaderEnabledTimer.Reset();
       }
+
+      mIsScreenReaderEnabled = std::get<0>(msg);
+      SwitchBridge();
+    });
+  }
+
+  void EmitScreenReaderEnabledSignal()
+  {
+    if (mIsScreenReaderEnabled)
+    {
+      mScreenReaderEnabledSignal.Emit();
+    }
+    else
+    {
+      mScreenReaderDisabledSignal.Emit();
+    }
+  }
+
+  void ListenScreenReaderEnabledProperty()
+  {
+    mAccessibilityStatusClient.addPropertyChangedEvent<bool>("ScreenReaderEnabled", [this](bool res) {
+      mIsScreenReaderEnabled = res;
+      EmitScreenReaderEnabledSignal();
+      SwitchBridge();
     });
   }
 
+  void ReadAndListenProperties()
+  {
+    ReadIsEnabledProperty();
+    ListenIsEnabledProperty();
+
+    ReadScreenReaderEnabledProperty();
+    ListenScreenReaderEnabledProperty();
+  }
+
   bool InitializeAccessibilityStatusClient()
   {
     mAccessibilityStatusClient = DBus::DBusClient{A11yDbusName, A11yDbusPath, A11yDbusStatusInterface, DBus::ConnectionType::SESSION};
@@ -405,16 +625,34 @@ public:
     return true;
   }
 
+  bool InitializeTimerCallback()
+  {
+    if ( InitializeAccessibilityStatusClient() )
+    {
+      ReadAndListenProperties();
+      return false;
+    }
+    return true;
+  }
+
   bool OnIdleSignal()
   {
     if ( InitializeAccessibilityStatusClient() )
     {
-      ReadAndListenProperty();
+      ReadAndListenProperties();
       mIdleCallback = NULL;
       return false;
     }
 
-    return true;
+    if(!mInitializeTimer)
+    {
+      mInitializeTimer = Dali::Timer::New(RETRY_INTERVAL);
+      mInitializeTimer.TickSignal().Connect(this, &BridgeImpl::InitializeTimerCallback);
+    }
+    mInitializeTimer.Start();
+
+    mIdleCallback = NULL;
+    return false;
   }
 
   /**
@@ -424,7 +662,7 @@ public:
   {
     if ( InitializeAccessibilityStatusClient() )
     {
-      ReadAndListenProperty();
+      ReadAndListenProperties();
       return;
     }
 
@@ -468,7 +706,7 @@ bool INITIALIZED_BRIDGE = false;
  * @return The BridgeImpl instance
  * @note This method is to check environment variable first. If ATSPI is disable using env, it returns dummy bridge instance.
  */
-Bridge* CreateBridge()
+std::shared_ptr<Bridge> CreateBridge()
 {
   INITIALIZED_BRIDGE = true;
 
@@ -481,7 +719,7 @@ Bridge* CreateBridge()
       return Dali::Accessibility::DummyBridge::GetInstance();
     }
 
-    return new BridgeImpl;
+    return std::make_shared<BridgeImpl>();
   }
   catch(const std::exception&)
   {
@@ -494,9 +732,9 @@ Bridge* CreateBridge()
 
 // Dali::Accessibility::Bridge class implementation
 
-Bridge* Bridge::GetCurrentBridge()
+std::shared_ptr<Bridge> Bridge::GetCurrentBridge()
 {
-  static Bridge* bridge;
+  static std::shared_ptr<Bridge> bridge;
 
   if(bridge)
   {
@@ -538,17 +776,19 @@ void Bridge::EnableAutoInit()
     return;
   }
 
-  auto rootLayer       = Dali::Stage::GetCurrent().GetRootLayer();
+  auto rootLayer       = Dali::Stage::GetCurrent().GetRootLayer(); // A root layer of the default window.
   auto window          = Dali::DevelWindow::Get(rootLayer);
   auto applicationName = Dali::Internal::Adaptor::Adaptor::GetApplicationPackageName();
 
-  auto* bridge = Bridge::GetCurrentBridge();
-  bridge->AddTopLevelWindow(Dali::Accessibility::Accessible::Get(rootLayer, true));
+  auto accessible = Accessibility::Accessible::Get(rootLayer);
+
+  auto bridge = Bridge::GetCurrentBridge();
+  bridge->AddTopLevelWindow(accessible);
   bridge->SetApplicationName(applicationName);
   bridge->Initialize();
 
   if(window && window.IsVisible())
   {
-    bridge->WindowShown();
+    bridge->WindowShown(window);
   }
 }