Aurum: Introduce new atspi event callback system. 96/283296/4
authorHosang Kim <hosang12.kim@samsung.com>
Fri, 21 Oct 2022 11:31:05 +0000 (20:31 +0900)
committerHosang Kim <hosang12.kim@samsung.com>
Fri, 18 Nov 2022 05:54:52 +0000 (14:54 +0900)
- usage :

1. callback function
bool deactive_cb(void *data, A11yEvent type, std::string pkg)
{
        return false;
}

AccessibleWatcher::getInstance()->registerCallback(A11yEvent::EVENT_WINDOW_DEACTIVATE, &deactive_cb, nullptr);

2. lambda expression
AccessibleWatcher::getInstance()->registerCallback(A11yEvent::EVENT_WINDOW_ACTIVATE, [](void *data, A11yEvent type, std::string pkg) { return true; }, nullptr);

Change-Id: I1401e6d71c5579967ab7094a8b84545bb3ad3c4b

12 files changed:
libaurum/inc/A11yEventHandler.h [new file with mode: 0644]
libaurum/inc/Accessibility/AccessibleWatcher.h
libaurum/inc/Aurum.h
libaurum/inc/Impl/Accessibility/AtspiAccessibleWatcher.h
libaurum/inc/Impl/Accessibility/MockAccessibleWatcher.h
libaurum/inc/UiDevice.h
libaurum/meson.build
libaurum/src/A11yEventHandler.cc [new file with mode: 0644]
libaurum/src/Impl/Accessibility/AtspiAccessibleWatcher.cc
libaurum/src/Impl/Accessibility/MockAccessibleWatcher.cc
libaurum/src/UiDevice.cc
libaurum/src/meson.build

diff --git a/libaurum/inc/A11yEventHandler.h b/libaurum/inc/A11yEventHandler.h
new file mode 100644 (file)
index 0000000..7687308
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *               http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+#ifndef _A11Y_EVENT_HANDLER_H_
+#define _A11Y_EVENT_HANDLER_H_
+
+#include "A11yEvent.h"
+
+namespace Aurum {
+
+typedef bool (*EventHandler)(void *data, const A11yEvent type, std::string pkg);
+
+/**
+ * @class A11yEventHandler
+ *
+ * @ingroup aurum
+ *
+ * @brief Class that handles the A11yEvent.
+ */
+class A11yEventHandler {
+public:
+    /**
+    * @brief A11yEventHandler constructor with event type, callback and data
+    *
+    * @param[in] type @A11yEvent
+    * @param[in] cb user callback
+    * @param[in] data user data
+    *
+    * @since_tizen 7.5
+    */
+    A11yEventHandler(A11yEvent type, EventHandler cb, void *data);
+
+    /**
+    * @brief Gets A11yEvent
+    *
+    * @return @A11yEvent
+    *
+    * @since_tizen 7.5
+    */
+    A11yEvent getType() const;
+
+    /**
+    * @brief Calls user callback function.
+    *
+    * @param[in] pkg package name
+    *
+    * @return false removes user callback
+    *
+    * @since_tizen 7.5
+    */
+    bool operator() (std::string pkg);
+
+private:
+    A11yEvent mType;
+    EventHandler mCb;
+    void *mData;
+};
+
+}
+
+#endif
index 5f2c300..c3b9308 100644 (file)
@@ -24,6 +24,7 @@
 #include "IEventSource.h"
 #include "Runnable.h"
 #include "A11yEvent.h"
+#include "A11yEventHandler.h"
 #include "AurumXML.h"
 #include <atspi/atspi.h>
 
@@ -126,6 +127,18 @@ public:
      */
     virtual std::map<std::string, std::shared_ptr<AurumXML>> getXMLDocMap(void) = 0;
 
+    /**
+     * @brief Register atspi event callback.
+     *
+     * @param[in] type @A11yEvent
+     * @param[in] cb @EventHandler
+     * @param[in] data @void pointer
+     *
+     * @return true if callback is registered, else false
+     * @since_tizen 7.0
+     */
+    virtual bool registerCallback(const A11yEvent type, EventHandler cb, void *data) = 0;
+
 public:
     /**
      * @brief Gets active application vector.
index c916371..e2ecd24 100644 (file)
@@ -61,6 +61,7 @@
 #include "PartialMatch.h"
 #include "Comparer.h"
 #include "A11yEvent.h"
+#include "A11yEventHandler.h"
 #include "Runnables.h"
 #ifdef MQTT_ENABLED
 #include "ScreenAnalyzerWatcher.h"
index 9285e6a..967b010 100644 (file)
@@ -117,6 +117,11 @@ public:
      */
     virtual std::map<std::string, std::shared_ptr<AurumXML>> getXMLDocMap(void) override;
 
+    /**
+     * @copydoc @AccessibleWatcher::registerCallback()
+     */
+    virtual bool registerCallback(const A11yEvent type, EventHandler cb, void *data) override;
+
 public:
     /**
      * @brief Listen atspi events.
@@ -137,23 +142,12 @@ public:
      */
     void onObjectDefunct(AtspiAccessible *node) override;
 
-    /**
-     * @brief AtspiEvent listener.
-     *
-     * @param[in] event AtspiEvent
-     * @param[in] user_data data for event by user
-     *
-     * @since_tizen 6.5
-     */
-    static void onEventListener(AtspiEvent *event, void *user_data);
 private:
     /** Private methods for Mock Test **/
     bool removeFromActivatedList(AtspiAccessible *node);
     bool addToActivatedList(AtspiAccessible *node);
     bool removeFromWindowSet(AtspiAccessible *node);
     bool addToWindowSet(AtspiAccessible *node);
-    void addEventListener(AtspiEventListener *listener, A11yEvent type);
-    void removeEventListener(AtspiEventListener *listener, A11yEvent type);
     static gpointer eventThreadLoop(gpointer data);
     void appendApp(AtspiAccessibleWatcher *instance, AtspiAccessible *app, char *pkg);
     void removeApp(AtspiAccessibleWatcher *instance, AtspiAccessible *app, char *pkg);
@@ -171,6 +165,7 @@ private:
     static GMainLoop *mLoop;
     bool isTv;
     std::mutex XMLMutex;
+    std::map<const A11yEvent, std::list<std::shared_ptr<A11yEventHandler>>> mHandlers;
 };
 
 }
index 0139365..80209bf 100644 (file)
@@ -73,6 +73,8 @@ public:
      */
     virtual bool executeAndWaitForEvents(const Runnable *cmd, const A11yEvent type, const int timeout, const std::string packageName) override;
 
+    virtual bool registerCallback(const A11yEvent type, EventHandler cb, void *data) override;
+
     std::map<AtspiAccessible *, std::shared_ptr<AccessibleApplication>> getActiveAppMap(void);
 
     std::map<std::string, std::shared_ptr<AurumXML>> getXMLDocMap(void);
index fa4a22a..025221f 100644 (file)
@@ -372,6 +372,19 @@ public:
      */
     bool executeAndWaitForEvents(
         const Runnable *cmd, const A11yEvent type, const int timeout, const std::string packageName) const;
+
+/**
+     * @brief Register atspi event callback.
+     *
+     * @param[in] type @A11yEvent
+     * @param[in] cb @EventHandler
+     * @param[in] data @void pointer
+     *
+     * @return true if callback is registered, else false
+     * @since_tizen 7.0
+     */
+    bool registerCallback(const A11yEvent type, EventHandler cb, void *data) const;
+
 public:
     /**
      * @brief Gets UiDevice instance.
index df52fc4..58e8e18 100644 (file)
@@ -15,6 +15,7 @@ libaurum_install_inc = [
   './inc/PartialMatch.h',
   './inc/UiScrollable.h',
   './inc/A11yEvent.h',
+  './inc/A11yEventHandler.h',
   './inc/Comparer.h',
   './inc/AurumXML.h',
   './inc/Accessibility/AccessibleNode.h',
diff --git a/libaurum/src/A11yEventHandler.cc b/libaurum/src/A11yEventHandler.cc
new file mode 100644 (file)
index 0000000..b0cb63d
--- /dev/null
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *               http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ *
+ */
+
+#include "Aurum.h"
+
+using namespace Aurum;
+
+A11yEventHandler::A11yEventHandler(A11yEvent type, EventHandler cb, void *data)
+    : mType(type), mCb(cb), mData(data)
+{
+}
+A11yEvent A11yEventHandler::getType() const
+{
+    return mType;
+}
+bool A11yEventHandler::operator() (std::string pkg)
+{
+    return mCb(mData, mType, pkg);
+}
+
index f3d7d8e..27f332e 100644 (file)
@@ -251,9 +251,24 @@ void AtspiAccessibleWatcher::onAtspiEvents(AtspiEvent *event, void *watcher)
         pkg = strdup("");
 
     mMutex.lock();
-    mEventQueue.push_back(std::make_shared<A11yEventInfo>(std::string(event->type), std::string(name), std::string(pkg)));
+    auto a11yEvent = std::make_shared<A11yEventInfo>(std::string(event->type), std::string(name), std::string(pkg));
+    mEventQueue.push_back(a11yEvent);
     mMutex.unlock();
 
+    if (instance->mHandlers.count(a11yEvent->getEvent())) {
+        auto &list = instance->mHandlers[a11yEvent->getEvent()];
+        auto it = list.begin();
+
+        while (it != list.end())
+        {
+            LOGI("Callback call type %s pkg %s", event->type, pkg);
+            auto handler = *it;
+            bool res = (*handler)(std::string(pkg));
+            if (!res) it = list.erase(it);
+            else ++it;
+        }
+    }
+
     if (!strcmp(event->type, "object:state-changed:defunct")) {
          instance->onObjectDefunct(
             static_cast<AtspiAccessible *>(event->source));
@@ -417,3 +432,18 @@ bool AtspiAccessibleWatcher::addToWindowSet(AtspiAccessible *node)
     }
     return false;
 }
+
+bool AtspiAccessibleWatcher::registerCallback(const A11yEvent type, EventHandler cb, void *data)
+{
+    auto handler = std::make_shared<A11yEventHandler>(type, cb, data);
+    if (mHandlers.count(type)) {
+        auto list = mHandlers[type];
+        list.push_back(handler);
+        mHandlers[type] = list;
+    } else {
+        std::list<std::shared_ptr<A11yEventHandler>> list;
+        list.push_back(handler);
+        mHandlers.insert(std::pair<const A11yEvent, std::list<std::shared_ptr<A11yEventHandler>>>(type, list));
+    }
+    return true;
+}
index 4377d88..ac44651 100644 (file)
@@ -72,3 +72,8 @@ bool MockAccessibleWatcher::executeAndWaitForEvents(const Runnable *cmd, const A
 {
     return true;
 }
+
+bool MockAccessibleWatcher::registerCallback(const A11yEvent type, EventHandler cb, void *data)
+{
+    return true;
+}
\ No newline at end of file
index eccd630..4865aa6 100644 (file)
@@ -475,3 +475,8 @@ bool UiDevice::getWithScreenAnalyzer()
 {
     return mIsWithSA;
 }
+
+bool UiDevice::registerCallback(const A11yEvent type, EventHandler cb, void *data) const
+{
+    return AccessibleWatcher::getInstance()->registerCallback(type, cb, data);
+}
\ No newline at end of file
index 778c7b0..7e6a215 100644 (file)
@@ -9,6 +9,7 @@ libaurum_src += [
   files('Waiter.cc'),
   files('PartialMatch.cc'),
   files('A11yEvent.cc'),
+  files('A11yEventHandler.cc'),
   files('AurumXML.cc'),
   files('TizenWindow.cc'),
 ]