2 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include <sys/types.h>
30 #include <linux/limits.h>
37 #include <bundle_internal.h>
38 #include <sensor_internal.h>
40 #include <system_info.h>
41 #include "appcore_base.h"
42 #include "appcore_base_private.h"
44 #define PATH_LOCALE "locale"
45 #define RESOURCED_FREEZER_PATH "/Org/Tizen/Resourced/Freezer"
46 #define RESOURCED_FREEZER_INTERFACE "org.tizen.resourced.freezer"
47 #define RESOURCED_FREEZER_SIGNAL "FreezerState"
49 typedef struct _appcore_base_context {
59 } appcore_base_context;
61 typedef struct _appcore_base_event_node {
63 appcore_base_event_cb cb;
65 } appcore_base_event_node;
67 typedef struct _appcore_base_rotation {
71 enum appcore_base_rm rm;
72 } appcore_base_rotation;
79 static appcore_base_context __context;
80 static GList *__events;
81 static GDBusConnection *__bus;
82 static guint __suspend_dbus_handler_initialized;
83 static char *__locale_dir;
84 static appcore_base_rotation __rotation;
86 appcore_base_tizen_profile_t appcore_base_get_tizen_profile(void)
88 static appcore_base_tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
89 char *profile_name = NULL;
91 if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
94 system_info_get_platform_string("http://tizen.org/feature/profile",
96 if (profile_name == NULL)
99 switch (*profile_name) {
102 profile = TIZEN_PROFILE_MOBILE;
106 profile = TIZEN_PROFILE_WEARABLE;
110 profile = TIZEN_PROFILE_TV;
114 profile = TIZEN_PROFILE_IVI;
117 profile = TIZEN_PROFILE_COMMON;
126 static void __invoke_callback(void *event, int type)
128 GList *iter = __events;
131 appcore_base_event_node *node = iter->data;
133 if (node->type == type)
134 node->cb(event, node->data);
135 iter = g_list_next(iter);
139 static bool __exist_callback(int type)
141 GList *iter = __events;
144 appcore_base_event_node *node = iter->data;
146 if (node->type == type)
149 iter = g_list_next(iter);
155 static enum appcore_base_rm __get_rm(sensor_data_t data)
158 enum appcore_base_rm rm;
160 if (data.value_count <= 0) {
161 _ERR("Failed to get sensor data");
162 return APPCORE_BASE_RM_UNKNOWN;
165 event = data.values[0];
167 case AUTO_ROTATION_DEGREE_0:
168 rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
170 case AUTO_ROTATION_DEGREE_90:
171 rm = APPCORE_BASE_RM_LANDSCAPE_NORMAL;
173 case AUTO_ROTATION_DEGREE_180:
174 rm = APPCORE_BASE_RM_PORTRAIT_REVERSE;
176 case AUTO_ROTATION_DEGREE_270:
177 rm = APPCORE_BASE_RM_LANDSCAPE_REVERSE;
180 rm = APPCORE_BASE_RM_UNKNOWN;
187 static void __lock_cb(keynode_t *node, void *user_data)
191 enum appcore_base_rm rm;
193 __rotation.lock = !vconf_keynode_get_bool(node);
194 if (__rotation.lock) {
195 _DBG("Rotation locked");
196 rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
198 _DBG("Rotation unlocked");
199 r = sensord_get_data(__rotation.conn, AUTO_ROTATION_SENSOR, &data);
201 _ERR("Failed to get sensor data");
206 if (rm == APPCORE_BASE_RM_UNKNOWN) {
207 _ERR("Unknown mode");
212 if (__rotation.rm == rm)
215 _DBG("Rotation: %d -> %d", __rotation.rm, rm);
217 __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
220 static void __auto_rotation_changed_cb(sensor_t sensor, unsigned int event_type,
221 sensor_data_t *data, void *user_data)
223 enum appcore_base_rm rm;
231 if (event_type != AUTO_ROTATION_CHANGE_STATE_EVENT)
234 rm = __get_rm(*data);
235 if (rm == APPCORE_BASE_RM_UNKNOWN) {
236 _ERR("Unknown mode");
240 _DBG("Rotation: %d -> %d", __rotation.rm, rm);
242 __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
245 static void __unregister_rotation_changed_event(void)
251 if (__rotation.ref > 1)
254 vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb);
255 sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
256 sensord_stop(__rotation.conn);
257 sensord_disconnect(__rotation.conn);
263 static void __register_rotation_changed_event(void)
269 if (__rotation.ref) {
274 sensor = sensord_get_sensor(AUTO_ROTATION_SENSOR);
275 __rotation.conn = sensord_connect(sensor);
276 if (__rotation.conn < 0) {
277 _ERR("Failed to connect sensord");
281 r = sensord_register_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT,
282 SENSOR_INTERVAL_NORMAL, 0, __auto_rotation_changed_cb, NULL);
284 _ERR("Failed to register auto rotation change event");
285 sensord_disconnect(__rotation.conn);
289 r = sensord_start(__rotation.conn, 0);
291 _ERR("Failed to start sensord");
292 sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
293 sensord_disconnect(__rotation.conn);
298 vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &lock);
299 vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb, NULL);
301 __rotation.lock = !lock;
305 static void __on_low_memory(keynode_t *key, void *data)
309 val = vconf_keynode_get_int(key);
311 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
312 __invoke_callback(&val, APPCORE_BASE_EVENT_LOW_MEMORY);
317 static void __on_low_battery(keynode_t *key, void *data)
321 val = vconf_keynode_get_int(key);
323 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
324 __invoke_callback(&val, APPCORE_BASE_EVENT_LOW_BATTERY);
327 static void __destroy_lang_info(gpointer data)
329 struct lang_info_s *info = (struct lang_info_s *)data;
335 g_list_free_full(info->list, free);
341 static struct lang_info_s *__create_lang_info(const char *lang)
343 struct lang_info_s *info;
345 info = calloc(1, sizeof(struct lang_info_s));
347 _ERR("Out of memory");
351 info->parent = strdup(lang);
352 if (info->parent == NULL) {
353 _ERR("Out of memory");
361 static gint __compare_langs(gconstpointer a, gconstpointer b)
369 static char *__get_string_before(const char *str, const char *delim)
375 dup_str = strdup(str);
379 token = strtok(dup_str, delim);
385 new_str = strdup(token);
391 static GHashTable *__get_lang_table(void)
395 struct dirent *dentry;
397 struct stat stat_buf;
400 struct lang_info_s *info;
402 if (__locale_dir == NULL || __locale_dir[0] == '\0')
405 table = g_hash_table_new_full(g_str_hash, g_str_equal,
406 NULL, __destroy_lang_info);
408 _ERR("Out of memory");
412 dp = opendir(__locale_dir);
414 g_hash_table_destroy(table);
418 while ((dentry = readdir(dp)) != NULL) {
419 if (!strcmp(dentry->d_name, ".") ||
420 !strcmp(dentry->d_name, ".."))
423 snprintf(buf, sizeof(buf), "%s/%s",
424 __locale_dir, dentry->d_name);
425 ret = stat(buf, &stat_buf);
426 if (ret != 0 || !S_ISDIR(stat_buf.st_mode))
429 parent_lang = __get_string_before(dentry->d_name, "_");
430 if (parent_lang == NULL) {
431 _ERR("Out of memory");
435 info = g_hash_table_lookup(table, parent_lang);
437 info = __create_lang_info(parent_lang);
442 g_hash_table_insert(table, info->parent, info);
444 info->list = g_list_append(info->list, strdup(dentry->d_name));
452 static GList *__append_langs(const char *lang, GList *list, GHashTable *table)
454 struct lang_info_s *info;
456 char *parent_lang = NULL;
462 list = g_list_append(list, strdup(lang));
464 extract_lang = __get_string_before(lang, ".");
465 if (extract_lang == NULL)
468 found = g_list_find_custom(list, extract_lang, __compare_langs);
470 list = g_list_remove_link(list, found);
471 list = g_list_concat(list, found);
475 parent_lang = __get_string_before(extract_lang, "_");
476 if (parent_lang == NULL)
479 info = g_hash_table_lookup(table, parent_lang);
483 found = g_list_find_custom(info->list, extract_lang, __compare_langs);
485 info->list = g_list_remove_link(info->list, found);
486 list = g_list_concat(list, found);
490 found = g_list_find_custom(info->list, parent_lang, __compare_langs);
492 info->list = g_list_remove_link(info->list, found);
493 list = g_list_concat(list, found);
497 found = g_list_first(info->list);
499 info->list = g_list_remove_link(info->list, found);
500 list = g_list_concat(list, found);
512 static GList *__split_language(const char *lang)
518 dup_lang = strdup(lang);
519 if (dup_lang == NULL) {
520 _ERR("Out of memory");
524 token = strtok(dup_lang, ":");
525 while (token != NULL) {
526 list = g_list_append(list, strdup(token));
527 token = strtok(NULL, ":");
534 static GList *__append_default_langs(GList *list)
536 const char *langs[] = {"en_US", "en_GB", "en"};
540 for (i = 0; i < (sizeof(langs) / sizeof(langs[0])); i++) {
541 found = g_list_find_custom(list, langs[i], __compare_langs);
543 list = g_list_append(list, strdup(langs[i]));
549 static char *__get_language(const char *lang)
553 GList *lang_list = NULL;
556 char buf[LINE_MAX] = {'\0'};
559 list = __split_language(lang);
563 table = __get_lang_table();
565 g_list_free_full(list, free);
569 iter = g_list_first(list);
571 language = (char *)iter->data;
572 lang_list = __append_langs(language, lang_list, table);
573 iter = g_list_next(iter);
575 g_list_free_full(list, free);
576 g_hash_table_destroy(table);
578 lang_list = __append_default_langs(lang_list);
579 iter = g_list_first(lang_list);
581 language = (char *)iter->data;
583 if (buf[0] == '\0') {
584 snprintf(buf, sizeof(buf), "%s", language);
586 n = sizeof(buf) - strlen(buf) - 1;
587 strncat(buf, ":", n);
588 n = sizeof(buf) - strlen(buf) - 1;
589 strncat(buf, language, n);
592 iter = g_list_next(iter);
594 g_list_free_full(lang_list, free);
599 static void __update_lang(void)
605 lang = vconf_get_str(VCONFKEY_LANGSET);
607 /* TODO: Use VCONFKEY_SETAPPL_LANGUAGES key */
608 language = __get_language(lang);
610 _DBG("*****language(%s)", language);
611 setenv("LANGUAGE", language, 1);
614 setenv("LANGUAGE", lang, 1);
616 setenv("LANG", lang, 1);
617 setenv("LC_MESSAGES", lang, 1);
618 r = setlocale(LC_ALL, "");
620 r = setlocale(LC_ALL, "en_US.UTF-8");
622 _DBG("*****appcore setlocale=%s\n", r);
624 _DBG("*****appcore setlocale=\"C\"");
625 setenv("LC_ALL", "C", 1);
626 r = setlocale(LC_ALL, "");
628 _ERR("failed to setlocale");
635 static void __update_region(void)
640 region = vconf_get_str(VCONFKEY_REGIONFORMAT);
642 setenv("LC_CTYPE", region, 1);
643 setenv("LC_NUMERIC", region, 1);
644 setenv("LC_TIME", region, 1);
645 setenv("LC_COLLATE", region, 1);
646 setenv("LC_MONETARY", region, 1);
647 setenv("LC_PAPER", region, 1);
648 setenv("LC_NAME", region, 1);
649 setenv("LC_ADDRESS", region, 1);
650 setenv("LC_TELEPHONE", region, 1);
651 setenv("LC_MEASUREMENT", region, 1);
652 setenv("LC_IDENTIFICATION", region, 1);
653 r = setlocale(LC_ALL, "");
655 _DBG("*****appcore setlocale=%s\n", r);
657 _DBG("*****appcore setlocale=\"C\"");
658 setenv("LC_ALL", "C", 1);
659 r = setlocale(LC_ALL, "");
661 _ERR("failed to setlocale");
668 static void __on_language_change(keynode_t *key, void *data)
673 g_source_remove(__context.sid);
677 val = vconf_keynode_get_str(key);
680 __invoke_callback((void *)val, APPCORE_BASE_EVENT_LANG_CHANGE);
683 static void __on_region_change(keynode_t *key, void *data)
688 name = vconf_keynode_get_name(key);
692 if (strcmp(name, VCONFKEY_REGIONFORMAT) &&
693 strcmp(name, VCONFKEY_REGIONFORMAT_TIME1224))
696 val = vconf_get_str(VCONFKEY_REGIONFORMAT);
699 __invoke_callback((void *)val, APPCORE_BASE_EVENT_REGION_CHANGE);
703 static gboolean __invoke_lang_change(gpointer data)
709 lang = getenv("LANG");
711 return G_SOURCE_REMOVE;
713 __invoke_callback((void *)lang, APPCORE_BASE_EVENT_LANG_CHANGE);
715 return G_SOURCE_REMOVE;
718 static void __verify_language(void)
721 const char *env_lang;
723 env_lang = getenv("LANG");
727 lang = vconf_get_str(VCONFKEY_LANGSET);
731 if (strcmp(env_lang, lang) != 0) {
732 _INFO("LANG(%s), LANGSET(%s)", env_lang, lang);
733 __context.sid = g_idle_add(__invoke_lang_change, NULL);
739 static gboolean __flush_memory(gpointer data)
741 int suspend = APPCORE_BASE_SUSPENDED_STATE_WILL_ENTER_SUSPEND;
743 appcore_base_flush_memory();
746 if (!__context.allowed_bg && !__context.suspended_state) {
747 _DBG("[__SUSPEND__] flush case");
748 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
749 __context.suspended_state = true;
755 static void __add_suspend_timer(void)
757 __context.tid = g_timeout_add_seconds(5, __flush_memory, NULL);
760 static void __remove_suspend_timer(void)
762 if (__context.tid > 0) {
763 g_source_remove(__context.tid);
768 static void __on_receive_suspend_signal(GDBusConnection *connection,
769 const gchar *sender_name,
770 const gchar *object_path,
771 const gchar *interface_name,
772 const gchar *signal_name,
773 GVariant *parameters,
776 gint suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
780 if (g_strcmp0(signal_name, RESOURCED_FREEZER_SIGNAL) == 0) {
781 g_variant_get(parameters, "(ii)", &status, &pid);
782 if (pid == getpid() && status == 0) {
783 if (!__context.allowed_bg && __context.suspended_state) {
784 __remove_suspend_timer();
785 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
786 __context.suspended_state = false;
787 __add_suspend_timer();
793 static int __init_suspend_dbus_handler(void)
797 if (__suspend_dbus_handler_initialized)
801 __bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
803 _ERR("Failed to connect to the D-BUS daemon: %s",
810 __suspend_dbus_handler_initialized = g_dbus_connection_signal_subscribe(
813 RESOURCED_FREEZER_INTERFACE,
814 RESOURCED_FREEZER_SIGNAL,
815 RESOURCED_FREEZER_PATH,
817 G_DBUS_SIGNAL_FLAGS_NONE,
818 __on_receive_suspend_signal,
821 if (__suspend_dbus_handler_initialized == 0) {
822 _ERR("g_dbus_connection_signal_subscribe() is failed.");
826 _DBG("[__SUSPEND__] suspend signal initialized");
831 static void __fini_suspend_dbus_handler(void)
836 if (__suspend_dbus_handler_initialized) {
837 g_dbus_connection_signal_unsubscribe(__bus,
838 __suspend_dbus_handler_initialized);
839 __suspend_dbus_handler_initialized = 0;
842 g_object_unref(__bus);
846 static gboolean __init_suspend(gpointer data)
848 __init_suspend_dbus_handler();
849 return G_SOURCE_REMOVE;
852 static int __get_locale_resource_dir(char *locale_dir, int size)
854 const char *res_path;
856 res_path = aul_get_app_resource_path();
857 if (res_path == NULL) {
858 _ERR("Failed to get resource path");
862 snprintf(locale_dir, size, "%s" PATH_LOCALE, res_path);
863 if (access(locale_dir, R_OK) != 0)
864 _DBG("%s does not exist", locale_dir);
869 static int __get_app_name(const char *appid, char **name)
871 char *name_token = NULL;
876 /* com.vendor.name -> name */
877 name_token = strrchr(appid, '.');
878 if (name_token == NULL)
883 *name = strdup(name_token);
890 static int __set_i18n(const char *domain, const char *dir)
894 if (domain == NULL) {
902 __locale_dir = strdup(dir);
908 r = setlocale(LC_ALL, "");
909 /* if locale is not set properly, try to set "en_US" again */
911 r = setlocale(LC_ALL, "en_US.UTF-8");
913 _DBG("*****appcore setlocale=%s\n", r);
916 _ERR("appcore: setlocale() error");
917 _DBG("*****appcore setlocale=\"C\"");
918 setenv("LC_ALL", "C", 1);
919 r = setlocale(LC_ALL, "");
921 _ERR("failed to setlocale");
924 r = bindtextdomain(domain, dir);
926 _ERR("appcore: bindtextdomain() error");
928 r = textdomain(domain);
930 _ERR("appcore: textdomain() error");
935 EXPORT_API int appcore_base_on_set_i18n(void)
938 char locale_dir[PATH_MAX];
939 char appid[PATH_MAX];
942 r = aul_app_get_appid_bypid(getpid(), appid, PATH_MAX);
944 _ERR("Failed to get application ID - pid(%d)", getpid());
948 r = __get_app_name(appid, &name);
952 r = __get_locale_resource_dir(locale_dir, sizeof(locale_dir));
958 r = __set_i18n(name, locale_dir);
969 EXPORT_API int appcore_base_set_i18n(const char *domain_name, const char *dir_name)
971 return __set_i18n(domain_name, dir_name);
974 EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, void *data)
980 __context.argc = argc;
981 __context.argv = argv;
982 __context.data = data;
984 __context.suspended_state = false;
985 __context.allowed_bg = false;
987 if (__context.ops.init)
988 __context.ops.init(argc, argv, data);
990 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
991 g_idle_add(__init_suspend, NULL);
993 if (!__context.dirty) {
994 __context.dirty = true;
996 for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
997 if (__exist_callback(i)) {
998 if (__context.ops.set_event)
999 __context.ops.set_event(i, __context.data);
1004 __verify_language();
1005 if (__context.ops.set_i18n)
1006 __context.ops.set_i18n(__context.data);
1008 if (__context.ops.create) {
1009 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:CREATE");
1010 r = __context.ops.create(__context.data);
1011 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1013 aul_status_update(STATUS_DYING);
1018 if (__context.ops.run)
1019 __context.ops.run(__context.data);
1024 EXPORT_API void appcore_base_fini(void)
1028 aul_status_update(STATUS_DYING);
1029 if (__context.ops.terminate) {
1030 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:TERMINATE");
1031 __context.ops.terminate(__context.data);
1032 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1035 for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
1036 if (__exist_callback(i)) {
1037 if (__context.ops.unset_event)
1038 __context.ops.unset_event(i, __context.data);
1042 if (__context.sid) {
1043 g_source_remove(__context.sid);
1047 g_list_free_full(__events, free);
1050 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
1051 __fini_suspend_dbus_handler();
1055 __locale_dir = NULL;
1058 __context.dirty = false;
1060 if (__context.ops.finish)
1061 __context.ops.finish();
1064 EXPORT_API int appcore_base_flush_memory(void)
1070 EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
1077 _DBG("[APP %d] AUL event: AUL_START", getpid());
1078 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1079 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
1080 if (bg && !strcmp(bg, "ALLOWED_BG")) {
1081 _DBG("[__SUSPEND__] allowed background");
1082 __context.allowed_bg = true;
1083 __remove_suspend_timer();
1087 if (__context.ops.control) {
1088 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:RESET");
1089 __context.ops.control(b, __context.data);
1090 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1094 _DBG("[APP %d] AUL event: AUL_RESUME", getpid());
1095 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1096 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
1097 if (bg && !strcmp(bg, "ALLOWED_BG")) {
1098 _DBG("[__SUSPEND__] allowed background");
1099 __context.allowed_bg = true;
1100 __remove_suspend_timer();
1105 _DBG("[APP %d] AUL event: AUL_TERMINATE", getpid());
1106 aul_status_update(STATUS_DYING);
1107 if (!__context.allowed_bg)
1108 __remove_suspend_timer();
1110 if (__context.ops.exit)
1111 __context.ops.exit(__context.data);
1113 case AUL_TERMINATE_BGAPP:
1114 _DBG("[APP %d] AUL event: AUL_TERMINATE_BGAPP", getpid());
1115 if (!__context.allowed_bg)
1116 __remove_suspend_timer();
1119 _DBG("[APP %d] AUL event: AUL_WAKE", getpid());
1120 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1121 if (!__context.allowed_bg &&
1122 __context.suspended_state) {
1123 int suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
1124 __remove_suspend_timer();
1125 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
1126 __context.suspended_state = false;
1131 _DBG("[APP %d] AUL event: AUL_SUSPEND", getpid());
1132 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1133 if (!__context.allowed_bg &&
1134 !__context.suspended_state) {
1135 __remove_suspend_timer();
1136 __flush_memory(NULL);
1140 case AUL_UPDATE_REQUESTED:
1141 _DBG("[APP %d] AUL event: AUL_UPDATE_REQUESTED", getpid());
1142 __invoke_callback((void *)&dummy, APPCORE_BASE_EVENT_UPDATE_REQUESTED);
1145 _DBG("[APP %d] AUL event: %d", getpid(), type);
1153 EXPORT_API int appcore_base_on_create(void)
1157 r = aul_launch_init(__context.ops.receive, NULL);
1159 _ERR("Aul init failed: %d", r);
1163 r = aul_launch_argv_handler(__context.argc, __context.argv);
1165 _ERR("Aul argv handler failed: %d", r);
1172 EXPORT_API int appcore_base_on_control(bundle *b)
1177 EXPORT_API int appcore_base_on_terminate()
1184 EXPORT_API void appcore_base_on_set_event(enum appcore_base_event event)
1189 case APPCORE_BASE_EVENT_LOW_MEMORY:
1190 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1192 case APPCORE_BASE_EVENT_LOW_BATTERY:
1193 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1195 case APPCORE_BASE_EVENT_LANG_CHANGE:
1196 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_language_change, NULL);
1198 case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
1199 __register_rotation_changed_event();
1201 case APPCORE_BASE_EVENT_REGION_CHANGE:
1202 r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change, NULL);
1206 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change, NULL);
1208 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
1217 EXPORT_API void appcore_base_on_unset_event(enum appcore_base_event event)
1222 case APPCORE_BASE_EVENT_LOW_MEMORY:
1223 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1225 case APPCORE_BASE_EVENT_LOW_BATTERY:
1226 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1228 case APPCORE_BASE_EVENT_LANG_CHANGE:
1229 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_language_change);
1231 case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
1232 __unregister_rotation_changed_event();
1234 case APPCORE_BASE_EVENT_REGION_CHANGE:
1235 r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change);
1238 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change);
1240 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
1247 EXPORT_API appcore_base_event_h appcore_base_add_event(enum appcore_base_event event,
1248 appcore_base_event_cb cb, void *data)
1250 appcore_base_event_node *node;
1252 if (__context.dirty && !__exist_callback(event)) {
1253 if (__context.ops.set_event)
1254 __context.ops.set_event(event, __context.data);
1257 node = malloc(sizeof(appcore_base_event_node));
1265 __events = g_list_append(__events, node);
1270 EXPORT_API int appcore_base_remove_event(appcore_base_event_h handle)
1272 appcore_base_event_node *node = handle;
1273 enum appcore_base_event event;
1275 if (!node || !g_list_find(__events, node))
1279 __events = g_list_remove(__events, node);
1281 if (__context.dirty && !__exist_callback(event)) {
1282 if (__context.ops.unset_event)
1283 __context.ops.unset_event(event, __context.data);
1289 EXPORT_API int appcore_base_raise_event(void *event, enum appcore_base_event type)
1291 __invoke_callback(event, type);
1295 EXPORT_API int appcore_base_get_rotation_state(enum appcore_base_rm *curr)
1300 if (!__rotation.ref)
1303 *curr = __rotation.rm;
1307 EXPORT_API bool appcore_base_is_bg_allowed(void)
1309 return __context.allowed_bg;
1312 EXPORT_API bool appcore_base_is_suspended(void)
1314 return __context.suspended_state;
1317 EXPORT_API void appcore_base_toggle_suspended_state(void)
1319 __context.suspended_state ^= __context.suspended_state;
1322 EXPORT_API void appcore_base_exit(void)
1324 if (__context.ops.exit)
1325 __context.ops.exit(__context.data);
1328 static int __on_receive(aul_type type, bundle *b, void *data)
1330 return appcore_base_on_receive(type, b);
1333 static int __on_create(void *data)
1335 return appcore_base_on_create();
1338 static int __on_control(bundle *b, void *data)
1340 return appcore_base_on_control(b);
1343 static int __on_terminate(void *data)
1345 return appcore_base_on_terminate();
1348 static int __on_set_i18n(void *data)
1350 return appcore_base_on_set_i18n();
1353 static void __on_set_event(enum appcore_base_event event, void *data)
1355 return appcore_base_on_set_event(event);
1358 static void __on_unset_event(enum appcore_base_event event, void *data)
1360 return appcore_base_on_unset_event(event);
1363 EXPORT_API appcore_base_ops appcore_base_get_default_ops(void)
1365 appcore_base_ops ops;
1367 ops.create = __on_create;
1368 ops.control = __on_control;
1369 ops.terminate = __on_terminate;
1370 ops.receive = __on_receive;
1371 ops.set_i18n = __on_set_i18n;
1376 ops.set_event = __on_set_event;
1377 ops.unset_event = __on_unset_event;