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.
17 #include <sys/types.h>
18 #include <sys/socket.h>
26 #include <linux/limits.h>
28 #include <Ecore_Wayland.h>
29 #include <wayland-client.h>
30 #include <wayland-tbm-client.h>
31 #include <tizen-extension-client-protocol.h>
34 #include <Ecore_Evas.h>
35 #include <Ecore_Input_Evas.h>
36 #include <Elementary.h>
37 #include <glib-object.h>
44 #include <bundle_internal.h>
47 #include "appcore_base.h"
48 #include "appcore_ui_base.h"
49 #include "appcore_ui_base_private.h"
50 #include "appcore_ui_plugin.h"
60 typedef struct _appcore_ui_base_context {
61 appcore_ui_base_ops ops;
70 Ecore_Event_Handler *hshow;
71 Ecore_Event_Handler *hhide;
72 Ecore_Event_Handler *hvchange;
73 Ecore_Event_Handler *hlower;
74 } appcore_ui_base_context;
77 static bool b_active = false;
78 static bool first_launch = true;
86 static GSList *g_winnode_list;
87 static appcore_ui_base_context __context;
88 static struct wl_display *dsp;
89 static struct wl_registry *reg;
90 static struct tizen_policy *tz_policy;
91 static bool bg_state = false;
93 static void __wl_listener_cb(void *data, struct wl_registry *reg,
94 uint32_t id, const char *interface, uint32_t ver)
96 if (interface && !strcmp(interface, "tizen_policy")) {
98 tz_policy = wl_registry_bind(reg, id,
99 &tizen_policy_interface, 1);
103 static void __wl_listener_remove_cb(void *data, struct wl_registry *reg,
109 static const struct wl_registry_listener reg_listener = {
111 __wl_listener_remove_cb
114 static Eina_Bool __stub_show_cb(void *data, int type, void *event)
116 if (__context.ops.window.show)
117 __context.ops.window.show(type, event, __context.data);
119 return ECORE_CALLBACK_RENEW;
122 static Eina_Bool __stub_hide_cb(void *data, int type, void *event)
124 if (__context.ops.window.hide)
125 __context.ops.window.hide(type, event, __context.data);
127 return ECORE_CALLBACK_RENEW;
130 static Eina_Bool __stub_visibility_cb(void *data, int type, void *event)
132 if (__context.ops.window.visibility)
133 __context.ops.window.visibility(type, event, __context.data);
135 return ECORE_CALLBACK_RENEW;
138 static Eina_Bool __stub_lower_cb(void *data, int type, void *event)
140 if (__context.ops.window.lower)
141 __context.ops.window.lower(type, event, __context.data);
143 return ECORE_CALLBACK_RENEW;
146 static void __prepare_to_suspend(void)
148 int suspend = APPCORE_BASE_SUSPENDED_STATE_WILL_ENTER_SUSPEND;
150 if (appcore_base_is_bg_allowed() && !appcore_base_is_suspended()) {
151 appcore_base_raise_event((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
152 appcore_base_toggle_suspended_state();
156 static void __exit_from_suspend(void)
158 int suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
160 if (appcore_base_is_suspended()) {
161 appcore_base_raise_event((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
162 appcore_base_toggle_suspended_state();
166 static void __do_pause(void)
168 if (__context.state == AS_RUNNING) {
169 if (__context.ops.pause) {
170 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:PAUSE");
171 __context.ops.pause(__context.data);
172 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
175 __context.state = AS_PAUSED;
176 __prepare_to_suspend();
178 aul_status_update(STATUS_BG);
181 static void __do_resume(void)
183 if (__context.state == AS_PAUSED || __context.state == AS_CREATED) {
184 __exit_from_suspend();
185 if (__context.ops.resume) {
186 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]", __context.appid);
187 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:RESUME");
188 __context.ops.resume(__context.data);
189 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
190 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]", __context.appid);
191 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]", __context.appid);
193 if ((__context.hint & APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL) &&
194 __context.below_app) {
195 aul_app_group_activate_below(__context.below_app);
196 free(__context.below_app);
197 __context.below_app = NULL;
199 __context.state = AS_RUNNING;
202 aul_status_update(STATUS_VISIBLE);
205 static GSList *__find_win(unsigned int win)
210 for (iter = g_winnode_list; iter; iter = g_slist_next(iter)) {
212 if (t && t->win == win)
219 static int __get_main_window(void)
221 struct win_node *entry = NULL;
223 if (g_winnode_list != NULL) {
224 entry = g_winnode_list->data;
225 return (unsigned int) entry->win;
231 static int __get_main_surface(void)
233 struct win_node *entry = NULL;
235 if (g_winnode_list != NULL) {
236 entry = g_winnode_list->data;
237 return (unsigned int) entry->surf;
243 static bool __add_win(unsigned int win, unsigned int surf)
248 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
253 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
257 t = calloc(1, sizeof(struct win_node));
263 t->bfobscured = FALSE;
265 g_winnode_list = g_slist_append(g_winnode_list, t);
270 static bool __delete_win(unsigned int win)
277 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
283 g_winnode_list = g_slist_delete_link(g_winnode_list, f);
288 static bool __update_win(unsigned int win, unsigned int surf, bool bfobscured)
293 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
299 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
303 t = (struct win_node *)f->data;
307 t->bfobscured = bfobscured;
312 static void __raise_win(void)
314 Ecore_Wl_Window *win;
317 if (!(__context.hint & APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL))
320 win_id = __get_main_window();
322 _DBG("Raise window: %d", win_id);
323 win = ecore_wl_window_find(win_id);
324 ecore_wl_window_activate(win);
327 static void __pause_win(void)
329 Ecore_Wl_Window *win;
330 GSList *wlist = g_winnode_list;
331 struct win_node *entry = NULL;
333 if (!(__context.hint & APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL))
336 _DBG("Pause window");
341 _DBG("Pause window: %d", entry->win);
342 win = ecore_wl_window_find(entry->win);
343 ecore_wl_window_iconified_set(win, EINA_TRUE);
349 static int __init_wl(void)
351 _DBG("initialize wayland");
352 dsp = wl_display_connect(NULL);
354 _ERR("Failed to connect wl display");
358 reg = wl_display_get_registry(dsp);
360 _ERR("Failed to get registry");
361 wl_display_disconnect(dsp);
365 wl_registry_add_listener(reg, ®_listener, NULL);
366 wl_display_roundtrip(dsp);
369 _ERR("Failed to get tizen policy interface");
370 wl_registry_destroy(reg);
371 wl_display_disconnect(dsp);
378 static void __finish_wl(void)
381 tizen_policy_destroy(tz_policy);
386 wl_registry_destroy(reg);
391 wl_display_disconnect(dsp);
396 static void __set_bg_state(void)
398 if (!tz_policy && __init_wl() < 0)
401 tizen_policy_set_background_state(tz_policy, getpid());
402 wl_display_roundtrip(dsp);
404 _DBG("bg state: %d", bg_state);
407 static void __unset_bg_state(void)
412 tizen_policy_unset_background_state(tz_policy, getpid());
413 wl_display_roundtrip(dsp);
415 _DBG("bg state: %d", bg_state);
418 static void __do_start(bundle *b)
420 const char *bg_launch;
421 const char *below_app;
423 if (__context.hint & APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL) {
424 if (__context.below_app) {
425 free(__context.below_app);
426 __context.below_app = NULL;
429 below_app = bundle_get_val(b, AUL_SVC_K_RELOCATE_BELOW);
431 __context.below_app = strdup(below_app);
435 first_launch = FALSE;
439 if (__context.hint & APPCORE_UI_BASE_HINT_BG_LAUNCH_CONTROL) {
440 bg_launch = bundle_get_val(b, AUL_SVC_K_BG_LAUNCH);
441 if (bg_launch && strcmp(bg_launch, "enable") == 0) {
442 if (!bg_state && __context.state != AS_RUNNING)
450 if (__context.hint & APPCORE_UI_BASE_HINT_WINDOW_AUTO_CONTROL) {
456 EXPORT_API int appcore_ui_base_on_receive(aul_type type, bundle *b)
458 if (__context.state == AS_DYING) {
459 _ERR("Skip the event in dying state");
463 if (type == AUL_TERMINATE_BGAPP && __context.state != AS_PAUSED)
466 if (type == AUL_START)
467 __exit_from_suspend();
469 appcore_base_on_receive(type, b);
482 case AUL_TERMINATE_BGAPP:
483 _DBG("[APP %d] is paused. TERMINATE", getpid());
484 __context.state = AS_DYING;
485 aul_status_update(STATUS_DYING);
486 if (__context.ops.base.exit)
487 __context.ops.base.exit(__context.data);
499 EXPORT_API int appcore_ui_base_on_create(void)
501 appcore_base_on_create();
503 __context.hshow = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __stub_show_cb, NULL);
504 __context.hhide = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __stub_hide_cb, NULL);
505 __context.hvchange = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE,
506 __stub_visibility_cb, NULL);
507 __context.hlower = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __stub_lower_cb, NULL);
508 __context.state = AS_CREATED;
509 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]", __context.appid);
514 EXPORT_API int appcore_ui_base_on_terminate(void)
516 if (__context.state == AS_RUNNING) {
517 if (__context.ops.pause)
518 __context.ops.pause(__context.data);
521 __context.state = AS_DYING;
523 appcore_base_on_terminate();
528 EXPORT_API int appcore_ui_base_on_pause(void)
533 EXPORT_API int appcore_ui_base_on_resume(void)
538 EXPORT_API int appcore_ui_base_on_control(bundle *b)
540 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]", __context.appid);
541 appcore_base_on_control(b);
542 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]", __context.appid);
547 static void __group_attach()
549 if (!(__context.hint & APPCORE_UI_BASE_HINT_WINDOW_GROUP_CONTROL))
552 appcore_ui_base_group_add();
555 static void __group_lower()
557 if (!(__context.hint & APPCORE_UI_BASE_HINT_WINDOW_GROUP_CONTROL))
559 appcore_ui_base_group_remove();
562 EXPORT_API void appcore_ui_base_group_add()
564 static bool attached = false;
566 _DBG("__group_attach");
570 int wid = __get_main_surface();
572 _ERR("window wasn't ready");
576 aul_app_group_set_window(wid);
580 EXPORT_API void appcore_ui_base_group_remove()
584 _DBG("__group_lower");
585 aul_app_group_lower(&exit);
587 _DBG("__group_lower : sub-app!");
588 if (__context.ops.base.exit)
589 __context.ops.base.exit(__context.data);
593 EXPORT_API void appcore_ui_base_window_on_show(int type, void *event)
595 Ecore_Wl_Event_Window_Show *ev;
598 if (ev->parent_win != 0) {
599 /* This is child window. Skip!!! */
603 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x, %d\n", ev->win, ev->data[0]);
605 if (!__find_win((unsigned int)ev->win))
606 __add_win((unsigned int)ev->win, (unsigned int)ev->data[0]);
608 __update_win((unsigned int)ev->win, (unsigned int)ev->data[0], FALSE);
610 if (ev->data[0] != 0)
614 static bool __check_visible(void)
617 struct win_node *entry = NULL;
619 _DBG("[EVENT_TEST][EVENT] __check_visible\n");
621 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
623 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
624 if (entry->bfobscured == FALSE)
631 EXPORT_API void appcore_ui_base_window_on_hide(int type, void *event)
633 Ecore_Wl_Event_Window_Hide *ev;
637 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
639 if (__find_win((unsigned int)ev->win)) {
640 __delete_win((unsigned int)ev->win);
641 bvisibility = __check_visible();
642 if (!bvisibility && b_active == TRUE) {
643 _DBG(" Go to Pasue state \n");
650 EXPORT_API void appcore_ui_base_window_on_lower(int type, void *event)
652 Ecore_Wl_Event_Window_Lower *ev;
657 _DBG("ECORE_WL_EVENT_WINDOW_LOWER window id:%u\n", ev->win);
661 EXPORT_API void appcore_ui_base_window_on_visibility(int type, void *event)
663 Ecore_Wl_Event_Window_Visibility_Change *ev;
667 __update_win((unsigned int)ev->win, 0, ev->fully_obscured);
668 bvisibility = __check_visible();
670 _DBG("bvisibility %d, b_active %d", bvisibility, b_active);
672 if (bvisibility && b_active == FALSE) {
673 _DBG(" Go to Resume state\n");
676 } else if (!bvisibility && b_active == TRUE) {
677 _DBG(" Go to Pasue state \n");
681 _DBG(" No change state \n");
686 EXPORT_API int appcore_ui_base_init(appcore_ui_base_ops ops, int argc, char **argv,
687 void *data, unsigned int hint)
690 const char *bg_launch;
692 char appid[PATH_MAX] = {0, };
694 appcore_ui_plugin_init(&ops, &hint);
695 aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
697 __context.data = data;
698 __context.argc = argc;
699 __context.argv = argv;
700 __context.hint = hint;
701 __context.state = AS_NONE;
702 __context.appid = strdup(appid);
704 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", appid);
705 elm_init(argc, argv);
707 if (__context.hint & APPCORE_UI_BASE_HINT_HW_ACC_CONTROL) {
708 hwacc = getenv("HWACC");
711 _DBG("elm_config_accel_preference_set is not called");
712 } else if (strcmp(hwacc, "USE") == 0) {
713 elm_config_accel_preference_set("hw");
714 _DBG("elm_config_accel_preference_set : hw");
715 } else if (strcmp(hwacc, "NOT_USE") == 0) {
716 elm_config_accel_preference_set("none");
717 _DBG("elm_config_accel_preference_set : none");
719 _DBG("elm_config_accel_preference_set is not called");
723 if (__context.hint & APPCORE_UI_BASE_HINT_BG_LAUNCH_CONTROL) {
724 b = bundle_import_from_argv(argc, argv);
726 bg_launch = bundle_get_val(b, AUL_SVC_K_BG_LAUNCH);
727 if (bg_launch && strcmp(bg_launch, "enable") == 0)
734 return appcore_base_init(ops.base, argc, argv, data);
737 EXPORT_API void appcore_ui_base_fini(void)
740 appcore_ui_plugin_fini();
743 ecore_event_handler_del(__context.hshow);
745 ecore_event_handler_del(__context.hhide);
746 if (__context.hvchange)
747 ecore_event_handler_del(__context.hvchange);
748 if (__context.hlower)
749 ecore_event_handler_del(__context.hlower);
754 /* Check loader case */
755 if (getenv("AUL_LOADER_INIT")) {
756 unsetenv("AUL_LOADER_INIT");
760 free(__context.appid);
761 __context.appid = NULL;
764 EXPORT_API void appcore_ui_base_pause(void)
769 EXPORT_API void appcore_ui_base_resume(void)
774 EXPORT_API bool appcore_ui_base_is_resumed(void)
776 return __context.state == AS_RUNNING;
779 EXPORT_API void appcore_ui_base_exit(void)
781 if (__context.ops.base.exit)
782 __context.ops.base.exit(__context.data);
785 EXPORT_API unsigned int appcore_ui_base_get_main_window(void)
787 return __get_main_window();
790 EXPORT_API unsigned int appcore_ui_base_get_main_surface(void)
792 return __get_main_surface();
795 static int __on_receive(aul_type type, bundle *b, void *data)
797 return appcore_ui_base_on_receive(type, b);
800 static int __on_create(void *data)
802 return appcore_ui_base_on_create();
805 static int __on_terminate(void *data)
807 return appcore_ui_base_on_terminate();
810 static int __on_pause(void *data)
812 return appcore_ui_base_on_pause();
815 static int __on_resume(void *data)
817 return appcore_ui_base_on_resume();
820 static void __window_on_show(int type, void *event, void *data)
822 appcore_ui_base_window_on_show(type, event);
825 static void __window_on_hide(int type, void *event, void *data)
827 appcore_ui_base_window_on_hide(type, event);
830 static void __window_on_lower(int type, void *event, void *data)
832 appcore_ui_base_window_on_lower(type, event);
835 static void __window_on_visibility(int type, void *event, void *data)
837 appcore_ui_base_window_on_visibility(type, event);
840 static void __run(void *data)
845 static void __exit(void *data)
850 EXPORT_API appcore_ui_base_ops appcore_ui_base_get_default_ops(void)
852 appcore_ui_base_ops ops;
854 ops.base = appcore_base_get_default_ops();
856 /* override methods */
857 ops.base.create = __on_create;
858 ops.base.terminate = __on_terminate;
859 ops.base.receive = __on_receive;
860 ops.base.run = __run;
861 ops.base.exit = __exit;
863 ops.pause = __on_pause;
864 ops.resume = __on_resume;
865 ops.window.show = __window_on_show;
866 ops.window.hide = __window_on_hide;
867 ops.window.lower = __window_on_lower;
868 ops.window.visibility = __window_on_visibility;