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>
42 #include "appcore_base.h"
43 #include "appcore_base_private.h"
44 #include "appcore_watchdog.h"
45 #include "appcore_base_control.h"
47 #define PATH_LOCALE "locale"
48 #define RESOURCED_FREEZER_PATH "/Org/Tizen/ResourceD/Freezer"
49 #define RESOURCED_FREEZER_INTERFACE "org.tizen.resourced.freezer"
50 #define RESOURCED_FREEZER_SIGNAL "FreezerState"
51 #define SQLITE_FLUSH_MAX (1024 * 1024)
53 typedef struct _appcore_base_context {
64 } appcore_base_context;
66 typedef struct _appcore_base_event_node {
68 appcore_base_event_cb cb;
71 } appcore_base_event_node;
73 typedef struct _appcore_base_rotation {
77 enum appcore_base_rm rm;
80 } appcore_base_rotation;
87 static appcore_base_context __context;
88 static GList *__events;
89 static GDBusConnection *__bus;
90 static guint __suspend_dbus_handler_initialized;
91 static char *__locale_dir;
92 static appcore_base_rotation __rotation;
94 appcore_base_tizen_profile_t appcore_base_get_tizen_profile(void)
96 static appcore_base_tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
97 char *profile_name = NULL;
99 if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
102 system_info_get_platform_string("http://tizen.org/feature/profile",
104 if (profile_name == NULL)
107 switch (*profile_name) {
110 profile = TIZEN_PROFILE_MOBILE;
114 profile = TIZEN_PROFILE_WEARABLE;
118 profile = TIZEN_PROFILE_TV;
122 profile = TIZEN_PROFILE_IVI;
125 profile = TIZEN_PROFILE_COMMON;
133 static int __compare_event(void *prev_event, void *curr_event, int type)
142 case APPCORE_BASE_EVENT_LOW_MEMORY:
143 case APPCORE_BASE_EVENT_LOW_BATTERY:
144 case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
145 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
146 prev_intval = GPOINTER_TO_INT(prev_event);
147 curr_intval = *(int *)curr_event;
148 if (prev_intval == curr_intval)
151 case APPCORE_BASE_EVENT_LANG_CHANGE:
152 case APPCORE_BASE_EVENT_REGION_CHANGE:
153 prev_strval = prev_event;
154 curr_strval = curr_event;
155 if (prev_strval && curr_strval &&
156 !strcmp(prev_strval, curr_strval))
166 static void __unset_prev_event(void **prev_event, int type)
168 if (type == APPCORE_BASE_EVENT_LANG_CHANGE ||
169 type == APPCORE_BASE_EVENT_REGION_CHANGE)
174 static void __set_prev_event(void **prev_event, void *curr_event, int type)
180 case APPCORE_BASE_EVENT_LOW_MEMORY:
181 case APPCORE_BASE_EVENT_LOW_BATTERY:
182 case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
183 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
184 curr_intval = *(int *)curr_event;
185 *prev_event = GINT_TO_POINTER(curr_intval);
187 case APPCORE_BASE_EVENT_LANG_CHANGE:
188 case APPCORE_BASE_EVENT_REGION_CHANGE:
189 curr_strval = curr_event;
191 *prev_event = strdup(curr_strval);
198 static void __invoke_callback(void *event, int type)
200 GList *iter = __events;
203 appcore_base_event_node *node = iter->data;
204 iter = g_list_next(iter);
206 if (node->type != type)
209 if (__compare_event(node->prev_event, event, type)) {
210 node->cb(event, node->data);
211 __unset_prev_event(&node->prev_event, type);
212 __set_prev_event(&node->prev_event, event, type);
217 static bool __exist_callback(int type)
219 GList *iter = __events;
222 appcore_base_event_node *node = iter->data;
224 if (node->type == type)
227 iter = g_list_next(iter);
233 static enum appcore_base_rm __get_rm(sensor_data_t data)
236 enum appcore_base_rm rm;
238 if (data.value_count <= 0) {
239 _ERR("Failed to get sensor data");
240 return APPCORE_BASE_RM_UNKNOWN;
243 event = data.values[0];
245 case AUTO_ROTATION_DEGREE_0:
246 rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
248 case AUTO_ROTATION_DEGREE_90:
249 rm = APPCORE_BASE_RM_LANDSCAPE_NORMAL;
251 case AUTO_ROTATION_DEGREE_180:
252 rm = APPCORE_BASE_RM_PORTRAIT_REVERSE;
254 case AUTO_ROTATION_DEGREE_270:
255 rm = APPCORE_BASE_RM_LANDSCAPE_REVERSE;
258 rm = APPCORE_BASE_RM_UNKNOWN;
265 static void __lock_cb(keynode_t *node, void *user_data)
269 enum appcore_base_rm rm;
271 __rotation.lock = !vconf_keynode_get_bool(node);
272 if (__rotation.lock) {
273 _DBG("Rotation locked");
274 rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
276 _DBG("Rotation unlocked");
277 r = sensord_get_data(__rotation.conn, AUTO_ROTATION_SENSOR, &data);
279 _ERR("Failed to get sensor data");
284 if (rm == APPCORE_BASE_RM_UNKNOWN) {
285 _ERR("Unknown mode");
290 if (__rotation.rm == rm)
293 _DBG("Rotation: %d -> %d", __rotation.rm, rm);
295 __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
298 static void __auto_rotation_changed_cb(sensor_t sensor, unsigned int event_type,
299 sensor_data_t *data, void *user_data)
301 enum appcore_base_rm rm;
309 if (event_type != AUTO_ROTATION_CHANGE_STATE_EVENT)
312 rm = __get_rm(*data);
313 if (rm == APPCORE_BASE_RM_UNKNOWN) {
314 _ERR("Unknown mode");
318 _DBG("Rotation: %d -> %d", __rotation.rm, rm);
320 __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
323 static void __fini_rotation(void)
325 if (!__rotation.initialized)
328 vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb);
329 sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
330 sensord_stop(__rotation.conn);
331 sensord_disconnect(__rotation.conn);
334 __rotation.initialized = false;
337 static void __init_rotation(void)
343 if (__rotation.initialized)
346 sensor = sensord_get_sensor(AUTO_ROTATION_SENSOR);
347 __rotation.conn = sensord_connect(sensor);
348 if (__rotation.conn < 0) {
349 _ERR("Failed to connect sensord");
353 r = sensord_register_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT,
354 SENSOR_INTERVAL_NORMAL, 0, __auto_rotation_changed_cb, NULL);
356 _ERR("Failed to register auto rotation change event");
357 sensord_disconnect(__rotation.conn);
361 r = sensord_start(__rotation.conn, 0);
363 _ERR("Failed to start sensord");
364 sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
365 sensord_disconnect(__rotation.conn);
370 vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &lock);
371 vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb, NULL);
373 __rotation.lock = !lock;
374 __rotation.initialized = true;
377 static void __charger_status_changed_cb(keynode_t *keynode, void *user_data)
379 if (TIZEN_FEATURE_CHARGER_STATUS) {
380 __rotation.charger_status = vconf_keynode_get_int(keynode);
381 if (__rotation.charger_status) {
388 _DBG("charger status(%d)", __rotation.charger_status);
392 static void __unregister_rotation_changed_event(void)
398 if (__rotation.ref > 1)
402 if (TIZEN_FEATURE_CHARGER_STATUS) {
403 vconf_ignore_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS,
404 __charger_status_changed_cb);
410 static void __register_rotation_changed_event(void)
412 if (__rotation.ref) {
417 if (TIZEN_FEATURE_CHARGER_STATUS) {
418 vconf_get_int(VCONFKEY_SYSMAN_CHARGER_STATUS,
419 &__rotation.charger_status);
420 vconf_notify_key_changed(VCONFKEY_SYSMAN_CHARGER_STATUS,
421 __charger_status_changed_cb, NULL);
422 if (__rotation.charger_status)
431 static void __on_low_memory(keynode_t *key, void *data)
435 val = vconf_keynode_get_int(key);
437 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
438 __invoke_callback(&val, APPCORE_BASE_EVENT_LOW_MEMORY);
443 static void __on_low_battery(keynode_t *key, void *data)
447 val = vconf_keynode_get_int(key);
449 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
450 __invoke_callback(&val, APPCORE_BASE_EVENT_LOW_BATTERY);
453 static void __destroy_lang_info(gpointer data)
455 struct lang_info_s *info = (struct lang_info_s *)data;
461 g_list_free_full(info->list, free);
467 static struct lang_info_s *__create_lang_info(const char *lang)
469 struct lang_info_s *info;
471 info = calloc(1, sizeof(struct lang_info_s));
473 _ERR("Out of memory");
477 info->parent = strdup(lang);
478 if (info->parent == NULL) {
479 _ERR("Out of memory");
487 static gint __compare_langs(gconstpointer a, gconstpointer b)
495 static char *__get_string_before(const char *str, const char *delim)
501 dup_str = strdup(str);
505 token = strtok(dup_str, delim);
511 new_str = strdup(token);
517 static GHashTable *__get_lang_table(void)
521 struct dirent *dentry;
523 struct stat stat_buf;
526 struct lang_info_s *info;
528 if (__locale_dir == NULL || __locale_dir[0] == '\0')
531 table = g_hash_table_new_full(g_str_hash, g_str_equal,
532 NULL, __destroy_lang_info);
534 _ERR("Out of memory");
538 dp = opendir(__locale_dir);
540 g_hash_table_destroy(table);
544 while ((dentry = readdir(dp)) != NULL) {
545 if (!strcmp(dentry->d_name, ".") ||
546 !strcmp(dentry->d_name, ".."))
549 snprintf(buf, sizeof(buf), "%s/%s",
550 __locale_dir, dentry->d_name);
551 ret = stat(buf, &stat_buf);
552 if (ret != 0 || !S_ISDIR(stat_buf.st_mode))
555 parent_lang = __get_string_before(dentry->d_name, "_");
556 if (parent_lang == NULL) {
557 _ERR("Out of memory");
561 info = g_hash_table_lookup(table, parent_lang);
563 info = __create_lang_info(parent_lang);
568 g_hash_table_insert(table, info->parent, info);
570 info->list = g_list_append(info->list, strdup(dentry->d_name));
578 static GList *__append_langs(const char *lang, GList *list, GHashTable *table)
580 struct lang_info_s *info;
582 char *parent_lang = NULL;
588 list = g_list_append(list, strdup(lang));
590 extract_lang = __get_string_before(lang, ".");
591 if (extract_lang == NULL)
594 found = g_list_find_custom(list, extract_lang, __compare_langs);
596 list = g_list_remove_link(list, found);
597 list = g_list_concat(list, found);
601 parent_lang = __get_string_before(extract_lang, "_");
602 if (parent_lang == NULL)
605 info = g_hash_table_lookup(table, parent_lang);
609 found = g_list_find_custom(info->list, extract_lang, __compare_langs);
611 info->list = g_list_remove_link(info->list, found);
612 list = g_list_concat(list, found);
616 found = g_list_find_custom(info->list, parent_lang, __compare_langs);
618 info->list = g_list_remove_link(info->list, found);
619 list = g_list_concat(list, found);
623 found = g_list_first(info->list);
625 info->list = g_list_remove_link(info->list, found);
626 list = g_list_concat(list, found);
638 static GList *__split_language(const char *lang)
644 dup_lang = strdup(lang);
645 if (dup_lang == NULL) {
646 _ERR("Out of memory");
650 token = strtok(dup_lang, ":");
651 while (token != NULL) {
652 list = g_list_append(list, strdup(token));
653 token = strtok(NULL, ":");
660 static GList *__append_default_langs(GList *list)
662 const char *langs[] = {"en_US", "en_GB", "en"};
666 for (i = 0; i < (sizeof(langs) / sizeof(langs[0])); i++) {
667 found = g_list_find_custom(list, langs[i], __compare_langs);
669 list = g_list_append(list, strdup(langs[i]));
675 static char *__get_language(const char *lang)
679 GList *lang_list = NULL;
682 char buf[LINE_MAX] = {'\0'};
685 list = __split_language(lang);
689 table = __get_lang_table();
691 g_list_free_full(list, free);
695 iter = g_list_first(list);
697 language = (char *)iter->data;
698 lang_list = __append_langs(language, lang_list, table);
699 iter = g_list_next(iter);
701 g_list_free_full(list, free);
702 g_hash_table_destroy(table);
704 lang_list = __append_default_langs(lang_list);
705 iter = g_list_first(lang_list);
707 language = (char *)iter->data;
709 if (buf[0] == '\0') {
710 snprintf(buf, sizeof(buf), "%s", language);
712 n = sizeof(buf) - strlen(buf) - 1;
713 strncat(buf, ":", n);
714 n = sizeof(buf) - strlen(buf) - 1;
715 strncat(buf, language, n);
718 iter = g_list_next(iter);
720 g_list_free_full(lang_list, free);
725 static void __update_lang(void)
731 lang = vconf_get_str(VCONFKEY_LANGSET);
733 /* TODO: Use VCONFKEY_SETAPPL_LANGUAGES key */
734 language = __get_language(lang);
736 _DBG("*****language(%s)", language);
737 setenv("LANGUAGE", language, 1);
740 setenv("LANGUAGE", lang, 1);
742 setenv("LANG", lang, 1);
743 setenv("LC_MESSAGES", lang, 1);
744 setenv("LC_ALL", lang, 1);
745 r = setlocale(LC_ALL, "");
747 r = setlocale(LC_ALL, "en_US.UTF-8");
749 _DBG("*****appcore setlocale=%s\n", r);
751 _DBG("*****appcore setlocale=\"C\"");
752 setenv("LC_ALL", "C", 1);
753 r = setlocale(LC_ALL, "");
755 _ERR("failed to setlocale");
762 static void __update_region(void)
767 region = vconf_get_str(VCONFKEY_REGIONFORMAT);
769 setenv("LC_CTYPE", region, 1);
770 setenv("LC_NUMERIC", region, 1);
771 setenv("LC_TIME", region, 1);
772 setenv("LC_COLLATE", region, 1);
773 setenv("LC_MONETARY", region, 1);
774 setenv("LC_PAPER", region, 1);
775 setenv("LC_NAME", region, 1);
776 setenv("LC_ADDRESS", region, 1);
777 setenv("LC_TELEPHONE", region, 1);
778 setenv("LC_MEASUREMENT", region, 1);
779 setenv("LC_IDENTIFICATION", region, 1);
780 r = setlocale(LC_ALL, "");
782 _DBG("*****appcore setlocale=%s\n", r);
784 _DBG("*****appcore setlocale=\"C\"");
785 setenv("LC_ALL", "C", 1);
786 r = setlocale(LC_ALL, "");
788 _ERR("failed to setlocale");
795 static void __on_language_change(keynode_t *key, void *data)
800 g_source_remove(__context.sid);
804 val = vconf_keynode_get_str(key);
807 __invoke_callback((void *)val, APPCORE_BASE_EVENT_LANG_CHANGE);
810 static void __on_region_change(keynode_t *key, void *data)
815 name = vconf_keynode_get_name(key);
819 if (strcmp(name, VCONFKEY_REGIONFORMAT) &&
820 strcmp(name, VCONFKEY_REGIONFORMAT_TIME1224))
823 val = vconf_get_str(VCONFKEY_REGIONFORMAT);
826 __invoke_callback((void *)val, APPCORE_BASE_EVENT_REGION_CHANGE);
830 static gboolean __invoke_lang_change(gpointer data)
836 lang = getenv("LANG");
838 return G_SOURCE_REMOVE;
840 __invoke_callback((void *)lang, APPCORE_BASE_EVENT_LANG_CHANGE);
842 return G_SOURCE_REMOVE;
845 static void __verify_language(void)
848 const char *env_lang;
850 env_lang = getenv("LANG");
854 lang = vconf_get_str(VCONFKEY_LANGSET);
858 if (strcmp(env_lang, lang) != 0) {
859 _INFO("LANG(%s), LANGSET(%s)", env_lang, lang);
860 __context.sid = g_idle_add(__invoke_lang_change, NULL);
866 static gboolean __flush_memory(gpointer data)
868 int suspend = APPCORE_BASE_SUSPENDED_STATE_WILL_ENTER_SUSPEND;
870 if (__context.ops.trim_memory)
871 __context.ops.trim_memory(__context.data);
875 if (!__context.allowed_bg && !__context.suspended_state) {
876 _DBG("[__SUSPEND__] flush case");
877 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
878 __context.suspended_state = true;
884 static void __add_suspend_timer(void)
886 __context.tid = g_timeout_add_seconds(5, __flush_memory, NULL);
889 static void __remove_suspend_timer(void)
891 if (__context.tid > 0) {
892 g_source_remove(__context.tid);
897 static void __on_receive_suspend_signal(GDBusConnection *connection,
898 const gchar *sender_name,
899 const gchar *object_path,
900 const gchar *interface_name,
901 const gchar *signal_name,
902 GVariant *parameters,
905 gint suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
909 if (g_strcmp0(signal_name, RESOURCED_FREEZER_SIGNAL) == 0) {
910 g_variant_get(parameters, "(ii)", &status, &pid);
911 if (pid == getpid() && status == 0) {
912 if (!__context.allowed_bg && __context.suspended_state) {
913 __remove_suspend_timer();
914 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
915 __context.suspended_state = false;
916 __add_suspend_timer();
922 static int __init_suspend_dbus_handler(void)
926 if (__suspend_dbus_handler_initialized)
930 __bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
932 _ERR("Failed to connect to the D-BUS daemon: %s",
939 __suspend_dbus_handler_initialized = g_dbus_connection_signal_subscribe(
942 RESOURCED_FREEZER_INTERFACE,
943 RESOURCED_FREEZER_SIGNAL,
944 RESOURCED_FREEZER_PATH,
946 G_DBUS_SIGNAL_FLAGS_NONE,
947 __on_receive_suspend_signal,
950 if (__suspend_dbus_handler_initialized == 0) {
951 _ERR("g_dbus_connection_signal_subscribe() is failed.");
955 _DBG("[__SUSPEND__] suspend signal initialized");
960 static void __fini_suspend_dbus_handler(void)
965 if (__suspend_dbus_handler_initialized) {
966 g_dbus_connection_signal_unsubscribe(__bus,
967 __suspend_dbus_handler_initialized);
968 __suspend_dbus_handler_initialized = 0;
971 g_object_unref(__bus);
975 static gboolean __init_suspend(gpointer data)
977 __init_suspend_dbus_handler();
978 return G_SOURCE_REMOVE;
981 static int __get_locale_resource_dir(char *locale_dir, int size)
983 const char *res_path;
985 res_path = aul_get_app_resource_path();
986 if (res_path == NULL) {
987 _ERR("Failed to get resource path");
991 snprintf(locale_dir, size, "%s" PATH_LOCALE, res_path);
992 if (access(locale_dir, R_OK) != 0)
993 _DBG("%s does not exist", locale_dir);
998 static int __get_app_name(const char *appid, char **name)
1000 char *name_token = NULL;
1005 /* com.vendor.name -> name */
1006 name_token = strrchr(appid, '.');
1007 if (name_token == NULL) {
1008 *name = strdup(appid);
1014 *name = strdup(name_token);
1021 static int __set_i18n(const char *domain, const char *dir)
1025 if (domain == NULL) {
1033 __locale_dir = strdup(dir);
1039 r = setlocale(LC_ALL, "");
1040 /* if locale is not set properly, try to set "en_US" again */
1042 r = setlocale(LC_ALL, "en_US.UTF-8");
1044 _DBG("*****appcore setlocale=%s\n", r);
1047 _ERR("appcore: setlocale() error");
1048 _DBG("*****appcore setlocale=\"C\"");
1049 setenv("LC_ALL", "C", 1);
1050 r = setlocale(LC_ALL, "");
1052 _ERR("failed to setlocale");
1055 r = bindtextdomain(domain, dir);
1057 _ERR("appcore: bindtextdomain() error");
1059 r = textdomain(domain);
1061 _ERR("appcore: textdomain() error");
1066 EXPORT_API int appcore_base_on_set_i18n(void)
1069 char locale_dir[PATH_MAX];
1070 char appid[PATH_MAX];
1073 r = aul_app_get_appid_bypid(getpid(), appid, PATH_MAX);
1075 _ERR("Failed to get application ID - pid(%d)", getpid());
1079 r = __get_app_name(appid, &name);
1083 r = __get_locale_resource_dir(locale_dir, sizeof(locale_dir));
1089 r = __set_i18n(name, locale_dir);
1100 EXPORT_API int appcore_base_set_i18n(const char *domain_name, const char *dir_name)
1102 return __set_i18n(domain_name, dir_name);
1105 static void __set_default_events(void)
1109 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_language_change, NULL);
1110 r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change, NULL);
1112 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change, NULL);
1113 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1116 static void __unset_default_events(void)
1120 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_language_change);
1121 r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change);
1123 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change);
1124 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1127 EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, void *data)
1132 __context.ops = ops;
1133 __context.argc = argc;
1134 __context.argv = argv;
1135 __context.data = data;
1137 __context.suspended_state = false;
1138 __context.allowed_bg = false;
1140 if (__context.ops.init)
1141 __context.ops.init(argc, argv, data);
1143 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
1144 g_idle_add(__init_suspend, NULL);
1146 if (!__context.dirty) {
1147 __context.dirty = true;
1149 for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
1150 if (__exist_callback(i)) {
1151 if (__context.ops.set_event)
1152 __context.ops.set_event(i, __context.data);
1157 __verify_language();
1158 __set_default_events();
1159 if (__context.ops.set_i18n)
1160 __context.ops.set_i18n(__context.data);
1162 if (__context.ops.create) {
1163 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:CREATE");
1164 r = __context.ops.create(__context.data);
1165 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1167 aul_status_update(STATUS_DYING);
1172 if (__context.ops.run)
1173 __context.ops.run(__context.data);
1178 EXPORT_API void appcore_base_fini(void)
1182 aul_status_update(STATUS_DYING);
1183 appcore_watchdog_disable();
1184 if (__context.ops.terminate) {
1185 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:TERMINATE");
1186 __context.ops.terminate(__context.data);
1187 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1190 for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
1191 if (__exist_callback(i)) {
1192 if (__context.ops.unset_event)
1193 __context.ops.unset_event(i, __context.data);
1197 appcore_base_control_fini();
1198 __unset_default_events();
1200 if (__context.sid) {
1201 g_source_remove(__context.sid);
1205 __remove_suspend_timer();
1207 g_list_free_full(__events, free);
1210 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
1211 __fini_suspend_dbus_handler();
1215 __locale_dir = NULL;
1218 __context.dirty = false;
1220 if (__context.ops.finish)
1221 __context.ops.finish();
1224 EXPORT_API int appcore_base_flush_memory(void)
1226 if (__context.ops.trim_memory)
1227 __context.ops.trim_memory(__context.data);
1232 EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
1239 _DBG("[APP %d] AUL event: AUL_START", getpid());
1240 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1241 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
1242 if (bg && !strcmp(bg, "ALLOWED_BG")) {
1243 _DBG("[__SUSPEND__] allowed background");
1244 __context.allowed_bg = true;
1245 __remove_suspend_timer();
1249 if (__context.ops.control) {
1250 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:RESET");
1251 __context.ops.control(b, __context.data);
1252 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1256 _DBG("[APP %d] AUL event: AUL_RESUME", getpid());
1257 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1258 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
1259 if (bg && !strcmp(bg, "ALLOWED_BG")) {
1260 _DBG("[__SUSPEND__] allowed background");
1261 __context.allowed_bg = true;
1262 __remove_suspend_timer();
1267 _DBG("[APP %d] AUL event: AUL_TERMINATE", getpid());
1268 aul_status_update(STATUS_DYING);
1269 if (!__context.allowed_bg)
1270 __remove_suspend_timer();
1272 if (__context.ops.exit)
1273 __context.ops.exit(__context.data);
1275 case AUL_TERMINATE_INST:
1276 case AUL_TERMINATE_BG_INST:
1277 case AUL_TERMINATE_BGAPP:
1278 _DBG("[APP %d] AUL event: %d", getpid(), type);
1279 if (!__context.allowed_bg)
1280 __remove_suspend_timer();
1283 _DBG("[APP %d] AUL event: AUL_WAKE", getpid());
1284 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1285 if (!__context.allowed_bg &&
1286 __context.suspended_state) {
1287 int suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
1288 __remove_suspend_timer();
1289 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
1290 __context.suspended_state = false;
1295 _DBG("[APP %d] AUL event: AUL_SUSPEND", getpid());
1296 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1297 if (!__context.allowed_bg &&
1298 !__context.suspended_state) {
1299 __remove_suspend_timer();
1300 __flush_memory(NULL);
1304 case AUL_UPDATE_REQUESTED:
1305 _DBG("[APP %d] AUL event: AUL_UPDATE_REQUESTED", getpid());
1306 __invoke_callback((void *)&dummy, APPCORE_BASE_EVENT_UPDATE_REQUESTED);
1309 _DBG("[APP %d] AUL event: %d", getpid(), type);
1317 EXPORT_API int appcore_base_on_create(void)
1321 r = aul_launch_init(__context.ops.receive, __context.data);
1323 _ERR("Aul init failed: %d", r);
1327 r = aul_launch_argv_handler(__context.argc, __context.argv);
1329 _ERR("Aul argv handler failed: %d", r);
1336 EXPORT_API int appcore_base_on_control(bundle *b)
1338 appcore_base_control_invoke(b);
1343 EXPORT_API int appcore_base_on_terminate()
1350 EXPORT_API void appcore_base_on_set_event(enum appcore_base_event event)
1353 case APPCORE_BASE_EVENT_LOW_BATTERY:
1354 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1356 case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
1357 __register_rotation_changed_event();
1359 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
1368 EXPORT_API void appcore_base_on_unset_event(enum appcore_base_event event)
1371 case APPCORE_BASE_EVENT_LOW_BATTERY:
1372 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1374 case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
1375 __unregister_rotation_changed_event();
1377 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
1384 EXPORT_API int appcore_base_on_trim_memory(void)
1386 int (*sqlite3_free_heap_memory)(int);
1388 sqlite3_free_heap_memory = dlsym(RTLD_DEFAULT,
1389 "sqlite3_release_memory");
1390 if (sqlite3_free_heap_memory)
1391 sqlite3_free_heap_memory(SQLITE_FLUSH_MAX);
1398 EXPORT_API appcore_base_event_h appcore_base_add_event(enum appcore_base_event event,
1399 appcore_base_event_cb cb, void *data)
1401 appcore_base_event_node *node;
1403 if (__context.dirty && !__exist_callback(event)) {
1404 if (__context.ops.set_event)
1405 __context.ops.set_event(event, __context.data);
1408 node = malloc(sizeof(appcore_base_event_node));
1417 if (event == APPCORE_BASE_EVENT_LANG_CHANGE ||
1418 event == APPCORE_BASE_EVENT_REGION_CHANGE)
1419 node->prev_event = NULL;
1421 node->prev_event = GINT_TO_POINTER(-1);
1422 __events = g_list_append(__events, node);
1427 EXPORT_API int appcore_base_remove_event(appcore_base_event_h handle)
1429 appcore_base_event_node *node = handle;
1430 enum appcore_base_event event;
1432 if (!node || !g_list_find(__events, node))
1436 __events = g_list_remove(__events, node);
1437 __unset_prev_event(&node->prev_event, event);
1439 if (__context.dirty && !__exist_callback(event)) {
1440 if (__context.ops.unset_event)
1441 __context.ops.unset_event(event, __context.data);
1447 EXPORT_API int appcore_base_raise_event(void *event, enum appcore_base_event type)
1449 __invoke_callback(event, type);
1453 EXPORT_API int appcore_base_get_rotation_state(enum appcore_base_rm *curr)
1458 if (!__rotation.ref)
1461 *curr = __rotation.rm;
1465 EXPORT_API bool appcore_base_is_bg_allowed(void)
1467 return __context.allowed_bg;
1470 EXPORT_API bool appcore_base_is_suspended(void)
1472 return __context.suspended_state;
1475 EXPORT_API void appcore_base_toggle_suspended_state(void)
1477 __context.suspended_state ^= __context.suspended_state;
1480 EXPORT_API void appcore_base_exit(void)
1482 if (__context.ops.exit)
1483 __context.ops.exit(__context.data);
1486 EXPORT_API void appcore_base_add_suspend_timer(void)
1488 __add_suspend_timer();
1491 EXPORT_API void appcore_base_remove_suspend_timer(void)
1493 __remove_suspend_timer();
1496 EXPORT_API void appcore_base_set_display_state(int display_state)
1498 __context.display_state = display_state;
1501 EXPORT_API int appcore_base_get_display_state(void)
1503 return __context.display_state;
1506 static int __on_receive(aul_type type, bundle *b, void *data)
1508 return appcore_base_on_receive(type, b);
1511 static int __on_create(void *data)
1513 return appcore_base_on_create();
1516 static int __on_control(bundle *b, void *data)
1518 return appcore_base_on_control(b);
1521 static int __on_terminate(void *data)
1523 return appcore_base_on_terminate();
1526 static int __on_set_i18n(void *data)
1528 return appcore_base_on_set_i18n();
1531 static void __on_set_event(enum appcore_base_event event, void *data)
1533 appcore_base_on_set_event(event);
1536 static void __on_unset_event(enum appcore_base_event event, void *data)
1538 appcore_base_on_unset_event(event);
1541 static void __on_trim_memory(void *data)
1543 appcore_base_on_trim_memory();
1546 EXPORT_API appcore_base_ops appcore_base_get_default_ops(void)
1548 appcore_base_ops ops;
1550 ops.create = __on_create;
1551 ops.control = __on_control;
1552 ops.terminate = __on_terminate;
1553 ops.receive = __on_receive;
1554 ops.set_i18n = __on_set_i18n;
1559 ops.set_event = __on_set_event;
1560 ops.unset_event = __on_unset_event;
1561 ops.trim_memory = __on_trim_memory;