Remove Profile Build Dependency: Do it at runtime
[platform/core/appfw/app-core.git] / src / appcore-efl.c
index 7542cd1..6ff2939 100644 (file)
@@ -1,9 +1,5 @@
 /*
- *  app-core
- *
- * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
+ * Copyright (c) 2000 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,7 +12,6 @@
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- *
  */
 
 #include <sys/types.h>
@@ -31,6 +26,9 @@
 
 #if defined(WAYLAND)
 #include <Ecore_Wayland.h>
+#include <wayland-client.h>
+#include <wayland-tbm-client.h>
+#include <tizen-extension-client-protocol.h>
 #elif defined(X11)
 #include <X11/Xatom.h>
 #include <X11/Xlib.h>
 #include <glib-object.h>
 #include <malloc.h>
 #include <glib.h>
-#include <dbus/dbus.h>
-#include <dbus/dbus-glib-lowlevel.h>
+#include <gio/gio.h>
 #include <stdbool.h>
 #include <aul.h>
+#include <aul_svc.h>
+#include <bundle_internal.h>
+#include <ttrace.h>
 
 #include "appcore-internal.h"
 #include "appcore-efl.h"
+#include <system_info.h>
 
 static pid_t _pid;
 static bool resource_reclaiming = TRUE;
 static int tmp_val = 0;
 
-/* cgroup command type */
-enum proc_status_type {
-       PROC_STATUS_LAUNCH,
-       PROC_STATUS_RESUME,
-       PROC_STATUS_TERMINATE,
-       PROC_STATUS_FOREGRD,
-       PROC_STATUS_BACKGRD,
-};
-
 struct ui_priv {
        const char *name;
        enum app_state state;
@@ -79,11 +71,9 @@ struct ui_priv {
 
        Ecore_Timer *mftimer; /* Ecore Timer for memory flushing */
 
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
        struct appcore *app_core;
        void (*prepare_to_suspend) (void *data);
        void (*exit_from_suspend) (void *data);
-#endif
        struct appcore_ops *ops;
        void (*mfcb) (void); /* Memory Flushing Callback */
 
@@ -94,6 +84,7 @@ struct ui_priv {
        void *rot_cb_data;
        enum appcore_rm rot_mode;
        bundle *pending_data;
+       char *below_app;
 };
 
 static struct ui_priv priv;
@@ -107,6 +98,7 @@ static const char *_ae_name[AE_MAX] = {
        [AE_RESET] = "RESET",
        [AE_LOWMEM_POST] = "LOWMEM_POST",
        [AE_MEM_FLUSH] = "MEM_FLUSH",
+       [AE_UPDATE_REQUESTED] = "UPDATE_REQUESTED",
 };
 
 static const char *_as_name[] = {
@@ -128,51 +120,108 @@ struct win_node {
        bool bfobscured;
 };
 
+#if defined(X11)
 static struct ui_wm_rotate wm_rotate;
+#endif
 static Eina_Bool __visibility_cb(void *data, int type, void *event);
+static GSList *g_winnode_list;
+
+static struct wl_display *dsp;
+static struct wl_registry *reg;
+static struct tizen_policy *tz_policy;
+static bool bg_state = false;
+
+static void __wl_listener_cb(void *data, struct wl_registry *reg,
+               uint32_t id, const char *interface, uint32_t ver)
+{
+       if (interface && !strcmp(interface, "tizen_policy")) {
+               if (!tz_policy)
+                       tz_policy = wl_registry_bind(reg, id,
+                                       &tizen_policy_interface, 1);
+       }
+}
 
-static void _send_to_resourced(enum proc_status_type type)
+static void __wl_listener_remove_cb(void *data, struct wl_registry *reg,
+               unsigned int id)
 {
-       DBusConnection *conn;
-       DBusMessage* msg;
-       DBusError dbus_error;
+       /* do nothing */
+}
 
-       dbus_error_init(&dbus_error);
+static const struct wl_registry_listener reg_listener = {
+       __wl_listener_cb,
+       __wl_listener_remove_cb
+};
 
-       conn = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
-       if (!conn) {
-               _ERR("dbus_bus_get failed : [%s]", dbus_error.message);
-               dbus_error_free(&dbus_error);
-               return;
+static int __init_wl(void)
+{
+       _DBG("initialize wayland");
+       dsp = wl_display_connect(NULL);
+       if (dsp == NULL) {
+               _ERR("Failed to connect wl display");
+               return -1;
        }
 
-       msg = dbus_message_new_signal("/Org/Tizen/ResourceD/Process",
-                       "org.tizen.resourced.process",
-                       "ProcStatus");
-       if (!msg) {
-               _ERR("dbus_message_new_signal is failed");
-               return;
+       reg = wl_display_get_registry(dsp);
+       if (reg == NULL) {
+               _ERR("Failed to get registry");
+               wl_display_disconnect(dsp);
+               return -1;
        }
 
-       if (!dbus_message_append_args(msg,
-                               DBUS_TYPE_INT32, &type,
-                               DBUS_TYPE_INT32, &_pid,
-                               DBUS_TYPE_INVALID)) {
-               _ERR("dbus_message_append_args is failed. type = %d, pid = %d",
-                               type, _pid);
-               dbus_message_unref(msg);
-               return;
+       wl_registry_add_listener(reg, &reg_listener, NULL);
+       wl_display_roundtrip(dsp);
+
+       if (!tz_policy) {
+               _ERR("Failed to get tizen policy interface");
+               wl_registry_destroy(reg);
+               wl_display_disconnect(dsp);
+               return -1;
        }
 
-       if (!dbus_connection_send(conn, msg, NULL))
-               _ERR("dbus_connection_send is failed");
+       return 0;
+}
+
+static void __finish_wl(void)
+{
+       if (tz_policy) {
+               tizen_policy_destroy(tz_policy);
+               tz_policy = NULL;
+       }
+
+       if (reg) {
+               wl_registry_destroy(reg);
+               reg = NULL;
+       }
 
-       dbus_message_unref(msg);
+       if (dsp) {
+               wl_display_disconnect(dsp);
+               dsp = NULL;
+       }
 }
 
-static GSList *g_winnode_list;
+static void __set_bg_state(void)
+{
+       if (__init_wl() < 0)
+               return;
+
+       tizen_policy_set_background_state(tz_policy, getpid());
+       wl_display_roundtrip(dsp);
+       bg_state = true;
+       _DBG("bg state: %d", bg_state);
+}
+
+static void __unset_bg_state(void)
+{
+       if (!tz_policy)
+               return;
+
+       tizen_policy_unset_background_state(tz_policy, getpid());
+       wl_display_roundtrip(dsp);
+       bg_state = false;
+       _DBG("bg state: %d", bg_state);
+       __finish_wl();
+}
 
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
 static void __appcore_efl_prepare_to_suspend(void *data)
 {
        struct ui_priv *ui = (struct ui_priv *)data;
@@ -204,7 +253,20 @@ static void __appcore_efl_exit_from_suspend(void *data)
        }
        _DBG("[__SUSPEND__]");
 }
-#endif
+
+static void __appcore_efl_update_requested(void *data)
+{
+       struct ui_priv *ui = (struct ui_priv *)data;
+       struct sys_op *op;
+       int dummy = 0;
+
+       if (ui->app_core) {
+               op = &ui->app_core->sops[SE_UPDATE_REQUESTED];
+               if (op && op->func)
+                       op->func((void *)&dummy, op->data);
+       }
+       _DBG("[__UPDATE_REQUESTED__]");
+}
 
 #if defined(MEMORY_FLUSH_ACTIVATE)
 static Eina_Bool __appcore_memory_flush_cb(void *data)
@@ -215,12 +277,10 @@ static Eina_Bool __appcore_memory_flush_cb(void *data)
        if (ui)
                ui->mftimer = NULL;
 
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
-       if (ui && ui->prepare_to_suspend) {
+       if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT && ui && ui->prepare_to_suspend) {
                _DBG("[__SUSPEND__] flush case");
                ui->prepare_to_suspend(ui);
        }
-#endif
 
        return ECORE_CALLBACK_CANCEL;
 }
@@ -301,6 +361,8 @@ static void __do_app(enum app_event event, void *data, bundle * b)
 {
        int r = -1;
        struct ui_priv *ui = data;
+       const char *below_app;
+       const char *bg_launch;
 
        _DBG("[APP %d] Event: %d", _pid, event);
        _ret_if(ui == NULL || event >= AE_MAX);
@@ -355,24 +417,43 @@ static void __do_app(enum app_event event, void *data, bundle * b)
        switch (event) {
        case AE_RESET:
                _DBG("[APP %d] RESET", _pid);
+               if (ui->pending_data)
+                       bundle_free(ui->pending_data);
                ui->pending_data = bundle_dup(b);
                LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]", ui->name);
 
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
-               if (ui->exit_from_suspend) {
+               if (ui->below_app) {
+                       free(ui->below_app);
+                       ui->below_app = NULL;
+               }
+
+               below_app = bundle_get_val(b, AUL_SVC_K_RELOCATE_BELOW);
+               if (below_app)
+                       ui->below_app = strdup(below_app);
+               if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT && ui->exit_from_suspend) {
                        _DBG("[__SUSPEND__] reset case");
                        ui->exit_from_suspend(ui);
                }
-#endif
 
-               if (ui->ops->reset)
+               if (ui->ops->reset) {
+                       traceBegin(TTRACE_TAG_APPLICATION_MANAGER,
+                                       "APPCORE:RESET");
                        r = ui->ops->reset(b, ui->ops->data);
+                       traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
+               }
                LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]", ui->name);
 
                if (first_launch) {
+                       if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT && ui->app_core->allowed_bg)
+                               __appcore_timer_add(ui);
                        first_launch = FALSE;
                } else {
                        _INFO("[APP %d] App already running, raise the window", _pid);
+                       if (bg_state) {
+                               bg_launch = bundle_get_val(b, AUL_SVC_K_BG_LAUNCH);
+                               if (!bg_launch || strcmp(bg_launch, "enable"))
+                                       __unset_bg_state();
+                       }
 #ifdef X11
                        x_raise_win(getpid());
 #else
@@ -385,33 +466,36 @@ static void __do_app(enum app_event event, void *data, bundle * b)
        case AE_PAUSE:
                if (ui->state == AS_RUNNING) {
                        _DBG("[APP %d] PAUSE", _pid);
-                       if (ui->ops->pause)
+                       if (ui->ops->pause) {
+                               traceBegin(TTRACE_TAG_APPLICATION_MANAGER,
+                                               "APPCORE:PAUSE");
                                r = ui->ops->pause(ui->ops->data);
+                               traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
+                       }
                        ui->state = AS_PAUSED;
                        if (r >= 0 && resource_reclaiming == TRUE)
                                __appcore_timer_add(ui);
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
-                       else if (r >= 0 && resource_reclaiming == FALSE
+                       else if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT && r >= 0 && resource_reclaiming == FALSE
                                        && ui->prepare_to_suspend) {
                                _DBG("[__SUSPEND__] pause case");
                                ui->prepare_to_suspend(ui);
                        }
-#endif
                }
                /* TODO : rotation stop */
                /* r = appcore_pause_rotation_cb(); */
                aul_status_update(STATUS_BG);
-               _send_to_resourced(PROC_STATUS_BACKGRD);
                break;
        case AE_RESUME:
                LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
                                ui->name);
-#ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
-               if (ui->exit_from_suspend) {
-                       _DBG("[__SUSPEND__] resume case");
-                       ui->exit_from_suspend(ui);
+               if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT) {
+                       if (ui->exit_from_suspend) {
+                               _DBG("[__SUSPEND__] resume case");
+                               ui->exit_from_suspend(ui);
+                       }
+                       if (ui->app_core->allowed_bg)
+                               __appcore_timer_del(ui);
                }
-#endif
 
                if (ui->state == AS_PAUSED || ui->state == AS_CREATED) {
                        _DBG("[APP %d] RESUME", _pid);
@@ -421,9 +505,19 @@ static void __do_app(enum app_event event, void *data, bundle * b)
                                ui->pending_data = NULL;
                        }
 
-                       if (ui->ops->resume)
+                       if (ui->ops->resume) {
+                               traceBegin(TTRACE_TAG_APPLICATION_MANAGER,
+                                       "APPCORE:RESUME");
                                ui->ops->resume(ui->ops->data);
+                               traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
+                       }
                        ui->state = AS_RUNNING;
+
+                       if (ui->below_app) {
+                               aul_app_group_activate_below(ui->below_app);
+                               free(ui->below_app);
+                               ui->below_app = NULL;
+                       }
                }
                /*TODO : rotation start*/
                /* r = appcore_resume_rotation_cb(); */
@@ -432,7 +526,6 @@ static void __do_app(enum app_event event, void *data, bundle * b)
                LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
                    ui->name);
                aul_status_update(STATUS_VISIBLE);
-               _send_to_resourced(PROC_STATUS_FOREGRD);
                break;
        case AE_TERMINATE_BGAPP:
                if (ui->state == AS_PAUSED) {
@@ -446,6 +539,9 @@ static void __do_app(enum app_event event, void *data, bundle * b)
                        _DBG("[APP %d] is another state", _pid);
                }
                break;
+       case AE_UPDATE_REQUESTED:
+               __appcore_efl_update_requested(ui);
+               break;
        default:
                /* do nothing */
                break;
@@ -577,14 +673,10 @@ static bool __update_win(unsigned int win, bool bfobscured)
                return FALSE;
        }
 
-       g_winnode_list = g_slist_remove_link(g_winnode_list, f);
-
        t = (struct win_node *)f->data;
        t->win = win;
        t->bfobscured = bfobscured;
 
-       g_winnode_list = g_slist_concat(g_winnode_list, f);
-
        return TRUE;
 }
 #elif defined(WAYLAND)
@@ -603,16 +695,12 @@ static bool __update_win(unsigned int win, unsigned int surf, bool bfobscured)
                return FALSE;
        }
 
-       g_winnode_list = g_slist_remove_link(g_winnode_list, f);
-
        t = (struct win_node *)f->data;
        t->win = win;
        if (surf != 0)
                t->surf = surf;
        t->bfobscured = bfobscured;
 
-       g_winnode_list = g_slist_concat(g_winnode_list, f);
-
        return TRUE;
 }
 #endif
@@ -699,8 +787,6 @@ static Eina_Bool __show_cb(void *data, int type, void *event)
 
 #elif defined(X11)
        Ecore_X_Event_Window_Show *ev;
-       int ret;
-       Ecore_X_Window parent;
 
        ev = event;
 
@@ -793,7 +879,7 @@ static Eina_Bool __visibility_cb(void *data, int type, void *event)
 
 }
 
-#ifdef X11
+#if defined(X11)
 /* WM_ROTATE */
 static Eina_Bool __cmsg_cb(void *data, int type, void *event)
 {
@@ -879,6 +965,9 @@ static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
 {
        int r;
        char *hwacc = NULL;
+       struct appcore *ac = NULL;
+       bundle *b;
+       const char *bg_launch;
 
        if (argc == NULL || argv == NULL) {
                _ERR("argc/argv is NULL");
@@ -907,20 +996,35 @@ static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
        r = appcore_init(ui->name, &efl_ops, *argc, *argv);
        _retv_if(r == -1, -1);
 
-#if _APPFW_FEATURE_BACKGROUND_MANAGEMENT
-       appcore_get_app_core(&ac);
-       ui->app_core = ac;
-       SECURE_LOGD("[__SUSPEND__] appcore initialized, appcore addr: 0x%x", ac);
-#endif
+       if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT) {
+               appcore_get_app_core(&ac);
+               ui->app_core = ac;
+               SECURE_LOGD("[__SUSPEND__] appcore initialized, appcore addr: #%x", ac);
+       }
+
+       b = bundle_import_from_argv(*argc, *argv);
+       if (b) {
+               bg_launch = bundle_get_val(b, AUL_SVC_K_BG_LAUNCH);
+               if (bg_launch && strcmp(bg_launch, "enable") == 0)
+                       __set_bg_state();
+
+               bundle_free(b);
+       }
 
        LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
        if (ui->ops && ui->ops->create) {
+               traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:CREATE");
                r = ui->ops->create(ui->ops->data);
+               traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
                if (r < 0) {
                        _ERR("create() return error");
                        appcore_exit();
-                       if (ui->ops && ui->ops->terminate)
+                       if (ui->ops && ui->ops->terminate) {
+                               traceBegin(TTRACE_TAG_APPLICATION_MANAGER,
+                                       "APPCORE:TERMINATE");
                                ui->ops->terminate(ui->ops->data);
+                               traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
+                       }
                        errno = ECANCELED;
                        return -1;
                }
@@ -941,12 +1045,19 @@ static void __after_loop(struct ui_priv *ui)
 
        if (ui->state == AS_RUNNING) {
                _DBG("[APP %d] PAUSE before termination", _pid);
-               if (ui->ops && ui->ops->pause)
+               if (ui->ops && ui->ops->pause) {
+                       traceBegin(TTRACE_TAG_APPLICATION_MANAGER,
+                                       "APPCORE:PAUSE");
                        ui->ops->pause(ui->ops->data);
+                       traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
+               }
        }
 
-       if (ui->ops && ui->ops->terminate)
+       if (ui->ops && ui->ops->terminate) {
+               traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:TERMINATE");
                ui->ops->terminate(ui->ops->data);
+               traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
+       }
 
        ui->state = AS_DYING;
 
@@ -964,6 +1075,12 @@ static void __after_loop(struct ui_priv *ui)
        __appcore_timer_del(ui);
 
        elm_shutdown();
+
+       /* Check loader case */
+       if (getenv("AUL_LOADER_INIT")) {
+               unsetenv("AUL_LOADER_INIT");
+               elm_shutdown();
+       }
 }
 
 static int __set_data(struct ui_priv *ui, const char *name,
@@ -1001,11 +1118,11 @@ static int __set_data(struct ui_priv *ui, const char *name,
        ui->rot_cb_data = NULL;
        ui->rot_mode = APPCORE_RM_UNKNOWN;
 
-#ifdef  _APPFW_FEATURE_BACKGROUND_MANAGEMENT
-       ui->app_core = NULL;
-       ui->prepare_to_suspend = __appcore_efl_prepare_to_suspend;
-       ui->exit_from_suspend = __appcore_efl_exit_from_suspend;
-#endif
+       if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT) {
+               ui->app_core = NULL;
+               ui->prepare_to_suspend = __appcore_efl_prepare_to_suspend;
+               ui->exit_from_suspend = __appcore_efl_exit_from_suspend;
+       }
 
        return 0;
 }
@@ -1018,6 +1135,7 @@ static void __unset_data(struct ui_priv *ui)
        memset(ui, 0, sizeof(struct ui_priv));
 }
 
+#if defined(X11)
 /* WM_ROTATE */
 static int __wm_set_rotation_cb(int (*cb) (void *event_info, enum appcore_rm, void *), void *data)
 {
@@ -1087,6 +1205,7 @@ static struct ui_wm_rotate wm_rotate = {
        __wm_pause_rotation_cb,
        __wm_resume_rotation_cb
 };
+#endif
 
 EXPORT_API int appcore_efl_init(const char *name, int *argc, char ***argv,
                     struct appcore_ops *ops)
@@ -1159,7 +1278,7 @@ EXPORT_API int appcore_set_preinit_window_name(const char *win_name)
                return ret;
        }
 
-       preinit_window = aul_get_preinit_window(win_name);
+       preinit_window = elm_win_precreated_object_get();
        if (!preinit_window) {
                _ERR("Failed to get preinit window");
                return ret;
@@ -1202,3 +1321,36 @@ EXPORT_API unsigned int appcore_get_main_surface(void)
        return 0;
 }
 #endif
+
+tizen_profile_t _get_tizen_profile()
+{
+       static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
+       if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
+               return profile;
+
+       char *profileName;
+       system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
+       switch (*profileName) {
+       case 'm':
+       case 'M':
+               profile = TIZEN_PROFILE_MOBILE;
+               break;
+       case 'w':
+       case 'W':
+               profile = TIZEN_PROFILE_WEARABLE;
+               break;
+       case 't':
+       case 'T':
+               profile = TIZEN_PROFILE_TV;
+               break;
+       case 'i':
+       case 'I':
+               profile = TIZEN_PROFILE_IVI;
+               break;
+       default: // common or unTIZEN_nown ==> ALL ARE COMMON.
+               profile = TIZEN_PROFILE_COMMON;
+       }
+       free(profileName);
+
+       return profile;
+}