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"
43 int __appcore_init_suspend_dbus_handler(void *data);
44 void __appcore_fini_suspend_dbus_handler(void);
47 #define SQLITE_FLUSH_MAX (1024*1024)
49 #define PATH_LOCALE "locale"
51 static struct appcore core;
54 static enum appcore_event to_ae[SE_MAX] = {
55 APPCORE_EVENT_UNKNOWN, /* SE_UNKNOWN */
56 APPCORE_EVENT_LOW_MEMORY, /* SE_LOWMEM */
57 APPCORE_EVENT_LOW_BATTERY, /* SE_LOWBAT */
58 APPCORE_EVENT_LANG_CHANGE, /* SE_LANGCGH */
59 APPCORE_EVENT_REGION_CHANGE,
60 APPCORE_EVENT_SUSPENDED_STATE_CHANGE,
63 static int appcore_event_initialized[SE_MAX] = {0,};
65 enum cb_type { /* callback */
75 enum appcore_event sys;
80 int (*cb_pre) (void *);
82 int (*cb_post) (void *);
84 int (*vcb_pre) (void *, void *);
85 int (*vcb) (void *, void *);
86 int (*vcb_post) (void *, void *);
90 int (*callback) (void *);
94 static struct open_s open;
96 static int __app_terminate(void *data);
97 static int __app_resume(void *data);
98 static int __app_reset(void *data, bundle *k);
100 static int __sys_lowmem_post(void *data, void *evt);
101 static int __sys_lowmem(void *data, void *evt);
102 static int __sys_lowbatt(void *data, void *evt);
103 static int __sys_langchg_pre(void *data, void *evt);
104 static int __sys_langchg(void *data, void *evt);
105 static int __sys_regionchg_pre(void *data, void *evt);
106 static int __sys_regionchg(void *data, void *evt);
107 extern void aul_finalize();
110 static struct evt_ops evtops[] = {
113 .key.vkey = VCONFKEY_SYSMAN_LOW_MEMORY,
114 .vcb_post = __sys_lowmem_post,
119 .key.vkey = VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
120 .vcb = __sys_lowbatt,
124 .key.vkey = VCONFKEY_LANGSET,
125 .vcb_pre = __sys_langchg_pre,
126 .vcb = __sys_langchg,
130 .key.vkey = VCONFKEY_REGIONFORMAT,
131 .vcb_pre = __sys_regionchg_pre,
132 .vcb = __sys_regionchg,
136 .key.vkey = VCONFKEY_REGIONFORMAT_TIME1224,
137 .vcb = __sys_regionchg,
141 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
142 static GDBusConnection *bus;
143 static guint __suspend_dbus_handler_initialized;
146 static int __get_locale_resource_dir(char *locale_dir, int size)
148 const char *res_path;
150 res_path = aul_get_app_resource_path();
151 if (res_path == NULL) {
152 _ERR("Failed to get resource path");
156 snprintf(locale_dir, size, "%s" PATH_LOCALE, res_path);
157 if (access(locale_dir, R_OK) != 0)
163 static int __app_terminate(void *data)
165 struct appcore *ac = data;
167 _retv_if(ac == NULL || ac->ops == NULL, -1);
168 _retv_if(ac->ops->cb_app == NULL, 0);
170 ac->ops->cb_app(AE_TERMINATE, ac->ops->data, NULL);
175 static int __bgapp_terminate(void *data)
177 struct appcore *ac = data;
179 _retv_if(ac == NULL || ac->ops == NULL, -1);
180 _retv_if(ac->ops->cb_app == NULL, 0);
182 ac->ops->cb_app(AE_TERMINATE_BGAPP, ac->ops->data, NULL);
187 static gboolean __prt_ltime(gpointer data)
191 msec = appcore_measure_time_from(NULL);
193 _DBG("[APP %d] first idle after reset: %d msec", _pid, msec);
198 static int __app_reset(void *data, bundle * k)
200 struct appcore *ac = data;
201 _retv_if(ac == NULL || ac->ops == NULL, -1);
202 _retv_if(ac->ops->cb_app == NULL, 0);
204 g_idle_add(__prt_ltime, ac);
206 ac->ops->cb_app(AE_RESET, ac->ops->data, k);
211 static int __app_resume(void *data)
213 struct appcore *ac = data;
214 _retv_if(ac == NULL || ac->ops == NULL, -1);
215 _retv_if(ac->ops->cb_app == NULL, 0);
217 ac->ops->cb_app(AE_RAISE, ac->ops->data, NULL);
221 static int __app_pause(void *data)
223 struct appcore *ac = data;
224 _retv_if(ac == NULL || ac->ops == NULL, -1);
225 _retv_if(ac->ops->cb_app == NULL, 0);
227 ac->ops->cb_app(AE_LOWER, ac->ops->data, NULL);
231 static int __sys_do_default(struct appcore *ac, enum sys_event event)
237 /*r = __def_lowbatt(ac);*/
248 static int __sys_do(struct appcore *ac, void *event_info, enum sys_event event)
252 _retv_if(ac == NULL || event >= SE_MAX, -1);
254 op = &ac->sops[event];
256 if (op->func == NULL)
257 return __sys_do_default(ac, event);
259 return op->func(event_info, op->data);
262 static int __sys_lowmem_post(void *data, void *evt)
264 keynode_t *key = evt;
267 val = vconf_keynode_get_int(key);
269 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
270 #if defined(MEMORY_FLUSH_ACTIVATE)
271 struct appcore *ac = data;
272 ac->ops->cb_app(AE_LOWMEM_POST, ac->ops->data, NULL);
280 static int __sys_lowmem(void *data, void *evt)
282 keynode_t *key = evt;
285 val = vconf_keynode_get_int(key);
287 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING)
288 return __sys_do(data, (void *)&val, SE_LOWMEM);
293 static int __sys_lowbatt(void *data, void *evt)
295 keynode_t *key = evt;
298 val = vconf_keynode_get_int(key);
300 /* VCONFKEY_SYSMAN_BAT_CRITICAL_LOW or VCONFKEY_SYSMAN_POWER_OFF */
301 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
302 return __sys_do(data, (void *)&val, SE_LOWBAT);
307 static int __sys_langchg_pre(void *data, void *evt)
313 static int __sys_langchg(void *data, void *evt)
315 keynode_t *key = evt;
318 val = vconf_keynode_get_str(key);
320 return __sys_do(data, (void *)val, SE_LANGCHG);
323 static int __sys_regionchg_pre(void *data, void *evt)
329 static int __sys_regionchg(void *data, void *evt)
331 keynode_t *key = evt;
335 name = vconf_keynode_get_name(key);
336 if (!strcmp(name, VCONFKEY_REGIONFORMAT))
337 val = vconf_keynode_get_str(key);
339 return __sys_do(data, (void *)val, SE_REGIONCHG);
342 static void __vconf_do(struct evt_ops *eo, keynode_t * key, void *data)
347 eo->vcb_pre(data, key);
353 eo->vcb_post(data, key);
356 static void __vconf_cb(keynode_t *key, void *data)
361 name = vconf_keynode_get_name(key);
362 _ret_if(name == NULL);
364 _DBG("[APP %d] vconf changed: %s", _pid, name);
366 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
367 struct evt_ops *eo = &evtops[i];
371 if (!strcmp(name, eo->key.vkey))
372 __vconf_do(eo, key, data);
381 static int __add_vconf(struct appcore *ac, enum sys_event se)
387 r = vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __vconf_cb, ac);
390 r = vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __vconf_cb, ac);
393 r = vconf_notify_key_changed(VCONFKEY_LANGSET, __vconf_cb, ac);
396 r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __vconf_cb, ac);
400 r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __vconf_cb, ac);
410 static int __del_vconf(enum sys_event se)
416 r = vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __vconf_cb);
419 r = vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __vconf_cb);
422 r = vconf_ignore_key_changed(VCONFKEY_LANGSET, __vconf_cb);
425 r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __vconf_cb);
429 r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __vconf_cb);
439 static int __del_vconf_list(void)
444 for (se = SE_LOWMEM; se < SE_MAX; se++) {
445 if (appcore_event_initialized[se]) {
448 _ERR("Delete vconf callback failed");
450 appcore_event_initialized[se] = 0;
457 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
458 static gboolean __flush_memory(gpointer data)
460 int suspend = APPCORE_SUSPENDED_STATE_WILL_ENTER_SUSPEND;
461 struct appcore *ac = (struct appcore *)data;
463 appcore_flush_memory();
470 if (!ac->allowed_bg && !ac->suspended_state) {
471 _DBG("[__SUSPEND__] flush case");
472 __sys_do(ac, &suspend, SE_SUSPENDED_STATE);
473 ac->suspended_state = true;
479 static void __add_suspend_timer(struct appcore *ac)
481 ac->tid = g_timeout_add_seconds(5, __flush_memory, ac);
484 static void __remove_suspend_timer(struct appcore *ac)
487 g_source_remove(ac->tid);
493 static int __aul_handler(aul_type type, bundle *b, void *data)
496 const char **tep_path = NULL;
499 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
500 const char *bg = NULL;
501 struct appcore *ac = data;
506 _DBG("[APP %d] AUL event: AUL_START", _pid);
507 tep_path = bundle_get_str_array(b, AUL_TEP_PATH, &len);
509 for (i = 0; i < len; i++) {
510 ret = aul_check_tep_mount(tep_path[i]);
512 _ERR("mount request not completed within 1 sec");
518 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
519 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
520 if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
521 _DBG("[__SUSPEND__] allowed background");
522 ac->allowed_bg = true;
523 __remove_suspend_timer(data);
527 __app_reset(data, b);
530 _DBG("[APP %d] AUL event: AUL_RESUME", _pid);
531 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
532 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
533 if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
534 _DBG("[__SUSPEND__] allowed background");
535 ac->allowed_bg = true;
536 __remove_suspend_timer(data);
541 ret = open.callback(open.cbdata);
549 _DBG("[APP %d] AUL event: AUL_TERMINATE", _pid);
550 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
552 __remove_suspend_timer(data);
555 __app_terminate(data);
557 case AUL_TERMINATE_BGAPP:
558 _DBG("[APP %d] AUL event: AUL_TERMINATE_BGAPP", _pid);
559 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
561 __remove_suspend_timer(data);
564 __bgapp_terminate(data);
567 _DBG("[APP %d] AUL event: AUL_PAUSE", _pid);
570 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
572 _DBG("[APP %d] AUL event: AUL_WAKE", _pid);
573 if (!ac->allowed_bg && ac->suspended_state) {
574 int suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
575 __remove_suspend_timer(data);
576 __sys_do(ac, &suspend, SE_SUSPENDED_STATE);
577 ac->suspended_state = false;
581 _DBG("[APP %d] AUL event: AUL_SUSPEND", _pid);
582 if (!ac->allowed_bg && !ac->suspended_state) {
583 __remove_suspend_timer(data);
584 __flush_memory((gpointer)ac);
589 _DBG("[APP %d] AUL event: %d", _pid, type);
598 static void __clear(struct appcore *ac)
600 memset(ac, 0, sizeof(struct appcore));
603 EXPORT_API void appcore_get_app_core(struct appcore **ac)
608 EXPORT_API int appcore_set_open_cb(int (*cb) (void *),
617 EXPORT_API int appcore_set_event_callback(enum appcore_event event,
618 int (*cb) (void *, void *), void *data)
620 struct appcore *ac = &core;
625 for (se = SE_UNKNOWN; se < SE_MAX; se++) {
626 if (event == to_ae[se])
630 if (se == SE_UNKNOWN || se >= SE_MAX) {
631 _ERR("Unregistered event");
641 if (op->func && !appcore_event_initialized[se]) {
642 r = __add_vconf(ac, se);
644 _ERR("Add vconf callback failed");
646 appcore_event_initialized[se] = 1;
647 } else if (!op->func && appcore_event_initialized[se]) {
650 _ERR("Delete vconf callback failed");
652 appcore_event_initialized[se] = 0;
658 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
659 static gboolean __init_suspend(gpointer data)
663 r = __appcore_init_suspend_dbus_handler(&core);
665 _ERR("Initailzing suspended state handler failed");
672 EXPORT_API int appcore_init(const char *name, const struct ui_ops *ops,
673 int argc, char **argv)
676 char locale_dir[PATH_MAX];
678 if (core.state != 0) {
679 _ERR("Already in use");
684 if (ops == NULL || ops->cb_app == NULL) {
685 _ERR("ops or callback function is null");
690 r = __get_locale_resource_dir(locale_dir, sizeof(locale_dir));
691 r = set_i18n(name, locale_dir);
692 _retv_if(r == -1, -1);
694 r = aul_launch_init(__aul_handler, &core);
696 _ERR("Aul init failed: %d", r);
700 r = aul_launch_argv_handler(argc, argv);
702 _ERR("Aul argv handler failed: %d", r);
707 core.state = 1; /* TODO: use enum value */
709 core.suspended_state = false;
710 core.allowed_bg = false;
714 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
715 g_idle_add(__init_suspend, NULL);
725 EXPORT_API void appcore_exit(void)
730 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
731 __remove_suspend_timer(&core);
732 __appcore_fini_suspend_dbus_handler();
738 EXPORT_API int appcore_flush_memory(void)
740 int (*flush_fn) (int);
742 struct appcore *ac = &core;
745 _ERR("Appcore not initialized");
749 _DBG("[APP %d] Flushing memory ...", _pid);
752 ac->ops->cb_app(AE_MEM_FLUSH, ac->ops->data, NULL);
754 flush_fn = dlsym(RTLD_DEFAULT, "sqlite3_release_memory");
756 flush_fn(SQLITE_FLUSH_MAX);
760 *Disabled - the impact of stack_trim() is unclear
764 _DBG("[APP %d] Flushing memory DONE", _pid);
769 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
770 static void __suspend_dbus_signal_handler(GDBusConnection *connection,
771 const gchar *sender_name,
772 const gchar *object_path,
773 const gchar *interface_name,
774 const gchar *signal_name,
775 GVariant *parameters,
778 struct appcore *ac = (struct appcore *)user_data;
779 gint suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
783 if (g_strcmp0(signal_name, RESOURCED_FREEZER_SIGNAL) == 0) {
784 g_variant_get(parameters, "(ii)", &status, &pid);
785 if (pid == getpid() && status == 0) { /* thawed */
786 if (ac && !ac->allowed_bg && ac->suspended_state) {
787 __remove_suspend_timer(ac);
788 __sys_do(ac, &suspend, SE_SUSPENDED_STATE);
789 ac->suspended_state = false;
790 __add_suspend_timer(ac);
796 int __appcore_init_suspend_dbus_handler(void *data)
800 if (__suspend_dbus_handler_initialized)
804 bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
806 _ERR("Failed to connect to the D-BUS daemon: %s",
813 __suspend_dbus_handler_initialized = g_dbus_connection_signal_subscribe(
816 RESOURCED_FREEZER_INTERFACE,
817 RESOURCED_FREEZER_SIGNAL,
818 RESOURCED_FREEZER_PATH,
820 G_DBUS_SIGNAL_FLAGS_NONE,
821 __suspend_dbus_signal_handler,
824 if (__suspend_dbus_handler_initialized == 0) {
825 _ERR("g_dbus_connection_signal_subscribe() is failed.");
829 _DBG("[__SUSPEND__] suspend signal initialized");
834 void __appcore_fini_suspend_dbus_handler(void)
839 if (__suspend_dbus_handler_initialized) {
840 g_dbus_connection_signal_unsubscribe(bus,
841 __suspend_dbus_handler_initialized);
842 __suspend_dbus_handler_initialized = 0;