2 * Copyright (c) 2000 - 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.
23 #include <sys/types.h>
27 #include <linux/limits.h>
33 #include <bundle_internal.h>
34 #include "appcore-internal.h"
36 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
39 #define RESOURCED_FREEZER_PATH "/Org/Tizen/Resourced/Freezer"
40 #define RESOURCED_FREEZER_INTERFACE "org.tizen.resourced.freezer"
41 #define RESOURCED_FREEZER_SIGNAL "FreezerState"
44 #define SQLITE_FLUSH_MAX (1024*1024)
46 #define PATH_LOCALE "locale"
48 static struct appcore core;
51 static enum appcore_event to_ae[SE_MAX] = {
52 APPCORE_EVENT_UNKNOWN, /* SE_UNKNOWN */
53 APPCORE_EVENT_LOW_MEMORY, /* SE_LOWMEM */
54 APPCORE_EVENT_LOW_BATTERY, /* SE_LOWBAT */
55 APPCORE_EVENT_LANG_CHANGE, /* SE_LANGCGH */
56 APPCORE_EVENT_REGION_CHANGE,
57 APPCORE_EVENT_SUSPENDED_STATE_CHANGE,
60 static int appcore_event_initialized[SE_MAX] = {0,};
62 enum cb_type { /* callback */
72 enum appcore_event sys;
77 int (*cb_pre) (void *);
79 int (*cb_post) (void *);
81 int (*vcb_pre) (void *, void *);
82 int (*vcb) (void *, void *);
83 int (*vcb_post) (void *, void *);
87 int (*callback) (void *);
91 static struct open_s open;
93 static int __app_terminate(void *data);
94 static int __app_resume(void *data);
95 static int __app_reset(void *data, bundle *k);
97 static int __sys_lowmem_post(void *data, void *evt);
98 static int __sys_lowmem(void *data, void *evt);
99 static int __sys_lowbatt(void *data, void *evt);
100 static int __sys_langchg_pre(void *data, void *evt);
101 static int __sys_langchg(void *data, void *evt);
102 static int __sys_regionchg_pre(void *data, void *evt);
103 static int __sys_regionchg(void *data, void *evt);
104 extern void aul_finalize();
107 static struct evt_ops evtops[] = {
110 .key.vkey = VCONFKEY_SYSMAN_LOW_MEMORY,
111 .vcb_post = __sys_lowmem_post,
116 .key.vkey = VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
117 .vcb = __sys_lowbatt,
121 .key.vkey = VCONFKEY_LANGSET,
122 .vcb_pre = __sys_langchg_pre,
123 .vcb = __sys_langchg,
127 .key.vkey = VCONFKEY_REGIONFORMAT,
128 .vcb_pre = __sys_regionchg_pre,
129 .vcb = __sys_regionchg,
133 .key.vkey = VCONFKEY_REGIONFORMAT_TIME1224,
134 .vcb = __sys_regionchg,
138 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
139 static GDBusConnection *bus = NULL;
140 static guint __suspend_dbus_handler_initialized = 0;
143 static int __get_locale_resource_dir(char *locale_dir, int size)
145 const char *res_path;
147 res_path = aul_get_app_resource_path();
148 if (res_path == NULL) {
149 _ERR("Failed to get resource path");
153 snprintf(locale_dir, size, "%s" PATH_LOCALE, res_path);
154 if (access(locale_dir, R_OK) != 0)
160 static int __app_terminate(void *data)
162 struct appcore *ac = data;
164 _retv_if(ac == NULL || ac->ops == NULL, -1);
165 _retv_if(ac->ops->cb_app == NULL, 0);
167 ac->ops->cb_app(AE_TERMINATE, ac->ops->data, NULL);
172 static int __bgapp_terminate(void *data)
174 struct appcore *ac = data;
176 _retv_if(ac == NULL || ac->ops == NULL, -1);
177 _retv_if(ac->ops->cb_app == NULL, 0);
179 ac->ops->cb_app(AE_TERMINATE_BGAPP, ac->ops->data, NULL);
184 static gboolean __prt_ltime(gpointer data)
188 msec = appcore_measure_time_from(NULL);
190 _DBG("[APP %d] first idle after reset: %d msec", _pid, msec);
195 static int __app_reset(void *data, bundle * k)
197 struct appcore *ac = data;
198 _retv_if(ac == NULL || ac->ops == NULL, -1);
199 _retv_if(ac->ops->cb_app == NULL, 0);
201 g_idle_add(__prt_ltime, ac);
203 ac->ops->cb_app(AE_RESET, ac->ops->data, k);
208 static int __app_resume(void *data)
210 struct appcore *ac = data;
211 _retv_if(ac == NULL || ac->ops == NULL, -1);
212 _retv_if(ac->ops->cb_app == NULL, 0);
214 ac->ops->cb_app(AE_RAISE, ac->ops->data, NULL);
218 static int __app_pause(void *data)
220 struct appcore *ac = data;
221 _retv_if(ac == NULL || ac->ops == NULL, -1);
222 _retv_if(ac->ops->cb_app == NULL, 0);
224 ac->ops->cb_app(AE_LOWER, ac->ops->data, NULL);
228 static int __sys_do_default(struct appcore *ac, enum sys_event event)
234 /*r = __def_lowbatt(ac);*/
245 static int __sys_do(struct appcore *ac, void *event_info, enum sys_event event)
249 _retv_if(ac == NULL || event >= SE_MAX, -1);
251 op = &ac->sops[event];
253 if (op->func == NULL)
254 return __sys_do_default(ac, event);
256 return op->func(event_info, op->data);
259 static int __sys_lowmem_post(void *data, void *evt)
261 keynode_t *key = evt;
264 val = vconf_keynode_get_int(key);
266 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
267 #if defined(MEMORY_FLUSH_ACTIVATE)
268 struct appcore *ac = data;
269 ac->ops->cb_app(AE_LOWMEM_POST, ac->ops->data, NULL);
277 static int __sys_lowmem(void *data, void *evt)
279 keynode_t *key = evt;
282 val = vconf_keynode_get_int(key);
284 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING)
285 return __sys_do(data, (void *)&val, SE_LOWMEM);
290 static int __sys_lowbatt(void *data, void *evt)
292 keynode_t *key = evt;
295 val = vconf_keynode_get_int(key);
297 /* VCONFKEY_SYSMAN_BAT_CRITICAL_LOW or VCONFKEY_SYSMAN_POWER_OFF */
298 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
299 return __sys_do(data, (void *)&val, SE_LOWBAT);
304 static int __sys_langchg_pre(void *data, void *evt)
310 static int __sys_langchg(void *data, void *evt)
312 keynode_t *key = evt;
315 val = vconf_keynode_get_str(key);
317 return __sys_do(data, (void *)val, SE_LANGCHG);
320 static int __sys_regionchg_pre(void *data, void *evt)
326 static int __sys_regionchg(void *data, void *evt)
328 keynode_t *key = evt;
332 name = vconf_keynode_get_name(key);
333 if (!strcmp(name, VCONFKEY_REGIONFORMAT))
334 val = vconf_keynode_get_str(key);
336 return __sys_do(data, (void *)val, SE_REGIONCHG);
339 static void __vconf_do(struct evt_ops *eo, keynode_t * key, void *data)
344 eo->vcb_pre(data, key);
350 eo->vcb_post(data, key);
353 static void __vconf_cb(keynode_t *key, void *data)
358 name = vconf_keynode_get_name(key);
359 _ret_if(name == NULL);
361 _DBG("[APP %d] vconf changed: %s", _pid, name);
363 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
364 struct evt_ops *eo = &evtops[i];
368 if (!strcmp(name, eo->key.vkey))
369 __vconf_do(eo, key, data);
378 static int __add_vconf(struct appcore *ac, enum sys_event se)
384 r = vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __vconf_cb, ac);
387 r = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __vconf_cb, ac);
390 r = vconf_notify_key_changed(VCONFKEY_LANGSET, __vconf_cb, ac);
393 r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __vconf_cb, ac);
397 r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __vconf_cb, ac);
407 static int __del_vconf(enum sys_event se)
413 r = vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __vconf_cb);
416 r = vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __vconf_cb);
419 r = vconf_ignore_key_changed(VCONFKEY_LANGSET, __vconf_cb);
422 r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __vconf_cb);
426 r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __vconf_cb);
436 static int __del_vconf_list(void)
441 for (se = SE_LOWMEM; se < SE_MAX; se++) {
442 if (appcore_event_initialized[se]) {
445 _ERR("Delete vconf callback failed");
447 appcore_event_initialized[se] = 0;
454 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
455 static gboolean __flush_memory(gpointer data)
457 int suspend = APPCORE_SUSPENDED_STATE_WILL_ENTER_SUSPEND;
458 struct appcore *ac = (struct appcore *)data;
460 appcore_flush_memory();
467 if (!ac->allowed_bg && !ac->suspended_state) {
468 _DBG("[__SUSPEND__] flush case");
469 __sys_do(ac, &suspend, SE_SUSPENDED_STATE);
470 ac->suspended_state = true;
476 static void __add_suspend_timer(struct appcore *ac)
478 ac->tid = g_timeout_add_seconds(5, __flush_memory, ac);
481 static void __remove_suspend_timer(struct appcore *ac)
484 g_source_remove(ac->tid);
490 static int __aul_handler(aul_type type, bundle *b, void *data)
493 const char **tep_path = NULL;
496 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
497 const char *bg = NULL;
498 struct appcore *ac = data;
503 _DBG("[APP %d] AUL event: AUL_START", _pid);
504 tep_path = bundle_get_str_array(b, AUL_TEP_PATH, &len);
506 for (i = 0; i < len; i++) {
507 ret = aul_check_tep_mount(tep_path[i]);
509 _ERR("mount request not completed within 1 sec");
515 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
516 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
517 if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
518 _DBG("[__SUSPEND__] allowed background");
519 ac->allowed_bg = true;
520 __remove_suspend_timer(data);
524 __app_reset(data, b);
527 _DBG("[APP %d] AUL event: AUL_RESUME", _pid);
528 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
529 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
530 if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
531 _DBG("[__SUSPEND__] allowed background");
532 ac->allowed_bg = true;
533 __remove_suspend_timer(data);
538 ret = open.callback(open.cbdata);
546 _DBG("[APP %d] AUL event: AUL_TERMINATE", _pid);
547 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
549 __remove_suspend_timer(data);
552 __app_terminate(data);
554 case AUL_TERMINATE_BGAPP:
555 _DBG("[APP %d] AUL event: AUL_TERMINATE_BGAPP", _pid);
556 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
558 __remove_suspend_timer(data);
561 __bgapp_terminate(data);
564 _DBG("[APP %d] AUL event: AUL_PAUSE", _pid);
567 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
569 _DBG("[APP %d] AUL event: AUL_WAKE", _pid);
570 if (!ac->allowed_bg && ac->suspended_state) {
571 int suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
572 __remove_suspend_timer(data);
573 __sys_do(ac, &suspend, SE_SUSPENDED_STATE);
574 ac->suspended_state = false;
578 _DBG("[APP %d] AUL event: AUL_SUSPEND", _pid);
579 if (!ac->allowed_bg && !ac->suspended_state) {
580 __remove_suspend_timer(data);
581 __flush_memory((gpointer)ac);
586 _DBG("[APP %d] AUL event: %d", _pid, type);
595 static void __clear(struct appcore *ac)
597 memset(ac, 0, sizeof(struct appcore));
600 EXPORT_API void appcore_get_app_core(struct appcore **ac)
605 EXPORT_API int appcore_set_open_cb(int (*cb) (void *),
614 EXPORT_API int appcore_set_event_callback(enum appcore_event event,
615 int (*cb) (void *, void *), void *data)
617 struct appcore *ac = &core;
622 for (se = SE_UNKNOWN; se < SE_MAX; se++) {
623 if (event == to_ae[se])
627 if (se == SE_UNKNOWN || se >= SE_MAX) {
628 _ERR("Unregistered event");
638 if (op->func && !appcore_event_initialized[se]) {
639 r = __add_vconf(ac, se);
641 _ERR("Add vconf callback failed");
643 appcore_event_initialized[se] = 1;
644 } else if (!op->func && appcore_event_initialized[se]) {
647 _ERR("Delete vconf callback failed");
649 appcore_event_initialized[se] = 0;
655 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
656 static gboolean __init_suspend(gpointer data)
660 r = _appcore_init_suspend_dbus_handler(&core);
662 _ERR("Initailzing suspended state handler failed");
669 EXPORT_API int appcore_init(const char *name, const struct ui_ops *ops,
670 int argc, char **argv)
673 char locale_dir[PATH_MAX];
675 if (core.state != 0) {
676 _ERR("Already in use");
681 if (ops == NULL || ops->cb_app == NULL) {
682 _ERR("ops or callback function is null");
687 r = __get_locale_resource_dir(locale_dir, sizeof(locale_dir));
688 r = set_i18n(name, locale_dir);
689 _retv_if(r == -1, -1);
691 r = aul_launch_init(__aul_handler, &core);
693 _ERR("Aul init failed: %d", r);
697 r = aul_launch_argv_handler(argc, argv);
699 _ERR("Aul argv handler failed: %d", r);
704 core.state = 1; /* TODO: use enum value */
706 core.suspended_state = false;
707 core.allowed_bg = false;
711 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
712 g_idle_add(__init_suspend, NULL);
722 EXPORT_API void appcore_exit(void)
727 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
728 __remove_suspend_timer(&core);
734 EXPORT_API int appcore_flush_memory(void)
736 int (*flush_fn) (int);
738 struct appcore *ac = &core;
741 _ERR("Appcore not initialized");
745 _DBG("[APP %d] Flushing memory ...", _pid);
748 ac->ops->cb_app(AE_MEM_FLUSH, ac->ops->data, NULL);
750 flush_fn = dlsym(RTLD_DEFAULT, "sqlite3_release_memory");
752 flush_fn(SQLITE_FLUSH_MAX);
756 *Disabled - the impact of stack_trim() is unclear
760 _DBG("[APP %d] Flushing memory DONE", _pid);
765 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
766 static void __suspend_dbus_signal_handler(GDBusConnection *connection,
767 const gchar *sender_name,
768 const gchar *object_path,
769 const gchar *interface_name,
770 const gchar *signal_name,
771 GVariant *parameters,
774 struct appcore *ac = (struct appcore *)user_data;
775 gint suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
779 if (g_strcmp0(signal_name, RESOURCED_FREEZER_SIGNAL) == 0) {
780 g_variant_get(parameters, "(ii)", &status, &pid);
781 if (pid == getpid() && status == 0) { /* thawed */
782 if (ac && !ac->allowed_bg && ac->suspended_state) {
783 __remove_suspend_timer(ac);
784 __sys_do(ac, &suspend, SE_SUSPENDED_STATE);
785 ac->suspended_state = false;
786 __add_suspend_timer(ac);
792 int _appcore_init_suspend_dbus_handler(void *data)
796 if (__suspend_dbus_handler_initialized)
800 bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
802 _ERR("Failed to connect to the D-BUS daemon: %s",
809 __suspend_dbus_handler_initialized = g_dbus_connection_signal_subscribe(
812 RESOURCED_FREEZER_INTERFACE,
813 RESOURCED_FREEZER_SIGNAL,
814 RESOURCED_FREEZER_PATH,
816 G_DBUS_SIGNAL_FLAGS_NONE,
817 __suspend_dbus_signal_handler,
820 if (__suspend_dbus_handler_initialized == 0) {
821 _ERR("g_dbus_connection_signal_subscribe() is failed.");
825 _DBG("[__SUSPEND__] suspend signal initialized");