Activate window for the AUL_RESUME command
[platform/core/appfw/app-core.git] / src / appcore.c
1 /*
2  *  app-core
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22
23 #define _GNU_SOURCE
24
25 #include <errno.h>
26 #include <string.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <unistd.h>
30 #include <malloc.h>
31 #include <locale.h>
32 #include <linux/limits.h>
33 #include <glib.h>
34 #include <sys/time.h>
35 #include <dlfcn.h>
36 #include <vconf.h>
37 #include <aul.h>
38 #include <tzplatform_config.h>
39 #include "appcore-internal.h"
40
41 #define SQLITE_FLUSH_MAX                (1024*1024)
42
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"
48
49 static struct appcore core;
50 static pid_t _pid;
51
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,
58 };
59
60
61 enum cb_type {                  /* callback */
62         _CB_NONE,
63         _CB_SYSNOTI,
64         _CB_APPNOTI,
65         _CB_VCONF,
66 };
67
68 struct evt_ops {
69         enum cb_type type;
70         union {
71                 enum appcore_event sys;
72                 enum app_event app;
73                 const char *vkey;
74         } key;
75
76         int (*cb_pre) (void *);
77         int (*cb) (void *);
78         int (*cb_post) (void *);
79
80         int (*vcb_pre) (void *, void *);
81         int (*vcb) (void *, void *);
82         int (*vcb_post) (void *, void *);
83 };
84
85 struct open_s {
86         int (*callback) (void *);
87         void *cbdata;
88 };
89
90 static struct open_s open;
91
92 static int __app_terminate(void *data);
93 static int __app_resume(void *data);
94 static int __app_reset(void *data, bundle *k);
95
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();
104
105
106 static struct evt_ops evtops[] = {
107         {
108          .type = _CB_VCONF,
109          .key.vkey = VCONFKEY_SYSMAN_LOW_MEMORY,
110          .vcb_post = __sys_lowmem_post,
111          .vcb = __sys_lowmem,
112          },
113         {
114          .type = _CB_VCONF,
115          .key.vkey = VCONFKEY_SYSMAN_BATTERY_STATUS_LOW,
116          .vcb = __sys_lowbatt,
117          },
118         {
119          .type = _CB_VCONF,
120          .key.vkey = VCONFKEY_LANGSET,
121          .vcb_pre = __sys_langchg_pre,
122          .vcb = __sys_langchg,
123          },
124         {
125          .type = _CB_VCONF,
126          .key.vkey = VCONFKEY_REGIONFORMAT,
127          .vcb_pre = __sys_regionchg_pre,
128          .vcb = __sys_regionchg,
129          },
130         {
131          .type = _CB_VCONF,
132          .key.vkey = VCONFKEY_REGIONFORMAT_TIME1224,
133          .vcb = __sys_regionchg,
134          },
135 };
136
137 static int __get_dir_name(char *dirname)
138 {
139         char pkg_name[PKGNAME_MAX];
140         int r;
141         int pid;
142
143         pid = getpid();
144         if (pid < 0)
145                 return -1;
146
147         if (aul_app_get_pkgname_bypid(pid, pkg_name, PKGNAME_MAX) != AUL_R_OK)
148                 return -1;
149
150         r = snprintf(dirname, PATH_MAX, "%s/%s" PATH_RES PATH_LOCALE,
151                         PATH_APP_ROOT, pkg_name);
152         if (r < 0)
153                 return -1;
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);
157         if (r < 0)
158                 return -1;
159
160         return 0;
161 }
162
163 static int __app_terminate(void *data)
164 {
165         struct appcore *ac = data;
166
167         _retv_if(ac == NULL || ac->ops == NULL, -1);
168         _retv_if(ac->ops->cb_app == NULL, 0);
169
170         ac->ops->cb_app(AE_TERMINATE, ac->ops->data, NULL);
171
172         return 0;
173 }
174
175 static gboolean __prt_ltime(gpointer data)
176 {
177         int msec;
178
179         msec = appcore_measure_time_from(NULL);
180         if (msec)
181                 _DBG("[APP %d] first idle after reset: %d msec", _pid, msec);
182
183         return FALSE;
184 }
185
186 static int __app_reset(void *data, bundle * k)
187 {
188         struct appcore *ac = data;
189         _retv_if(ac == NULL || ac->ops == NULL, -1);
190         _retv_if(ac->ops->cb_app == NULL, 0);
191
192         g_idle_add(__prt_ltime, ac);
193
194         ac->ops->cb_app(AE_RESET, ac->ops->data, k);
195
196         return 0;
197 }
198
199 static int __app_resume(void *data)
200 {
201         struct appcore *ac = data;
202         _retv_if(ac == NULL || ac->ops == NULL, -1);
203         _retv_if(ac->ops->cb_app == NULL, 0);
204
205         ac->ops->cb_app(AE_RAISE, ac->ops->data, NULL);
206         return 0;
207 }
208
209 static int __sys_do_default(struct appcore *ac, enum sys_event event)
210 {
211         int r;
212
213         switch (event) {
214         case SE_LOWBAT:
215                 /*r = __def_lowbatt(ac);*/
216                 r = 0;
217                 break;
218         default:
219                 r = 0;
220                 break;
221         };
222
223         return r;
224 }
225
226 static int __sys_do(struct appcore *ac, void *event_info, enum sys_event event)
227 {
228         struct sys_op *op;
229
230         _retv_if(ac == NULL || event >= SE_MAX, -1);
231
232         op = &ac->sops[event];
233
234         if (op->func == NULL)
235                 return __sys_do_default(ac, event);
236
237         return op->func(event_info, op->data);
238 }
239
240 static int __sys_lowmem_post(void *data, void *evt)
241 {
242         keynode_t *key = evt;
243         int val;
244
245         val = vconf_keynode_get_int(key);
246
247         if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING)     {
248 #if defined(MEMORY_FLUSH_ACTIVATE)
249                 struct appcore *ac = data;
250                 ac->ops->cb_app(AE_LOWMEM_POST, ac->ops->data, NULL);
251 #else
252                 malloc_trim(0);
253 #endif
254         }
255         return 0;
256 }
257
258 static int __sys_lowmem(void *data, void *evt)
259 {
260         keynode_t *key = evt;
261         int val;
262
263         val = vconf_keynode_get_int(key);
264
265         if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING)
266                 return __sys_do(data, (void *)&val, SE_LOWMEM);
267
268         return 0;
269 }
270
271 static int __sys_lowbatt(void *data, void *evt)
272 {
273         keynode_t *key = evt;
274         int val;
275
276         val = vconf_keynode_get_int(key);
277
278         /* VCONFKEY_SYSMAN_BAT_CRITICAL_LOW or VCONFKEY_SYSMAN_POWER_OFF */
279         if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
280                 return __sys_do(data, (void *)&val, SE_LOWBAT);
281
282         return 0;
283 }
284
285 static int __sys_langchg_pre(void *data, void *evt)
286 {
287         update_lang();
288         return 0;
289 }
290
291 static int __sys_langchg(void *data, void *evt)
292 {
293         keynode_t *key = evt;
294         char *val;
295
296         val = vconf_keynode_get_str(key);
297
298         return __sys_do(data, (void *)val, SE_LANGCHG);
299 }
300
301 static int __sys_regionchg_pre(void *data, void *evt)
302 {
303         update_region();
304         return 0;
305 }
306
307 static int __sys_regionchg(void *data, void *evt)
308 {
309         keynode_t *key = evt;
310         char *val = NULL;
311         const char *name;
312
313         name = vconf_keynode_get_name(key);
314         if (!strcmp(name, VCONFKEY_REGIONFORMAT))
315                 val = vconf_keynode_get_str(key);
316
317         return __sys_do(data, (void *)val, SE_REGIONCHG);
318 }
319
320 static void __vconf_do(struct evt_ops *eo, keynode_t * key, void *data)
321 {
322         _ret_if(eo == NULL);
323
324         if (eo->vcb_pre)
325                 eo->vcb_pre(data, key);
326
327         if (eo->vcb)
328                 eo->vcb(data, key);
329
330         if (eo->vcb_post)
331                 eo->vcb_post(data, key);
332 }
333
334 static void __vconf_cb(keynode_t *key, void *data)
335 {
336         int i;
337         const char *name;
338
339         name = vconf_keynode_get_name(key);
340         _ret_if(name == NULL);
341
342         _DBG("[APP %d] vconf changed: %s", _pid, name);
343
344         for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
345                 struct evt_ops *eo = &evtops[i];
346
347                 switch (eo->type) {
348                 case _CB_VCONF:
349                         if (!strcmp(name, eo->key.vkey))
350                                 __vconf_do(eo, key, data);
351                         break;
352                 default:
353                         /* do nothing */
354                         break;
355                 }
356         }
357 }
358
359 static int __add_vconf(struct appcore *ac)
360 {
361         int i;
362         int r;
363
364         for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
365                 struct evt_ops *eo = &evtops[i];
366
367                 switch (eo->type) {
368                 case _CB_VCONF:
369                         r = vconf_notify_key_changed(eo->key.vkey, __vconf_cb,
370                                                      ac);
371                         break;
372                 default:
373                         /* do nothing */
374                         break;
375                 }
376         }
377
378         return 0;
379 }
380
381 static int __del_vconf(void)
382 {
383         int i;
384         int r;
385
386         for (i = 0; i < sizeof(evtops) / sizeof(evtops[0]); i++) {
387                 struct evt_ops *eo = &evtops[i];
388
389                 switch (eo->type) {
390                 case _CB_VCONF:
391                         r = vconf_ignore_key_changed(eo->key.vkey, __vconf_cb);
392                         break;
393                 default:
394                         /* do nothing */
395                         break;
396                 }
397         }
398
399         return 0;
400 }
401
402 static int __aul_handler(aul_type type, bundle *b, void *data)
403 {
404         int ret;
405
406         switch (type) {
407         case AUL_START:
408                 _DBG("[APP %d]     AUL event: AUL_START", _pid);
409                 __app_reset(data, b);
410                 break;
411         case AUL_RESUME:
412                 _DBG("[APP %d]     AUL event: AUL_RESUME", _pid);
413                 if(open.callback) {
414                         ret = open.callback(open.cbdata);
415                         if (ret == 0)
416                                 __app_resume(data);
417                 } else {
418                         __app_resume(data);
419                 }
420                 break;
421         case AUL_TERMINATE:
422                 _DBG("[APP %d]     AUL event: AUL_TERMINATE", _pid);
423                 __app_terminate(data);
424                 break;
425         default:
426                 _DBG("[APP %d]     AUL event: %d", _pid, type);
427                 /* do nothing */
428                 break;
429         }
430
431         return 0;
432 }
433
434
435 static void __clear(struct appcore *ac)
436 {
437         memset(ac, 0, sizeof(struct appcore));
438 }
439
440 EXPORT_API int appcore_set_open_cb(int (*cb) (void *),
441                                        void *data)
442 {
443         open.callback = cb;
444         open.cbdata = data;
445
446         return 0;
447 }
448
449 EXPORT_API int appcore_set_event_callback(enum appcore_event event,
450                                           int (*cb) (void *, void *), void *data)
451 {
452         struct appcore *ac = &core;
453         struct sys_op *op;
454         enum sys_event se;
455
456         for (se = SE_UNKNOWN; se < SE_MAX; se++) {
457                 if (event == to_ae[se])
458                         break;
459         }
460
461         if (se == SE_UNKNOWN || se >= SE_MAX) {
462                 _ERR("Unregistered event");
463                 errno = EINVAL;
464                 return -1;
465         }
466
467         op = &ac->sops[se];
468
469         op->func = cb;
470         op->data = data;
471
472         return 0;
473 }
474
475
476
477 EXPORT_API int appcore_init(const char *name, const struct ui_ops *ops,
478                             int argc, char **argv)
479 {
480         int r;
481         char dirname[PATH_MAX];
482
483         if (core.state != 0) {
484                 _ERR("Already in use");
485                 errno = EALREADY;
486                 return -1;
487         }
488
489         if (ops == NULL || ops->cb_app == NULL) {
490                 _ERR("ops or callback function is null");
491                 errno = EINVAL;
492                 return -1;
493         }
494
495         r = __get_dir_name(dirname);
496         r = set_i18n(name, dirname);
497         _retv_if(r == -1, -1);
498
499         r = __add_vconf(&core);
500         if (r == -1) {
501                 _ERR("Add vconf callback failed");
502                 goto err;
503         }
504
505         r = aul_launch_init(__aul_handler, &core);
506         if (r < 0) {
507                 _ERR("Aul init failed: %d", r);
508                 goto err;
509         }
510
511         r = aul_launch_argv_handler(argc, argv);
512         if (r < 0) {
513                 _ERR("Aul argv handler failed: %d", r);
514                 goto err;
515         }
516
517         core.ops = ops;
518         core.state = 1;         /* TODO: use enum value */
519
520         _pid = getpid();
521
522         return 0;
523  err:
524         __del_vconf();
525         __clear(&core);
526         return -1;
527 }
528
529 EXPORT_API void appcore_exit(void)
530 {
531         if (core.state) {
532                 __del_vconf();
533                 __clear(&core);
534         }
535         aul_finalize();
536 }
537
538 EXPORT_API int appcore_flush_memory(void)
539 {
540         int (*flush_fn) (int);
541         int size = 0;
542
543         struct appcore *ac = &core;
544
545         if (!core.state) {
546                 _ERR("Appcore not initialized");
547                 return -1;
548         }
549
550         _DBG("[APP %d] Flushing memory ...", _pid);
551
552         if (ac->ops->cb_app) {
553                 ac->ops->cb_app(AE_MEM_FLUSH, ac->ops->data, NULL);
554         }
555
556         flush_fn = dlsym(RTLD_DEFAULT, "sqlite3_release_memory");
557         if (flush_fn) {
558                 size = flush_fn(SQLITE_FLUSH_MAX);
559         }
560
561         malloc_trim(0);
562         /*
563         *Disabled - the impact of stack_trim() is unclear
564         *stack_trim();
565         */
566
567         _DBG("[APP %d] Flushing memory DONE", _pid);
568
569         return 0;
570 }