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 static void __set_default_events(void)
978 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_language_change, NULL);
979 r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change, NULL);
981 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change, NULL);
982 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
985 static void __unset_default_events(void)
989 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_language_change);
990 r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change);
992 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change);
993 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
996 EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, void *data)
1001 __context.ops = ops;
1002 __context.argc = argc;
1003 __context.argv = argv;
1004 __context.data = data;
1006 __context.suspended_state = false;
1007 __context.allowed_bg = false;
1009 if (__context.ops.init)
1010 __context.ops.init(argc, argv, data);
1012 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
1013 g_idle_add(__init_suspend, NULL);
1015 if (!__context.dirty) {
1016 __context.dirty = true;
1018 for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
1019 if (__exist_callback(i)) {
1020 if (__context.ops.set_event)
1021 __context.ops.set_event(i, __context.data);
1026 __verify_language();
1027 __set_default_events();
1028 if (__context.ops.set_i18n)
1029 __context.ops.set_i18n(__context.data);
1031 if (__context.ops.create) {
1032 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:CREATE");
1033 r = __context.ops.create(__context.data);
1034 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1036 aul_status_update(STATUS_DYING);
1041 if (__context.ops.run)
1042 __context.ops.run(__context.data);
1047 EXPORT_API void appcore_base_fini(void)
1051 aul_status_update(STATUS_DYING);
1052 if (__context.ops.terminate) {
1053 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:TERMINATE");
1054 __context.ops.terminate(__context.data);
1055 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1058 for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
1059 if (__exist_callback(i)) {
1060 if (__context.ops.unset_event)
1061 __context.ops.unset_event(i, __context.data);
1065 __unset_default_events();
1067 if (__context.sid) {
1068 g_source_remove(__context.sid);
1072 g_list_free_full(__events, free);
1075 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT)
1076 __fini_suspend_dbus_handler();
1080 __locale_dir = NULL;
1083 __context.dirty = false;
1085 if (__context.ops.finish)
1086 __context.ops.finish();
1089 EXPORT_API int appcore_base_flush_memory(void)
1095 EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
1102 _DBG("[APP %d] AUL event: AUL_START", getpid());
1103 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1104 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
1105 if (bg && !strcmp(bg, "ALLOWED_BG")) {
1106 _DBG("[__SUSPEND__] allowed background");
1107 __context.allowed_bg = true;
1108 __remove_suspend_timer();
1112 if (__context.ops.control) {
1113 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:RESET");
1114 __context.ops.control(b, __context.data);
1115 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
1119 _DBG("[APP %d] AUL event: AUL_RESUME", getpid());
1120 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1121 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
1122 if (bg && !strcmp(bg, "ALLOWED_BG")) {
1123 _DBG("[__SUSPEND__] allowed background");
1124 __context.allowed_bg = true;
1125 __remove_suspend_timer();
1130 _DBG("[APP %d] AUL event: AUL_TERMINATE", getpid());
1131 aul_status_update(STATUS_DYING);
1132 if (!__context.allowed_bg)
1133 __remove_suspend_timer();
1135 if (__context.ops.exit)
1136 __context.ops.exit(__context.data);
1138 case AUL_TERMINATE_BGAPP:
1139 _DBG("[APP %d] AUL event: AUL_TERMINATE_BGAPP", getpid());
1140 if (!__context.allowed_bg)
1141 __remove_suspend_timer();
1144 _DBG("[APP %d] AUL event: AUL_WAKE", getpid());
1145 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1146 if (!__context.allowed_bg &&
1147 __context.suspended_state) {
1148 int suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
1149 __remove_suspend_timer();
1150 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
1151 __context.suspended_state = false;
1156 _DBG("[APP %d] AUL event: AUL_SUSPEND", getpid());
1157 if (TIZEN_FEATURE_BACKGROUND_MANAGEMENT) {
1158 if (!__context.allowed_bg &&
1159 !__context.suspended_state) {
1160 __remove_suspend_timer();
1161 __flush_memory(NULL);
1165 case AUL_UPDATE_REQUESTED:
1166 _DBG("[APP %d] AUL event: AUL_UPDATE_REQUESTED", getpid());
1167 __invoke_callback((void *)&dummy, APPCORE_BASE_EVENT_UPDATE_REQUESTED);
1170 _DBG("[APP %d] AUL event: %d", getpid(), type);
1178 EXPORT_API int appcore_base_on_create(void)
1182 r = aul_launch_init(__context.ops.receive, NULL);
1184 _ERR("Aul init failed: %d", r);
1188 r = aul_launch_argv_handler(__context.argc, __context.argv);
1190 _ERR("Aul argv handler failed: %d", r);
1197 EXPORT_API int appcore_base_on_control(bundle *b)
1202 EXPORT_API int appcore_base_on_terminate()
1209 EXPORT_API void appcore_base_on_set_event(enum appcore_base_event event)
1212 case APPCORE_BASE_EVENT_LOW_BATTERY:
1213 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1215 case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
1216 __register_rotation_changed_event();
1218 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
1227 EXPORT_API void appcore_base_on_unset_event(enum appcore_base_event event)
1230 case APPCORE_BASE_EVENT_LOW_BATTERY:
1231 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1233 case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
1234 __unregister_rotation_changed_event();
1236 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
1243 EXPORT_API appcore_base_event_h appcore_base_add_event(enum appcore_base_event event,
1244 appcore_base_event_cb cb, void *data)
1246 appcore_base_event_node *node;
1248 if (__context.dirty && !__exist_callback(event)) {
1249 if (__context.ops.set_event)
1250 __context.ops.set_event(event, __context.data);
1253 node = malloc(sizeof(appcore_base_event_node));
1261 __events = g_list_append(__events, node);
1266 EXPORT_API int appcore_base_remove_event(appcore_base_event_h handle)
1268 appcore_base_event_node *node = handle;
1269 enum appcore_base_event event;
1271 if (!node || !g_list_find(__events, node))
1275 __events = g_list_remove(__events, node);
1277 if (__context.dirty && !__exist_callback(event)) {
1278 if (__context.ops.unset_event)
1279 __context.ops.unset_event(event, __context.data);
1285 EXPORT_API int appcore_base_raise_event(void *event, enum appcore_base_event type)
1287 __invoke_callback(event, type);
1291 EXPORT_API int appcore_base_get_rotation_state(enum appcore_base_rm *curr)
1296 if (!__rotation.ref)
1299 *curr = __rotation.rm;
1303 EXPORT_API bool appcore_base_is_bg_allowed(void)
1305 return __context.allowed_bg;
1308 EXPORT_API bool appcore_base_is_suspended(void)
1310 return __context.suspended_state;
1313 EXPORT_API void appcore_base_toggle_suspended_state(void)
1315 __context.suspended_state ^= __context.suspended_state;
1318 EXPORT_API void appcore_base_exit(void)
1320 if (__context.ops.exit)
1321 __context.ops.exit(__context.data);
1324 EXPORT_API void appcore_base_add_suspend_timer(void)
1326 __add_suspend_timer();
1329 EXPORT_API void appcore_base_remove_suspend_timer(void)
1331 __remove_suspend_timer();
1334 static int __on_receive(aul_type type, bundle *b, void *data)
1336 return appcore_base_on_receive(type, b);
1339 static int __on_create(void *data)
1341 return appcore_base_on_create();
1344 static int __on_control(bundle *b, void *data)
1346 return appcore_base_on_control(b);
1349 static int __on_terminate(void *data)
1351 return appcore_base_on_terminate();
1354 static int __on_set_i18n(void *data)
1356 return appcore_base_on_set_i18n();
1359 static void __on_set_event(enum appcore_base_event event, void *data)
1361 return appcore_base_on_set_event(event);
1364 static void __on_unset_event(enum appcore_base_event event, void *data)
1366 return appcore_base_on_unset_event(event);
1369 EXPORT_API appcore_base_ops appcore_base_get_default_ops(void)
1371 appcore_base_ops ops;
1373 ops.create = __on_create;
1374 ops.control = __on_control;
1375 ops.terminate = __on_terminate;
1376 ops.receive = __on_receive;
1377 ops.set_i18n = __on_set_i18n;
1382 ops.set_event = __on_set_event;
1383 ops.unset_event = __on_unset_event;