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 "appcore-internal.h"
40 #define SQLITE_FLUSH_MAX (1024*1024)
42 #define PKGNAME_MAX 256
43 #define PATH_APP_ROOT "/opt/apps"
44 #define PATH_RO_APP_ROOT "/usr/apps"
45 #define PATH_RES "/res"
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,
60 enum cb_type { /* callback */
70 enum appcore_event sys;
75 int (*cb_pre) (void *);
77 int (*cb_post) (void *);
79 int (*vcb_pre) (void *, void *);
80 int (*vcb) (void *, void *);
81 int (*vcb_post) (void *, void *);
85 int (*callback) (void *);
89 static struct open_s open;
91 static int __app_terminate(void *data);
92 static int __app_resume(void *data);
93 static int __app_reset(void *data, bundle *k);
95 static int __sys_lowmem_post(void *data, void *evt);
96 static int __sys_lowmem(void *data, void *evt);
97 static int __sys_lowbatt(void *data, void *evt);
98 static int __sys_langchg_pre(void *data, void *evt);
99 static int __sys_langchg(void *data, void *evt);
100 static int __sys_regionchg_pre(void *data, void *evt);
101 static int __sys_regionchg(void *data, void *evt);
102 extern void aul_finalize();
105 static struct evt_ops evtops[] = {
108 .key.vkey = VCONFKEY_SYSMAN_LOW_MEMORY,
109 .vcb_post = __sys_lowmem_post,
114 .key.vkey = VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
115 .vcb = __sys_lowbatt,
119 .key.vkey = VCONFKEY_LANGSET,
120 .vcb_pre = __sys_langchg_pre,
121 .vcb = __sys_langchg,
125 .key.vkey = VCONFKEY_REGIONFORMAT,
126 .vcb_pre = __sys_regionchg_pre,
127 .vcb = __sys_regionchg,
131 .key.vkey = VCONFKEY_REGIONFORMAT_TIME1224,
132 .vcb = __sys_regionchg,
136 static int __get_dir_name(char *dirname)
138 char pkg_name[PKGNAME_MAX];
146 aul_app_get_pkgname_bypid(pid, pkg_name, PKGNAME_MAX);
148 r = snprintf(dirname, PATH_MAX, PATH_APP_ROOT "/%s" PATH_RES PATH_LOCALE,pkg_name);
151 if (access(dirname, R_OK) == 0) return 0;
152 r = snprintf(dirname, PATH_MAX, PATH_RO_APP_ROOT "/%s" PATH_RES PATH_LOCALE,pkg_name);
159 static int __app_terminate(void *data)
161 struct appcore *ac = data;
163 _retv_if(ac == NULL || ac->ops == NULL, -1);
164 _retv_if(ac->ops->cb_app == NULL, 0);
166 ac->ops->cb_app(AE_TERMINATE, ac->ops->data, NULL);
171 static gboolean __prt_ltime(gpointer data)
175 msec = appcore_measure_time_from(NULL);
177 _DBG("[APP %d] first idle after reset: %d msec", _pid, msec);
182 static int __app_reset(void *data, bundle * k)
184 struct appcore *ac = data;
185 _retv_if(ac == NULL || ac->ops == NULL, -1);
186 _retv_if(ac->ops->cb_app == NULL, 0);
188 g_idle_add(__prt_ltime, ac);
190 ac->ops->cb_app(AE_RESET, ac->ops->data, k);
195 static int __app_resume(void *data)
197 x_raise_win(getpid());
201 static int __sys_do_default(struct appcore *ac, enum sys_event event)
207 /*r = __def_lowbatt(ac);*/
218 static int __sys_do(struct appcore *ac, enum sys_event event)
222 _retv_if(ac == NULL || event >= SE_MAX, -1);
224 op = &ac->sops[event];
226 if (op->func == NULL)
227 return __sys_do_default(ac, event);
229 return op->func(op->data);
232 static int __sys_lowmem_post(void *data, void *evt)
234 keynode_t *key = evt;
237 val = vconf_keynode_get_int(key);
238 if(val != VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING)
240 #if defined(MEMORY_FLUSH_ACTIVATE)
241 struct appcore *ac = data;
242 ac->ops->cb_app(AE_LOWMEM_POST, ac->ops->data, NULL);
249 static int __sys_lowmem(void *data, void *evt)
251 keynode_t *key = evt;
254 val = vconf_keynode_get_int(key);
255 if(val != VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING)
257 return __sys_do(data, SE_LOWMEM);
260 static int __sys_lowbatt(void *data, void *evt)
262 keynode_t *key = evt;
265 val = vconf_keynode_get_int(key);
267 /* VCONFKEY_SYSMAN_BAT_CRITICAL_LOW or VCONFKEY_SYSMAN_POWER_OFF */
268 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
269 return __sys_do(data, SE_LOWBAT);
274 static int __sys_langchg_pre(void *data, void *evt)
280 static int __sys_langchg(void *data, void *evt)
282 return __sys_do(data, SE_LANGCHG);
285 static int __sys_regionchg_pre(void *data, void *evt)
291 static int __sys_regionchg(void *data, void *evt)
293 return __sys_do(data, SE_REGIONCHG);
296 static void __vconf_do(struct evt_ops *eo, keynode_t * key, void *data)
301 eo->vcb_pre(data, key);
307 eo->vcb_post(data, key);
310 static void __vconf_cb(keynode_t *key, void *data)
315 name = vconf_keynode_get_name(key);
316 _ret_if(name == NULL);
318 _DBG("[APP %d] vconf changed: %s", _pid, name);
320 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
321 struct evt_ops *eo = &evtops[i];
325 if (!strcmp(name, eo->key.vkey))
326 __vconf_do(eo, key, data);
335 static int __add_vconf(struct appcore *ac)
340 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
341 struct evt_ops *eo = &evtops[i];
345 r = vconf_notify_key_changed(eo->key.vkey, __vconf_cb,
357 static int __del_vconf(void)
362 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
363 struct evt_ops *eo = &evtops[i];
367 r = vconf_ignore_key_changed(eo->key.vkey, __vconf_cb);
378 static int __aul_handler(aul_type type, bundle *b, void *data)
384 _DBG("[APP %d] AUL event: AUL_START", _pid);
385 __app_reset(data, b);
388 _DBG("[APP %d] AUL event: AUL_RESUME", _pid);
390 ret = open.callback(open.cbdata);
398 _DBG("[APP %d] AUL event: AUL_TERMINATE", _pid);
399 __app_terminate(data);
402 _DBG("[APP %d] AUL event: %d", _pid, type);
411 static void __clear(struct appcore *ac)
413 memset(ac, 0, sizeof(struct appcore));
416 EXPORT_API int appcore_set_open_cb(int (*cb) (void *),
425 EXPORT_API int appcore_set_event_callback(enum appcore_event event,
426 int (*cb) (void *), void *data)
428 struct appcore *ac = &core;
432 for (se = SE_UNKNOWN; se < SE_MAX; se++) {
433 if (event == to_ae[se])
437 if (se == SE_UNKNOWN || se >= SE_MAX) {
438 _ERR("Unregistered event");
453 EXPORT_API int appcore_init(const char *name, const struct ui_ops *ops,
454 int argc, char **argv)
457 char dirname[PATH_MAX];
459 if (core.state != 0) {
460 _ERR("Already in use");
465 if (ops == NULL || ops->cb_app == NULL) {
466 _ERR("ops or callback function is null");
471 r = __get_dir_name(dirname);
472 r = set_i18n(name, dirname);
473 _retv_if(r == -1, -1);
475 r = __add_vconf(&core);
477 _ERR("Add vconf callback failed");
481 r = aul_launch_init(__aul_handler, &core);
483 _ERR("Aul init failed: %d", r);
487 r = aul_launch_argv_handler(argc, argv);
489 _ERR("Aul argv handler failed: %d", r);
494 core.state = 1; /* TODO: use enum value */
505 EXPORT_API void appcore_exit(void)
514 EXPORT_API int appcore_flush_memory(void)
516 int (*flush_fn) (int);
519 struct appcore *ac = &core;
522 _ERR("Appcore not initialized");
526 _DBG("[APP %d] Flushing memory ...", _pid);
528 if (ac->ops->cb_app) {
529 ac->ops->cb_app(AE_MEM_FLUSH, ac->ops->data, NULL);
532 flush_fn = dlsym(RTLD_DEFAULT, "sqlite3_release_memory");
534 size = flush_fn(SQLITE_FLUSH_MAX);
539 *Disabled - the impact of stack_trim() is unclear
543 _DBG("[APP %d] Flushing memory DONE", _pid);