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 <glib-object.h>
41 #include <bundle_internal.h>
44 #include "appcore_base.h"
45 #include "appcore_ui_base.h"
46 #include "appcore_ui_base_private.h"
47 #include "appcore_ui_plugin.h"
57 typedef struct _appcore_ui_base_context {
58 appcore_ui_base_ops ops;
67 Ecore_Event_Handler *hshow;
68 Ecore_Event_Handler *hhide;
69 Ecore_Event_Handler *hvchange;
70 Ecore_Event_Handler *hlower;
71 } appcore_ui_base_context;
74 static bool b_active = false;
75 static bool first_launch = true;
83 static GSList *g_winnode_list;
84 static appcore_ui_base_context __context;
85 static struct wl_display *dsp;
86 static struct wl_registry *reg;
87 static struct tizen_policy *tz_policy;
88 static bool bg_state = false;
90 static void __wl_listener_cb(void *data, struct wl_registry *reg,
91 uint32_t id, const char *interface, uint32_t ver)
93 if (interface && !strcmp(interface, "tizen_policy")) {
95 tz_policy = wl_registry_bind(reg, id,
96 &tizen_policy_interface, 1);
100 static void __wl_listener_remove_cb(void *data, struct wl_registry *reg,
106 static const struct wl_registry_listener reg_listener = {
108 __wl_listener_remove_cb
111 static Eina_Bool __stub_show_cb(void *data, int type, void *event)
113 if (__context.ops.window.show)
114 __context.ops.window.show(type, event, __context.data);
116 return ECORE_CALLBACK_RENEW;
119 static Eina_Bool __stub_hide_cb(void *data, int type, void *event)
121 if (__context.ops.window.hide)
122 __context.ops.window.hide(type, event, __context.data);
124 return ECORE_CALLBACK_RENEW;
127 static Eina_Bool __stub_visibility_cb(void *data, int type, void *event)
129 if (__context.ops.window.visibility)
130 __context.ops.window.visibility(type, event, __context.data);
132 return ECORE_CALLBACK_RENEW;
135 static Eina_Bool __stub_lower_cb(void *data, int type, void *event)
137 if (__context.ops.window.lower)
138 __context.ops.window.lower(type, event, __context.data);
140 return ECORE_CALLBACK_RENEW;
143 static void __prepare_to_suspend(void)
145 int suspend = APPCORE_BASE_SUSPENDED_STATE_WILL_ENTER_SUSPEND;
147 if (appcore_base_is_bg_allowed() && !appcore_base_is_suspended()) {
148 appcore_base_raise_event((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
149 appcore_base_toggle_suspended_state();
153 static void __exit_from_suspend(void)
155 int suspend = APPCORE_BASE_SUSPENDED_STATE_DID_EXIT_FROM_SUSPEND;
157 if (appcore_base_is_suspended()) {
158 appcore_base_raise_event((void *)&suspend, APPCORE_BASE_EVENT_SUSPENDED_STATE_CHANGE);
159 appcore_base_toggle_suspended_state();
163 static void __do_pause(void)
165 if (__context.state == AS_RUNNING) {
166 if (__context.ops.pause) {
167 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:PAUSE");
168 __context.ops.pause(__context.data);
169 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
172 __context.state = AS_PAUSED;
173 __prepare_to_suspend();
175 aul_status_update(STATUS_BG);
178 static void __do_resume(void)
180 if (__context.state == AS_PAUSED || __context.state == AS_CREATED) {
181 __exit_from_suspend();
182 if (__context.ops.resume) {
183 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]", __context.appid);
184 traceBegin(TTRACE_TAG_APPLICATION_MANAGER, "APPCORE:RESUME");
185 __context.ops.resume(__context.data);
186 traceEnd(TTRACE_TAG_APPLICATION_MANAGER);
187 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]", __context.appid);
188 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]", __context.appid);
190 if ((__context.hint & APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL) &&
191 __context.below_app) {
192 aul_app_group_activate_below(__context.below_app);
193 free(__context.below_app);
194 __context.below_app = NULL;
196 __context.state = AS_RUNNING;
199 aul_status_update(STATUS_VISIBLE);
202 static GSList *__find_win(unsigned int win)
207 for (iter = g_winnode_list; iter; iter = g_slist_next(iter)) {
209 if (t && t->win == win)
216 static int __get_main_window(void)
218 struct win_node *entry = NULL;
220 if (g_winnode_list != NULL) {
221 entry = g_winnode_list->data;
222 return (unsigned int) entry->win;
228 static int __get_main_surface(void)
230 struct win_node *entry = NULL;
232 if (g_winnode_list != NULL) {
233 entry = g_winnode_list->data;
234 return (unsigned int) entry->surf;
240 static bool __add_win(unsigned int win, unsigned int surf)
245 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
250 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
254 t = calloc(1, sizeof(struct win_node));
260 t->bfobscured = FALSE;
262 g_winnode_list = g_slist_append(g_winnode_list, t);
267 static bool __delete_win(unsigned int win)
274 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
280 g_winnode_list = g_slist_delete_link(g_winnode_list, f);
285 static bool __update_win(unsigned int win, unsigned int surf, bool bfobscured)
290 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
296 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
300 t = (struct win_node *)f->data;
304 t->bfobscured = bfobscured;
309 static void __raise_win(void)
311 Ecore_Wl_Window *win;
314 if (!(__context.hint & APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL))
317 win_id = __get_main_window();
319 _DBG("Raise window: %d", win_id);
320 win = ecore_wl_window_find(win_id);
321 ecore_wl_window_activate(win);
324 static void __pause_win(void)
326 Ecore_Wl_Window *win;
327 GSList *wlist = g_winnode_list;
328 struct win_node *entry = NULL;
330 if (!(__context.hint & APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL))
333 _DBG("Pause window");
338 _DBG("Pause window: %d", entry->win);
339 win = ecore_wl_window_find(entry->win);
340 ecore_wl_window_iconified_set(win, EINA_TRUE);
346 static int __init_wl(void)
348 _DBG("initialize wayland");
349 dsp = wl_display_connect(NULL);
351 _ERR("Failed to connect wl display");
355 reg = wl_display_get_registry(dsp);
357 _ERR("Failed to get registry");
358 wl_display_disconnect(dsp);
362 wl_registry_add_listener(reg, ®_listener, NULL);
363 wl_display_roundtrip(dsp);
366 _ERR("Failed to get tizen policy interface");
367 wl_registry_destroy(reg);
368 wl_display_disconnect(dsp);
375 static void __finish_wl(void)
378 tizen_policy_destroy(tz_policy);
383 wl_registry_destroy(reg);
388 wl_display_disconnect(dsp);
393 static void __set_bg_state(void)
395 if (!tz_policy && __init_wl() < 0)
398 tizen_policy_set_background_state(tz_policy, getpid());
399 wl_display_roundtrip(dsp);
401 _DBG("bg state: %d", bg_state);
404 static void __unset_bg_state(void)
409 tizen_policy_unset_background_state(tz_policy, getpid());
410 wl_display_roundtrip(dsp);
412 _DBG("bg state: %d", bg_state);
415 static void __do_start(bundle *b)
417 const char *bg_launch;
418 const char *below_app;
420 if (__context.hint & APPCORE_UI_BASE_HINT_WINDOW_STACK_CONTROL) {
421 if (__context.below_app) {
422 free(__context.below_app);
423 __context.below_app = NULL;
426 below_app = bundle_get_val(b, AUL_SVC_K_RELOCATE_BELOW);
428 __context.below_app = strdup(below_app);
432 first_launch = FALSE;
436 if (__context.hint & APPCORE_UI_BASE_HINT_BG_LAUNCH_CONTROL) {
437 bg_launch = bundle_get_val(b, AUL_SVC_K_BG_LAUNCH);
438 if (bg_launch && strcmp(bg_launch, "enable") == 0) {
439 if (!bg_state && __context.state != AS_RUNNING)
447 if (__context.hint & APPCORE_UI_BASE_HINT_WINDOW_AUTO_CONTROL) {
453 EXPORT_API int appcore_ui_base_on_receive(aul_type type, bundle *b)
455 if (__context.state == AS_DYING) {
456 _ERR("Skip the event in dying state");
460 if (type == AUL_TERMINATE_BGAPP && __context.state != AS_PAUSED)
463 if (type == AUL_START)
464 __exit_from_suspend();
466 appcore_base_on_receive(type, b);
479 case AUL_TERMINATE_BGAPP:
480 _DBG("[APP %d] is paused. TERMINATE", getpid());
481 __context.state = AS_DYING;
482 aul_status_update(STATUS_DYING);
483 if (__context.ops.base.exit)
484 __context.ops.base.exit(__context.data);
496 EXPORT_API int appcore_ui_base_on_create(void)
498 appcore_base_on_create();
500 __context.hshow = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __stub_show_cb, NULL);
501 __context.hhide = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __stub_hide_cb, NULL);
502 __context.hvchange = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE,
503 __stub_visibility_cb, NULL);
504 __context.hlower = ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER, __stub_lower_cb, NULL);
505 __context.state = AS_CREATED;
506 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]", __context.appid);
511 EXPORT_API int appcore_ui_base_on_terminate(void)
513 if (__context.state == AS_RUNNING) {
514 if (__context.ops.pause)
515 __context.ops.pause(__context.data);
518 __context.state = AS_DYING;
520 appcore_base_on_terminate();
525 EXPORT_API int appcore_ui_base_on_pause(void)
530 EXPORT_API int appcore_ui_base_on_resume(void)
535 EXPORT_API int appcore_ui_base_on_control(bundle *b)
537 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]", __context.appid);
538 appcore_base_on_control(b);
539 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]", __context.appid);
544 static void __group_attach()
546 if (!(__context.hint & APPCORE_UI_BASE_HINT_WINDOW_GROUP_CONTROL))
549 appcore_ui_base_group_add();
552 static void __group_lower()
554 if (!(__context.hint & APPCORE_UI_BASE_HINT_WINDOW_GROUP_CONTROL))
556 appcore_ui_base_group_remove();
559 EXPORT_API void appcore_ui_base_group_add()
561 static bool attached = false;
563 _DBG("__group_attach");
567 int wid = __get_main_surface();
569 _ERR("window wasn't ready");
573 aul_app_group_set_window(wid);
577 EXPORT_API void appcore_ui_base_group_remove()
581 _DBG("__group_lower");
582 aul_app_group_lower(&exit);
584 _DBG("__group_lower : sub-app!");
585 if (__context.ops.base.exit)
586 __context.ops.base.exit(__context.data);
590 EXPORT_API void appcore_ui_base_window_on_show(int type, void *event)
592 Ecore_Wl_Event_Window_Show *ev;
595 if (ev->parent_win != 0) {
596 /* This is child window. Skip!!! */
600 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x, %d\n", ev->win, ev->data[0]);
602 if (!__find_win((unsigned int)ev->win))
603 __add_win((unsigned int)ev->win, (unsigned int)ev->data[0]);
605 __update_win((unsigned int)ev->win, (unsigned int)ev->data[0], FALSE);
607 if (ev->data[0] != 0)
611 static bool __check_visible(void)
614 struct win_node *entry = NULL;
616 _DBG("[EVENT_TEST][EVENT] __check_visible\n");
618 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
620 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
621 if (entry->bfobscured == FALSE)
628 EXPORT_API void appcore_ui_base_window_on_hide(int type, void *event)
630 Ecore_Wl_Event_Window_Hide *ev;
634 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
636 if (__find_win((unsigned int)ev->win)) {
637 __delete_win((unsigned int)ev->win);
638 bvisibility = __check_visible();
639 if (!bvisibility && b_active == TRUE) {
640 _DBG(" Go to Pasue state \n");
647 EXPORT_API void appcore_ui_base_window_on_lower(int type, void *event)
649 Ecore_Wl_Event_Window_Lower *ev;
654 _DBG("ECORE_WL_EVENT_WINDOW_LOWER window id:%u\n", ev->win);
658 EXPORT_API void appcore_ui_base_window_on_visibility(int type, void *event)
660 Ecore_Wl_Event_Window_Visibility_Change *ev;
664 __update_win((unsigned int)ev->win, 0, ev->fully_obscured);
665 bvisibility = __check_visible();
667 _DBG("bvisibility %d, b_active %d", bvisibility, b_active);
669 if (bvisibility && b_active == FALSE) {
670 _DBG(" Go to Resume state\n");
673 } else if (!bvisibility && b_active == TRUE) {
674 _DBG(" Go to Pasue state \n");
678 _DBG(" No change state \n");
683 EXPORT_API int appcore_ui_base_init(appcore_ui_base_ops ops, int argc, char **argv,
684 void *data, unsigned int hint)
686 const char *bg_launch;
688 char appid[PATH_MAX] = {0, };
690 appcore_ui_plugin_init(&ops, argc, argv, &hint);
691 aul_app_get_appid_bypid(getpid(), appid, sizeof(appid));
693 __context.data = data;
694 __context.argc = argc;
695 __context.argv = argv;
696 __context.hint = hint;
697 __context.state = AS_NONE;
698 __context.appid = strdup(appid);
700 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", appid);
701 if (__context.hint & APPCORE_UI_BASE_HINT_BG_LAUNCH_CONTROL) {
702 b = bundle_import_from_argv(argc, argv);
704 bg_launch = bundle_get_val(b, AUL_SVC_K_BG_LAUNCH);
705 if (bg_launch && strcmp(bg_launch, "enable") == 0)
712 return appcore_base_init(ops.base, argc, argv, data);
715 EXPORT_API void appcore_ui_base_fini(void)
718 ecore_event_handler_del(__context.hshow);
720 ecore_event_handler_del(__context.hhide);
721 if (__context.hvchange)
722 ecore_event_handler_del(__context.hvchange);
723 if (__context.hlower)
724 ecore_event_handler_del(__context.hlower);
728 free(__context.appid);
729 __context.appid = NULL;
731 appcore_ui_plugin_fini();
735 EXPORT_API void appcore_ui_base_pause(void)
740 EXPORT_API void appcore_ui_base_resume(void)
745 EXPORT_API bool appcore_ui_base_is_resumed(void)
747 return __context.state == AS_RUNNING;
750 EXPORT_API void appcore_ui_base_exit(void)
752 if (__context.ops.base.exit)
753 __context.ops.base.exit(__context.data);
756 EXPORT_API unsigned int appcore_ui_base_get_main_window(void)
758 return __get_main_window();
761 EXPORT_API unsigned int appcore_ui_base_get_main_surface(void)
763 return __get_main_surface();
766 EXPORT_API int appcore_ui_base_get_hint(void)
768 return __context.hint;
771 static int __on_receive(aul_type type, bundle *b, void *data)
773 return appcore_ui_base_on_receive(type, b);
776 static int __on_create(void *data)
778 return appcore_ui_base_on_create();
781 static int __on_terminate(void *data)
783 return appcore_ui_base_on_terminate();
786 static int __on_pause(void *data)
788 return appcore_ui_base_on_pause();
791 static int __on_resume(void *data)
793 return appcore_ui_base_on_resume();
796 static void __window_on_show(int type, void *event, void *data)
798 appcore_ui_base_window_on_show(type, event);
801 static void __window_on_hide(int type, void *event, void *data)
803 appcore_ui_base_window_on_hide(type, event);
806 static void __window_on_lower(int type, void *event, void *data)
808 appcore_ui_base_window_on_lower(type, event);
811 static void __window_on_visibility(int type, void *event, void *data)
813 appcore_ui_base_window_on_visibility(type, event);
816 EXPORT_API appcore_ui_base_ops appcore_ui_base_get_default_ops(void)
818 appcore_ui_base_ops ops;
820 ops.base = appcore_base_get_default_ops();
822 /* override methods */
823 ops.base.create = __on_create;
824 ops.base.terminate = __on_terminate;
825 ops.base.receive = __on_receive;
826 ops.base.init = NULL;
827 ops.base.finish = NULL;
829 ops.base.exit = NULL;
831 ops.pause = __on_pause;
832 ops.resume = __on_resume;
833 ops.window.show = __window_on_show;
834 ops.window.hide = __window_on_hide;
835 ops.window.lower = __window_on_lower;
836 ops.window.visibility = __window_on_visibility;