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