Add new functions for widget event 55/232855/2
authorHwankyu Jhun <h.jhun@samsung.com>
Fri, 8 May 2020 04:09:48 +0000 (13:09 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Fri, 8 May 2020 04:23:35 +0000 (13:23 +0900)
Adds:
 - aul_widget_set_event_cb()
 - aul_widget_unset_event_cb()
 - aul_widget_send_event()

Change-Id: I3fdad31c86676d1512a0034ab21bc14cd8e7f22d
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
include/aul_cmd.h
include/aul_key.h
include/aul_widget.h
src/aul_cmd.c
src/widget.c

index aa2ced9..48f405d 100755 (executable)
@@ -180,6 +180,7 @@ enum app_cmd {
 
        WIDGET_DISABLE = 141,
        TRIGGER_APP_SCREEN_FOCUSED_FORCE = 142,
+       WIDGET_EVENT = 143,
 
        APP_CMD_MAX
 };
index 8133e94..80bd72b 100644 (file)
  * @since_tizen 5.5
  */
 #define AUL_K_MULTI_INSTANCE_SHORTCUT   "__AUL_MULTI_INSTANCE_SHORTCUT__"
+
+/**
+ * @brief Definition for AUL: The event name.
+ * @since_tizen 5.5
+ */
+#define AUL_K_EVENT_NAME                "__AUL_EVENT_NAME__"
+
+/**
+ * @brief Definition for AUL: The event data.
+ * @since_tizen 5.5
+ */
+#define AUL_K_EVENT_DATA                "__AUL_EVENT_DATA__"
index 3fd6eff..7e06d0e 100755 (executable)
@@ -152,6 +152,65 @@ int aul_widget_service_set_disable(const char *widget_id, bool is_disable);
 
 int aul_widget_service_set_disable_db(const char *widget_id, bool is_disable);
 
+/**
+ * @brief Called when the widget event is delivered.
+ * @since_tizen 5.5
+ *
+ * @param[in]   event_name      The event name
+ * @param[in]   event_data      The event data
+ * @param[in]   user_data       The user data passed from the registration function
+ *
+ * @see aul_widget_set_event_cb()
+ */
+typedef void (*aul_widget_event_cb)(const char *event_name, bundle *event_data,
+               void *user_data);
+
+/**
+ * @brief Definition for AUL widget event: Saves image.
+ * @since_tizen 5.5
+ */
+#define AUL_WIDGET_EVENT_SAVE_IMAGE "save.image"
+
+/**
+ * @brief Registers the event callback function.
+ * @since_tizen 5.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
+ *
+ * @see aul_widget_event_cb()
+ * @see aul_widget_unset_event_cb()
+ */
+int aul_widget_set_event_cb(aul_widget_event_cb callback, void *user_data);
+
+/**
+ * @brief Unregisters the event callback function.
+ * @since_tizen 5.5
+ *
+ * @return      @c 0 on success,
+ *              otherwise a negative error value
+ *
+ * @see aul_widget_set_event_cb()
+ */
+int aul_widget_unset_event_cb(void);
+
+/**
+ * @brief Sends the widget event.
+ * @since_tizen 5.5
+ * @privlevel   platform
+ * @privilege   %http://tizen.org/privilege/internal/default/platform
+ *
+ * @param[in]   event_name      The event name
+ * @param[in]   event_data      The event data
+ * @return      @c 0 on success,
+ *              otherwise a negative error value
+ *
+ * @see aul_widget_set_event_cb()
+ */
+int aul_widget_send_event(const char *event_name, bundle *event_data);
+
 #ifdef __cplusplus
 }
 #endif
index 5058515..03f4d70 100755 (executable)
@@ -310,6 +310,8 @@ API const char *aul_cmd_convert_to_string(int cmd)
                return "WIDGET_DISABLE";
        case TRIGGER_APP_SCREEN_FOCUSED_FORCE:
                return "TRIGGER_APP_SCREEN_FOCUSED_FORCE";
+       case WIDGET_EVENT:
+               return "WIDGET_EVENT";
        default:
                return "CUSTOM_COMMAND";
        }
index 14c282e..30ccf79 100644 (file)
 
 #include "aul.h"
 #include "aul_api.h"
+#include "aul_app_com.h"
 #include "aul_cmd.h"
+#include "aul_db.h"
 #include "aul_error.h"
 #include "aul_sock.h"
 #include "aul_util.h"
 #include "aul_widget.h"
 #include "launch.h"
-#include "aul_db.h"
 
 struct aul_widget_info_s {
        char *widget_id;
@@ -52,6 +53,12 @@ struct widget_cb_info {
        void *user_data;
 };
 
+struct widget_event_s {
+       aul_app_com_connection_h conn;
+       aul_widget_event_cb callback;
+       void *user_data;
+};
+
 #define WIDGET_LOG_BUFFER_SIZE 10000
 #define WIDGET_LOG_BUFFER_STRING_SIZE 256
 #define WIDGET_SERVICE_DB ".widget.db"
@@ -59,6 +66,7 @@ struct widget_cb_info {
 static int __log_index;
 static int __log_fd;
 static bool __log_init = false;
+static struct widget_event_s __event;
 
 static int __init_log(void)
 {
@@ -716,3 +724,128 @@ API int aul_widget_service_set_disable_db(const char *widget_id, bool is_disable
 
        return AUL_R_OK;
 }
+
+static int __aul_widget_event_cb(const char *endpoint,
+               aul_app_com_result_e result,
+               bundle *envelope,
+               void *user_data)
+{
+       const char *event_name;
+       const char *event_data_raw;
+       bundle *event_data;
+
+       event_name = bundle_get_val(envelope, AUL_K_EVENT_NAME);
+       if (!event_name) {
+               _E("Failed to get event name");
+               return -1;
+       }
+
+       event_data_raw = bundle_get_val(envelope, AUL_K_EVENT_DATA);
+       if (!event_data_raw) {
+               _E("Failed to get event data");
+               return -1;
+       }
+
+       event_data = bundle_decode((const bundle_raw *)event_data_raw,
+                       strlen(event_data_raw));
+       if (!event_data) {
+               _E("Failed to decode event data");
+               return -1;
+       }
+
+       if (__event.callback)
+               __event.callback(event_name, event_data, __event.user_data);
+
+       bundle_free(event_data);
+
+       return 0;
+}
+
+static int __register_widget_event(void)
+{
+       int ret;
+
+       if (!__event.conn) {
+               ret = aul_app_com_create("widget.event", NULL,
+                               __aul_widget_event_cb, NULL,
+                               &__event.conn);
+               if (ret != AUL_R_OK) {
+                       _E("Failed to join aul app com");
+                       return ret;
+               }
+       }
+
+       return AUL_R_OK;
+}
+
+static int __unregister_widget_event(void)
+{
+       if (__event.conn) {
+               aul_app_com_leave(__event.conn);
+               __event.conn = NULL;
+       }
+
+       return AUL_R_OK;
+}
+
+API int aul_widget_set_event_cb(aul_widget_event_cb callback, void *user_data)
+{
+       if (!callback) {
+               _E("Invalid parameter");
+               return AUL_R_EINVAL;
+       }
+
+       __event.callback = callback;
+       __event.user_data = user_data;
+
+       return __register_widget_event();
+}
+
+API int aul_widget_unset_event_cb(void)
+{
+       __event.callback = NULL;
+       __event.user_data = NULL;
+
+       return __unregister_widget_event();
+}
+
+API int aul_widget_send_event(const char *event_name, bundle *event_data)
+{
+       bundle *b;
+       bundle_raw *raw = NULL;
+       int len = 0;
+       int ret;
+
+       if (!event_name || !event_data) {
+               _E("Invalid parameter");
+               return AUL_R_EINVAL;
+       }
+
+       b = bundle_create();
+       if (!b) {
+               _E("Out of memory");
+               return AUL_R_ENOMEM;
+       }
+
+       bundle_add(b, AUL_K_EVENT_NAME, event_name);
+
+       bundle_encode(event_data, &raw, &len);
+       if (!raw) {
+               _E("Failed to encode event data");
+               bundle_free(b);
+               return AUL_R_ERROR;
+       }
+
+       bundle_add(b, AUL_K_EVENT_DATA, (const char *)raw);
+       free(raw);
+
+       ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(), WIDGET_EVENT, b,
+                       AUL_SOCK_NOREPLY);
+       bundle_free(b);
+       if (ret != 0) {
+               _E("Failed to send widget event. error(%d)", ret);
+               return aul_error_convert(ret);
+       }
+
+       return AUL_R_OK;
+}