2 * Copyright (c) 2000 - 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.
17 #include <sys/types.h>
18 #include <sys/socket.h>
27 #include <Ecore_Wayland.h>
28 #include <wayland-client.h>
29 #include <wayland-tbm-client.h>
30 #include <tizen-extension-client-protocol.h>
33 #include <Ecore_Evas.h>
34 #include <Ecore_Input_Evas.h>
35 #include <Elementary.h>
36 #include <glib-object.h>
43 #include <bundle_internal.h>
46 #include "appcore-internal.h"
47 #include "appcore-efl.h"
48 #include <system_info.h>
51 static bool resource_reclaiming = TRUE;
52 static int tmp_val = 0;
58 Ecore_Event_Handler *hshow;
59 Ecore_Event_Handler *hhide;
60 Ecore_Event_Handler *hvchange;
61 Ecore_Event_Handler *hlower;
62 Ecore_Event_Handler *hcmsg; /* WM_ROTATE */
64 Ecore_Timer *mftimer; /* Ecore Timer for memory flushing */
66 struct appcore *app_core;
67 void (*prepare_to_suspend) (void *data);
68 void (*exit_from_suspend) (void *data);
69 struct appcore_ops *ops;
70 void (*mfcb) (void); /* Memory Flushing Callback */
75 int (*rot_cb) (void *event_info, enum appcore_rm, void *);
77 enum appcore_rm rot_mode;
81 static struct ui_priv priv;
83 static const char *_ae_name[AE_MAX] = {
84 [AE_UNKNOWN] = "UNKNOWN",
85 [AE_CREATE] = "CREATE",
86 [AE_TERMINATE] = "TERMINATE",
88 [AE_RESUME] = "RESUME",
90 [AE_LOWMEM_POST] = "LOWMEM_POST",
91 [AE_MEM_FLUSH] = "MEM_FLUSH",
92 [AE_UPDATE_REQUESTED] = "UPDATE_REQUESTED",
95 static const char *_as_name[] = {
97 [AS_CREATED] = "CREATED",
98 [AS_RUNNING] = "RUNNING",
99 [AS_PAUSED] = "PAUSED",
100 [AS_DYING] = "DYING",
103 static bool b_active = FALSE;
104 static bool first_launch = TRUE;
112 static Eina_Bool __visibility_cb(void *data, int type, void *event);
113 static GSList *g_winnode_list;
115 static struct wl_display *dsp;
116 static struct wl_registry *reg;
117 static struct tizen_policy *tz_policy;
118 static bool bg_state = false;
120 static void __wl_listener_cb(void *data, struct wl_registry *reg,
121 uint32_t id, const char *interface, uint32_t ver)
123 if (interface && !strcmp(interface, "tizen_policy")) {
125 tz_policy = wl_registry_bind(reg, id,
126 &tizen_policy_interface, 1);
130 static void __wl_listener_remove_cb(void *data, struct wl_registry *reg,
136 static const struct wl_registry_listener reg_listener = {
138 __wl_listener_remove_cb
141 static int __init_wl(void)
143 _DBG("initialize wayland");
144 dsp = wl_display_connect(NULL);
146 _ERR("Failed to connect wl display");
150 reg = wl_display_get_registry(dsp);
152 _ERR("Failed to get registry");
153 wl_display_disconnect(dsp);
157 wl_registry_add_listener(reg, ®_listener, NULL);
158 wl_display_roundtrip(dsp);
161 _ERR("Failed to get tizen policy interface");
162 wl_registry_destroy(reg);
163 wl_display_disconnect(dsp);
170 static void __finish_wl(void)
173 tizen_policy_destroy(tz_policy);
178 wl_registry_destroy(reg);
183 wl_display_disconnect(dsp);
188 static void __set_bg_state(void)
190 if (!tz_policy && __init_wl() < 0)
193 tizen_policy_set_background_state(tz_policy, getpid());
194 wl_display_roundtrip(dsp);
196 _DBG("bg state: %d", bg_state);
199 static void __unset_bg_state(void)
204 tizen_policy_unset_background_state(tz_policy, getpid());
205 wl_display_roundtrip(dsp);
207 _DBG("bg state: %d", bg_state);
210 static void __appcore_efl_prepare_to_suspend(void *data)
212 struct ui_priv *ui = (struct ui_priv *)data;
213 struct sys_op *op = NULL;
214 int suspend = APPCORE_SUSPENDED_STATE_WILL_ENTER_SUSPEND;
216 if (ui->app_core && !ui->app_core->allowed_bg && !ui->app_core->suspended_state) {
217 op = &ui->app_core->sops[SE_SUSPENDED_STATE];
219 op->func((void *)&suspend, op->data); /* calls c-api handler */
221 ui->app_core->suspended_state = true;
223 _DBG("[__SUSPEND__]");
226 static void __appcore_efl_exit_from_suspend(void *data)
228 struct ui_priv *ui = (struct ui_priv *)data;
229 struct sys_op *op = NULL;
230 int suspend = APPCORE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
232 if (ui->app_core && !ui->app_core->allowed_bg && ui->app_core->suspended_state) {
233 op = &ui->app_core->sops[SE_SUSPENDED_STATE];
235 op->func((void *)&suspend, op->data); /* calls c-api handler */
237 ui->app_core->suspended_state = false;
239 _DBG("[__SUSPEND__]");
242 static void __appcore_efl_update_requested(void *data)
244 struct ui_priv *ui = (struct ui_priv *)data;
249 op = &ui->app_core->sops[SE_UPDATE_REQUESTED];
251 op->func((void *)&dummy, op->data);
253 _DBG("[__UPDATE_REQUESTED__]");
256 #if defined(MEMORY_FLUSH_ACTIVATE)
257 static Eina_Bool __appcore_memory_flush_cb(void *data)
259 struct ui_priv *ui = (struct ui_priv *)data;
261 appcore_flush_memory();
265 if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT && ui && ui->prepare_to_suspend) {
266 _DBG("[__SUSPEND__] flush case");
267 ui->prepare_to_suspend(ui);
270 return ECORE_CALLBACK_CANCEL;
273 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
275 if (ui->state == AS_PAUSED)
276 appcore_flush_memory();
283 static void __appcore_timer_add(struct ui_priv *ui)
285 ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
288 static void __appcore_timer_del(struct ui_priv *ui)
291 ecore_timer_del(ui->mftimer);
298 static int __appcore_low_memory_post_cb(ui_priv *ui)
303 #define __appcore_timer_add(ui) 0
304 #define __appcore_timer_del(ui) 0
308 static void __appcore_efl_memory_flush_cb(void)
310 _DBG("[APP %d] __appcore_efl_memory_flush_cb()", _pid);
311 elm_cache_all_flush();
313 static void wl_raise_win(void)
315 Ecore_Wl_Window *win;
316 unsigned int win_id = appcore_get_main_window();
318 _DBG("Raise window: %d", win_id);
319 win = ecore_wl_window_find(win_id);
320 ecore_wl_window_activate(win);
323 static void wl_pause_win(void)
325 Ecore_Wl_Window *win;
326 GSList *wlist = g_winnode_list;
327 struct win_node *entry = NULL;
329 _DBG("Pause window");
334 _DBG("Pause window: %d", entry->win);
335 win = ecore_wl_window_find(entry->win);
336 ecore_wl_window_iconified_set(win, EINA_TRUE);
343 static void __do_app(enum app_event event, void *data, bundle * b)
346 struct ui_priv *ui = data;
347 const char *below_app;
348 const char *bg_launch;
350 _DBG("[APP %d] Event: %d", _pid, event);
351 _ret_if(ui == NULL || event >= AE_MAX);
352 _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
353 _as_name[ui->state]);
355 if (event == AE_MEM_FLUSH) {
360 if (event == AE_LOWMEM_POST) {
361 if (__appcore_low_memory_post_cb(ui) == 0)
365 if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
366 __appcore_timer_del(ui);
368 if (ui->state == AS_DYING) {
369 _ERR("Skip the event in dying state");
373 if (event == AE_TERMINATE) {
374 _DBG("[APP %d] TERMINATE", _pid);
376 aul_status_update(STATUS_DYING);
380 if (event == AE_RAISE) {
385 if (event == AE_LOWER) {
390 _ret_if(ui->ops == NULL);
394 _DBG("[APP %d] RESET", _pid);
395 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]", ui->name);
399 ui->below_app = NULL;
402 below_app = bundle_get_val(b, AUL_SVC_K_RELOCATE_BELOW);
404 ui->below_app = strdup(below_app);
405 if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT && ui->exit_from_suspend) {
406 _DBG("[__SUSPEND__] reset case");
407 ui->exit_from_suspend(ui);
410 if (ui->ops->reset) {
411 traceBegin(TTRACE_TAG_APPLICATION_MANAGER,
413 r = ui->ops->reset(b, ui->ops->data);
414 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
416 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]", ui->name);
419 if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT && ui->app_core->allowed_bg)
420 __appcore_timer_add(ui);
421 first_launch = FALSE;
423 _INFO("[APP %d] App already running, raise the window", _pid);
424 bg_launch = bundle_get_val(b, AUL_SVC_K_BG_LAUNCH);
425 if (bg_launch && strcmp(bg_launch, "enable") == 0) {
426 if (!bg_state && ui->state != AS_RUNNING)
435 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
439 if (ui->state == AS_RUNNING) {
440 _DBG("[APP %d] PAUSE", _pid);
441 if (ui->ops->pause) {
442 traceBegin(TTRACE_TAG_APPLICATION_MANAGER,
444 r = ui->ops->pause(ui->ops->data);
445 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
447 ui->state = AS_PAUSED;
448 if (r >= 0 && resource_reclaiming == TRUE)
449 __appcore_timer_add(ui);
450 else if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT && r >= 0 && resource_reclaiming == FALSE
451 && ui->prepare_to_suspend) {
452 _DBG("[__SUSPEND__] pause case");
453 ui->prepare_to_suspend(ui);
456 /* TODO : rotation stop */
457 /* r = appcore_pause_rotation_cb(); */
458 aul_status_update(STATUS_BG);
461 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
463 if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT) {
464 if (ui->exit_from_suspend) {
465 _DBG("[__SUSPEND__] resume case");
466 ui->exit_from_suspend(ui);
468 if (ui->app_core->allowed_bg)
469 __appcore_timer_del(ui);
472 if (ui->state == AS_PAUSED || ui->state == AS_CREATED) {
473 _DBG("[APP %d] RESUME", _pid);
475 if (ui->ops->resume) {
476 traceBegin(TTRACE_TAG_APPLICATION_MANAGER,
478 ui->ops->resume(ui->ops->data);
479 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
481 ui->state = AS_RUNNING;
484 aul_app_group_activate_below(ui->below_app);
486 ui->below_app = NULL;
489 /*TODO : rotation start*/
490 /* r = appcore_resume_rotation_cb(); */
491 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
493 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
495 aul_status_update(STATUS_VISIBLE);
497 case AE_TERMINATE_BGAPP:
498 if (ui->state == AS_PAUSED) {
499 _DBG("[APP %d] is paused. TERMINATE", _pid);
500 ui->state = AS_DYING;
501 aul_status_update(STATUS_DYING);
503 } else if (ui->state == AS_RUNNING) {
504 _DBG("[APP %d] is running.", _pid);
506 _DBG("[APP %d] is another state", _pid);
509 case AE_UPDATE_REQUESTED:
510 __appcore_efl_update_requested(ui);
518 static struct ui_ops efl_ops = {
523 static bool __check_visible(void)
526 struct win_node *entry = NULL;
528 _DBG("[EVENT_TEST][EVENT] __check_visible\n");
530 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
532 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
533 if (entry->bfobscured == FALSE)
540 static GSList *__find_win(unsigned int win)
545 for (iter = g_winnode_list; iter; iter = g_slist_next(iter)) {
547 if (t && t->win == win)
554 static bool __add_win(unsigned int win, unsigned int surf)
559 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
564 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
568 t = calloc(1, sizeof(struct win_node));
574 t->bfobscured = FALSE;
576 g_winnode_list = g_slist_append(g_winnode_list, t);
581 static bool __delete_win(unsigned int win)
588 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
594 g_winnode_list = g_slist_delete_link(g_winnode_list, f);
599 static bool __update_win(unsigned int win, unsigned int surf, bool bfobscured)
604 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
610 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
614 t = (struct win_node *)f->data;
618 t->bfobscured = bfobscured;
623 static Eina_Bool __show_cb(void *data, int type, void *event)
625 Ecore_Wl_Event_Window_Show *ev;
628 if (ev->parent_win != 0) {
629 /* This is child window. Skip!!! */
630 return ECORE_CALLBACK_PASS_ON;
633 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x, %d\n", ev->win, ev->data[0]);
635 if (!__find_win((unsigned int)ev->win))
636 __add_win((unsigned int)ev->win, (unsigned int)ev->data[0]);
638 __update_win((unsigned int)ev->win, (unsigned int)ev->data[0], FALSE);
640 if (ev->data[0] != 0)
641 appcore_group_attach();
642 return ECORE_CALLBACK_RENEW;
645 static Eina_Bool __hide_cb(void *data, int type, void *event)
647 Ecore_Wl_Event_Window_Hide *ev;
652 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
654 if (__find_win((unsigned int)ev->win)) {
655 __delete_win((unsigned int)ev->win);
656 bvisibility = __check_visible();
657 if (!bvisibility && b_active == TRUE) {
658 _DBG(" Go to Pasue state \n");
660 __do_app(AE_PAUSE, data, NULL);
664 return ECORE_CALLBACK_RENEW;
667 static Eina_Bool __lower_cb(void *data, int type, void *event)
669 Ecore_Wl_Event_Window_Lower *ev;
671 if (!ev) return ECORE_CALLBACK_RENEW;
672 _DBG("ECORE_WL_EVENT_WINDOW_LOWER window id:%u\n", ev->win);
673 appcore_group_lower();
674 return ECORE_CALLBACK_RENEW;
677 static Eina_Bool __visibility_cb(void *data, int type, void *event)
679 Ecore_Wl_Event_Window_Visibility_Change *ev;
682 __update_win((unsigned int)ev->win, 0, ev->fully_obscured);
683 bvisibility = __check_visible();
685 _DBG("bvisibility %d, b_active %d", bvisibility, b_active);
687 if (bvisibility && b_active == FALSE) {
688 _DBG(" Go to Resume state\n");
690 __do_app(AE_RESUME, data, NULL);
692 } else if (!bvisibility && b_active == TRUE) {
693 _DBG(" Go to Pasue state \n");
695 __do_app(AE_PAUSE, data, NULL);
697 _DBG(" No change state \n");
699 return ECORE_CALLBACK_RENEW;
703 static void __add_climsg_cb(struct ui_priv *ui)
707 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, ui);
709 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, ui);
711 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE,
712 __visibility_cb, ui);
714 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER,
718 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
722 struct appcore *ac = NULL;
724 const char *bg_launch;
726 if (argc == NULL || argv == NULL) {
727 _ERR("argc/argv is NULL");
732 #if !(GLIB_CHECK_VERSION(2, 36, 0))
735 elm_init(*argc, *argv);
737 hwacc = getenv("HWACC");
739 _DBG("elm_config_accel_preference_set is not called");
740 } else if (strcmp(hwacc, "USE") == 0) {
741 elm_config_accel_preference_set("hw");
742 _DBG("elm_config_accel_preference_set : hw");
743 } else if (strcmp(hwacc, "NOT_USE") == 0) {
744 elm_config_accel_preference_set("none");
745 _DBG("elm_config_accel_preference_set : none");
747 _DBG("elm_config_accel_preference_set is not called");
750 r = appcore_init(ui->name, &efl_ops, *argc, *argv);
751 _retv_if(r == -1, -1);
753 if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT) {
754 appcore_get_app_core(&ac);
756 SECURE_LOGD("[__SUSPEND__] appcore initialized, appcore addr: #%x", ac);
759 b = bundle_import_from_argv(*argc, *argv);
761 bg_launch = bundle_get_val(b, AUL_SVC_K_BG_LAUNCH);
762 if (bg_launch && strcmp(bg_launch, "enable") == 0)
768 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
769 if (ui->ops && ui->ops->create) {
770 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:CREATE");
771 r = ui->ops->create(ui->ops->data);
772 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
774 _ERR("create() return error");
776 if (ui->ops && ui->ops->terminate) {
777 traceBegin(TTRACE_TAG_APPLICATION_MANAGER,
778 "APPCORE:TERMINATE");
779 ui->ops->terminate(ui->ops->data);
780 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
785 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
788 ui->state = AS_CREATED;
795 static void __after_loop(struct ui_priv *ui)
797 appcore_unset_rotation_cb();
800 if (ui->state == AS_RUNNING) {
801 _DBG("[APP %d] PAUSE before termination", _pid);
802 if (ui->ops && ui->ops->pause) {
803 traceBegin(TTRACE_TAG_APPLICATION_MANAGER,
805 ui->ops->pause(ui->ops->data);
806 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
810 if (ui->ops && ui->ops->terminate) {
811 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:TERMINATE");
812 ui->ops->terminate(ui->ops->data);
813 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
816 ui->state = AS_DYING;
819 ecore_event_handler_del(ui->hshow);
821 ecore_event_handler_del(ui->hhide);
823 ecore_event_handler_del(ui->hvchange);
825 ecore_event_handler_del(ui->hlower);
827 __appcore_timer_del(ui);
832 /* Check loader case */
833 if (getenv("AUL_LOADER_INIT")) {
834 unsetenv("AUL_LOADER_INIT");
839 static int __set_data(struct ui_priv *ui, const char *name,
840 struct appcore_ops *ops)
843 _ERR("Mainloop already started");
848 if (name == NULL || name[0] == '\0') {
849 _ERR("Invalid name");
860 ui->name = strdup(name);
861 _retv_if(ui->name == NULL, -1);
864 ui->mfcb = __appcore_efl_memory_flush_cb;
868 ui->wm_rot_supported = 0;
871 ui->rot_cb_data = NULL;
872 ui->rot_mode = APPCORE_RM_UNKNOWN;
874 if (_APPFW_FEATURE_BACKGROUND_MANAGEMENT) {
876 ui->prepare_to_suspend = __appcore_efl_prepare_to_suspend;
877 ui->exit_from_suspend = __appcore_efl_exit_from_suspend;
883 static void __unset_data(struct ui_priv *ui)
886 free((void *)ui->name);
888 memset(ui, 0, sizeof(struct ui_priv));
891 EXPORT_API int appcore_efl_init(const char *name, int *argc, char ***argv,
892 struct appcore_ops *ops)
896 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
898 r = __set_data(&priv, name, ops);
899 _retv_if(r == -1, -1);
901 r = __before_loop(&priv, argc, argv);
903 aul_status_update(STATUS_DYING);
911 EXPORT_API void appcore_efl_fini(void)
913 aul_status_update(STATUS_DYING);
920 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
921 struct appcore_ops *ops)
925 r = appcore_efl_init(name, argc, argv, ops);
926 _retv_if(r == -1, -1);
935 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable)
937 resource_reclaiming = enable;
942 EXPORT_API int appcore_set_app_state(int state)
951 EXPORT_API int appcore_set_preinit_window_name(const char *win_name)
954 void *preinit_window = NULL;
955 const Evas *e = NULL;
958 _ERR("invalid parameter");
962 preinit_window = elm_win_precreated_object_get();
963 if (!preinit_window) {
964 _ERR("Failed to get preinit window");
968 e = evas_object_evas_get((const Evas_Object *)preinit_window);
970 Ecore_Evas *ee = ecore_evas_ecore_evas_get(e);
972 ecore_evas_name_class_set(ee, win_name, win_name);
980 EXPORT_API unsigned int appcore_get_main_window(void)
982 struct win_node *entry = NULL;
984 if (g_winnode_list != NULL) {
985 entry = g_winnode_list->data;
986 return (unsigned int) entry->win;
992 EXPORT_API unsigned int appcore_get_main_surface(void)
994 struct win_node *entry = NULL;
996 if (g_winnode_list != NULL) {
997 entry = g_winnode_list->data;
998 return (unsigned int) entry->surf;
1004 tizen_profile_t _get_tizen_profile()
1006 static tizen_profile_t profile = TIZEN_PROFILE_UNKNOWN;
1007 if (__builtin_expect(profile != TIZEN_PROFILE_UNKNOWN, 1))
1011 system_info_get_platform_string("http://tizen.org/feature/profile", &profileName);
1012 switch (*profileName) {
1015 profile = TIZEN_PROFILE_MOBILE;
1019 profile = TIZEN_PROFILE_WEARABLE;
1023 profile = TIZEN_PROFILE_TV;
1027 profile = TIZEN_PROFILE_IVI;
1029 default: // common or unTIZEN_nown ==> ALL ARE COMMON.
1030 profile = TIZEN_PROFILE_COMMON;