Send create aborted event to the widget viewer
[platform/core/appfw/appcore-widget.git] / src / widget_app.c
index 8898766..e25fdd1 100755 (executable)
@@ -16,6 +16,7 @@
 
 
 #include <stdlib.h>
+#include <stdbool.h>
 
 #include <bundle.h>
 #include <bundle_internal.h>
@@ -36,7 +37,7 @@
 #include <system_info.h>
 #include <vconf.h>
 #include <vconf-internal-keys.h>
-#include <aul_rsm_provider.h>
+#include <screen_connector_provider.h>
 
 #include "widget_app.h"
 #include "widget-log.h"
@@ -50,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,
@@ -86,6 +90,7 @@ static char *appid;
 static widget_class_h class_provider;
 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);
@@ -149,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);
@@ -283,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;
        }
@@ -318,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;
        }
@@ -409,8 +455,10 @@ 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 = strdup(id);
@@ -427,7 +475,18 @@ static int __instance_create(widget_class_h handle, const char *id, const char *
 
        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);
@@ -891,8 +950,7 @@ static int __before_loop(int argc, char **argv)
                _E("failed to get launch argv"); /* LCOV_EXCL_LINE */
        }
 
-       aul_rsm_provider_init();
-
+       screen_connector_provider_init();
        elm_init(argc, argv);
 
        r = aul_launch_init(__aul_handler, NULL);
@@ -963,7 +1021,7 @@ static void __after_loop()
        if (app_ops->terminate)
                app_ops->terminate(app_user_data);
 
-       aul_rsm_provider_fini();
+       screen_connector_provider_fini();
 
        _widget_app_free_viewer_endpoint();
        _widget_core_unset_appcore_event_cb();
@@ -974,6 +1032,11 @@ static void __after_loop()
                package_id = NULL;
        }
 
+       if (appid) {
+               free(appid);
+               appid = NULL;
+       }
+
        elm_shutdown();
 }
 
@@ -1149,8 +1212,13 @@ EXPORT_API int widget_app_main(int argc, char **argv,
        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);
@@ -1362,6 +1430,7 @@ EXPORT_API int widget_app_get_elm_win(widget_context_h context,
        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 */
@@ -1378,6 +1447,9 @@ EXPORT_API int widget_app_get_elm_win(widget_context_h context,
                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 */
@@ -1389,8 +1461,7 @@ EXPORT_API int widget_app_get_elm_win(widget_context_h context,
                _E("failed to get surface"); /* LCOV_EXCL_LINE */
                goto fault; /* LCOV_EXCL_LINE */
        }
-
-       aul_rsm_provider_remote_enable(cxt->id, surface);
+       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");
@@ -1536,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,