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)
1012 *name = strdup(name_token);
1019 static int __set_i18n(const char *domain, const char *dir)
1023 if (domain == NULL) {
1031 __locale_dir = strdup(dir);
1037 r = setlocale(LC_ALL, "");
1038 /* if locale is not set properly, try to set "en_US" again */
1040 r = setlocale(LC_ALL, "en_US.UTF-8");
1042 _DBG("*****appcore setlocale=%s\n", r);
1045 _ERR("appcore: setlocale() error");
1046 _DBG("*****appcore setlocale=\"C\"");
1047 setenv("LC_ALL", "C", 1);
1048 r = setlocale(LC_ALL, "");
1050 _ERR("failed to setlocale");
1053 r = bindtextdomain(domain, dir);
1055 _ERR("appcore: bindtextdomain() error");
1057 r = textdomain(domain);
1059 _ERR("appcore: textdomain() error");
1064 EXPORT_API int appcore_base_on_set_i18n(void)
1067 char locale_dir[PATH_MAX];
1068 char appid[PATH_MAX];
1071 r = aul_app_get_appid_bypid(getpid(), appid, PATH_MAX);
1073 _ERR("Failed to get application ID - pid(%d)", getpid());
1077 r = __get_app_name(appid, &name);
1081 r = __get_locale_resource_dir(locale_dir, sizeof(locale_dir));
1087 r = __set_i18n(name, locale_dir);
1098 EXPORT_API int appcore_base_set_i18n(const char *domain_name, const char *dir_name)
1100 return __set_i18n(domain_name, dir_name);
1103 static void __set_default_events(void)
1107 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_language_change, NULL);
1108 r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change, NULL);
1110 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change, NULL);
1111 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1114 static void __unset_default_events(void)
1118 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_language_change);
1119 r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change);
1121 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change);
1122 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1125 EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, void *data)
1130 __context.ops = ops;
1131 __context.argc = argc;
1132 __context.argv = argv;
1133 __context.data = data;
1135 __context.suspended_state = false;
1136 __context.allowed_bg = false;
1138 if (__context.ops.init)
1139 __context.ops.init(argc, argv, data);
1141 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
1142 g_idle_add(__init_suspend, NULL);
1144 if (!__context.dirty) {
1145 __context.dirty = true;
1147 for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
1148 if (__exist_callback(i)) {
1149 if (__context.ops.set_event)
1150 __context.ops.set_event(i, __context.data);
1155 __verify_language();
1156 __set_default_events();
1157 if (__context.ops.set_i18n)
1158 __context.ops.set_i18n(__context.data);
1160 if (__context.ops.create) {
1161 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:CREATE");
1162 r = __context.ops.create(__context.data);
1163 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1165 aul_status_update(STATUS_DYING);
1170 if (__context.ops.run)
1171 __context.ops.run(__context.data);
1176 EXPORT_API void appcore_base_fini(void)
1180 aul_status_update(STATUS_DYING);
1181 appcore_watchdog_disable();
1182 if (__context.ops.terminate) {
1183 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:TERMINATE");
1184 __context.ops.terminate(__context.data);
1185 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1188 for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
1189 if (__exist_callback(i)) {
1190 if (__context.ops.unset_event)
1191 __context.ops.unset_event(i, __context.data);
1195 appcore_base_control_fini();
1196 __unset_default_events();
1198 if (__context.sid) {
1199 g_source_remove(__context.sid);
1203 __remove_suspend_timer();
1205 g_list_free_full(__events, free);
1208 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
1209 __fini_suspend_dbus_handler();
1213 __locale_dir = NULL;
1216 __context.dirty = false;
1218 if (__context.ops.finish)
1219 __context.ops.finish();
1222 EXPORT_API int appcore_base_flush_memory(void)
1224 if (__context.ops.trim_memory)
1225 __context.ops.trim_memory(__context.data);
1230 EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
1237 _DBG("[APP %d] AUL event: AUL_START", getpid());
1238 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1239 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
1240 if (bg && !strcmp(bg, "ALLOWED_BG")) {
1241 _DBG("[__SUSPEND__] allowed background");
1242 __context.allowed_bg = true;
1243 __remove_suspend_timer();
1247 if (__context.ops.control) {
1248 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:RESET");
1249 __context.ops.control(b, __context.data);
1250 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1254 _DBG("[APP %d] AUL event: AUL_RESUME", getpid());
1255 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1256 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
1257 if (bg && !strcmp(bg, "ALLOWED_BG")) {
1258 _DBG("[__SUSPEND__] allowed background");
1259 __context.allowed_bg = true;
1260 __remove_suspend_timer();
1265 _DBG("[APP %d] AUL event: AUL_TERMINATE", getpid());
1266 aul_status_update(STATUS_DYING);
1267 if (!__context.allowed_bg)
1268 __remove_suspend_timer();
1270 if (__context.ops.exit)
1271 __context.ops.exit(__context.data);
1273 case AUL_TERMINATE_INST:
1274 case AUL_TERMINATE_BG_INST:
1275 case AUL_TERMINATE_BGAPP:
1276 _DBG("[APP %d] AUL event: %d", getpid(), type);
1277 if (!__context.allowed_bg)
1278 __remove_suspend_timer();
1281 _DBG("[APP %d] AUL event: AUL_WAKE", getpid());
1282 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1283 if (!__context.allowed_bg &&
1284 __context.suspended_state) {
1285 int suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
1286 __remove_suspend_timer();
1287 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
1288 __context.suspended_state = false;
1293 _DBG("[APP %d] AUL event: AUL_SUSPEND", getpid());
1294 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1295 if (!__context.allowed_bg &&
1296 !__context.suspended_state) {
1297 __remove_suspend_timer();
1298 __flush_memory(NULL);
1302 case AUL_UPDATE_REQUESTED:
1303 _DBG("[APP %d] AUL event: AUL_UPDATE_REQUESTED", getpid());
1304 __invoke_callback((void *)&dummy, APPCORE_BASE_EVENT_UPDATE_REQUESTED);
1307 _DBG("[APP %d] AUL event: %d", getpid(), type);
1315 EXPORT_API int appcore_base_on_create(void)
1319 r = aul_launch_init(__context.ops.receive, __context.data);
1321 _ERR("Aul init failed: %d", r);
1325 r = aul_launch_argv_handler(__context.argc, __context.argv);
1327 _ERR("Aul argv handler failed: %d", r);
1334 EXPORT_API int appcore_base_on_control(bundle *b)
1336 appcore_base_control_invoke(b);
1341 EXPORT_API int appcore_base_on_terminate()
1348 EXPORT_API void appcore_base_on_set_event(enum appcore_base_event event)
1351 case APPCORE_BASE_EVENT_LOW_BATTERY:
1352 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1354 case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
1355 __register_rotation_changed_event();
1357 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
1366 EXPORT_API void appcore_base_on_unset_event(enum appcore_base_event event)
1369 case APPCORE_BASE_EVENT_LOW_BATTERY:
1370 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1372 case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
1373 __unregister_rotation_changed_event();
1375 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
1382 EXPORT_API int appcore_base_on_trim_memory(void)
1384 int (*sqlite3_free_heap_memory)(int);
1386 sqlite3_free_heap_memory = dlsym(RTLD_DEFAULT,
1387 "sqlite3_release_memory");
1388 if (sqlite3_free_heap_memory)
1389 sqlite3_free_heap_memory(SQLITE_FLUSH_MAX);
1396 EXPORT_API appcore_base_event_h appcore_base_add_event(enum appcore_base_event event,
1397 appcore_base_event_cb cb, void *data)
1399 appcore_base_event_node *node;
1401 if (__context.dirty && !__exist_callback(event)) {
1402 if (__context.ops.set_event)
1403 __context.ops.set_event(event, __context.data);
1406 node = malloc(sizeof(appcore_base_event_node));
1415 if (event == APPCORE_BASE_EVENT_LANG_CHANGE ||
1416 event == APPCORE_BASE_EVENT_REGION_CHANGE)
1417 node->prev_event = NULL;
1419 node->prev_event = GINT_TO_POINTER(-1);
1420 __events = g_list_append(__events, node);
1425 EXPORT_API int appcore_base_remove_event(appcore_base_event_h handle)
1427 appcore_base_event_node *node = handle;
1428 enum appcore_base_event event;
1430 if (!node || !g_list_find(__events, node))
1434 __events = g_list_remove(__events, node);
1435 __unset_prev_event(&node->prev_event, event);
1437 if (__context.dirty && !__exist_callback(event)) {
1438 if (__context.ops.unset_event)
1439 __context.ops.unset_event(event, __context.data);
1445 EXPORT_API int appcore_base_raise_event(void *event, enum appcore_base_event type)
1447 __invoke_callback(event, type);
1451 EXPORT_API int appcore_base_get_rotation_state(enum appcore_base_rm *curr)
1456 if (!__rotation.ref)
1459 *curr = __rotation.rm;
1463 EXPORT_API bool appcore_base_is_bg_allowed(void)
1465 return __context.allowed_bg;
1468 EXPORT_API bool appcore_base_is_suspended(void)
1470 return __context.suspended_state;
1473 EXPORT_API void appcore_base_toggle_suspended_state(void)
1475 __context.suspended_state ^= __context.suspended_state;
1478 EXPORT_API void appcore_base_exit(void)
1480 if (__context.ops.exit)
1481 __context.ops.exit(__context.data);
1484 EXPORT_API void appcore_base_add_suspend_timer(void)
1486 __add_suspend_timer();
1489 EXPORT_API void appcore_base_remove_suspend_timer(void)
1491 __remove_suspend_timer();
1494 EXPORT_API void appcore_base_set_display_state(int display_state)
1496 __context.display_state = display_state;
1499 EXPORT_API int appcore_base_get_display_state(void)
1501 return __context.display_state;
1504 static int __on_receive(aul_type type, bundle *b, void *data)
1506 return appcore_base_on_receive(type, b);
1509 static int __on_create(void *data)
1511 return appcore_base_on_create();
1514 static int __on_control(bundle *b, void *data)
1516 return appcore_base_on_control(b);
1519 static int __on_terminate(void *data)
1521 return appcore_base_on_terminate();
1524 static int __on_set_i18n(void *data)
1526 return appcore_base_on_set_i18n();
1529 static void __on_set_event(enum appcore_base_event event, void *data)
1531 appcore_base_on_set_event(event);
1534 static void __on_unset_event(enum appcore_base_event event, void *data)
1536 appcore_base_on_unset_event(event);
1539 static void __on_trim_memory(void *data)
1541 appcore_base_on_trim_memory();
1544 EXPORT_API appcore_base_ops appcore_base_get_default_ops(void)
1546 appcore_base_ops ops;
1548 ops.create = __on_create;
1549 ops.control = __on_control;
1550 ops.terminate = __on_terminate;
1551 ops.receive = __on_receive;
1552 ops.set_i18n = __on_set_i18n;
1557 ops.set_event = __on_set_event;
1558 ops.unset_event = __on_unset_event;
1559 ops.trim_memory = __on_trim_memory;