2 * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <sys/types.h>
30 #include <linux/limits.h>
37 #include <bundle_internal.h>
38 #include "appcore_base.h"
39 #include "appcore_base_private.h"
41 #define PATH_LOCALE "locale"
42 #define RESOURCED_FREEZER_PATH "/Org/Tizen/Resourced/Freezer"
43 #define RESOURCED_FREEZER_INTERFACE "org.tizen.resourced.freezer"
44 #define RESOURCED_FREEZER_SIGNAL "FreezerState"
46 typedef struct _appcore_base_context {
54 } appcore_base_context;
56 typedef struct _appcore_base_event_node {
58 appcore_base_event_cb cb;
60 } appcore_base_event_node;
62 static appcore_base_context __context;
63 static GList *__events;
64 static GDBusConnection *__bus;
65 static guint __suspend_dbus_handler_initialized;
66 static char *__locale_dir;
68 static void __invoke_callback(void *event, int type)
70 GList *iter = __events;
73 appcore_base_event_node *node = iter->data;
75 if (node->type == type)
76 node->cb(event, node->data);
77 iter = g_list_next(iter);
81 static bool __exist_callback(int type)
83 GList *iter = __events;
86 appcore_base_event_node *node = iter->data;
88 if (node->type == type)
91 iter = g_list_next(iter);
97 static void __on_low_memory(keynode_t *key, void *data)
101 val = vconf_keynode_get_int(key);
103 if (val >= VCONFKEY_SYSMAN_LOW_MEMORY_SOFT_WARNING) {
104 __invoke_callback(key, APPCORE_BASE_EVENT_LOW_MEMORY);
109 static void __on_low_battery(keynode_t *key, void *data)
113 val = vconf_keynode_get_int(key);
115 if (val <= VCONFKEY_SYSMAN_BAT_CRITICAL_LOW)
116 __invoke_callback(key, APPCORE_BASE_EVENT_LOW_BATTERY);
119 static void __free_children_langs(gpointer data)
121 GList *list = (GList *)data;
126 g_list_free_full(list, (GDestroyNotify)free);
129 static gint __compare_langs(gconstpointer a, gconstpointer b)
134 static GHashTable *__get_lang_table(void)
138 struct dirent *dentry;
140 struct stat stat_buf;
146 if (__locale_dir == NULL || __locale_dir[0] == '\0')
149 table = g_hash_table_new_full(g_str_hash, g_str_equal,
150 free, __free_children_langs);
152 _ERR("Out of memory");
156 dp = opendir(__locale_dir);
158 g_hash_table_destroy(table);
162 while ((dentry = readdir(dp)) != NULL) {
163 if (!strcmp(dentry->d_name, ".") ||
164 !strcmp(dentry->d_name, ".."))
167 snprintf(buf, sizeof(buf), "%s/%s",
168 __locale_dir, dentry->d_name);
169 ret = stat(buf, &stat_buf);
170 if (ret != 0 || !S_ISDIR(stat_buf.st_mode))
173 dup_lang = strdup(dentry->d_name);
174 if (dup_lang == NULL) {
175 _ERR("Out of memory");
179 token = strtok(dup_lang, "_");
185 list = (GList *)g_hash_table_lookup(table, token);
187 list = g_list_append(list, strdup(dentry->d_name));
188 g_hash_table_insert(table, strdup(token), list);
190 list = g_list_append(list, strdup(dentry->d_name));
199 static char *__get_string_before(const char *str, const char *delim)
205 dup_str = strdup(str);
209 token = strtok(dup_str, delim);
215 new_str = strdup(token);
221 static GList *__append_langs(const char *lang, GList *list, GHashTable *table)
234 found = g_list_find_custom(g_list_first(list), lang,
237 tmp = (char *)found->data;
238 list = g_list_remove(list, tmp);
239 list = g_list_append(list, tmp);
243 extract_lang = __get_string_before(lang, ".");
244 if (extract_lang == NULL)
247 parent_lang = __get_string_before(extract_lang, "_");
248 if (parent_lang == NULL) {
253 child_list = g_hash_table_lookup(table, parent_lang);
254 if (child_list == NULL) {
260 found = g_list_find_custom(g_list_first(child_list),
261 extract_lang, __compare_langs);
263 tmp = (char *)found->data;
264 child_list = g_list_remove(child_list, tmp);
265 list = g_list_append(list, tmp);
272 found = g_list_find_custom(g_list_first(child_list),
273 parent_lang, __compare_langs);
275 tmp = (char *)found->data;
276 child_list = g_list_remove(child_list, tmp);
277 list = g_list_append(list, tmp);
283 child_iter = g_list_first(child_list);
285 child_lang = (char *)child_iter->data;
286 child_iter = g_list_next(child_iter);
288 list = g_list_append(list, strdup(child_lang));
289 child_list = g_list_remove(child_list, child_lang);
298 static GList *__split_language(const char *lang)
304 dup_lang = strdup(lang);
305 if (dup_lang == NULL) {
306 _ERR("Out of memory");
310 token = strtok(dup_lang, ":");
311 while (token != NULL) {
312 list = g_list_append(list, strdup(token));
313 token = strtok(NULL, ":");
320 static GList *__append_default_langs(GList *list)
322 const char *langs[] = {"en_US", "en_GB", "en"};
326 for (i = 0; i < (sizeof(langs) / sizeof(langs[0])); i++) {
327 found = g_list_find_custom(g_list_first(list), langs[i],
330 list = g_list_append(list, strdup(langs[i]));
336 static char *__get_language(const char *lang)
340 GList *lang_list = NULL;
343 char buf[LINE_MAX] = {'\0'};
346 list = __split_language(lang);
350 table = __get_lang_table();
352 g_list_free_full(list, free);
356 iter = g_list_first(list);
358 language = (char *)iter->data;
359 lang_list = __append_langs(language, lang_list, table);
360 iter = g_list_next(iter);
362 g_list_free_full(list, free);
363 g_hash_table_destroy(table);
365 lang_list = __append_default_langs(lang_list);
366 iter = g_list_first(lang_list);
368 language = (char *)iter->data;
370 if (buf[0] == '\0') {
371 snprintf(buf, sizeof(buf), "%s", language);
373 n = sizeof(buf) - strlen(buf) - 1;
374 strncat(buf, ":", n);
375 n = sizeof(buf) - strlen(buf) - 1;
376 strncat(buf, language, n);
379 iter = g_list_next(iter);
381 g_list_free_full(lang_list, free);
386 static void __update_lang(void)
392 lang = vconf_get_str(VCONFKEY_LANGSET);
394 /* TODO: Use VCONFKEY_SETAPPL_LANGUAGES key */
395 language = __get_language(lang);
397 _DBG("*****language(%s)", language);
398 setenv("LANGUAGE", language, 1);
401 setenv("LANGUAGE", lang, 1);
403 setenv("LANG", lang, 1);
404 setenv("LC_MESSAGES", lang, 1);
405 r = setlocale(LC_ALL, "");
407 r = setlocale(LC_ALL, lang);
409 _DBG("*****appcore setlocale=%s\n", r);
415 static void __update_region(void)
420 region = vconf_get_str(VCONFKEY_REGIONFORMAT);
422 setenv("LC_CTYPE", region, 1);
423 setenv("LC_NUMERIC", region, 1);
424 setenv("LC_TIME", region, 1);
425 setenv("LC_COLLATE", region, 1);
426 setenv("LC_MONETARY", region, 1);
427 setenv("LC_PAPER", region, 1);
428 setenv("LC_NAME", region, 1);
429 setenv("LC_ADDRESS", region, 1);
430 setenv("LC_TELEPHONE", region, 1);
431 setenv("LC_MEASUREMENT", region, 1);
432 setenv("LC_IDENTIFICATION", region, 1);
433 r = setlocale(LC_ALL, "");
435 _DBG("*****appcore setlocale=%s\n", r);
441 static void __on_language_change(keynode_t *key, void *data)
445 val = vconf_keynode_get_str(key);
448 __invoke_callback((void *)val, APPCORE_BASE_EVENT_LANG_CHANGE);
451 static void __on_region_change(keynode_t *key, void *data)
456 name = vconf_keynode_get_name(key);
457 if (!strcmp(name, VCONFKEY_REGIONFORMAT))
458 val = vconf_keynode_get_str(key);
461 __invoke_callback((void *)val, APPCORE_BASE_EVENT_REGION_CHANGE);
464 static gboolean __flush_memory(gpointer data)
466 int suspend = APPCORE_BASE_SUSPENDED_STATE_WILL_ENTER_SUSPEND;
468 appcore_base_flush_memory();
471 if (!__context.allowed_bg && !__context.suspended_state) {
472 _DBG("[__SUSPEND__] flush case");
473 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
474 __context.suspended_state = true;
480 static void __add_suspend_timer(void)
482 __context.tid = g_timeout_add_seconds(5, __flush_memory, NULL);
485 static void __remove_suspend_timer(void)
487 if (__context.tid > 0) {
488 g_source_remove(__context.tid);
493 static void __on_receive_suspend_signal(GDBusConnection *connection,
494 const gchar *sender_name,
495 const gchar *object_path,
496 const gchar *interface_name,
497 const gchar *signal_name,
498 GVariant *parameters,
501 gint suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
505 if (g_strcmp0(signal_name, RESOURCED_FREEZER_SIGNAL) == 0) {
506 g_variant_get(parameters, "(ii)", &status, &pid);
507 if (pid == getpid() && status == 0) {
508 if (!__context.allowed_bg && __context.suspended_state) {
509 __remove_suspend_timer();
510 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
511 __context.suspended_state = false;
512 __add_suspend_timer();
518 static int __init_suspend_dbus_handler(void)
522 if (__suspend_dbus_handler_initialized)
526 __bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
528 _ERR("Failed to connect to the D-BUS daemon: %s",
535 __suspend_dbus_handler_initialized = g_dbus_connection_signal_subscribe(
538 RESOURCED_FREEZER_INTERFACE,
539 RESOURCED_FREEZER_SIGNAL,
540 RESOURCED_FREEZER_PATH,
542 G_DBUS_SIGNAL_FLAGS_NONE,
543 __on_receive_suspend_signal,
546 if (__suspend_dbus_handler_initialized == 0) {
547 _ERR("g_dbus_connection_signal_subscribe() is failed.");
551 _DBG("[__SUSPEND__] suspend signal initialized");
556 static void __fini_suspend_dbus_handler(void)
561 if (__suspend_dbus_handler_initialized) {
562 g_dbus_connection_signal_unsubscribe(__bus,
563 __suspend_dbus_handler_initialized);
564 __suspend_dbus_handler_initialized = 0;
567 g_object_unref(__bus);
571 static int __get_locale_resource_dir(char *locale_dir, int size)
573 const char *res_path;
575 res_path = aul_get_app_resource_path();
576 if (res_path == NULL) {
577 _ERR("Failed to get resource path");
581 snprintf(locale_dir, size, "%s" PATH_LOCALE, res_path);
582 if (access(locale_dir, R_OK) != 0)
588 static int __get_app_name(const char *appid, char **name)
590 char *name_token = NULL;
595 /* com.vendor.name -> name */
596 name_token = strrchr(appid, '.');
597 if (name_token == NULL)
602 *name = strdup(name_token);
609 static int __set_i18n(const char *domain, const char *dir)
614 if (domain == NULL) {
622 __locale_dir = strdup(dir);
628 r = setlocale(LC_ALL, "");
629 /* if locale is not set properly, try again to set as language base */
631 lan = vconf_get_str(VCONFKEY_LANGSET);
633 r = setlocale(LC_ALL, lan);
634 _DBG("*****appcore setlocale=%s\n", r);
639 _ERR("appcore: setlocale() error");
641 r = bindtextdomain(domain, dir);
643 _ERR("appcore: bindtextdomain() error");
645 r = textdomain(domain);
647 _ERR("appcore: textdomain() error");
652 EXPORT_API int appcore_base_on_set_i18n(void)
655 char locale_dir[PATH_MAX];
656 char appid[PATH_MAX];
659 r = aul_app_get_appid_bypid(getpid(), appid, PATH_MAX);
663 r = __get_app_name(appid, &name);
667 r = __get_locale_resource_dir(locale_dir, sizeof(locale_dir));
673 r = __set_i18n(name, locale_dir);
684 EXPORT_API int appcore_base_init(appcore_base_ops ops, int argc, char **argv, void *data)
687 __context.argc = argc;
688 __context.argv = argv;
689 __context.data = data;
691 __context.suspended_state = false;
692 __context.allowed_bg = false;
694 if (__context.ops.set_i18n)
695 __context.ops.set_i18n(__context.data);
697 __init_suspend_dbus_handler();
699 if (__context.ops.create && __context.ops.create(__context.data) < 0) {
700 aul_status_update(STATUS_DYING);
704 if (__context.ops.run)
705 __context.ops.run(__context.data);
710 EXPORT_API void appcore_base_fini(void)
714 for (i = APPCORE_BASE_EVENT_START + 1; i < APPCORE_BASE_EVENT_MAX; i++) {
715 if (__exist_callback(i)) {
716 if (__context.ops.unset_event)
717 __context.ops.unset_event(i, __context.data);
721 g_list_free_full(__events, free);
723 __fini_suspend_dbus_handler();
725 aul_status_update(STATUS_DYING);
726 if (__context.ops.terminate)
727 __context.ops.terminate(__context.data);
735 EXPORT_API int appcore_base_flush_memory(void)
741 EXPORT_API int appcore_base_on_receive(aul_type type, bundle *b)
744 const char **tep_path;
752 _DBG("[APP %d] AUL event: AUL_START", getpid());
753 tep_path = bundle_get_str_array(b, AUL_TEP_PATH, &len);
755 for (i = 0; i < len; i++) {
756 ret = aul_check_tep_mount(tep_path[i]);
758 _ERR("mount request not completed within 1 sec");
764 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
765 if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
766 _DBG("[__SUSPEND__] allowed background");
767 __context.allowed_bg = true;
768 __remove_suspend_timer();
771 if (__context.ops.control)
772 __context.ops.control(b, __context.data);
775 _DBG("[APP %d] AUL event: AUL_RESUME", getpid());
776 bg = bundle_get_val(b, AUL_K_ALLOWED_BG);
777 if (bg && strncmp(bg, "ALLOWED_BG", strlen("ALLOWED_BG")) == 0) {
778 _DBG("[__SUSPEND__] allowed background");
779 __context.allowed_bg = true;
780 __remove_suspend_timer();
784 _DBG("[APP %d] AUL event: AUL_TERMINATE", getpid());
785 aul_status_update(STATUS_DYING);
786 if (!__context.allowed_bg)
787 __remove_suspend_timer();
789 if (__context.ops.exit)
790 __context.ops.exit(__context.data);
792 case AUL_TERMINATE_BGAPP:
793 _DBG("[APP %d] AUL event: AUL_TERMINATE_BGAPP", getpid());
794 if (!__context.allowed_bg)
795 __remove_suspend_timer();
798 _DBG("[APP %d] AUL event: AUL_WAKE", getpid());
799 if (!__context.allowed_bg && __context.suspended_state) {
800 int suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
801 __remove_suspend_timer();
802 __invoke_callback((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
803 __context.suspended_state = false;
807 _DBG("[APP %d] AUL event: AUL_SUSPEND", getpid());
808 if (!__context.allowed_bg && !__context.suspended_state) {
809 __remove_suspend_timer();
810 __flush_memory(NULL);
813 case AUL_UPDATE_REQUESTED:
814 _DBG("[APP %d] AUL event: AUL_UPDATE_REQUESTED", getpid());
815 __invoke_callback((void *)&dummy, APPCORE_BASE_EVENT_UPDATE_REQUESTED);
818 _DBG("[APP %d] AUL event: %d", getpid(), type);
826 EXPORT_API int appcore_base_on_create(void)
829 r = aul_launch_init(__context.ops.receive, NULL);
831 _ERR("Aul init failed: %d", r);
835 r = aul_launch_argv_handler(__context.argc, __context.argv);
837 _ERR("Aul argv handler failed: %d", r);
844 EXPORT_API int appcore_base_on_control(bundle *b)
849 EXPORT_API int appcore_base_on_terminate()
852 if (__context.ops.exit)
853 __context.ops.exit(__context.data);
858 EXPORT_API void appcore_base_on_set_event(enum appcore_base_event event)
863 case APPCORE_BASE_EVENT_LOW_MEMORY:
864 vconf_notify_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory, NULL);
866 case APPCORE_BASE_EVENT_LOW_BATTERY:
867 vconf_notify_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery, NULL);
869 case APPCORE_BASE_EVENT_LANG_CHANGE:
870 vconf_notify_key_changed(VCONFKEY_LANGSET, __on_language_change, NULL);
872 case APPCORE_BASE_EVENT_REGION_CHANGE:
873 r = vconf_notify_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change, NULL);
877 vconf_notify_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change, NULL);
879 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
888 EXPORT_API void appcore_base_on_unset_event(enum appcore_base_event event)
893 case APPCORE_BASE_EVENT_LOW_MEMORY:
894 vconf_ignore_key_changed(VCONFKEY_SYSMAN_LOW_MEMORY, __on_low_memory);
896 case APPCORE_BASE_EVENT_LOW_BATTERY:
897 vconf_ignore_key_changed(VCONFKEY_SYSMAN_BATTERY_STATUS_LOW, __on_low_battery);
899 case APPCORE_BASE_EVENT_LANG_CHANGE:
900 vconf_ignore_key_changed(VCONFKEY_LANGSET, __on_language_change);
902 case APPCORE_BASE_EVENT_REGION_CHANGE:
903 r = vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT, __on_region_change);
906 vconf_ignore_key_changed(VCONFKEY_REGIONFORMAT_TIME1224, __on_region_change);
908 case APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE:
915 EXPORT_API appcore_base_event_h appcore_base_add_event(enum appcore_base_event event,
916 appcore_base_event_cb cb, void *data)
918 appcore_base_event_node *node;
920 if (!__exist_callback(event)) {
921 if (__context.ops.set_event)
922 __context.ops.set_event(event, __context.data);
925 node = malloc(sizeof(appcore_base_event_node));
933 __events = g_list_append(__events, node);
938 EXPORT_API int appcore_base_remove_event(appcore_base_event_h handle)
940 appcore_base_event_node *node = handle;
941 enum appcore_base_event event;
944 __events = g_list_remove(__events, node);
946 if (!__exist_callback(event)) {
947 if (__context.ops.unset_event)
948 __context.ops.unset_event(event, __context.data);
954 EXPORT_API int appcore_base_raise_event(void *event, enum appcore_base_event type)
956 __invoke_callback(event, type);
960 EXPORT_API int appcore_base_get_rotation_state(enum appcore_base_rm *curr)
965 EXPORT_API bool appcore_base_is_bg_allowed(void)
967 return __context.allowed_bg;
970 EXPORT_API bool appcore_base_is_suspended(void)
972 return __context.suspended_state;
975 EXPORT_API void appcore_base_toggle_suspended_state(void)
977 __context.suspended_state ^= __context.suspended_state;
980 static int __on_receive(aul_type type, bundle *b, void *data)
982 return appcore_base_on_receive(type, b);
985 static int __on_create(void *data)
987 return appcore_base_on_create();
990 static int __on_control(bundle *b, void *data)
992 return appcore_base_on_control(b);
995 static int __on_terminate(void *data)
997 return appcore_base_on_terminate();
1000 static int __on_set_i18n(void *data)
1002 return appcore_base_on_set_i18n();
1005 static void __on_set_event(enum appcore_base_event event, void *data)
1007 return appcore_base_on_set_event(event);
1010 static void __on_unset_event(enum appcore_base_event event, void *data)
1012 return appcore_base_on_unset_event(event);
1015 EXPORT_API appcore_base_ops appcore_base_get_default_ops(void)
1017 appcore_base_ops ops;
1019 ops.create = __on_create;
1020 ops.control = __on_control;
1021 ops.terminate = __on_terminate;
1022 ops.receive = __on_receive;
1023 ops.set_i18n = __on_set_i18n;
1026 ops.set_event = __on_set_event;
1027 ops.unset_event = __on_unset_event;