Remake old APIs using new APIs
[platform/core/appfw/app-core.git] / src / base / appcore_base.c
index 7a15b33..3e3c767 100644 (file)
@@ -35,6 +35,7 @@
 #include <vconf.h>
 #include <aul.h>
 #include <bundle_internal.h>
+#include <sensor_internal.h>
 #include "appcore_base.h"
 #include "appcore_base_private.h"
 
@@ -60,11 +61,24 @@ 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;
+};
+
 static appcore_base_context __context;
 static GList *__events;
 static GDBusConnection *__bus;
 static guint __suspend_dbus_handler_initialized;
 static char *__locale_dir;
+static appcore_base_rotation __rotation;
 
 static void __invoke_callback(void *event, int type)
 {
@@ -95,6 +109,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;
@@ -102,7 +266,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);
        }
 }
@@ -114,24 +278,73 @@ 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 __free_children_langs(gpointer data)
+static void __destroy_lang_info(gpointer data)
 {
-       GList *list = (GList *)data;
+       struct lang_info_s *info = (struct lang_info_s *)data;
 
-       if (list == NULL)
+       if (info == NULL)
                return;
 
-       g_list_free_full(list, (GDestroyNotify)free);
+       if (info->list)
+               g_list_free_full(info->list, free);
+       if (info->parent)
+               free(info->parent);
+       free(info);
+}
+
+static struct lang_info_s *__create_lang_info(const char *lang)
+{
+       struct lang_info_s *info;
+
+       info = calloc(1, sizeof(struct lang_info_s));
+       if (info == NULL) {
+               _ERR("Out of memory");
+               return NULL;
+       }
+
+       info->parent = strdup(lang);
+       if (info->parent == NULL) {
+               _ERR("Out of memory");
+               free(info);
+               return NULL;
+       }
+
+       return info;
 }
 
 static gint __compare_langs(gconstpointer a, gconstpointer b)
 {
+       if (!a || !b)
+               return -1;
+
        return strcmp(a, b);
 }
 
+static char *__get_string_before(const char *str, const char *delim)
+{
+       char *new_str;
+       char *dup_str;
+       char *token;
+
+       dup_str = strdup(str);
+       if (dup_str == NULL)
+               return NULL;
+
+       token = strtok(dup_str, delim);
+       if (token == NULL) {
+               free(dup_str);
+               return NULL;
+       }
+
+       new_str = strdup(token);
+       free(dup_str);
+
+       return new_str;
+}
+
 static GHashTable *__get_lang_table(void)
 {
        GHashTable *table;
@@ -140,15 +353,14 @@ static GHashTable *__get_lang_table(void)
        char buf[PATH_MAX];
        struct stat stat_buf;
        int ret;
-       char *dup_lang;
-       char *token;
-       GList *list;
+       char *parent_lang;
+       struct lang_info_s *info;
 
        if (__locale_dir == NULL || __locale_dir[0] == '\0')
                return NULL;
 
        table = g_hash_table_new_full(g_str_hash, g_str_equal,
-                       free, __free_children_langs);
+                       NULL, __destroy_lang_info);
        if (table == NULL) {
                _ERR("Out of memory");
                return NULL;
@@ -171,128 +383,84 @@ static GHashTable *__get_lang_table(void)
                if (ret != 0 || !S_ISDIR(stat_buf.st_mode))
                        continue;
 
-               dup_lang = strdup(dentry->d_name);
-               if (dup_lang == NULL) {
+               parent_lang = __get_string_before(dentry->d_name, "_");
+               if (parent_lang == NULL) {
                        _ERR("Out of memory");
                        break;
                }
 
-               token = strtok(dup_lang, "_");
-               if (token == NULL) {
-                       free(dup_lang);
-                       continue;
-               }
-
-               list = (GList *)g_hash_table_lookup(table, token);
-               if (list == NULL) {
-                       list = g_list_append(list, strdup(dentry->d_name));
-                       g_hash_table_insert(table, strdup(token), list);
-               } else {
-                       list = g_list_append(list, strdup(dentry->d_name));
+               info = g_hash_table_lookup(table, parent_lang);
+               if (info == NULL) {
+                       info = __create_lang_info(parent_lang);
+                       if (info == NULL) {
+                               free(parent_lang);
+                               break;
+                       }
+                       g_hash_table_insert(table, info->parent, info);
                }
-               free(dup_lang);
+               info->list = g_list_append(info->list, strdup(dentry->d_name));
+               free(parent_lang);
        }
        closedir(dp);
 
        return table;
 }
 
-static char *__get_string_before(const char *str, const char *delim)
-{
-       char *new_str;
-       char *dup_str;
-       char *token;
-
-       dup_str = strdup(str);
-       if (dup_str == NULL)
-               return NULL;
-
-       token = strtok(dup_str, delim);
-       if (token == NULL) {
-               free(dup_str);
-               return NULL;
-       }
-
-       new_str = strdup(token);
-       free(dup_str);
-
-       return new_str;
-}
-
 static GList *__append_langs(const char *lang, GList *list, GHashTable *table)
 {
-       GList *child_list;
-       GList *child_iter;
+       struct lang_info_s *info;
        GList *found;
-       char *child_lang;
-       char *parent_lang;
+       char *parent_lang = NULL;
        char *extract_lang;
-       char *tmp;
 
        if (lang == NULL)
                return list;
 
-       found = g_list_find_custom(g_list_first(list), lang,
-                       __compare_langs);
-       if (found) {
-               tmp = (char *)found->data;
-               list = g_list_remove(list, tmp);
-               list = g_list_append(list, tmp);
-               return list;
-       }
-
        extract_lang = __get_string_before(lang, ".");
        if (extract_lang == NULL)
                return list;
 
-       parent_lang = __get_string_before(extract_lang, "_");
-       if (parent_lang == NULL) {
-               free(extract_lang);
-               return list;
+       found = g_list_find_custom(list, extract_lang, __compare_langs);
+       if (found) {
+               list = g_list_remove_link(list, found);
+               list = g_list_concat(list, found);
+               goto end;
        }
 
-       child_list = g_hash_table_lookup(table, parent_lang);
-       if (child_list == NULL) {
-               free(parent_lang);
-               free(extract_lang);
-               return list;
-       }
+       parent_lang = __get_string_before(extract_lang, "_");
+       if (parent_lang == NULL)
+               goto end;
 
-       found = g_list_find_custom(g_list_first(child_list),
-                       extract_lang, __compare_langs);
+       info = g_hash_table_lookup(table, parent_lang);
+       if (info == NULL)
+               goto end;
+
+       found = g_list_find_custom(info->list, extract_lang, __compare_langs);
        if (found) {
-               tmp = (char *)found->data;
-               child_list = g_list_remove(child_list, tmp);
-               list = g_list_append(list, tmp);
-               free(parent_lang);
-               free(extract_lang);
-               return list;
+               info->list = g_list_remove_link(info->list, found);
+               list = g_list_concat(list, found);
+               goto end;
        }
-       free(extract_lang);
 
-       found = g_list_find_custom(g_list_first(child_list),
-                       parent_lang, __compare_langs);
+       found = g_list_find_custom(info->list, parent_lang, __compare_langs);
        if (found) {
-               tmp = (char *)found->data;
-               child_list = g_list_remove(child_list, tmp);
-               list = g_list_append(list, tmp);
-               free(parent_lang);
-               return list;
+               info->list = g_list_remove_link(info->list, found);
+               list = g_list_concat(list, found);
+               goto end;
        }
-       free(parent_lang);
-
-       child_iter = g_list_first(child_list);
-       while (child_iter) {
-               child_lang = (char *)child_iter->data;
-               child_iter = g_list_next(child_iter);
-               if (child_lang) {
-                       list = g_list_append(list, strdup(child_lang));
-                       child_list = g_list_remove(child_list, child_lang);
-                       free(child_lang);
-                       break;
-               }
+
+       found = g_list_first(info->list);
+       if (found) {
+               info->list = g_list_remove_link(info->list, found);
+               list = g_list_concat(list, found);
        }
 
+end:
+       if (extract_lang)
+               free(extract_lang);
+       if (parent_lang)
+               free(parent_lang);
+
        return list;
 }
 
@@ -325,8 +493,7 @@ static GList *__append_default_langs(GList *list)
        GList *found;
 
        for (i = 0; i < (sizeof(langs) / sizeof(langs[0])); i++) {
-               found = g_list_find_custom(g_list_first(list), langs[i],
-                               __compare_langs);
+               found = g_list_find_custom(list, langs[i], __compare_langs);
                if (found == NULL)
                        list = g_list_append(list, strdup(langs[i]));
        }
@@ -581,7 +748,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;
 }
@@ -682,6 +849,11 @@ 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;
@@ -885,6 +1057,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)
@@ -915,6 +1090,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)
@@ -975,6 +1153,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;
 }