Add new internal APIs 88/263688/5
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 8 Sep 2021 06:56:44 +0000 (15:56 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 8 Sep 2021 23:51:39 +0000 (08:51 +0900)
When the window of the application is appeared, the event callback function
is called.

Adds:
 - aul_window_register_event_cb()
 - aul_window_deregister_event_cb()

Change-Id: I9f588043fca36c37a74f6be53e531f479ad9fa88
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
include/aul_window.h
src/aul_window.cc
tool/aul_window/CMakeLists.txt
tool/aul_window/aul_window.cc

index 5012ef8..22c944f 100644 (file)
@@ -242,6 +242,38 @@ int aul_window_info_get_opaque(aul_window_info_h info, bool *opaque);
  */
 int aul_window_attach_below(const char *parent_appid, const char *child_appid);
 
+/**
+ * @brief Called when the window of the application is appeared.
+ * @since_tizen 6.5
+ * @details When the window is appeared, the event_name is "Appeared".
+ *
+ * @param[in]   event_name      The event name
+ * @param[in]   appid           The application ID
+ * @param[in]   wid             The window ID
+ * @param[in]   pid             The process ID
+ * @param[in]   user_data       The user data passed from the registration function
+ * @see aul_window_register_event_cb()
+ * @see aul_window_deregister_event_cb()
+ */
+typedef void (*aul_window_event_cb)(const char *event_name, const char *appid, int wid, int pid, void *user_data);
+
+/**
+ * @brief Registers the window event callback function.
+ * @since_tizen 6.5
+ *
+ * @param[in]   callback        The callback function
+ * @param[in]   user_data       The user data to be passed to the callback function
+ * @return      @c 0 on success,
+ *              otherwise a negative error value
+ */
+int aul_window_register_event_cb(aul_window_event_cb callback, void *user_data);
+
+/**
+ * @brief Deregisters the window event callback function.
+ * @since_tizen 6.5
+ */
+void aul_window_deregister_event_cb(void);
+
 #ifdef __cplusplus
 }
 #endif
index aa09949..56b5752 100644 (file)
 #include <bundle_cpp.h>
 
 #include <memory>
+#include <mutex>
 #include <string>
 
 #include "app_request.h"
 #include "aul_api.h"
 #include "aul_util.h"
 #include "include/aul.h"
+#include "include/aul_app_com.h"
 #include "include/aul_cmd.h"
 #include "include/aul_window.h"
 #include "launch.h"
@@ -147,6 +149,76 @@ void WindowInfoDestroyFunc(gpointer data) {
   delete info;
 }
 
+class WindowEventListener {
+ public:
+  WindowEventListener() = default;
+
+  ~WindowEventListener() {
+    Deregister();
+  }
+
+  int Register(aul_window_event_cb cb, void* user_data) {
+    std::lock_guard<std::recursive_mutex> lock(GetMutex());
+    cb_ = cb;
+    user_data_ = user_data;
+
+    if (conn_ != nullptr)
+      return AUL_R_OK;
+
+    int ret = aul_app_com_create_async("aul_window_event", nullptr,
+        AppComMessageCb, this, &conn_);
+    if (ret != AUL_R_OK) {
+      _E("aul_app_com_create_async() is failed. error(%d)", ret);
+      return ret;
+    }
+
+    return AUL_R_OK;
+  }
+
+  void Deregister() {
+    std::lock_guard<std::recursive_mutex> lock(GetMutex());
+    cb_ = nullptr;
+    user_data_ = nullptr;
+
+    if (conn_ == nullptr)
+      return;
+
+    aul_app_com_leave(conn_);
+    conn_ = nullptr;
+  }
+
+ private:
+  static int AppComMessageCb(const char* endpoint, aul_app_com_result_e res,
+      bundle* envelope, void* user_data) {
+    tizen_base::Bundle b(envelope, false, false);
+    std::string event_name = b.GetString(AUL_K_EVENT_NAME);
+    std::string appid = b.GetString(AUL_K_APPID);
+    int wid = std::stoi(b.GetString(AUL_K_WID));
+    int pid = std::stoi(b.GetString(AUL_K_PID));
+
+    auto* handle = static_cast<WindowEventListener*>(user_data);
+    std::lock_guard<std::recursive_mutex> lock(handle->GetMutex());
+    if (handle->cb_) {
+      handle->cb_(event_name.c_str(), appid.c_str(), wid, pid,
+          handle->user_data_);
+    }
+
+    return 0;
+  }
+
+  std::recursive_mutex& GetMutex() const {
+    return mutex_;
+  }
+
+ private:
+  aul_app_com_connection_h conn_ = nullptr;
+  aul_window_event_cb cb_ = nullptr;
+  void* user_data_ = nullptr;
+  mutable std::recursive_mutex mutex_;
+};
+
+WindowEventListener listener;
+
 }  // namespace
 
 extern "C" API int aul_window_stack_get(aul_window_stack_h* handle) {
@@ -499,3 +571,21 @@ extern "C" API int aul_window_attach_below(const char* parent_appid,
 
   return AUL_R_OK;
 }
+
+extern "C" API int aul_window_register_event_cb(aul_window_event_cb callback,
+    void* user_data) {
+  if (callback == nullptr) {
+    _E("Invalid parameter");
+    return AUL_R_EINVAL;
+  }
+
+  int ret = listener.Register(callback, user_data);
+  if (ret != AUL_R_OK)
+    return ret;
+
+  return AUL_R_OK;
+}
+
+extern "C" API void aul_window_deregister_event_cb(void) {
+  listener.Deregister();
+}
index 1e20ed2..e8bd4e4 100644 (file)
@@ -15,4 +15,8 @@ TARGET_INCLUDE_DIRECTORIES(${TARGET_AUL_WINDOW} PUBLIC
   ${CMAKE_CURRENT_SOURCE_DIR}/../../src
   ${CMAKE_CURRENT_SOURCE_DIR}/../../include)
 
+APPLY_PKG_CONFIG(${TARGET_AUL_WINDOW} PUBLIC
+  GLIB_DEPS
+)
+
 INSTALL(TARGETS ${TARGET_AUL_WINDOW} DESTINATION bin)
index a3c9cad..761bfbf 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <aul.h>
 #include <aul_window.h>
+#include <glib.h>
 #include <stdio.h>
 #include <stdlib.h>
 
@@ -34,6 +35,7 @@ void PrintUsage(const char* cmdline) {
   printf("   attach_window <parent_appid> <child_appid>\n");
   printf("   detach_window <child_appid>\n");
   printf("   attach_window_below <parent_appid> <child_appid>\n");
+  printf("   monitor_window_event\n");
 }
 
 const char* GetNotificationLevelString(aul_window_notification_level_e level) {
@@ -151,12 +153,36 @@ int HandleAttachWindowBelow(int argc, char** argv) {
   return ret;
 }
 
+void WindowEventCb(const char* event_name, const char* appid, int wid, int pid,
+    void* user_data) {
+  printf("[Window Event]\n");
+  printf(" - Event_name: %s\n", event_name);
+  printf(" - Application ID: %s\n", appid);
+  printf(" - Window ID: %d\n", wid);
+  printf(" - Process ID: %d\n", pid);
+  printf("\n");
+}
+
+int HandleMonitorWindowEvent(int argc, char** argv) {
+  printf("[%s]\n", argv[1]);
+  int ret = aul_window_register_event_cb(WindowEventCb, nullptr);
+  printf("[%s] result: %d\n", argv[1], ret);
+  if (ret != AUL_R_OK)
+    return ret;
+
+  GMainLoop* loop = g_main_loop_new(nullptr, FALSE);
+  g_main_loop_run(loop);
+  g_main_loop_unref(loop);
+  return 0;
+}
+
 std::map<std::string, HandleFunc> handlers = {
   { "foreach_window_stack", HandleForeachWindowStack },
   { "get_focused_pid", HandleGetFocusedPid },
   { "attach_window", HandleAttachWindow },
   { "detach_window", HandleDetachWindow },
   { "attach_window_below", HandleAttachWindowBelow },
+  { "monitor_window_event", HandleMonitorWindowEvent },
 };
 
 }  // namespace