Send create aborted event to the widget viewer
[platform/core/appfw/appcore-widget.git] / src / widget_app.c
index 9d717ae..e25fdd1 100755 (executable)
@@ -16,6 +16,7 @@
 
 
 #include <stdlib.h>
+#include <stdbool.h>
 
 #include <bundle.h>
 #include <bundle_internal.h>
@@ -36,6 +37,7 @@
 #include <system_info.h>
 #include <vconf.h>
 #include <vconf-internal-keys.h>
+#include <screen_connector_provider.h>
 
 #include "widget_app.h"
 #include "widget-log.h"
@@ -49,6 +51,9 @@
 #define STR_MAX_BUF 128
 #define LOG_TAG "CAPI_WIDGET_APPLICATION"
 #define K_REASON    "__WC_K_REASON__"
+#define APP_TYPE_WIDGET "widgetapp"
+#define STATUS_FOREGROUND "fg"
+#define STATUS_BACKGROUND "bg"
 
 typedef enum _widget_obj_state_e {
        WC_READY = 0,
@@ -62,14 +67,6 @@ enum {
        UPDATE_ALL = 1,
 };
 
-struct _widget_class {
-       void *user_data;
-       widget_instance_lifecycle_callback_s ops;
-       char *classid;
-       struct _widget_class *next;
-       struct _widget_class *prev;
-};
-
 struct app_event_handler {
        app_event_type_e type;
        app_event_cb cb;
@@ -81,19 +78,7 @@ struct app_event_info {
        void *value;
 };
 
-struct _widget_context {
-       char *id;
-       struct _widget_class *provider;
-       int state;
-       void *tag;
-       Evas_Object *win;
-       int win_id;
-       char *content;
-       widget_instance_lifecycle_callback_s ops;
-};
-
 typedef struct _widget_class widget_class_s;
-typedef struct _widget_context widget_context_s;
 
 #define WIDGET_APP_EVENT_MAX 5
 static GList *handler_list[WIDGET_APP_EVENT_MAX] = {NULL, };
@@ -103,9 +88,9 @@ static widget_app_lifecycle_callback_s *app_ops;
 static void *app_user_data;
 static char *appid;
 static widget_class_h class_provider;
-static GList *contexts;
-static char *viewer_endpoint;
 static int exit_called;
+static char *package_id;
+static bool fg_signal;
 
 static void _widget_core_set_appcore_event_cb(void);
 static void _widget_core_unset_appcore_event_cb(void);
@@ -157,6 +142,7 @@ static gint __comp_by_id(gconstpointer a, gconstpointer b)
 static widget_context_s *__find_context_by_id(const char *id)
 {
        GList *ret;
+       GList *contexts = _widget_app_get_contexts();
 
        if (id == NULL)
                return NULL;
@@ -168,6 +154,28 @@ static widget_context_s *__find_context_by_id(const char *id)
        return ret->data;
 }
 
+static gint __comp_by_state(gconstpointer a, gconstpointer b)
+{
+       widget_context_s *wc = (widget_context_s *)a;
+
+       if (wc->state == (widget_obj_state_e)GPOINTER_TO_INT(b))
+               return 0;
+
+       return -1;
+}
+
+static widget_context_s *__find_context_by_state(widget_obj_state_e state)
+{
+       GList *ret;
+       GList *contexts = _widget_app_get_contexts();
+
+       ret = g_list_find_custom(contexts, GINT_TO_POINTER((int)state), __comp_by_state);
+       if (ret == NULL)
+               return NULL;
+
+       return ret->data;
+}
+
 static gint __comp_by_win(gconstpointer a, gconstpointer b)
 {
        int win = GPOINTER_TO_INT(b);
@@ -178,6 +186,7 @@ static gint __comp_by_win(gconstpointer a, gconstpointer b)
 
 static widget_context_s *__find_context_by_win(int win)
 {
+       GList *contexts = _widget_app_get_contexts();
        GList *ret = g_list_find_custom(contexts, GINT_TO_POINTER(win), __comp_by_win);
 
        if (ret == NULL)
@@ -190,6 +199,7 @@ static int __send_lifecycle_event(const char *class_id, const char *instance_id,
        int status)
 {
        bundle *b = bundle_create();
+       char pkgid[256] = {0, };
        int ret;
 
        if (b == NULL) {
@@ -197,9 +207,17 @@ static int __send_lifecycle_event(const char *class_id, const char *instance_id,
                return -1; /* LCOV_EXCL_LINE */
        }
 
-       bundle_add_str(b, WIDGET_K_ID, class_id);
-       bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
-       bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
+       if (package_id == NULL) {
+               ret = aul_app_get_pkgid_bypid(getpid(), pkgid, sizeof(pkgid));
+               if (ret == 0)
+                       package_id = strdup(pkgid);
+       }
+
+       bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
+       bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
+       bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
+       if (package_id)
+               bundle_add_str(b, AUL_K_PKGID, package_id);
 
        _D("send lifecycle %s(%d)", instance_id, status);
        ret = aul_app_com_send("widget.status", b);
@@ -218,6 +236,7 @@ static int __send_update_status(const char *class_id, const char *instance_id,
        int lifecycle = -1;
        bundle_raw *raw = NULL;
        int len;
+       char *viewer_endpoint = _widget_app_get_viewer_endpoint();
 
        b = bundle_create();
        if (!b) {
@@ -225,9 +244,9 @@ static int __send_update_status(const char *class_id, const char *instance_id,
                return -1; /* LCOV_EXCL_LINE */
        }
 
-       bundle_add_str(b, WIDGET_K_ID, class_id);
-       bundle_add_str(b, WIDGET_K_INSTANCE, instance_id);
-       bundle_add_byte(b, WIDGET_K_STATUS, &status, sizeof(int));
+       bundle_add_str(b, AUL_K_WIDGET_ID, class_id);
+       bundle_add_str(b, AUL_K_WIDGET_INSTANCE_ID, instance_id);
+       bundle_add_byte(b, AUL_K_WIDGET_STATUS, &status, sizeof(int));
 
        if (extra) {
                bundle_encode(extra, &raw, &len);
@@ -291,6 +310,15 @@ static int __instance_resume(widget_class_h handle, const char *id, int send_upd
        if (send_update) {
                ret = __send_update_status(handle->classid, wc->id,
                        WIDGET_INSTANCE_EVENT_RESUME, NULL);
+               if (!fg_signal) {
+                       _D("Send fg signal to resourceD");
+                       aul_send_app_status_change_signal(getpid(),
+                                       appid,
+                                       package_id,
+                                       STATUS_FOREGROUND,
+                                       APP_TYPE_WIDGET);
+                       fg_signal = true;
+               }
        } else {
                ret = 0;
        }
@@ -326,6 +354,16 @@ static int __instance_pause(widget_class_h handle, const char *id, int send_upda
        if (send_update) {
                ret = __send_update_status(handle->classid, wc->id,
                        WIDGET_INSTANCE_EVENT_PAUSE, NULL);
+               wc = __find_context_by_state(WC_RUNNING);
+               if (!wc && fg_signal) {
+                       _D("Send bg signal to resourceD");
+                       aul_send_app_status_change_signal(getpid(),
+                                       appid,
+                                       package_id,
+                                       STATUS_BACKGROUND,
+                                       APP_TYPE_WIDGET);
+                       fg_signal = false;
+               }
        } else {
                ret = 0;
        }
@@ -359,7 +397,7 @@ static int __instance_update_all(widget_class_h handle, int force, const char *c
        widget_context_s *wc;
        int ret = 0;
        bundle *b = NULL;
-       GList *context = contexts;
+       GList *context = _widget_app_get_contexts();
 
        if (content)
                b = bundle_decode((const bundle_raw *)content, strlen(content));
@@ -417,11 +455,13 @@ static int __instance_create(widget_class_h handle, const char *id, const char *
        bundle *content_info = NULL;
 
        wc = (widget_context_s *)calloc(1, sizeof(widget_context_s));
-       if (!wc)
+       if (!wc) {
+               _E("Out of memory");
                return WIDGET_ERROR_OUT_OF_MEMORY;
+       }
 
        wc->state = WC_READY;
-       wc->id = g_strdup(id);
+       wc->id = strdup(id);
        wc->provider = handle;
        wc->win = NULL;
        wc->win_id = -1;
@@ -431,11 +471,22 @@ static int __instance_create(widget_class_h handle, const char *id, const char *
                content_info = bundle_decode((const bundle_raw *)content, strlen(content));
        }
 
-       contexts = g_list_append(contexts, wc);
+       _widget_app_add_context(wc);
 
        ret = handle->ops.create(wc, content_info, w, h, handle->user_data);
        if (ret < 0) {
-               /* TODO send abort */
+               _W("Create callback resturns error(%d)", ret);
+               ret = __send_update_status(handle->classid, wc->id,
+                               WIDGET_INSTANCE_EVENT_CREATE_ABORTED, NULL);
+               _widget_app_remove_context(wc);
+               if (wc->id)
+                       free(wc->id);
+               if (wc->content)
+                       free(wc->content);
+               free(wc);
+
+               if (_widget_app_get_contexts() == NULL && !exit_called)
+                       widget_app_exit();
        } else {
                ret = __send_update_status(handle->classid, wc->id,
                        WIDGET_INSTANCE_EVENT_CREATE, NULL);
@@ -461,8 +512,9 @@ static int __instance_destroy(widget_class_h handle, const char *id,
        bundle *content_info;
 
        if (!wc) {
-               _E("could not find widget obj: %s", id); /* LCOV_EXCL_LINE */
-               return WIDGET_ERROR_INVALID_PARAMETER; /* LCOV_EXCL_LINE */
+               _E("could not find widget obj: %s, clear amd info", id);        /* LCOV_EXCL_LINE */
+               aul_widget_instance_del(handle->classid, id);                   /* LCOV_EXCL_LINE */
+               return WIDGET_ERROR_NONE;                                       /* LCOV_EXCL_LINE */
        }
 
        wc->state = WC_TERMINATED;
@@ -476,19 +528,18 @@ static int __instance_destroy(widget_class_h handle, const char *id,
 
        if (reason == WIDGET_APP_DESTROY_TYPE_PERMANENT) {
                event = WIDGET_INSTANCE_EVENT_DESTROY;
+               aul_widget_instance_del(handle->classid, id);
        } else {
                ret = __send_update_status(handle->classid, id,
                                WIDGET_INSTANCE_EVENT_EXTRA_UPDATED, content_info);
        }
 
-       aul_widget_instance_del(handle->classid, id);
-
        if (content_info)
                bundle_free(content_info);
 
        ret = __send_update_status(handle->classid, id, event, NULL);
 
-       contexts = g_list_remove(contexts, wc);
+       _widget_app_remove_context(wc);
 
        if (wc->id)
                free(wc->id);
@@ -498,7 +549,7 @@ static int __instance_destroy(widget_class_h handle, const char *id,
 
        free(wc);
 
-       if (contexts == NULL && !exit_called) /* all instance destroyed */
+       if (_widget_app_get_contexts() == NULL && !exit_called) /* all instance destroyed */
                widget_app_exit();
 
        return ret;
@@ -559,7 +610,7 @@ static void __control(bundle *b)
        if (class_id == NULL)
                class_id = appid;
 
-       bundle_get_str(b, WIDGET_K_INSTANCE, &id);
+       bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &id);
        bundle_get_str(b, WIDGET_K_OPERATION, &operation);
 
        handle = __find_class_handler(class_id, class_provider);
@@ -617,6 +668,7 @@ error:
 
 static void __pause_all(int send_update)
 {
+       GList *contexts = _widget_app_get_contexts();
        GList *iter = g_list_first(contexts);
 
        while (iter != NULL) {
@@ -631,6 +683,7 @@ static void __pause_all(int send_update)
                        __instance_pause(cxt->provider, cxt->id, send_update);
                        break;
                }
+               LOGD("pause %s", cxt->id);
                iter = g_list_next(iter);
        }
 }
@@ -638,6 +691,7 @@ static void __pause_all(int send_update)
 /* LCOV_EXCL_START */
 static void __resume_all(int send_update)
 {
+       GList *contexts = _widget_app_get_contexts();
        GList *iter = g_list_first(contexts);
 
        while (iter != NULL) {
@@ -658,19 +712,19 @@ static void __resume_all(int send_update)
 
 static void __destroy_all(int reason, int send_update)
 {
+       GList *contexts = _widget_app_get_contexts();
        GList *iter = g_list_first(contexts);
 
        __pause_all(send_update);
-
        while (iter != NULL) {
                widget_context_s *cxt = (widget_context_s *)iter->data;
-
+               iter = g_list_next(iter);
                switch (cxt->state) {
                case WC_PAUSED:
+                       LOGD("destroy %d : %s", cxt->state, cxt->id);
                        __instance_destroy(cxt->provider, cxt->id, reason, send_update);
                        break;
                }
-               iter = g_list_next(iter);
        }
 }
 
@@ -765,6 +819,30 @@ static void __add_climsg()
        ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_CONFIGURE, __configure_cb, NULL);
 }
 
+static void __get_content(bundle *b)
+{
+       char *instance_id = NULL;
+       widget_context_s *cxt = NULL;
+
+       bundle_get_str(b, AUL_K_WIDGET_INSTANCE_ID, &instance_id);
+       if (!instance_id)
+               return;
+
+       cxt = __find_context_by_id(instance_id);
+       if (!cxt) {
+               _E("can not find instance id:%s", instance_id);
+               return;
+       }
+
+       if (cxt->content) {
+               bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, cxt->content);
+               _D("content info of %s found", cxt->id);
+       } else {
+               bundle_add_str(b, AUL_K_WIDGET_CONTENT_INFO, "");
+               _D("empty content info added");
+       }
+}
+
 static int __aul_handler(aul_type type, bundle *b, void *data)
 {
        char *caller = NULL;
@@ -791,6 +869,9 @@ static int __aul_handler(aul_type type, bundle *b, void *data)
        case AUL_TERMINATE:
                widget_app_exit();
                break;
+       case AUL_WIDGET_CONTENT:
+               __get_content(b);
+               break;
        default:
                break;
        }
@@ -846,9 +927,8 @@ static int __before_loop(int argc, char **argv)
 {
        int r;
        bundle *kb = NULL;
-       char *wayland_display = NULL;
-       char *xdg_runtime_dir = NULL;
        char *name;
+       char *viewer_endpoint = NULL;
 
 #if !(GLIB_CHECK_VERSION(2, 36, 0))
        g_type_init();
@@ -856,32 +936,21 @@ static int __before_loop(int argc, char **argv)
 
        kb = bundle_import_from_argv(argc, argv);
        if (kb) {
-               bundle_get_str(kb, AUL_K_WAYLAND_WORKING_DIR, &xdg_runtime_dir);
-               bundle_get_str(kb, AUL_K_WAYLAND_DISPLAY, &wayland_display);
                bundle_get_str(kb, WIDGET_K_ENDPOINT, &viewer_endpoint);
                if (viewer_endpoint) {
                        _E("viewer endpoint :%s", viewer_endpoint);
-                       viewer_endpoint = strdup(viewer_endpoint);
+                       _widget_app_set_viewer_endpoint(viewer_endpoint);
                } else {
                        _E("endpoint is missing");
                }
 
-               if (xdg_runtime_dir)
-                       setenv("XDG_RUNTIME_DIR", xdg_runtime_dir, 1);
-
-               _D("xdg_runtime_dir:%s", xdg_runtime_dir);
-
-               if (wayland_display)
-                       setenv("WAYLAND_DISPLAY", wayland_display, 1);
-
-               _D("wayland_display:%s", wayland_display);
-
                bundle_free(kb);
                kb = NULL;
        } else {
                _E("failed to get launch argv"); /* LCOV_EXCL_LINE */
        }
 
+       screen_connector_provider_init();
        elm_init(argc, argv);
 
        r = aul_launch_init(__aul_handler, NULL);
@@ -952,11 +1021,22 @@ static void __after_loop()
        if (app_ops->terminate)
                app_ops->terminate(app_user_data);
 
-       if (viewer_endpoint)
-               free(viewer_endpoint);
+       screen_connector_provider_fini();
 
+       _widget_app_free_viewer_endpoint();
        _widget_core_unset_appcore_event_cb();
        __free_handler_list();
+
+       if (package_id) {
+               free(package_id);
+               package_id = NULL;
+       }
+
+       if (appid) {
+               free(appid);
+               appid = NULL;
+       }
+
        elm_shutdown();
 }
 
@@ -1129,12 +1209,16 @@ EXPORT_API int widget_app_main(int argc, char **argv,
                                __FUNCTION__,
                                "widget_app_create_cb() callback must be "
                                "registered");
-
        app_ops = callback;
        app_user_data = user_data;
        r = __before_loop(argc, argv);
-       if (r < 0)
+       if (r < 0) {
+               if (appid) {
+                       free(appid);
+                       appid = NULL;
+               }
                return r;
+       }
 
        ecore_main_loop_begin();
        aul_status_update(STATUS_DYING);
@@ -1202,6 +1286,7 @@ EXPORT_API int widget_app_terminate_context(widget_context_h context)
 
 EXPORT_API int widget_app_foreach_context(widget_context_cb cb, void *data)
 {
+       GList *contexts = _widget_app_get_contexts();
        GList *list;
        widget_context_s *wc;
 
@@ -1329,12 +1414,23 @@ EXPORT_API const char *widget_app_get_id(widget_context_h context)
        return context->id;
 }
 
+static void _win_del_cb(void *data, Evas *e, Evas_Object *obj, void *event_info)
+{
+       /* Remove data used in accessibility */
+       char *plug_id;
+       plug_id = evas_object_data_del(obj, "___PLUGID");
+       free(plug_id);
+}
+
 EXPORT_API int widget_app_get_elm_win(widget_context_h context,
                                        Evas_Object **win)
 {
        widget_context_s *cxt = (widget_context_s *)context;
        Evas_Object *ret_win;
        Ecore_Wl_Window *wl_win;
+       struct wl_surface *surface;
+       char buffer[256];
+       int rots[3] = {0};
 
        if (!_is_widget_feature_enabled()) {
                _E("not supported"); /* LCOV_EXCL_LINE */
@@ -1348,25 +1444,47 @@ EXPORT_API int widget_app_get_elm_win(widget_context_h context,
        ret_win = elm_win_add(NULL, cxt->id, ELM_WIN_BASIC);
        if (ret_win == NULL) {
                _E("failed to create window"); /* LCOV_EXCL_LINE */
-               return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
+               goto fault; /* LCOV_EXCL_LINE */
        }
 
+       elm_win_wm_rotation_preferred_rotation_set(ret_win, -1);
+       elm_win_wm_rotation_available_rotations_set(ret_win, rots, 1);
+
        wl_win = elm_win_wl_window_get(ret_win);
        if (wl_win == NULL) {
                _E("failed to get wayland window"); /* LCOV_EXCL_LINE */
-               evas_object_del(ret_win); /* LCOV_EXCL_LINE */
-               return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
+               goto fault;
        }
 
+       surface = ecore_wl_window_surface_get(wl_win);
+       if (surface == NULL) {
+               _E("failed to get surface"); /* LCOV_EXCL_LINE */
+               goto fault; /* LCOV_EXCL_LINE */
+       }
+       screen_connector_provider_remote_enable(cxt->id, surface);
+
        ecore_wl_window_class_name_set(wl_win, cxt->id);
+       elm_win_aux_hint_add(ret_win, "wm.policy.win.user.geometry", "1");
 
        *win = ret_win;
        cxt->win = ret_win;
        cxt->win_id = ecore_wl_window_id_get(wl_win);
 
+       /* Set data to use in accessibility */
+       snprintf(buffer, sizeof(buffer), "%s:%d", cxt->id, getpid());
+       evas_object_data_set(ret_win, "___PLUGID", strdup(buffer));
+       evas_object_event_callback_add(ret_win, EVAS_CALLBACK_DEL, _win_del_cb, NULL);
+
        _D("window created: %d", cxt->win_id);
 
        return WIDGET_ERROR_NONE;
+
+fault:
+       if (ret_win)    /* LCOV_EXCL_LINE */
+               evas_object_del(ret_win); /* LCOV_EXCL_LINE */
+
+       return WIDGET_ERROR_FAULT; /* LCOV_EXCL_LINE */
+
 }
 
 widget_class_h _widget_class_create(widget_class_s *prev, const char *class_id,
@@ -1489,6 +1607,7 @@ EXPORT_API int widget_app_context_set_content_info(widget_context_h context,
        else
                context->content = NULL;
 
+       free(raw);
        if (ret < 0) {
                /* LCOV_EXCL_START */
                _E("failed to send content info: %s of %s (%d)", context->id,
@@ -1517,4 +1636,3 @@ EXPORT_API int widget_app_context_set_title(widget_context_h context,
 
        return WIDGET_ERROR_NONE;
 }
-