Modify screen connector 66/114866/27
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 15 Feb 2017 10:09:21 +0000 (19:09 +0900)
committerHwanKyu Jhun <h.jhun@samsung.com>
Wed, 8 Mar 2017 05:39:27 +0000 (21:39 -0800)
If the application registers the private screen viewer,
it can only get own sub-app screen information.
This concept is for the widget viewer.
And, the screen viewer can get the instance-id in the callback.

Change-Id: I33462b110f5f0207f1ce65cc0b833f1a66a22aa6
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
include/aul.h
include/aul_cmd.h
include/aul_screen_connector.h
src/aul_screen_connector.c
src/launch.c

index 3c77781..d318f4e 100644 (file)
@@ -261,6 +261,12 @@ typedef enum aul_widget_instance_event {
 #define AUL_K_ORG_CALLER_UID   "__AUL_ORG_CALLER_UID__"
 /** AUL internal private key */
 #define AUL_K_CHECKSUM         "__AUL_CHECKSUM__"
+/** AUL internal private key */
+#define AUL_K_PRIVATE          "__AUL_PRIVATE__"
+/** AUL internal private key */
+#define AUL_K_SCREEN_TYPE      "__AUL_SCREEN_TYPE__"
+/** AUL internal private key */
+#define AUL_K_VIEWER_REF       "__AUL_VIEWER_REF__"
 
 /**
  * @brief      This is callback function for aul_launch_init
index 93c6398..e41190f 100644 (file)
@@ -109,7 +109,7 @@ enum app_cmd {
        ADD_APP_SCREEN = 80,
 
        REMOVE_APP_SCREEN = 81,
-       UPDATE_APP_SCREEN = 82,
+       APP_UPDATE_REQUESTED = 82,
        ADD_SCREEN_VIEWER = 83,
        REMOVE_SCREEN_VIEWER = 84,
        LAUNCHPAD_LAUNCH_SIGNAL = 85,
index 9eb5839..247c50f 100644 (file)
 #ifndef __AUL_SCREEN_CONNECTOR_H__
 #define __AUL_SCREEN_CONNECTOR_H__
 
+#include <stdbool.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-typedef void (*aul_app_screen_added)(const char *appid, const int pid,
-               const unsigned int surface_id, void *data);
-typedef void (*aul_app_screen_removed)(const char *appid, const int pid,
-               void *data);
+typedef enum {
+       AUL_SCREEN_TYPE_WIDGET = 0x01,
+       AUL_SCREEN_TYPE_WATCH = 0x02,
+       AUL_SCREEN_TYPE_UI = 0x04,
+       AUL_SCREEN_TYPE_ALL = AUL_SCREEN_TYPE_WIDGET | AUL_SCREEN_TYPE_WATCH | AUL_SCREEN_TYPE_UI,
+} aul_screen_type_e;
+
+typedef enum {
+       AUL_SCREEN_CONNECTOR_EVENT_TYPE_ADD,
+       AUL_SCREEN_CONNECTOR_EVENT_TYPE_REMOVE,
+       AUL_SCREEN_CONNECTOR_EVENT_TYPE_UPDATE,
+} aul_screen_connector_event_type_e;
+
+typedef void (*aul_screen_viewer_cb)(const char *appid,
+               const char *instance_id, const int pid,
+               const unsigned int surface_id,
+               aul_screen_connector_event_type_e event_type, void *data);
 
-typedef struct aul_screen_viewer_handler_s {
-       aul_app_screen_added app_added;
-       aul_app_screen_removed app_removed;
-} aul_screen_viewer_handler;
+typedef struct aul_screen_viewer_s *aul_screen_viewer_h;
 
 /*
  * This API is only for Appfw internally.
  */
-int aul_screen_connector_add_app_screen(unsigned int surf);
+int aul_screen_connector_add_app_screen(const char *instance_id,
+               unsigned int surf);
 
 /*
  * This API is only for Appfw internally.
  */
-int aul_screen_connector_remove_app_screen(void);
+int aul_screen_connector_remove_app_screen(const char *instance_id);
 
 /*
  * This API is only for Appfw internally.
  */
-int aul_screen_connector_update_app_screen(const char *appid);
+int aul_screen_connector_send_update_request(const char *appid,
+               const char *instance_id);
 
 /*
  * This API is only for Appfw internally.
  */
-int aul_screen_connector_add_screen_viewer(aul_screen_viewer_handler *cbs,
-               void *data);
+int aul_screen_connector_add_screen_viewer(aul_screen_viewer_cb callback,
+               aul_screen_type_e type, bool priv,
+               void *data, aul_screen_viewer_h *handle);
 
 /*
  * This API is only for Appfw internally.
  */
-int aul_screen_connector_remove_screen_viewer(void);
+int aul_screen_connector_remove_screen_viewer(aul_screen_viewer_h handle);
 
 #ifdef __cplusplus
 }
index f844903..3f03565 100644 (file)
@@ -17,6 +17,7 @@
 #define _GNU_SOURCE
 #include <stdio.h>
 #include <stdbool.h>
+#include <stdlib.h>
 #include <sys/types.h>
 #include <unistd.h>
 #include <glib.h>
 #include "aul_app_com.h"
 #include "aul_screen_connector.h"
 
-static aul_app_com_connection_h added_conn;
-static aul_app_com_connection_h removed_conn;
-static bool screen_viewer_initialized;
-static aul_screen_viewer_handler *screen_viewer_handler;
-static void *screen_viewer_data;
+struct aul_screen_viewer_s {
+       aul_app_com_connection_h conn;
+       aul_screen_viewer_cb callback;
+       aul_screen_type_e type;
+       bool priv;
+       unsigned int ref;
+       void *user_data;
+};
 
-static int __add_screen_viewer(void)
+static unsigned int ref;
+
+static unsigned int __get_ref(void)
+{
+       return ++ref;
+}
+
+static int __add_screen_viewer(int type, bool priv, unsigned int ref)
 {
        int ret;
        bundle *b;
+       char buf[MAX_PID_STR_BUFSZ];
 
        b = bundle_create();
        if (b == NULL) {
@@ -47,8 +59,33 @@ static int __add_screen_viewer(void)
                return -1;
        }
 
+       snprintf(buf, sizeof(buf), "%d", type);
+       ret = bundle_add(b, AUL_K_SCREEN_TYPE, buf);
+       if (ret != BUNDLE_ERROR_NONE) {
+               _E("Failed to add screen type(%d)", type);
+               bundle_free(b);
+               return -1;
+       }
+
+       snprintf(buf, sizeof(buf), "%u", ref);
+       ret = bundle_add(b, AUL_K_VIEWER_REF, buf);
+       if (ret != BUNDLE_ERROR_NONE) {
+               _E("Failed to add viewer reference(%u)", ref);
+               bundle_free(b);
+               return -1;
+       }
+
+       if (priv) {
+               ret = bundle_add(b, AUL_K_PRIVATE, "true");
+               if (ret != BUNDLE_ERROR_NONE) {
+                       _E("Failed to add bundle data");
+                       bundle_free(b);
+                       return -1;
+               }
+       }
+
        ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
-                       ADD_SCREEN_VIEWER, b, AUL_SOCK_NONE);
+                       ADD_SCREEN_VIEWER, b, AUL_SOCK_NOREPLY);
        bundle_free(b);
        if (ret < 0)
                return -1;
@@ -56,10 +93,11 @@ static int __add_screen_viewer(void)
        return 0;
 }
 
-static int __remove_screen_viewer(void)
+static int __remove_screen_viewer(int type, bool priv, unsigned int ref)
 {
        int ret;
        bundle *b;
+       char buf[MAX_PID_STR_BUFSZ];
 
        b = bundle_create();
        if (b == NULL) {
@@ -67,6 +105,31 @@ static int __remove_screen_viewer(void)
                return -1;
        }
 
+       snprintf(buf, sizeof(buf), "%d", type);
+       ret = bundle_add(b, AUL_K_SCREEN_TYPE, buf);
+       if (ret != BUNDLE_ERROR_NONE) {
+               _E("Failed to add view mode");
+               bundle_free(b);
+               return -1;
+       }
+
+       snprintf(buf, sizeof(buf), "%u", ref);
+       ret = bundle_add(b, AUL_K_VIEWER_REF, buf);
+       if (ret != BUNDLE_ERROR_NONE) {
+               _E("Failed to add viewer reference(%u)", ref);
+               bundle_free(b);
+               return -1;
+       }
+
+       if (priv) {
+               ret = bundle_add(b, AUL_K_PRIVATE, "true");
+               if (ret != BUNDLE_ERROR_NONE) {
+                       _E("Failed to add bundle data");
+                       bundle_free(b);
+                       return -1;
+               }
+       }
+
        ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
                        REMOVE_SCREEN_VIEWER, b, AUL_SOCK_NOREPLY);
        bundle_free(b);
@@ -76,13 +139,32 @@ static int __remove_screen_viewer(void)
        return 0;
 }
 
-static int __app_screen_added(const char *endpoint, aul_app_com_result_e res,
+static int __app_screen_event_cb(const char *endpoint, aul_app_com_result_e res,
                bundle *envelope, void *user_data)
 {
+       aul_screen_viewer_h handle = (aul_screen_viewer_h)user_data;
        char *appid = NULL;
+       char *instance_id = NULL;
        unsigned int *surf = NULL;
        int *pid = NULL;
        size_t size;
+       char *event = NULL;
+       aul_screen_connector_event_type_e event_type;
+
+       bundle_get_str(envelope, "__AUL_SC_EVENT__", &event);
+       if (event == NULL) {
+               _E("Failed to get screen connector event");
+               return -1;
+       } else if (strcmp(event, "add_screen") == 0) {
+               event_type = AUL_SCREEN_CONNECTOR_EVENT_TYPE_ADD;
+       } else if (strcmp(event, "remove_screen") == 0) {
+               event_type = AUL_SCREEN_CONNECTOR_EVENT_TYPE_REMOVE;
+       } else if (strcmp(event, "update_screen") == 0) {
+               event_type = AUL_SCREEN_CONNECTOR_EVENT_TYPE_UPDATE;
+       } else {
+               _E("Unknown event type(%s)", event);
+               return -1;
+       }
 
        bundle_get_str(envelope, "__AUL_SC_APPID__", &appid);
        if (appid == NULL) {
@@ -102,143 +184,110 @@ static int __app_screen_added(const char *endpoint, aul_app_com_result_e res,
                _E("Failed to get pid");
                return -1;
        }
+       bundle_get_str(envelope, "__AUL_SC_INSTANCE_ID__", &instance_id);
 
-       if (screen_viewer_handler->app_added) {
-               screen_viewer_handler->app_added(appid, *pid, *surf,
-                               screen_viewer_data);
+       if (handle->callback) {
+               handle->callback(appid, instance_id, *pid, *surf,
+                               event_type, handle->user_data);
        }
-       _D("appid(%s), pid(%d), surface(%d)", appid, *pid, *surf);
+       _D("appid(%s), instance_id(%s), pid(%d), surface(%d), event_type(%d)",
+                       appid, instance_id, *pid, *surf, event_type);
 
        return 0;
 }
 
-static int __app_screen_removed(const char *endpoint, aul_app_com_result_e res,
-               bundle *envelope, void *user_data)
-{
-       char *appid = NULL;
-       int *pid = NULL;
-       size_t size;
-
-       bundle_get_str(envelope, "__AUL_SC_APPID__", &appid);
-       if (appid == NULL) {
-               _E("Failed to get appid");
-               return -1;
-       }
-
-       bundle_get_byte(envelope, "__AUL_SC_PID__", (void **)&pid, &size);
-       if (pid == NULL) {
-               _E("Failed to get pid");
-               return -1;
-       }
-
-       if (screen_viewer_handler->app_removed) {
-               screen_viewer_handler->app_removed(appid, *pid,
-                               screen_viewer_data);
-       }
-       _D("appid(%s), pid(%d)", appid, *pid);
-
-       return 0;
-}
-
-static int __screen_viewer_fini(void)
+static int __screen_viewer_fini(aul_screen_viewer_h screen_viewer)
 {
        int ret;
 
-       if (!screen_viewer_initialized)
-               return 0;
-
-       if (removed_conn) {
-               aul_app_com_leave(removed_conn);
-               removed_conn = NULL;
+       if (screen_viewer->conn) {
+               aul_app_com_leave(screen_viewer->conn);
+               screen_viewer->conn = NULL;
        }
 
-       if (added_conn) {
-               aul_app_com_leave(added_conn);
-               added_conn = NULL;
-       }
-
-       ret = __remove_screen_viewer();
+       ret = __remove_screen_viewer(screen_viewer->type, screen_viewer->priv,
+                       screen_viewer->ref);
        if (ret < 0) {
                _E("Failed to remove screen watcher");
                return -1;
        }
 
-       screen_viewer_initialized = false;
-
        return 0;
 }
 
-static int __screen_viewer_init(void)
+static int __screen_viewer_init(aul_screen_viewer_h screen_viewer)
 {
        int ret;
        char endpoint[128];
        pid_t pid = getpid();
 
-       if (screen_viewer_initialized)
-               return 0;
-
-       snprintf(endpoint, sizeof(endpoint), "app_screen_added:%d", pid);
-       aul_app_com_create(endpoint, NULL, __app_screen_added,
-                       NULL, &added_conn);
-       if (added_conn == NULL) {
-               _E("Failed to create app com - watch_app_added");
+       snprintf(endpoint, sizeof(endpoint), "app_screen_event:%u:%d",
+                       screen_viewer->ref, pid);
+       aul_app_com_create(endpoint, NULL, __app_screen_event_cb,
+                       screen_viewer, &screen_viewer->conn);
+       if (screen_viewer->conn == NULL) {
+               _E("Failed to create app com");
                return -1;
        }
 
-       snprintf(endpoint, sizeof(endpoint), "app_screen_removed:%d", pid);
-       aul_app_com_create(endpoint, NULL, __app_screen_removed,
-                       NULL, &removed_conn);
-       if (removed_conn == NULL) {
-               _E("Failed to create app com - watch_app_removed");
-               __screen_viewer_fini();
-               return -1;
-       }
-
-       ret = __add_screen_viewer();
+       ret = __add_screen_viewer(screen_viewer->type, screen_viewer->priv,
+                       screen_viewer->ref);
        if (ret < 0) {
                _E("Failed to add screen watcher");
-               __screen_viewer_fini();
                return -1;
        }
 
-       screen_viewer_initialized = true;
-
        return 0;
 }
 
-API int aul_screen_connector_add_screen_viewer(aul_screen_viewer_handler *cbs,
-               void *data)
+API int aul_screen_connector_add_screen_viewer(aul_screen_viewer_cb callback,
+               aul_screen_type_e type, bool priv,
+               void *data, aul_screen_viewer_h *handle)
 {
-       if (cbs == NULL) {
+       struct aul_screen_viewer_s *screen_viewer;
+
+       if (handle == NULL || callback == NULL ||
+                       !(type & AUL_SCREEN_TYPE_ALL)) {
                _E("Invalid parameter");
                return AUL_R_EINVAL;
        }
 
-       if (__screen_viewer_init() < 0) {
-               _E("Failed to initialize screen viewer");
-               return AUL_R_ERROR;
+       screen_viewer = (struct aul_screen_viewer_s *)calloc(1,
+                       sizeof(struct aul_screen_viewer_s));
+       if (screen_viewer == NULL) {
+               _E("Out of memory");
+               return AUL_R_EINVAL;
        }
 
-       screen_viewer_handler = cbs;
-       screen_viewer_data = data;
+       screen_viewer->callback = callback;
+       screen_viewer->type = type;
+       screen_viewer->priv = priv;
+       screen_viewer->ref = __get_ref();
+       screen_viewer->user_data = data;
+
+       if (__screen_viewer_init(screen_viewer) < 0) {
+               __screen_viewer_fini(screen_viewer);
+               free(screen_viewer);
+               return AUL_R_ERROR;
+       }
+       *handle = screen_viewer;
 
        return AUL_R_OK;
 }
 
-API int aul_screen_connector_remove_screen_viewer(void)
+API int aul_screen_connector_remove_screen_viewer(aul_screen_viewer_h handle)
 {
-       if (__screen_viewer_fini() < 0) {
-               _E("Failed to finalize screen viewer");
-               return -1;
-       }
+       if (handle == NULL)
+               return AUL_R_EINVAL;
 
-       screen_viewer_handler = NULL;
-       screen_viewer_data = NULL;
+       __screen_viewer_fini(handle);
+       free(handle);
 
        return AUL_R_OK;
 }
 
-API int aul_screen_connector_add_app_screen(unsigned int surf)
+API int aul_screen_connector_add_app_screen(const char *instance_id,
+               unsigned int surf)
 {
        int ret;
        bundle *b;
@@ -258,8 +307,17 @@ API int aul_screen_connector_add_app_screen(unsigned int surf)
                return AUL_R_ERROR;
        }
 
+       if (instance_id) {
+               ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
+               if (ret != BUNDLE_ERROR_NONE) {
+                       _E("Failed to add instance id");
+                       bundle_free(b);
+                       return AUL_R_ERROR;
+               }
+       }
+
        ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
-                       ADD_APP_SCREEN, b, AUL_SOCK_NONE);
+                       ADD_APP_SCREEN, b, AUL_SOCK_NOREPLY);
        bundle_free(b);
        if (ret < 0) {
                _E("Failed to add app screen");
@@ -269,7 +327,7 @@ API int aul_screen_connector_add_app_screen(unsigned int surf)
        return AUL_R_OK;
 }
 
-API int aul_screen_connector_remove_app_screen(void)
+API int aul_screen_connector_remove_app_screen(const char *instance_id)
 {
        int ret;
        bundle *b;
@@ -280,8 +338,17 @@ API int aul_screen_connector_remove_app_screen(void)
                return AUL_R_ERROR;
        }
 
+       if (instance_id) {
+               ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
+               if (ret != BUNDLE_ERROR_NONE) {
+                       _E("Failed to add instance id");
+                       bundle_free(b);
+                       return AUL_R_ERROR;
+               }
+       }
+
        ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
-                       REMOVE_APP_SCREEN, b, AUL_SOCK_NONE);
+                       REMOVE_APP_SCREEN, b, AUL_SOCK_NOREPLY);
        bundle_free(b);
        if (ret < 0) {
                _E("Failed to remove app screen");
@@ -291,7 +358,8 @@ API int aul_screen_connector_remove_app_screen(void)
        return AUL_R_OK;
 }
 
-API int aul_screen_connector_update_app_screen(const char *appid)
+API int aul_screen_connector_send_update_request(const char *appid,
+               const char *instance_id)
 {
        int ret;
        bundle *b;
@@ -314,8 +382,17 @@ API int aul_screen_connector_update_app_screen(const char *appid)
                return AUL_R_ERROR;
        }
 
+       if (instance_id) {
+               ret = bundle_add(b, AUL_K_INSTANCE_ID, instance_id);
+               if (ret != BUNDLE_ERROR_NONE) {
+                       _E("Failed to add instance id");
+                       bundle_free(b);
+                       return AUL_R_ERROR;
+               }
+       }
+
        ret = aul_sock_send_bundle(AUL_UTIL_PID, getuid(),
-                       UPDATE_APP_SCREEN, b, AUL_SOCK_NONE);
+                       APP_UPDATE_REQUESTED, b, AUL_SOCK_NOREPLY);
        bundle_free(b);
        if (ret < 0) {
                _E("Failed to update app screen");
index 4f092c9..2475543 100755 (executable)
@@ -504,7 +504,7 @@ int aul_sock_handler(int fd)
        case WIDGET_GET_CONTENT:
                widget_get_content(clifd, kbundle);
                break;
-       case UPDATE_APP_SCREEN:
+       case APP_UPDATE_REQUESTED:
                app_update_requested();
                break;
        default: