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;
502 int suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
507 _DBG("[APP %d] AUL event: AUL_START", _pid);
508 tep_path = bundle_get_str_array(b, AUL_TEP_PATH, &len);
510 for (i = 0; i < len; i++) {
511 ret = aul_check_tep_mount(tep_path[i]);
513 _ERR("mount request not completed within 1 sec");
519 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
520 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
521 if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
522 _DBG("[__SUSPEND__] allowed background");
523 ac->allowed_bg = true;
524 __remove_suspend_timer(data);
528 __app_reset(data, b);
531 _DBG("[APP %d] AUL event: AUL_RESUME", _pid);
532 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
533 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
534 if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
535 _DBG("[__SUSPEND__] allowed background");
536 ac->allowed_bg = true;
537 __remove_suspend_timer(data);
542 ret = open.callback(open.cbdata);
550 _DBG("[APP %d] AUL event: AUL_TERMINATE", _pid);
551 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
553 __remove_suspend_timer(data);
556 __app_terminate(data);
558 case AUL_TERMINATE_BGAPP:
559 _DBG("[APP %d] AUL event: AUL_TERMINATE_BGAPP", _pid);
560 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
562 __remove_suspend_timer(data);
565 __bgapp_terminate(data);
568 _DBG("[APP %d] AUL event: AUL_PAUSE", _pid);
571 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
573 _DBG("[APP %d] AUL event: AUL_WAKE", _pid);
574 if (!ac->allowed_bg && ac->suspended_state) {
575 __remove_suspend_timer(data);
576 __sys_do(ac, &suspend, SE_SUSPENDED_STATE);
577 ac->suspended_state = false;
581 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
582 if (bg && strcmp(bg, "ALLOWED_BG") == 0) {
583 _DBG("[__SUSPEND__] allowed background");
584 ac->allowed_bg = true;
589 _DBG("[APP %d] AUL event: AUL_SUSPEND", _pid);
590 ac->allowed_bg = false;
591 if (!ac->suspended_state) {
592 __remove_suspend_timer(data);
593 __flush_memory((gpointer)ac);
598 _DBG("[APP %d] AUL event: %d", _pid, type);
607 static void __clear(struct appcore *ac)
609 memset(ac, 0, sizeof(struct appcore));
612 EXPORT_API void appcore_get_app_core(struct appcore **ac)
617 EXPORT_API int appcore_set_open_cb(int (*cb) (void *),
626 EXPORT_API int appcore_set_event_callback(enum appcore_event event,
627 int (*cb) (void *, void *), void *data)
629 struct appcore *ac = &core;
634 for (se = SE_UNKNOWN; se < SE_MAX; se++) {
635 if (event == to_ae[se])
639 if (se == SE_UNKNOWN || se >= SE_MAX) {
640 _ERR("Unregistered event");
650 if (op->func && !appcore_event_initialized[se]) {
651 r = __add_vconf(ac, se);
653 _ERR("Add vconf callback failed");
655 appcore_event_initialized[se] = 1;
656 } else if (!op->func && appcore_event_initialized[se]) {
659 _ERR("Delete vconf callback failed");
661 appcore_event_initialized[se] = 0;
667 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
668 static gboolean __init_suspend(gpointer data)
672 r = __appcore_init_suspend_dbus_handler(&core);
674 _ERR("Initailzing suspended state handler failed");
681 EXPORT_API int appcore_init(const char *name, const struct ui_ops *ops,
682 int argc, char **argv)
685 char locale_dir[PATH_MAX];
687 if (core.state != 0) {
688 _ERR("Already in use");
693 if (ops == NULL || ops->cb_app == NULL) {
694 _ERR("ops or callback function is null");
699 r = __get_locale_resource_dir(locale_dir, sizeof(locale_dir));
700 r = set_i18n(name, locale_dir);
701 _retv_if(r == -1, -1);
703 r = aul_launch_init(__aul_handler, &core);
705 _ERR("Aul init failed: %d", r);
709 r = aul_launch_argv_handler(argc, argv);
711 _ERR("Aul argv handler failed: %d", r);
716 core.state = 1; /* TODO: use enum value */
718 core.suspended_state = false;
719 core.allowed_bg = false;
723 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
724 g_idle_add(__init_suspend, NULL);
734 EXPORT_API void appcore_exit(void)
739 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
740 __remove_suspend_timer(&core);
741 __appcore_fini_suspend_dbus_handler();
747 EXPORT_API int appcore_flush_memory(void)
749 int (*flush_fn) (int);
751 struct appcore *ac = &core;
754 _ERR("Appcore not initialized");
758 _DBG("[APP %d] Flushing memory ...", _pid);
761 ac->ops->cb_app(AE_MEM_FLUSH, ac->ops->data, NULL);
763 flush_fn = dlsym(RTLD_DEFAULT, "sqlite3_release_memory");
765 flush_fn(SQLITE_FLUSH_MAX);
769 *Disabled - the impact of stack_trim() is unclear
773 _DBG("[APP %d] Flushing memory DONE", _pid);
778 #ifdef _APPFW_FEATURE_BACKGROUND_MANAGEMENT
779 static void __suspend_dbus_signal_handler(GDBusConnection *connection,
780 const gchar *sender_name,
781 const gchar *object_path,
782 const gchar *interface_name,
783 const gchar *signal_name,
784 GVariant *parameters,
787 struct appcore *ac = (struct appcore *)user_data;
788 gint suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
792 if (g_strcmp0(signal_name, RESOURCED_FREEZER_SIGNAL) == 0) {
793 g_variant_get(parameters, "(ii)", &status, &pid);
794 if (pid == getpid() && status == 0) { /* thawed */
795 if (ac && !ac->allowed_bg && ac->suspended_state) {
796 __remove_suspend_timer(ac);
797 __sys_do(ac, &suspend, SE_SUSPENDED_STATE);
798 ac->suspended_state = false;
799 __add_suspend_timer(ac);
805 int __appcore_init_suspend_dbus_handler(void *data)
809 if (__suspend_dbus_handler_initialized)
813 bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
815 _ERR("Failed to connect to the D-BUS daemon: %s",
822 __suspend_dbus_handler_initialized = g_dbus_connection_signal_subscribe(
825 RESOURCED_FREEZER_INTERFACE,
826 RESOURCED_FREEZER_SIGNAL,
827 RESOURCED_FREEZER_PATH,
829 G_DBUS_SIGNAL_FLAGS_NONE,
830 __suspend_dbus_signal_handler,
833 if (__suspend_dbus_handler_initialized == 0) {
834 _ERR("g_dbus_connection_signal_subscribe() is failed.");
838 _DBG("[__SUSPEND__] suspend signal initialized");
843 void __appcore_fini_suspend_dbus_handler(void)
848 if (__suspend_dbus_handler_initialized) {
849 g_dbus_connection_signal_unsubscribe(bus,
850 __suspend_dbus_handler_initialized);
851 __suspend_dbus_handler_initialized = 0;