4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
28 #include <sys/types.h>
32 #include <linux/limits.h>
38 #include <tzplatform_config.h>
39 #include "appcore-internal.h"
41 #define SQLITE_FLUSH_MAX (1024*1024)
43 #define PKGNAME_MAX 256
44 #define PATH_APP_ROOT tzplatform_getenv(TZ_USER_APP)
45 #define PATH_RO_APP_ROOT tzplatform_getenv(TZ_SYS_RO_APP)
46 #define PATH_RES "/res"
47 #define PATH_LOCALE "/locale"
49 static struct appcore core;
52 static enum appcore_event to_ae[SE_MAX] = {
53 APPCORE_EVENT_UNKNOWN, /* SE_UNKNOWN */
54 APPCORE_EVENT_LOW_MEMORY, /* SE_LOWMEM */
55 APPCORE_EVENT_LOW_BATTERY, /* SE_LOWBAT */
56 APPCORE_EVENT_LANG_CHANGE, /* SE_LANGCGH */
57 APPCORE_EVENT_REGION_CHANGE,
61 enum cb_type { /* callback */
71 enum appcore_event sys;
76 int (*cb_pre) (void *);
78 int (*cb_post) (void *);
80 int (*vcb_pre) (void *, void *);
81 int (*vcb) (void *, void *);
82 int (*vcb_post) (void *, void *);
86 int (*callback) (void *);
90 static struct open_s open;
92 static int __app_terminate(void *data);
93 static int __app_resume(void *data);
94 static int __app_reset(void *data, bundle *k);
96 static int __sys_lowmem_post(void *data, void *evt);
97 static int __sys_lowmem(void *data, void *evt);
98 static int __sys_lowbatt(void *data, void *evt);
99 static int __sys_langchg_pre(void *data, void *evt);
100 static int __sys_langchg(void *data, void *evt);
101 static int __sys_regionchg_pre(void *data, void *evt);
102 static int __sys_regionchg(void *data, void *evt);
103 extern void aul_finalize();
106 static struct evt_ops evtops[] = {
109 .key.vkey = VCONFKEY_SYSMAN_LOW_MEMORY,
110 .vcb_post = __sys_lowmem_post,
115 .key.vkey = VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
116 .vcb = __sys_lowbatt,
120 .key.vkey = VCONFKEY_LANGSET,
121 .vcb_pre = __sys_langchg_pre,
122 .vcb = __sys_langchg,
126 .key.vkey = VCONFKEY_REGIONFORMAT,
127 .vcb_pre = __sys_regionchg_pre,
128 .vcb = __sys_regionchg,
132 .key.vkey = VCONFKEY_REGIONFORMAT_TIME1224,
133 .vcb = __sys_regionchg,
137 static int __get_dir_name(char *dirname)
139 char pkg_name[PKGNAME_MAX];
147 if (aul_app_get_pkgname_bypid(pid, pkg_name, PKGNAME_MAX) != AUL_R_OK)
150 r = snprintf(dirname, PATH_MAX, "%s/%s" PATH_RES PATH_LOCALE,
151 PATH_APP_ROOT, pkg_name);
154 if (access(dirname, R_OK) == 0) return 0;
155 r = snprintf(dirname, PATH_MAX, "%s/%s" PATH_RES PATH_LOCALE,
156 PATH_RO_APP_ROOT, pkg_name);
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)
386 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
387 struct evt_ops *eo = &evtops[i];
391 r = vconf_notify_key_changed(eo->key.vkey, __vconf_cb,
403 static int __del_vconf(void)
408 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
409 struct evt_ops *eo = &evtops[i];
413 r = vconf_ignore_key_changed(eo->key.vkey, __vconf_cb);
424 static int __aul_handler(aul_type type, bundle *b, void *data)
430 _DBG("[APP %d] AUL event: AUL_START", _pid);
431 __app_reset(data, b);
434 _DBG("[APP %d] AUL event: AUL_RESUME", _pid);
436 ret = open.callback(open.cbdata);
444 _DBG("[APP %d] AUL event: AUL_TERMINATE", _pid);
445 __app_terminate(data);
447 case AUL_TERMINATE_BGAPP:
448 _DBG("[APP %d] AUL event: AUL_TERMINATE_BGAPP", _pid);
449 __bgapp_terminate(data);
452 _DBG("[APP %d] AUL event: AUL_PAUSE", _pid);
456 _DBG("[APP %d] AUL event: %d", _pid, type);
465 static void __clear(struct appcore *ac)
467 memset(ac, 0, sizeof(struct appcore));
470 EXPORT_API int appcore_set_open_cb(int (*cb) (void *),
479 EXPORT_API int appcore_set_event_callback(enum appcore_event event,
480 int (*cb) (void *, void *), void *data)
482 struct appcore *ac = &core;
486 for (se = SE_UNKNOWN; se < SE_MAX; se++) {
487 if (event == to_ae[se])
491 if (se == SE_UNKNOWN || se >= SE_MAX) {
492 _ERR("Unregistered event");
507 EXPORT_API int appcore_init(const char *name, const struct ui_ops *ops,
508 int argc, char **argv)
511 char dirname[PATH_MAX];
513 if (core.state != 0) {
514 _ERR("Already in use");
519 if (ops == NULL || ops->cb_app == NULL) {
520 _ERR("ops or callback function is null");
525 r = __get_dir_name(dirname);
526 r = set_i18n(name, dirname);
527 _retv_if(r == -1, -1);
529 r = __add_vconf(&core);
531 _ERR("Add vconf callback failed");
535 r = aul_launch_init(__aul_handler, &core);
537 _ERR("Aul init failed: %d", r);
541 r = aul_launch_argv_handler(argc, argv);
543 _ERR("Aul argv handler failed: %d", r);
548 core.state = 1; /* TODO: use enum value */
559 EXPORT_API void appcore_exit(void)
568 EXPORT_API int appcore_flush_memory(void)
570 int (*flush_fn) (int);
573 struct appcore *ac = &core;
576 _ERR("Appcore not initialized");
580 _DBG("[APP %d] Flushing memory ...", _pid);
582 if (ac->ops->cb_app) {
583 ac->ops->cb_app(AE_MEM_FLUSH, ac->ops->data, NULL);
586 flush_fn = dlsym(RTLD_DEFAULT, "sqlite3_release_memory");
588 size = flush_fn(SQLITE_FLUSH_MAX);
593 *Disabled - the impact of stack_trim() is unclear
597 _DBG("[APP %d] Flushing memory DONE", _pid);