c431058bcc127110946900f2ce105609a51fecc7
[platform/core/appfw/app-core.git] / src / base / appcore_base.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17
18 #define _GNU_SOURCE
19
20 #include <stdbool.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <malloc.h>
28 #include <locale.h>
29 #include <libintl.h>
30 #include <linux/limits.h>
31 #include <glib.h>
32 #include <gio/gio.h>
33 #include <sys/time.h>
34 #include <dlfcn.h>
35 #include <vconf.h>
36 #include <aul.h>
37 #include <bundle_internal.h>
38 #include <sensor_internal.h>
39 #include <ttrace.h>
40 #include "appcore_base.h"
41 #include "appcore_base_private.h"
42
43 #define PATH_LOCALE "locale"
44 #define RESOURCED_FREEZER_PATH "/Org/Tizen/Resourced/Freezer"
45 #define RESOURCED_FREEZER_INTERFACE "org.tizen.resourced.freezer"
46 #define RESOURCED_FREEZER_SIGNAL "FreezerState"
47
48 typedef struct _appcore_base_context {
49         appcore_base_ops ops;
50         void *data;
51         int argc;
52         char **argv;
53         unsigned int tid;
54         bool suspended_state;
55         bool allowed_bg;
56         bool dirty;
57 } appcore_base_context;
58
59 typedef struct _appcore_base_event_node {
60         int type;
61         appcore_base_event_cb cb;
62         void *data;
63 } appcore_base_event_node;
64
65 typedef struct _appcore_base_rotation {
66         int conn;
67         int lock;
68         int ref;
69         enum appcore_base_rm rm;
70 } appcore_base_rotation;
71
72 struct lang_info_s {
73         char *parent;
74         GList *list;
75 };
76
77 static appcore_base_context __context;
78 static GList *__events;
79 static GDBusConnection *__bus;
80 static guint __suspend_dbus_handler_initialized;
81 static char *__locale_dir;
82 static appcore_base_rotation __rotation;
83
84 static void __invoke_callback(void *event, int type)
85 {
86         GList *iter = __events;
87
88         while (iter) {
89                 appcore_base_event_node *node = iter->data;
90
91                 if (node->type == type)
92                         node->cb(event, node->data);
93                 iter = g_list_next(iter);
94         }
95 }
96
97 static bool __exist_callback(int type)
98 {
99         GList *iter = __events;
100
101         while (iter) {
102                 appcore_base_event_node *node = iter->data;
103
104                 if (node->type == type)
105                         return true;
106
107                 iter = g_list_next(iter);
108         }
109
110         return false;
111 }
112
113 static enum appcore_base_rm __get_rm(sensor_data_t data)
114 {
115         int event;
116         enum appcore_base_rm rm;
117
118         if (data.value_count <= 0) {
119                 _ERR("Failed to get sensor data");
120                 return APPCORE_BASE_RM_UNKNOWN;
121         }
122
123         event = data.values[0];
124         switch (event) {
125         case AUTO_ROTATION_DEGREE_0:
126                 rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
127                 break;
128         case AUTO_ROTATION_DEGREE_90:
129                 rm = APPCORE_BASE_RM_LANDSCAPE_NORMAL;
130                 break;
131         case AUTO_ROTATION_DEGREE_180:
132                 rm = APPCORE_BASE_RM_PORTRAIT_REVERSE;
133                 break;
134         case AUTO_ROTATION_DEGREE_270:
135                 rm = APPCORE_BASE_RM_LANDSCAPE_REVERSE;
136                 break;
137         default:
138                 rm = APPCORE_BASE_RM_UNKNOWN;
139                 break;
140         }
141
142         return rm;
143 }
144
145 static void __lock_cb(keynode_t *node, void *user_data)
146 {
147         bool r;
148         sensor_data_t data;
149         enum appcore_base_rm rm;
150
151         __rotation.lock = !vconf_keynode_get_bool(node);
152         if (__rotation.lock) {
153                 _DBG("Rotation locked");
154                 rm = APPCORE_BASE_RM_PORTRAIT_NORMAL;
155         } else {
156                 _DBG("Rotation unlocked");
157                 r = sensord_get_data(__rotation.conn, AUTO_ROTATION_SENSOR, &data);
158                 if (!r) {
159                         _ERR("Failed to get sensor data");
160                         return;
161                 }
162
163                 rm = __get_rm(data);
164                 if (rm == APPCORE_BASE_RM_UNKNOWN) {
165                         _ERR("Unknown mode");
166                         return;
167                 }
168         }
169
170         if (__rotation.rm == rm)
171                 return;
172
173         _DBG("Rotation: %d -> %d", __rotation.rm, rm);
174         __rotation.rm = rm;
175         __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
176 }
177
178 static void __auto_rotation_changed_cb(sensor_t sensor, unsigned int event_type,
179                 sensor_data_t *data, void *user_data)
180 {
181         enum appcore_base_rm rm;
182
183         if (data == NULL)
184                 return;
185
186         if (__rotation.lock)
187                 return;
188
189         if (event_type != AUTO_ROTATION_CHANGE_STATE_EVENT)
190                 return;
191
192         rm = __get_rm(*data);
193         if (rm == APPCORE_BASE_RM_UNKNOWN) {
194                 _ERR("Unknown mode");
195                 return;
196         }
197
198         _DBG("Rotation: %d -> %d", __rotation.rm, rm);
199         __rotation.rm = rm;
200         __invoke_callback((void *)&__rotation.rm, APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED);
201 }
202
203 static void __unregister_rotation_changed_event(void)
204 {
205         if (!__rotation.ref)
206                 return;
207
208         __rotation.ref--;
209         if (__rotation.ref > 1)
210                 return;
211
212         vconf_ignore_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb);
213         sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
214         sensord_stop(__rotation.conn);
215         sensord_disconnect(__rotation.conn);
216
217         __rotation.lock = 0;
218         __rotation.ref = 0;
219 }
220
221 static void __register_rotation_changed_event(void)
222 {
223         sensor_t sensor;
224         int lock;
225         bool r;
226
227         if (__rotation.ref) {
228                 __rotation.ref++;
229                 return;
230         }
231
232         sensor = sensord_get_sensor(AUTO_ROTATION_SENSOR);
233         __rotation.conn = sensord_connect(sensor);
234         if (__rotation.conn < 0) {
235                 _ERR("Failed to connect sensord");
236                 return;
237         }
238
239         r = sensord_register_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT,
240                         SENSOR_INTERVAL_NORMAL, 0, __auto_rotation_changed_cb, NULL);
241         if (!r) {
242                 _ERR("Failed to register auto rotation change event");
243                 sensord_disconnect(__rotation.conn);
244                 return;
245         }
246
247         r = sensord_start(__rotation.conn, 0);
248         if (!r) {
249                 _ERR("Failed to start sensord");
250                 sensord_unregister_event(__rotation.conn, AUTO_ROTATION_CHANGE_STATE_EVENT);
251                 sensord_disconnect(__rotation.conn);
252                 return;
253         }
254
255         lock = 0;
256         vconf_get_bool(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, &lock);
257         vconf_notify_key_changed(VCONFKEY_SETAPPL_AUTO_ROTATE_SCREEN_BOOL, __lock_cb, NULL);
258
259         __rotation.lock = !lock;
260         __rotation.ref++;
261 }
262
263 static void __on_low_memory(keynode_t *key, void *data)
264 {
265         int val;
266
267         val = vconf_keynode_get_int(key);
268
269         if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
270                 __invoke_callback(&val, APPCORE_BASE_EVENT_LOW_MEMORY);
271                 malloc_trim(0);
272         }
273 }
274
275 static void __on_low_battery(keynode_t *key, void *data)
276 {
277         int val;
278
279         val = vconf_keynode_get_int(key);
280
281         if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
282                 __invoke_callback(&val, APPCORE_BASE_EVENT_LOW_BATTERY);
283 }
284
285 static void __destroy_lang_info(gpointer data)
286 {
287         struct lang_info_s *info = (struct lang_info_s *)data;
288
289         if (info == NULL)
290                 return;
291
292         if (info->list)
293                 g_list_free_full(info->list, free);
294         if (info->parent)
295                 free(info->parent);
296         free(info);
297 }
298
299 static struct lang_info_s *__create_lang_info(const char *lang)
300 {
301         struct lang_info_s *info;
302
303         info = calloc(1, sizeof(struct lang_info_s));
304         if (info == NULL) {
305                 _ERR("Out of memory");
306                 return NULL;
307         }
308
309         info->parent = strdup(lang);
310         if (info->parent == NULL) {
311                 _ERR("Out of memory");
312                 free(info);
313                 return NULL;
314         }
315
316         return info;
317 }
318
319 static gint __compare_langs(gconstpointer a, gconstpointer b)
320 {
321         if (!a || !b)
322                 return -1;
323
324         return strcmp(a, b);
325 }
326
327 static char *__get_string_before(const char *str, const char *delim)
328 {
329         char *new_str;
330         char *dup_str;
331         char *token;
332
333         dup_str = strdup(str);
334         if (dup_str == NULL)
335                 return NULL;
336
337         token = strtok(dup_str, delim);
338         if (token == NULL) {
339                 free(dup_str);
340                 return NULL;
341         }
342
343         new_str = strdup(token);
344         free(dup_str);
345
346         return new_str;
347 }
348
349 static GHashTable *__get_lang_table(void)
350 {
351         GHashTable *table;
352         DIR *dp;
353         struct dirent *dentry;
354         char buf[PATH_MAX];
355         struct stat stat_buf;
356         int ret;
357         char *parent_lang;
358         struct lang_info_s *info;
359
360         if (__locale_dir == NULL || __locale_dir[0] == '\0')
361                 return NULL;
362
363         table = g_hash_table_new_full(g_str_hash, g_str_equal,
364                         NULL, __destroy_lang_info);
365         if (table == NULL) {
366                 _ERR("Out of memory");
367                 return NULL;
368         }
369
370         dp = opendir(__locale_dir);
371         if (dp == NULL) {
372                 g_hash_table_destroy(table);
373                 return NULL;
374         }
375
376         while ((dentry = readdir(dp)) != NULL) {
377                 if (!strcmp(dentry->d_name, ".") ||
378                                 !strcmp(dentry->d_name, ".."))
379                         continue;
380
381                 snprintf(buf, sizeof(buf), "%s/%s",
382                                 __locale_dir, dentry->d_name);
383                 ret = stat(buf, &stat_buf);
384                 if (ret != 0 || !S_ISDIR(stat_buf.st_mode))
385                         continue;
386
387                 parent_lang = __get_string_before(dentry->d_name, "_");
388                 if (parent_lang == NULL) {
389                         _ERR("Out of memory");
390                         break;
391                 }
392
393                 info = g_hash_table_lookup(table, parent_lang);
394                 if (info == NULL) {
395                         info = __create_lang_info(parent_lang);
396                         if (info == NULL) {
397                                 free(parent_lang);
398                                 break;
399                         }
400                         g_hash_table_insert(table, info->parent, info);
401                 }
402                 info->list = g_list_append(info->list, strdup(dentry->d_name));
403                 free(parent_lang);
404         }
405         closedir(dp);
406
407         return table;
408 }
409
410 static GList *__append_langs(const char *lang, GList *list, GHashTable *table)
411 {
412         struct lang_info_s *info;
413         GList *found;
414         char *parent_lang = NULL;
415         char *extract_lang;
416
417         if (lang == NULL)
418                 return list;
419
420         extract_lang = __get_string_before(lang, ".");
421         if (extract_lang == NULL)
422                 return list;
423
424         found = g_list_find_custom(list, extract_lang, __compare_langs);
425         if (found) {
426                 list = g_list_remove_link(list, found);
427                 list = g_list_concat(list, found);
428                 goto end;
429         }
430
431         parent_lang = __get_string_before(extract_lang, "_");
432         if (parent_lang == NULL)
433                 goto end;
434
435         info = g_hash_table_lookup(table, parent_lang);
436         if (info == NULL)
437                 goto end;
438
439         found = g_list_find_custom(info->list, extract_lang, __compare_langs);
440         if (found) {
441                 info->list = g_list_remove_link(info->list, found);
442                 list = g_list_concat(list, found);
443                 goto end;
444         }
445
446         found = g_list_find_custom(info->list, parent_lang, __compare_langs);
447         if (found) {
448                 info->list = g_list_remove_link(info->list, found);
449                 list = g_list_concat(list, found);
450                 goto end;
451         }
452
453         found = g_list_first(info->list);
454         if (found) {
455                 info->list = g_list_remove_link(info->list, found);
456                 list = g_list_concat(list, found);
457         }
458
459 end:
460         if (extract_lang)
461                 free(extract_lang);
462         if (parent_lang)
463                 free(parent_lang);
464
465         return list;
466 }
467
468 static GList *__split_language(const char *lang)
469 {
470         GList *list = NULL;
471         char *dup_lang;
472         char *token;
473
474         dup_lang = strdup(lang);
475         if (dup_lang == NULL) {
476                 _ERR("Out of memory");
477                 return NULL;
478         }
479
480         token = strtok(dup_lang, ":");
481         while (token != NULL) {
482                 list = g_list_append(list, strdup(token));
483                 token = strtok(NULL, ":");
484         }
485         free(dup_lang);
486
487         return list;
488 }
489
490 static GList *__append_default_langs(GList *list)
491 {
492         const char *langs[] = {"en_US", "en_GB", "en"};
493         unsigned int i;
494         GList *found;
495
496         for (i = 0; i < (sizeof(langs) / sizeof(langs[0])); i++) {
497                 found = g_list_find_custom(list, langs[i], __compare_langs);
498                 if (found == NULL)
499                         list = g_list_append(list, strdup(langs[i]));
500         }
501
502         return list;
503 }
504
505 static char *__get_language(const char *lang)
506 {
507         GHashTable *table;
508         GList *list;
509         GList *lang_list = NULL;
510         GList *iter;
511         char *language;
512         char buf[LINE_MAX] = {'\0'};
513         size_t n;
514
515         list = __split_language(lang);
516         if (list == NULL)
517                 return NULL;
518
519         table = __get_lang_table();
520         if (table == NULL) {
521                 g_list_free_full(list, free);
522                 return NULL;
523         }
524
525         iter = g_list_first(list);
526         while (iter) {
527                 language = (char *)iter->data;
528                 lang_list = __append_langs(language, lang_list, table);
529                 iter = g_list_next(iter);
530         }
531         g_list_free_full(list, free);
532         g_hash_table_destroy(table);
533
534         lang_list = __append_default_langs(lang_list);
535         iter = g_list_first(lang_list);
536         while (iter) {
537                 language = (char *)iter->data;
538                 if (language) {
539                         if (buf[0] == '\0') {
540                                 snprintf(buf, sizeof(buf), "%s", language);
541                         } else {
542                                 n = sizeof(buf) - strlen(buf) - 1;
543                                 strncat(buf, ":", n);
544                                 n = sizeof(buf) - strlen(buf) - 1;
545                                 strncat(buf, language, n);
546                         }
547                 }
548                 iter = g_list_next(iter);
549         }
550         g_list_free_full(lang_list, free);
551
552         return strdup(buf);
553 }
554
555 static void __update_lang(void)
556 {
557         char *language;
558         char *lang;
559         char *r;
560
561         lang = vconf_get_str(VCONFKEY_LANGSET);
562         if (lang) {
563                 /* TODO: Use VCONFKEY_SETAPPL_LANGUAGES key */
564                 language = __get_language(lang);
565                 if (language) {
566                         _DBG("*****language(%s)", language);
567                         setenv("LANGUAGE", language, 1);
568                         free(language);
569                 } else {
570                         setenv("LANGUAGE", lang, 1);
571                 }
572                 setenv("LANG", lang, 1);
573                 setenv("LC_MESSAGES", lang, 1);
574                 r = setlocale(LC_ALL, "");
575                 if (r == NULL) {
576                         r = setlocale(LC_ALL, lang);
577                         if (r != NULL)
578                                 _DBG("*****appcore setlocale=%s\n", r);
579                 }
580                 free(lang);
581         }
582 }
583
584 static void __update_region(void)
585 {
586         char *region;
587         char *r;
588
589         region = vconf_get_str(VCONFKEY_REGIONFORMAT);
590         if (region) {
591                 setenv("LC_CTYPE", region, 1);
592                 setenv("LC_NUMERIC", region, 1);
593                 setenv("LC_TIME", region, 1);
594                 setenv("LC_COLLATE", region, 1);
595                 setenv("LC_MONETARY", region, 1);
596                 setenv("LC_PAPER", region, 1);
597                 setenv("LC_NAME", region, 1);
598                 setenv("LC_ADDRESS", region, 1);
599                 setenv("LC_TELEPHONE", region, 1);
600                 setenv("LC_MEASUREMENT", region, 1);
601                 setenv("LC_IDENTIFICATION", region, 1);
602                 r = setlocale(LC_ALL, "");
603                 if (r != NULL)
604                         _DBG("*****appcore setlocale=%s\n", r);
605
606                 free(region);
607         }
608 }
609
610 static void __on_language_change(keynode_t *key, void *data)
611 {
612         char *val;
613
614         val = vconf_keynode_get_str(key);
615
616         __update_lang();
617         __invoke_callback((void *)val, APPCORE_BASE_EVENT_LANG_CHANGE);
618 }
619
620 static void __on_region_change(keynode_t *key, void *data)
621 {
622         char *val = NULL;
623         const char *name;
624
625         name = vconf_keynode_get_name(key);
626         if (name && !strcmp(name, VCONFKEY_REGIONFORMAT))
627                 val = vconf_keynode_get_str(key);
628
629         __update_region();
630         __invoke_callback((void *)val, APPCORE_BASE_EVENT_REGION_CHANGE);
631 }
632
633 static gboolean __flush_memory(gpointer data)
634 {
635         int suspend = APPCORE_BASE_SUSPENDED_STATE_WILL_ENTER_SUSPEND;
636
637         appcore_base_flush_memory();
638         __context.tid = 0;
639
640         if (!__context.allowed_bg && !__context.suspended_state) {
641                 _DBG("[__SUSPEND__] flush case");
642                 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
643                 __context.suspended_state = true;
644         }
645
646         return FALSE;
647 }
648
649 static void __add_suspend_timer(void)
650 {
651         __context.tid = g_timeout_add_seconds(5, __flush_memory, NULL);
652 }
653
654 static void __remove_suspend_timer(void)
655 {
656         if (__context.tid > 0) {
657                 g_source_remove(__context.tid);
658                 __context.tid = 0;
659         }
660 }
661
662 static void __on_receive_suspend_signal(GDBusConnection *connection,
663                                         const gchar *sender_name,
664                                         const gchar *object_path,
665                                         const gchar *interface_name,
666                                         const gchar *signal_name,
667                                         GVariant *parameters,
668                                         gpointer user_data)
669 {
670         gint suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
671         gint pid;
672         gint status;
673
674         if (g_strcmp0(signal_name, RESOURCED_FREEZER_SIGNAL) == 0) {
675                 g_variant_get(parameters, "(ii)", &status, &pid);
676                 if (pid == getpid() && status == 0) {
677                         if (!__context.allowed_bg && __context.suspended_state) {
678                                 __remove_suspend_timer();
679                                 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
680                                 __context.suspended_state = false;
681                                 __add_suspend_timer();
682                         }
683                 }
684         }
685 }
686
687 static int __init_suspend_dbus_handler(void)
688 {
689         GError *err = NULL;
690
691         if (__suspend_dbus_handler_initialized)
692                 return 0;
693
694         if (!__bus) {
695                 __bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
696                 if (!__bus) {
697                         _ERR("Failed to connect to the D-BUS daemon: %s",
698                                                 err->message);
699                         g_error_free(err);
700                         return -1;
701                 }
702         }
703
704         __suspend_dbus_handler_initialized = g_dbus_connection_signal_subscribe(
705                                                 __bus,
706                                                 NULL,
707                                                 RESOURCED_FREEZER_INTERFACE,
708                                                 RESOURCED_FREEZER_SIGNAL,
709                                                 RESOURCED_FREEZER_PATH,
710                                                 NULL,
711                                                 G_DBUS_SIGNAL_FLAGS_NONE,
712                                                 __on_receive_suspend_signal,
713                                                 NULL,
714                                                 NULL);
715         if (__suspend_dbus_handler_initialized == 0) {
716                 _ERR("g_dbus_connection_signal_subscribe() is failed.");
717                 return -1;
718         }
719
720         _DBG("[__SUSPEND__] suspend signal initialized");
721
722         return 0;
723 }
724
725 static void __fini_suspend_dbus_handler(void)
726 {
727         if (__bus == NULL)
728                 return;
729
730         if (__suspend_dbus_handler_initialized) {
731                 g_dbus_connection_signal_unsubscribe(__bus,
732                                 __suspend_dbus_handler_initialized);
733                 __suspend_dbus_handler_initialized = 0;
734         }
735
736         g_object_unref(__bus);
737         __bus = NULL;
738 }
739
740 static int __get_locale_resource_dir(char *locale_dir, int size)
741 {
742         const char *res_path;
743
744         res_path = aul_get_app_resource_path();
745         if (res_path == NULL) {
746                 _ERR("Failed to get resource path");
747                 return -1;
748         }
749
750         snprintf(locale_dir, size, "%s" PATH_LOCALE, res_path);
751         if (access(locale_dir, R_OK) != 0)
752                 _DBG("%s does not exist", locale_dir);
753
754         return 0;
755 }
756
757 static int __get_app_name(const char *appid, char **name)
758 {
759         char *name_token = NULL;
760
761         if (appid == NULL)
762                 return -1;
763
764         /* com.vendor.name -> name */
765         name_token = strrchr(appid, '.');
766         if (name_token == NULL)
767                 return -1;
768
769         name_token++;
770
771         *name = strdup(name_token);
772         if (*name == NULL)
773                 return -1;
774
775         return 0;
776 }
777
778 static int __set_i18n(const char *domain, const char *dir)
779 {
780         char *r;
781         char *lan;
782
783         if (domain == NULL) {
784                 errno = EINVAL;
785                 return -1;
786         }
787
788         if (dir) {
789                 if (__locale_dir)
790                         free(__locale_dir);
791                 __locale_dir = strdup(dir);
792         }
793
794         __update_lang();
795         __update_region();
796
797         r = setlocale(LC_ALL, "");
798         /* if locale is not set properly, try again to set as language base */
799         if (r == NULL) {
800                 lan = vconf_get_str(VCONFKEY_LANGSET);
801                 if (lan != NULL) {
802                         r = setlocale(LC_ALL, lan);
803                         _DBG("*****appcore setlocale=%s\n", r);
804                         free(lan);
805                 }
806         }
807         if (r == NULL)
808                 _ERR("appcore: setlocale() error");
809
810         r = bindtextdomain(domain, dir);
811         if (r == NULL)
812                 _ERR("appcore: bindtextdomain() error");
813
814         r = textdomain(domain);
815         if (r == NULL)
816                 _ERR("appcore: textdomain() error");
817
818         return 0;
819 }
820
821 EXPORT_API int appcore_base_on_set_i18n(void)
822 {
823         int r;
824         char locale_dir[PATH_MAX];
825         char appid[PATH_MAX];
826         char *name = NULL;
827
828         r = aul_app_get_appid_bypid(getpid(), appid, PATH_MAX);
829         if (r < 0) {
830                 _ERR("Failed to get application ID - pid(%d)", getpid());
831                 return -1;
832         }
833
834         r = __get_app_name(appid, &name);
835         if (r < 0)
836                 return -1;
837
838         r = __get_locale_resource_dir(locale_dir, sizeof(locale_dir));
839         if (r < 0) {
840                 free(name);
841                 return -1;
842         }
843
844         r = __set_i18n(name, locale_dir);
845         if (r < 0) {
846                 free(name);
847                 return -1;
848         }
849
850         free(name);
851
852         return 0;
853 }
854
855 EXPORT_API int appcore_base_set_i18n(const char *domain_name, const char *dir_name)
856 {
857         return __set_i18n(domain_name, dir_name);
858 }
859
860 EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, void *data)
861 {
862         int i;
863         int r;
864
865         __context.ops = ops;
866         __context.argc = argc;
867         __context.argv = argv;
868         __context.data = data;
869         __context.tid = 0;
870         __context.suspended_state = false;
871         __context.allowed_bg = false;
872
873         if (__context.ops.init)
874                 __context.ops.init(argc, argv, data);
875
876         if (__context.ops.set_i18n)
877                 __context.ops.set_i18n(__context.data);
878
879         __init_suspend_dbus_handler();
880
881         if (!__context.dirty) {
882                 __context.dirty = true;
883
884                 for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
885                         if (__exist_callback(i)) {
886                                 if (__context.ops.set_event)
887                                         __context.ops.set_event(i, __context.data);
888                         }
889                 }
890         }
891
892         if (__context.ops.create) {
893                 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:CREATE");
894                 r = __context.ops.create(__context.data);
895                 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
896                 if (r < 0) {
897                         aul_status_update(STATUS_DYING);
898                         return 0;
899                 }
900         }
901
902         if (__context.ops.run)
903                 __context.ops.run(__context.data);
904
905         return 0;
906 }
907
908 EXPORT_API void appcore_base_fini(void)
909 {
910         int i;
911
912         aul_status_update(STATUS_DYING);
913         if (__context.ops.terminate) {
914                 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:TERMINATE");
915                 __context.ops.terminate(__context.data);
916                 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
917         }
918
919         for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
920                 if (__exist_callback(i)) {
921                         if (__context.ops.unset_event)
922                                 __context.ops.unset_event(i, __context.data);
923                 }
924         }
925
926         g_list_free_full(__events, free);
927         __events = NULL;
928         __fini_suspend_dbus_handler();
929
930         if (__locale_dir) {
931                 free(__locale_dir);
932                 __locale_dir = NULL;
933         }
934
935         __context.dirty = false;
936
937         if (__context.ops.finish)
938                 __context.ops.finish();
939 }
940
941 EXPORT_API int appcore_base_flush_memory(void)
942 {
943         malloc_trim(0);
944         return 0;
945 }
946
947 EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
948 {
949         int ret;
950         const char **tep_path;
951         int len = 0;
952         int i;
953         const char *bg;
954         int dummy = 0;
955
956         switch (type) {
957         case AUL_START:
958                 _DBG("[APP %d]     AUL event: AUL_START", getpid());
959                 tep_path = bundle_get_str_array(b, AUL_TEP_PATH, &len);
960                 if (tep_path) {
961                         for (i = 0; i < len; i++) {
962                                 ret = aul_check_tep_mount(tep_path[i]);
963                                 if (ret == -1) {
964                                         _ERR("mount request not completed within 1 sec");
965                                         exit(-1);
966                                 }
967                         }
968                 }
969
970                 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
971                 if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
972                         _DBG("[__SUSPEND__] allowed background");
973                         __context.allowed_bg = true;
974                         __remove_suspend_timer();
975                 }
976
977                 if (__context.ops.control) {
978                         traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:RESET");
979                         __context.ops.control(b, __context.data);
980                         traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
981                 }
982                 break;
983         case AUL_RESUME:
984                 _DBG("[APP %d]     AUL event: AUL_RESUME", getpid());
985                 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
986                 if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
987                         _DBG("[__SUSPEND__] allowed background");
988                         __context.allowed_bg = true;
989                         __remove_suspend_timer();
990                 }
991                 break;
992         case AUL_TERMINATE:
993                 _DBG("[APP %d]     AUL event: AUL_TERMINATE", getpid());
994                 aul_status_update(STATUS_DYING);
995                 if (!__context.allowed_bg)
996                         __remove_suspend_timer();
997
998                 if (__context.ops.exit)
999                         __context.ops.exit(__context.data);
1000                 break;
1001         case AUL_TERMINATE_BGAPP:
1002                 _DBG("[APP %d]     AUL event: AUL_TERMINATE_BGAPP", getpid());
1003                 if (!__context.allowed_bg)
1004                         __remove_suspend_timer();
1005                 break;
1006         case AUL_WAKE:
1007                 _DBG("[APP %d]     AUL event: AUL_WAKE", getpid());
1008                 if (!__context.allowed_bg && __context.suspended_state) {
1009                         int suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
1010                         __remove_suspend_timer();
1011                         __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
1012                         __context.suspended_state = false;
1013                 }
1014                 break;
1015         case AUL_SUSPEND:
1016                 _DBG("[APP %d]     AUL event: AUL_SUSPEND", getpid());
1017                 if (!__context.allowed_bg && !__context.suspended_state) {
1018                         __remove_suspend_timer();
1019                         __flush_memory(NULL);
1020                 }
1021                 break;
1022         case AUL_UPDATE_REQUESTED:
1023                 _DBG("[APP %d]     AUL event: AUL_UPDATE_REQUESTED", getpid());
1024                 __invoke_callback((void *)&dummy, APPCORE_BASE_EVENT_UPDATE_REQUESTED);
1025                 break;
1026         default:
1027                 _DBG("[APP %d]     AUL event: %d", getpid(), type);
1028                 /* do nothing */
1029                 break;
1030         }
1031
1032         return 0;
1033 }
1034
1035 EXPORT_API int appcore_base_on_create(void)
1036 {
1037         int r;
1038         r = aul_launch_init(__context.ops.receive, NULL);
1039         if (r < 0) {
1040                 _ERR("Aul init failed: %d", r);
1041                 return -1;
1042         }
1043
1044         r = aul_launch_argv_handler(__context.argc, __context.argv);
1045         if (r < 0) {
1046                 _ERR("Aul argv handler failed: %d", r);
1047                 return -1;
1048         }
1049
1050         return 0;
1051 }
1052
1053 EXPORT_API int appcore_base_on_control(bundle *b)
1054 {
1055         return 0;
1056 }
1057
1058 EXPORT_API int appcore_base_on_terminate()
1059 {
1060         aul_finalize();
1061         if (__context.ops.exit)
1062                 __context.ops.exit(__context.data);
1063
1064         return 0;
1065 }
1066
1067 EXPORT_API void appcore_base_on_set_event(enum appcore_base_event event)
1068 {
1069         int r;
1070
1071         switch (event) {
1072         case APPCORE_BASE_EVENT_LOW_MEMORY:
1073                 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
1074                 break;
1075         case APPCORE_BASE_EVENT_LOW_BATTERY:
1076                 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
1077                 break;
1078         case APPCORE_BASE_EVENT_LANG_CHANGE:
1079                 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_language_change, NULL);
1080                 break;
1081         case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
1082                 __register_rotation_changed_event();
1083                 break;
1084         case APPCORE_BASE_EVENT_REGION_CHANGE:
1085                 r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change, NULL);
1086                 if (r < 0)
1087                         break;
1088
1089                 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change, NULL);
1090                 break;
1091         case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
1092                 break;
1093
1094         default:
1095                 break;
1096         }
1097
1098 }
1099
1100 EXPORT_API void appcore_base_on_unset_event(enum appcore_base_event event)
1101 {
1102         int r;
1103
1104         switch (event) {
1105         case APPCORE_BASE_EVENT_LOW_MEMORY:
1106                 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
1107                 break;
1108         case APPCORE_BASE_EVENT_LOW_BATTERY:
1109                 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
1110                 break;
1111         case APPCORE_BASE_EVENT_LANG_CHANGE:
1112                 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_language_change);
1113                 break;
1114         case APPCORE_BASE_EVENT_DEVICE_ORIENTATION_CHANGED:
1115                 __unregister_rotation_changed_event();
1116                 break;
1117         case APPCORE_BASE_EVENT_REGION_CHANGE:
1118                 r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change);
1119                 if (r < 0)
1120                         break;
1121                 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change);
1122                 break;
1123         case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
1124                 break;
1125         default:
1126                 break;
1127         }
1128 }
1129
1130 EXPORT_API appcore_base_event_h appcore_base_add_event(enum appcore_base_event event,
1131                 appcore_base_event_cb cb, void *data)
1132 {
1133         appcore_base_event_node *node;
1134
1135         if (__context.dirty && !__exist_callback(event)) {
1136                 if (__context.ops.set_event)
1137                         __context.ops.set_event(event, __context.data);
1138         }
1139
1140         node = malloc(sizeof(appcore_base_event_node));
1141
1142         if (node == NULL)
1143                 return NULL;
1144
1145         node->cb = cb;
1146         node->type = event;
1147         node->data = data;
1148         __events = g_list_append(__events, node);
1149
1150         return node;
1151 }
1152
1153 EXPORT_API int appcore_base_remove_event(appcore_base_event_h handle)
1154 {
1155         appcore_base_event_node *node = handle;
1156         enum appcore_base_event event;
1157
1158         if (!node || !g_list_find(__events, node))
1159                 return -1;
1160
1161         event = node->type;
1162         __events = g_list_remove(__events, node);
1163         free(node);
1164         if (__context.dirty && !__exist_callback(event)) {
1165                 if (__context.ops.unset_event)
1166                         __context.ops.unset_event(event, __context.data);
1167         }
1168
1169         return 0;
1170 }
1171
1172 EXPORT_API int appcore_base_raise_event(void *event, enum appcore_base_event type)
1173 {
1174         __invoke_callback(event, type);
1175         return 0;
1176 }
1177
1178 EXPORT_API int appcore_base_get_rotation_state(enum appcore_base_rm *curr)
1179 {
1180         if (curr == NULL)
1181                 return -1;
1182
1183         if (!__rotation.ref)
1184                 return -1;
1185
1186         *curr = __rotation.rm;
1187         return 0;
1188 }
1189
1190 EXPORT_API bool appcore_base_is_bg_allowed(void)
1191 {
1192         return __context.allowed_bg;
1193 }
1194
1195 EXPORT_API bool appcore_base_is_suspended(void)
1196 {
1197         return __context.suspended_state;
1198 }
1199
1200 EXPORT_API void appcore_base_toggle_suspended_state(void)
1201 {
1202         __context.suspended_state ^= __context.suspended_state;
1203 }
1204
1205 static int __on_receive(aul_type type, bundle *b, void *data)
1206 {
1207         return appcore_base_on_receive(type, b);
1208 }
1209
1210 static int __on_create(void *data)
1211 {
1212         return appcore_base_on_create();
1213 }
1214
1215 static int __on_control(bundle *b, void *data)
1216 {
1217         return appcore_base_on_control(b);
1218 }
1219
1220 static int __on_terminate(void *data)
1221 {
1222         return appcore_base_on_terminate();
1223 }
1224
1225 static int __on_set_i18n(void *data)
1226 {
1227         return appcore_base_on_set_i18n();
1228 }
1229
1230 static void __on_set_event(enum appcore_base_event event, void *data)
1231 {
1232         return appcore_base_on_set_event(event);
1233 }
1234
1235 static void __on_unset_event(enum appcore_base_event event, void *data)
1236 {
1237         return appcore_base_on_unset_event(event);
1238 }
1239
1240 EXPORT_API appcore_base_ops appcore_base_get_default_ops(void)
1241 {
1242         appcore_base_ops ops;
1243
1244         ops.create = __on_create;
1245         ops.control = __on_control;
1246         ops.terminate = __on_terminate;
1247         ops.receive = __on_receive;
1248         ops.set_i18n = __on_set_i18n;
1249         ops.init = NULL;
1250         ops.finish = NULL;
1251         ops.run = NULL;
1252         ops.exit = NULL;
1253         ops.set_event = __on_set_event;
1254         ops.unset_event = __on_unset_event;
1255
1256         return ops;
1257 }
1258
1259