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>
39 #include "appcore-internal.h"
41 #define SQLITE_FLUSH_MAX (1024*1024)
43 #define PKGNAME_MAX 256
44 #define PATH_APP_ROOT "/opt/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);
103 static struct evt_ops evtops[] = {
106 .key.vkey = VCONFKEY_SYSMAN_LOW_MEMORY,
107 .vcb_post = __sys_lowmem_post,
112 .key.vkey = VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
113 .vcb = __sys_lowbatt,
117 .key.vkey = VCONFKEY_LANGSET,
118 .vcb_pre = __sys_langchg_pre,
119 .vcb = __sys_langchg,
123 .key.vkey = VCONFKEY_REGIONFORMAT,
124 .vcb_pre = __sys_regionchg_pre,
125 .vcb = __sys_regionchg,
129 .key.vkey = VCONFKEY_REGIONFORMAT_TIME1224,
130 .vcb = __sys_regionchg,
134 static int __get_dir_name(char *dirname)
136 char pkg_name[PKGNAME_MAX];
144 aul_app_get_pkgname_bypid(pid, pkg_name, PKGNAME_MAX);
146 r = snprintf(dirname, PATH_MAX, PATH_APP_ROOT "/%s" PATH_RES PATH_LOCALE,pkg_name);
154 static int __get_cmd(char *buf)
158 char fname[FILENAME_MAX];
160 r = snprintf(fname, sizeof(fname), "/proc/%d/cmdline", getpid());
164 fp = fopen(fname, "r");
169 if (fgets(buf, PATH_MAX, fp) == NULL)
177 static int __update_rua(bundle *b)
189 _DBG("[APP %d] rua init error", _pid);
193 memset(&rec, 0, sizeof(rec));
195 rec.pkg_name = getenv("PKG_NAME");
199 bundle_encode(b, (bundle_raw **)&rec.arg, NULL);
201 r = rua_add_history(&rec);
203 _DBG("[APP %d] rua add history error", _pid);
213 static int __app_terminate(void *data)
215 struct appcore *ac = data;
217 _retv_if(ac == NULL || ac->ops == NULL, -1);
218 _retv_if(ac->ops->cb_app == NULL, 0);
220 ac->ops->cb_app(AE_TERMINATE, ac->ops->data, NULL);
225 static gboolean __prt_ltime(gpointer data)
229 msec = appcore_measure_time_from(NULL);
231 _DBG("[APP %d] first idle after reset: %d msec", _pid, msec);
236 static int __app_reset(void *data, bundle * k)
238 struct appcore *ac = data;
239 _retv_if(ac == NULL || ac->ops == NULL, -1);
240 _retv_if(ac->ops->cb_app == NULL, 0);
242 g_idle_add(__prt_ltime, ac);
244 ac->ops->cb_app(AE_RESET, ac->ops->data, k);
250 static int __app_resume(void *data)
252 x_raise_win(getpid());
257 static int __def_lowbatt(struct appcore *ac)
259 return __app_terminate(ac);
262 static int __sys_do_default(struct appcore *ac, enum sys_event event)
268 /*r = __def_lowbatt(ac);*/
279 static int __sys_do(struct appcore *ac, enum sys_event event)
283 _retv_if(ac == NULL || event >= SE_MAX, -1);
285 op = &ac->sops[event];
287 if (op->func == NULL)
288 return __sys_do_default(ac, event);
290 return op->func(op->data);
293 static int __sys_lowmem_post(void *data, void *evt)
295 #if defined(MEMORY_FLUSH_ACTIVATE)
296 struct appcore *ac = data;
297 ac->ops->cb_app(AE_LOWMEM_POST, ac->ops->data, NULL);
304 static int __sys_lowmem(void *data, void *evt)
306 return __sys_do(data, SE_LOWMEM);
309 static int __sys_lowbatt(void *data, void *evt)
311 keynode_t *key = evt;
314 val = vconf_keynode_get_int(key);
316 /* VCONFKEY_SYSMAN_BAT_CRITICAL_LOW or VCONFKEY_SYSMAN_POWER_OFF */
317 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
318 return __sys_do(data, SE_LOWBAT);
323 static int __sys_langchg_pre(void *data, void *evt)
329 static int __sys_langchg(void *data, void *evt)
331 return __sys_do(data, SE_LANGCHG);
334 static int __sys_regionchg_pre(void *data, void *evt)
340 static int __sys_regionchg(void *data, void *evt)
342 return __sys_do(data, SE_REGIONCHG);
345 static void __vconf_do(struct evt_ops *eo, keynode_t * key, void *data)
350 eo->vcb_pre(data, key);
356 eo->vcb_post(data, key);
359 static void __vconf_cb(keynode_t *key, void *data)
364 name = vconf_keynode_get_name(key);
365 _ret_if(name == NULL);
367 _DBG("[APP %d] vconf changed: %s", _pid, name);
369 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
370 struct evt_ops *eo = &evtops[i];
374 if (!strcmp(name, eo->key.vkey))
375 __vconf_do(eo, key, data);
384 static int __add_vconf(struct appcore *ac)
389 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
390 struct evt_ops *eo = &evtops[i];
394 r = vconf_notify_key_changed(eo->key.vkey, __vconf_cb,
406 static int __del_vconf(void)
411 for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
412 struct evt_ops *eo = &evtops[i];
416 r = vconf_ignore_key_changed(eo->key.vkey, __vconf_cb);
427 static int __aul_handler(aul_type type, bundle *b, void *data)
433 _DBG("[APP %d] AUL event: AUL_START", _pid);
434 __app_reset(data, b);
437 _DBG("[APP %d] AUL event: AUL_RESUME", _pid);
439 ret = open.callback(open.cbdata);
447 _DBG("[APP %d] AUL event: AUL_TERMINATE", _pid);
448 __app_terminate(data);
451 _DBG("[APP %d] AUL event: %d", _pid, type);
460 static void __clear(struct appcore *ac)
462 memset(ac, 0, sizeof(struct appcore));
465 EXPORT_API int appcore_set_open_cb(int (*cb) (void *),
474 EXPORT_API int appcore_set_event_callback(enum appcore_event event,
475 int (*cb) (void *), void *data)
477 struct appcore *ac = &core;
481 for (se = SE_UNKNOWN; se < SE_MAX; se++) {
482 if (event == to_ae[se])
486 if (se == SE_UNKNOWN || se >= SE_MAX) {
487 _ERR("Unregistered event");
502 EXPORT_API int appcore_init(const char *name, const struct ui_ops *ops,
503 int argc, char **argv)
506 char dirname[PATH_MAX];
508 if (core.state != 0) {
509 _ERR("Already in use");
514 if (ops == NULL || ops->cb_app == NULL) {
515 _ERR("ops or callback function is null");
520 r = __get_dir_name(dirname);
521 r = set_i18n(name, dirname);
522 _retv_if(r == -1, -1);
524 r = __add_vconf(&core);
526 _ERR("Add vconf callback failed");
530 r = aul_launch_init(__aul_handler, &core);
532 _ERR("Aul init failed: %d", r);
536 r = aul_launch_argv_handler(argc, argv);
538 _ERR("Aul argv handler failed: %d", r);
543 core.state = 1; /* TODO: use enum value */
554 EXPORT_API void appcore_exit(void)
562 EXPORT_API int appcore_flush_memory(void)
564 int (*flush_fn) (int);
567 struct appcore *ac = &core;
570 _ERR("Appcore not initialized");
574 _DBG("[APP %d] Flushing memory ...", _pid);
576 if (ac->ops->cb_app) {
577 ac->ops->cb_app(AE_MEM_FLUSH, ac->ops->data, NULL);
580 flush_fn = dlsym(RTLD_DEFAULT, "sqlite3_release_memory");
582 size = flush_fn(SQLITE_FLUSH_MAX);
587 *Disabled - the impact of stack_trim() is unclear
591 _DBG("[APP %d] Flushing memory DONE", _pid);