diff options
author | Hwankyu Jhun <h.jhun@samsung.com> | 2021-09-08 15:56:44 +0900 |
---|---|---|
committer | Hwankyu Jhun <h.jhun@samsung.com> | 2021-09-09 08:51:39 +0900 |
commit | 76e2e5eaff79dfceb424107925d336a79d0d45ac (patch) | |
tree | 19bd4ae4c0a2eb18c405808ea17a5425b8c9bc27 | |
parent | d30f7536a6648781d24bface46e1caf0f95ee658 (diff) | |
download | aul-1-76e2e5eaff79dfceb424107925d336a79d0d45ac.tar.gz aul-1-76e2e5eaff79dfceb424107925d336a79d0d45ac.tar.bz2 aul-1-76e2e5eaff79dfceb424107925d336a79d0d45ac.zip |
Add new internal APIs
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>
-rw-r--r-- | include/aul_window.h | 32 | ||||
-rw-r--r-- | src/aul_window.cc | 90 | ||||
-rw-r--r-- | tool/aul_window/CMakeLists.txt | 4 | ||||
-rw-r--r-- | tool/aul_window/aul_window.cc | 26 |
4 files changed, 152 insertions, 0 deletions
diff --git a/include/aul_window.h b/include/aul_window.h index 5012ef88..22c944ff 100644 --- a/include/aul_window.h +++ b/include/aul_window.h @@ -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 diff --git a/src/aul_window.cc b/src/aul_window.cc index aa09949d..56b57525 100644 --- a/src/aul_window.cc +++ b/src/aul_window.cc @@ -22,12 +22,14 @@ #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(); +} diff --git a/tool/aul_window/CMakeLists.txt b/tool/aul_window/CMakeLists.txt index 1e20ed20..e8bd4e44 100644 --- a/tool/aul_window/CMakeLists.txt +++ b/tool/aul_window/CMakeLists.txt @@ -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) diff --git a/tool/aul_window/aul_window.cc b/tool/aul_window/aul_window.cc index a3c9cad7..761bfbf6 100644 --- a/tool/aul_window/aul_window.cc +++ b/tool/aul_window/aul_window.cc @@ -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 |