Add feature definition about background management
[platform/core/appfw/app-core.git] / src / base / appcore_base.c
index 305c065..37e1840 100644 (file)
@@ -35,6 +35,9 @@
 #include <vconf.h>
 #include <aul.h>
 #include <bundle_internal.h>
+#include <sensor_internal.h>
+#include <ttrace.h>
+#include <system_info.h>
 #include "appcore_base.h"
 #include "appcore_base_private.h"
 
@@ -60,6 +63,13 @@ typedef struct _appcore_base_event_node {
        void *data;
 } appcore_base_event_node;
 
+typedef struct _appcore_base_rotation {
+       int conn;
+       int lock;
+       int ref;
+       enum appcore_base_rm rm;
+} appcore_base_rotation;
+
 struct lang_info_s {
        char *parent;
        GList *list;
@@ -70,6 +80,47 @@ static GList *__events;
 static GDBusConnection *__bus;
 static guint __suspend_dbus_handler_initialized;
 static char *__locale_dir;
+static appcore_base_rotation __rotation;
+
+appcore_base_tizen_profile_t appcore_base_get_tizen_profile(void)
+{
+       static appcore_base_tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
+       char *profile_name = NULL;
+
+       if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
+               return profile;
+
+       system_info_get_platform_string("http://tizen.org/feature/profile",
+                       &profile_name);
+       if (profile_name == NULL)
+               return profile;
+
+       switch (*profile_name) {
+       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:
+               profile = TIZEN_PROFILE_COMMON;
+               break;
+       }
+       free(profile_name);
+
+       return profile;
+}
+
 
 static void __invoke_callback(void *event, int type)
 {
@@ -100,6 +151,156 @@ static bool __exist_callback(int type)
        return false;
 }
 
+static enum appcore_base_rm __get_rm(sensor_data_t data)
+{
+       int event;
+       enum appcore_base_rm rm;
+
+       if (data.value_count <= 0) {
+               _ERR("Failed to get sensor data");
+               return APPCORE_BASE_RM_UNKNOWN;
+       }
+
+       event = data.values[0];
+       switch (event) {
+       case AUTO_ROTATION_DEGREE_0:
+               rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
+               break;
+       case AUTO_ROTATION_DEGREE_90:
+               rm = APPCORE_BASE_RM_LANDSCAPE_NORMAL;
+               break;
+       case AUTO_ROTATION_DEGREE_180:
+               rm = APPCORE_BASE_RM_PORTRAIT_REVERSE;
+               break;
+       case AUTO_ROTATION_DEGREE_270:
+               rm = APPCORE_BASE_RM_LANDSCAPE_REVERSE;
+               break;
+       default:
+               rm = APPCORE_BASE_RM_UNKNOWN;
+               break;
+       }
+
+       return rm;
+}
+
+static void __lock_cb(keynode_t *node, void *user_data)
+{
+       bool r;
+       sensor_data_t data;
+       enum appcore_base_rm rm;
+
+       __rotation.lock = !vconf_keynode_get_bool(node);
+       if (__rotation.lock) {
+               _DBG("Rotation locked");
+               rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
+       } else {
+               _DBG("Rotation unlocked");
+               r = sensord_get_data(__rotation.conn, AUTO_ROTATION_SENSOR, &data);
+               if (!r) {
+                       _ERR("Failed to get sensor data");
+                       return;
+               }
+
+               rm = __get_rm(data);
+               if (rm == APPCORE_BASE_RM_UNKNOWN) {
+                       _ERR("Unknown mode");
+                       return;
+               }
+       }
+
+       if (__rotation.rm == rm)
+               return;
+
+       _DBG("Rotation: %d -> %d", __rotation.rm, rm);
+       __rotation.rm = rm;
+       __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
+}
+
+static void __auto_rotation_changed_cb(sensor_t sensor, unsigned int event_type,
+               sensor_data_t *data, void *user_data)
+{
+       enum appcore_base_rm rm;
+
+       if (data == NULL)
+               return;
+
+       if (__rotation.lock)
+               return;
+
+       if (event_type != AUTO_ROTATION_CHANGE_STATE_EVENT)
+               return;
+
+       rm = __get_rm(*data);
+       if (rm == APPCORE_BASE_RM_UNKNOWN) {
+               _ERR("Unknown mode");
+               return;
+       }
+
+       _DBG("Rotation: %d -> %d", __rotation.rm, rm);
+       __rotation.rm = rm;
+       __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
+}
+
+static void __unregister_rotation_changed_event(void)
+{
+       if (!__rotation.ref)
+               return;
+
+       __rotation.ref--;
+       if (__rotation.ref > 1)
+               return;
+
+       vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb);
+       sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
+       sensord_stop(__rotation.conn);
+       sensord_disconnect(__rotation.conn);
+
+       __rotation.lock = 0;
+       __rotation.ref = 0;
+}
+
+static void __register_rotation_changed_event(void)
+{
+       sensor_t sensor;
+       int lock;
+       bool r;
+
+       if (__rotation.ref) {
+               __rotation.ref++;
+               return;
+       }
+
+       sensor = sensord_get_sensor(AUTO_ROTATION_SENSOR);
+       __rotation.conn = sensord_connect(sensor);
+       if (__rotation.conn < 0) {
+               _ERR("Failed to connect sensord");
+               return;
+       }
+
+       r = sensord_register_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT,
+                       SENSOR_INTERVAL_NORMAL, 0, __auto_rotation_changed_cb, NULL);
+       if (!r) {
+               _ERR("Failed to register auto rotation change event");
+               sensord_disconnect(__rotation.conn);
+               return;
+       }
+
+       r = sensord_start(__rotation.conn, 0);
+       if (!r) {
+               _ERR("Failed to start sensord");
+               sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
+               sensord_disconnect(__rotation.conn);
+               return;
+       }
+
+       lock = 0;
+       vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &lock);
+       vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb, NULL);
+
+       __rotation.lock = !lock;
+       __rotation.ref++;
+}
+
 static void __on_low_memory(keynode_t *key, void *data)
 {
        int val;
@@ -107,7 +308,7 @@ static void __on_low_memory(keynode_t *key, void *data)
        val = vconf_keynode_get_int(key);
 
        if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
-               __invoke_callback(key, APPCORE_BASE_EVENT_LOW_MEMORY);
+               __invoke_callback(&val, APPCORE_BASE_EVENT_LOW_MEMORY);
                malloc_trim(0);
        }
 }
@@ -119,7 +320,7 @@ static void __on_low_battery(keynode_t *key, void *data)
        val = vconf_keynode_get_int(key);
 
        if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
-               __invoke_callback(key, APPCORE_BASE_EVENT_LOW_BATTERY);
+               __invoke_callback(&val, APPCORE_BASE_EVENT_LOW_BATTERY);
 }
 
 static void __destroy_lang_info(gpointer data)
@@ -257,6 +458,8 @@ static GList *__append_langs(const char *lang, GList *list, GHashTable *table)
        if (lang == NULL)
                return list;
 
+       list = g_list_append(list, strdup(lang));
+
        extract_lang = __get_string_before(lang, ".");
        if (extract_lang == NULL)
                return list;
@@ -413,7 +616,7 @@ static void __update_lang(void)
                setenv("LC_MESSAGES", lang, 1);
                r = setlocale(LC_ALL, "");
                if (r == NULL) {
-                       r = setlocale(LC_ALL, lang);
+                       r = setlocale(LC_ALL, "en_US.UTF-8");
                        if (r != NULL)
                                _DBG("*****appcore setlocale=%s\n", r);
                }
@@ -459,15 +662,22 @@ static void __on_language_change(keynode_t *key, void *data)
 
 static void __on_region_change(keynode_t *key, void *data)
 {
-       char *val = NULL;
+       char *val;
        const char *name;
 
        name = vconf_keynode_get_name(key);
-       if (!strcmp(name, VCONFKEY_REGIONFORMAT))
-               val = vconf_keynode_get_str(key);
+       if (name == NULL)
+               return;
+
+       if (strcmp(name, VCONFKEY_REGIONFORMAT) &&
+                       strcmp(name, VCONFKEY_REGIONFORMAT_TIME1224))
+               return;
+
+       val = vconf_get_str(VCONFKEY_REGIONFORMAT);
 
        __update_region();
        __invoke_callback((void *)val, APPCORE_BASE_EVENT_REGION_CHANGE);
+       free(val);
 }
 
 static gboolean __flush_memory(gpointer data)
@@ -589,7 +799,7 @@ static int __get_locale_resource_dir(char *locale_dir, int size)
 
        snprintf(locale_dir, size, "%s" PATH_LOCALE, res_path);
        if (access(locale_dir, R_OK) != 0)
-               return -1;
+               _DBG("%s does not exist", locale_dir);
 
        return 0;
 }
@@ -618,7 +828,6 @@ static int __get_app_name(const char *appid, char **name)
 static int __set_i18n(const char *domain, const char *dir)
 {
        char *r;
-       char *lan;
 
        if (domain == NULL) {
                errno = EINVAL;
@@ -635,14 +844,10 @@ static int __set_i18n(const char *domain, const char *dir)
        __update_region();
 
        r = setlocale(LC_ALL, "");
-       /* if locale is not set properly, try again to set as language base */
+       /* if locale is not set properly, try to set "en_US" again */
        if (r == NULL) {
-               lan = vconf_get_str(VCONFKEY_LANGSET);
-               if (lan != NULL) {
-                       r = setlocale(LC_ALL, lan);
-                       _DBG("*****appcore setlocale=%s\n", r);
-                       free(lan);
-               }
+               r = setlocale(LC_ALL, "en_US.UTF-8");
+               _DBG("*****appcore setlocale=%s\n", r);
        }
        if (r == NULL)
                _ERR("appcore: setlocale() error");
@@ -666,8 +871,10 @@ EXPORT_API int appcore_base_on_set_i18n(void)
        char *name = NULL;
 
        r = aul_app_get_appid_bypid(getpid(), appid, PATH_MAX);
-       if (r < 0)
+       if (r < 0) {
+               _ERR("Failed to get application ID - pid(%d)", getpid());
                return -1;
+       }
 
        r = __get_app_name(appid, &name);
        if (r < 0)
@@ -690,9 +897,15 @@ EXPORT_API int appcore_base_on_set_i18n(void)
        return 0;
 }
 
+EXPORT_API int appcore_base_set_i18n(const char *domain_name, const char *dir_name)
+{
+       return __set_i18n(domain_name, dir_name);
+}
+
 EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, void *data)
 {
        int i;
+       int r;
 
        __context.ops = ops;
        __context.argc = argc;
@@ -702,10 +915,14 @@ EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, vo
        __context.suspended_state = false;
        __context.allowed_bg = false;
 
+       if (__context.ops.init)
+               __context.ops.init(argc, argv, data);
+
        if (__context.ops.set_i18n)
                __context.ops.set_i18n(__context.data);
 
-       __init_suspend_dbus_handler();
+       if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
+               __init_suspend_dbus_handler();
 
        if (!__context.dirty) {
                __context.dirty = true;
@@ -718,9 +935,14 @@ EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, vo
                }
        }
 
-       if (__context.ops.create && __context.ops.create(__context.data) < 0) {
-               aul_status_update(STATUS_DYING);
-               return 0;
+       if (__context.ops.create) {
+               traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:CREATE");
+               r = __context.ops.create(__context.data);
+               traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
+               if (r < 0) {
+                       aul_status_update(STATUS_DYING);
+                       return 0;
+               }
        }
 
        if (__context.ops.run)
@@ -734,8 +956,11 @@ EXPORT_API void appcore_base_fini(void)
        int i;
 
        aul_status_update(STATUS_DYING);
-       if (__context.ops.terminate)
+       if (__context.ops.terminate) {
+               traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:TERMINATE");
                __context.ops.terminate(__context.data);
+               traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
+       }
 
        for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
                if (__exist_callback(i)) {
@@ -746,7 +971,9 @@ EXPORT_API void appcore_base_fini(void)
 
        g_list_free_full(__events, free);
        __events = NULL;
-       __fini_suspend_dbus_handler();
+
+       if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
+               __fini_suspend_dbus_handler();
 
        if (__locale_dir) {
                free(__locale_dir);
@@ -754,6 +981,9 @@ EXPORT_API void appcore_base_fini(void)
        }
 
        __context.dirty = false;
+
+       if (__context.ops.finish)
+               __context.ops.finish();
 }
 
 EXPORT_API int appcore_base_flush_memory(void)
@@ -785,23 +1015,30 @@ EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
                        }
                }
 
-               bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
-               if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
-                       _DBG("[__SUSPEND__] allowed background");
-                       __context.allowed_bg = true;
-                       __remove_suspend_timer();
+               if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
+                       bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
+                       if (bg && !strcmp(bg, "ALLOWED_BG")) {
+                               _DBG("[__SUSPEND__] allowed background");
+                               __context.allowed_bg = true;
+                               __remove_suspend_timer();
+                       }
                }
 
-               if (__context.ops.control)
-                      __context.ops.control(b, __context.data);
+               if (__context.ops.control) {
+                       traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:RESET");
+                       __context.ops.control(b, __context.data);
+                       traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
+               }
                break;
        case AUL_RESUME:
                _DBG("[APP %d]     AUL event: AUL_RESUME", getpid());
-               bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
-               if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
-                       _DBG("[__SUSPEND__] allowed background");
-                       __context.allowed_bg = true;
-                       __remove_suspend_timer();
+               if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
+                       bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
+                       if (bg && !strcmp(bg, "ALLOWED_BG")) {
+                               _DBG("[__SUSPEND__] allowed background");
+                               __context.allowed_bg = true;
+                               __remove_suspend_timer();
+                       }
                }
                break;
        case AUL_TERMINATE:
@@ -820,18 +1057,24 @@ EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
                break;
        case AUL_WAKE:
                _DBG("[APP %d]     AUL event: AUL_WAKE", getpid());
-               if (!__context.allowed_bg && __context.suspended_state) {
-                       int suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
-                       __remove_suspend_timer();
-                       __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
-                       __context.suspended_state = false;
+               if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
+                       if (!__context.allowed_bg &&
+                                       __context.suspended_state) {
+                               int suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
+                               __remove_suspend_timer();
+                               __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
+                               __context.suspended_state = false;
+                       }
                }
                break;
        case AUL_SUSPEND:
                _DBG("[APP %d]     AUL event: AUL_SUSPEND", getpid());
-               if (!__context.allowed_bg && !__context.suspended_state) {
-                       __remove_suspend_timer();
-                       __flush_memory(NULL);
+               if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
+                       if (!__context.allowed_bg &&
+                                       !__context.suspended_state) {
+                               __remove_suspend_timer();
+                               __flush_memory(NULL);
+                       }
                }
                break;
        case AUL_UPDATE_REQUESTED:
@@ -873,8 +1116,6 @@ EXPORT_API int appcore_base_on_control(bundle *b)
 EXPORT_API int appcore_base_on_terminate()
 {
        aul_finalize();
-       if (__context.ops.exit)
-               __context.ops.exit(__context.data);
 
        return 0;
 }
@@ -893,6 +1134,9 @@ EXPORT_API void appcore_base_on_set_event(enum appcore_base_event event)
        case APPCORE_BASE_EVENT_LANG_CHANGE:
                vconf_notify_key_changed(VCONFKEY_LANGSET, __on_language_change, NULL);
                break;
+       case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
+               __register_rotation_changed_event();
+               break;
        case APPCORE_BASE_EVENT_REGION_CHANGE:
                r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change, NULL);
                if (r < 0)
@@ -923,6 +1167,9 @@ EXPORT_API void appcore_base_on_unset_event(enum appcore_base_event event)
        case APPCORE_BASE_EVENT_LANG_CHANGE:
                vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_language_change);
                break;
+       case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
+               __unregister_rotation_changed_event();
+               break;
        case APPCORE_BASE_EVENT_REGION_CHANGE:
                r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change);
                if (r < 0)
@@ -964,6 +1211,9 @@ EXPORT_API int appcore_base_remove_event(appcore_base_event_h handle)
        appcore_base_event_node *node = handle;
        enum appcore_base_event event;
 
+       if (!node || !g_list_find(__events, node))
+               return -1;
+
        event = node->type;
        __events = g_list_remove(__events, node);
        free(node);
@@ -983,6 +1233,13 @@ EXPORT_API int appcore_base_raise_event(void *event, enum appcore_base_event typ
 
 EXPORT_API int appcore_base_get_rotation_state(enum appcore_base_rm *curr)
 {
+       if (curr == NULL)
+               return -1;
+
+       if (!__rotation.ref)
+               return -1;
+
+       *curr = __rotation.rm;
        return 0;
 }
 
@@ -1001,6 +1258,12 @@ EXPORT_API void appcore_base_toggle_suspended_state(void)
        __context.suspended_state ^= __context.suspended_state;
 }
 
+EXPORT_API void appcore_base_exit(void)
+{
+       if (__context.ops.exit)
+               __context.ops.exit(__context.data);
+}
+
 static int __on_receive(aul_type type, bundle *b, void *data)
 {
        return appcore_base_on_receive(type, b);
@@ -1045,6 +1308,8 @@ EXPORT_API appcore_base_ops appcore_base_get_default_ops(void)
        ops.terminate = __on_terminate;
        ops.receive = __on_receive;
        ops.set_i18n = __on_set_i18n;
+       ops.init = NULL;
+       ops.finish = NULL;
        ops.run = NULL;
        ops.exit = NULL;
        ops.set_event = __on_set_event;
@@ -1052,5 +1317,3 @@ EXPORT_API appcore_base_ops appcore_base_get_default_ops(void)
 
        return ops;
 }
-
-