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/usr/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 if (aul_app_get_pkgname_bypid(pid, pkg_name, PKGNAME_MAX) != AUL_R_OK)
149 r = snprintf(dirname, PATH_MAX, PATH_APP_ROOT "/%s" PATH_RES PATH_LOCALE,pkg_name);
152 if (access(dirname, R_OK) == 0) return 0;
153 r = snprintf(dirname, PATH_MAX, PATH_RO_APP_ROOT "/%s" PATH_RES PATH_LOCALE,pkg_name);
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 gboolean __prt_ltime(gpointer data)
176 msec = appcore_measure_time_from(NULL);
178 _DBG("[APP %d] first idle after reset: %d msec", _pid, msec);
183 static int __app_reset(void *data, bundle * k)
185 struct appcore *ac = data;
186 _retv_if(ac == NULL || ac->ops == NULL, -1);
187 _retv_if(ac->ops->cb_app == NULL, 0);
189 g_idle_add(__prt_ltime, ac);
191 ac->ops->cb_app(AE_RESET, ac->ops->data, k);
196 static int __app_resume(void *data)
199 struct appcore *ac = data;
201 _retv_if(ac == NULL || ac->ops == NULL, -1);
202 _retv_if(ac->ops->cb_app == NULL, 0);
204 ac->ops->cb_app(AE_RESUME, ac->ops->data, NULL);
207 x_raise_win(getpid());
212 static int __sys_do_default(struct appcore *ac, enum sys_event event)
218 /*r = __def_lowbatt(ac);*/
229 static int __sys_do(struct appcore *ac, enum sys_event event)
233 _retv_if(ac == NULL || event >= SE_MAX, -1);
235 op = &ac->sops[event];
237 if (op->func == NULL)
238 return __sys_do_default(ac, event);
240 return op->func(op->data);
243 static int __sys_lowmem_post(void *data, void *evt)
245 keynode_t *key = evt;
248 val = vconf_keynode_get_int(key);
250 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
251 #if defined(MEMORY_FLUSH_ACTIVATE)
252 struct appcore *ac = data;
253 ac->ops->cb_app(AE_LOWMEM_POST, ac->ops->data, NULL);
261 static int __sys_lowmem(void *data, void *evt)
263 keynode_t *key = evt;
266 val = vconf_keynode_get_int(key);
268 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING)
269 return __sys_do(data, SE_LOWMEM);
274 static int __sys_lowbatt(void *data, void *evt)
276 keynode_t *key = evt;
279 val = vconf_keynode_get_int(key);
281 /* VCONFKEY_SYSMAN_BAT_CRITICAL_LOW or VCONFKEY_SYSMAN_POWER_OFF */
282 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
283 return __sys_do(data, SE_LOWBAT);
288 static int __sys_langchg_pre(void *data, void *evt)
294 static int __sys_langchg(void *data, void *evt)
296 return __sys_do(data, SE_LANGCHG);
299 static int __sys_regionchg_pre(void *data, void *evt)
305 static int __sys_regionchg(void *data, void *evt)
307 return __sys_do(data, SE_REGIONCHG);
310 static void __vconf_do(struct evt_ops *eo, keynode_t * key, void *data)
315 eo->vcb_pre(data, key);
321 eo->vcb_post(data, key);
324 static void __vconf_cb(keynode_t *key, void *data)
329 name = vconf_keynode_get_name(key);
330 _ret_if(name == NULL);
332 _DBG("[APP %d] vconf changed: %s", _pid, name);
334 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
335 struct evt_ops *eo = &evtops[i];
339 if (!strcmp(name, eo->key.vkey))
340 __vconf_do(eo, key, data);
349 static int __add_vconf(struct appcore *ac)
354 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
355 struct evt_ops *eo = &evtops[i];
359 r = vconf_notify_key_changed(eo->key.vkey, __vconf_cb,
371 static int __del_vconf(void)
376 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
377 struct evt_ops *eo = &evtops[i];
381 r = vconf_ignore_key_changed(eo->key.vkey, __vconf_cb);
392 static int __aul_handler(aul_type type, bundle *b, void *data)
398 _DBG("[APP %d] AUL event: AUL_START", _pid);
399 __app_reset(data, b);
402 _DBG("[APP %d] AUL event: AUL_RESUME", _pid);
404 ret = open.callback(open.cbdata);
412 _DBG("[APP %d] AUL event: AUL_TERMINATE", _pid);
413 __app_terminate(data);
416 _DBG("[APP %d] AUL event: %d", _pid, type);
425 static void __clear(struct appcore *ac)
427 memset(ac, 0, sizeof(struct appcore));
430 EXPORT_API int appcore_set_open_cb(int (*cb) (void *),
439 EXPORT_API int appcore_set_event_callback(enum appcore_event event,
440 int (*cb) (void *), void *data)
442 struct appcore *ac = &core;
446 for (se = SE_UNKNOWN; se < SE_MAX; se++) {
447 if (event == to_ae[se])
451 if (se == SE_UNKNOWN || se >= SE_MAX) {
452 _ERR("Unregistered event");
467 EXPORT_API int appcore_init(const char *name, const struct ui_ops *ops,
468 int argc, char **argv)
471 char dirname[PATH_MAX];
473 if (core.state != 0) {
474 _ERR("Already in use");
479 if (ops == NULL || ops->cb_app == NULL) {
480 _ERR("ops or callback function is null");
485 r = __get_dir_name(dirname);
486 r = set_i18n(name, dirname);
487 _retv_if(r == -1, -1);
489 r = __add_vconf(&core);
491 _ERR("Add vconf callback failed");
495 r = aul_launch_init(__aul_handler, &core);
497 _ERR("Aul init failed: %d", r);
501 r = aul_launch_argv_handler(argc, argv);
503 _ERR("Aul argv handler failed: %d", r);
508 core.state = 1; /* TODO: use enum value */
519 EXPORT_API void appcore_exit(void)
528 EXPORT_API int appcore_flush_memory(void)
530 int (*flush_fn) (int);
533 struct appcore *ac = &core;
536 _ERR("Appcore not initialized");
540 _DBG("[APP %d] Flushing memory ...", _pid);
542 if (ac->ops->cb_app) {
543 ac->ops->cb_app(AE_MEM_FLUSH, ac->ops->data, NULL);
546 flush_fn = dlsym(RTLD_DEFAULT, "sqlite3_release_memory");
548 size = flush_fn(SQLITE_FLUSH_MAX);
553 *Disabled - the impact of stack_trim() is unclear
557 _DBG("[APP %d] Flushing memory DONE", _pid);