4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
22 #include <sys/types.h>
23 #include <sys/socket.h>
33 #include <Ecore_Wayland.h>
35 #include <X11/Xatom.h>
41 #include <Ecore_Evas.h>
42 #include <Ecore_Input_Evas.h>
43 #include <Elementary.h>
44 #include <glib-object.h>
47 #include <dbus/dbus.h>
51 #include "appcore-internal.h"
52 #include "appcore-efl.h"
55 static bool resource_reclaiming = TRUE;
56 static int tmp_val = 0;
58 /* cgroup command type */
59 enum proc_status_type {
62 PROC_STATUS_TERMINATE,
71 Ecore_Event_Handler *hshow;
72 Ecore_Event_Handler *hhide;
73 Ecore_Event_Handler *hvchange;
75 Ecore_Event_Handler *hlower;
77 Ecore_Event_Handler *hcmsg; /* WM_ROTATE */
79 Ecore_Timer *mftimer; /* Ecore Timer for memory flushing */
81 struct appcore_ops *ops;
82 void (*mfcb) (void); /* Memory Flushing Callback */
87 int (*rot_cb) (void *event_info, enum appcore_rm, void *);
89 enum appcore_rm rot_mode;
93 static struct ui_priv priv;
95 static const char *_ae_name[AE_MAX] = {
96 [AE_UNKNOWN] = "UNKNOWN",
97 [AE_CREATE] = "CREATE",
98 [AE_TERMINATE] = "TERMINATE",
100 [AE_RESUME] = "RESUME",
101 [AE_RESET] = "RESET",
102 [AE_LOWMEM_POST] = "LOWMEM_POST",
103 [AE_MEM_FLUSH] = "MEM_FLUSH",
106 static const char *_as_name[] = {
108 [AS_CREATED] = "CREATED",
109 [AS_RUNNING] = "RUNNING",
110 [AS_PAUSED] = "PAUSED",
111 [AS_DYING] = "DYING",
114 static bool b_active = FALSE;
115 static bool first_launch = TRUE;
125 static struct ui_wm_rotate wm_rotate;
126 static Eina_Bool __visibility_cb(void *data, int type, void *event);
128 static void _send_to_resourced(enum proc_status_type type)
130 DBusConnection *conn;
132 DBusError dbus_error;
134 dbus_error_init(&dbus_error);
136 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
138 _ERR("dbus_bus_get failed : [%s]", dbus_error.message);
139 dbus_error_free(&dbus_error);
143 msg = dbus_message_new_signal("/Org/Tizen/ResourceD/Process",
144 "org.tizen.resourced.process",
147 _ERR("dbus_message_new_signal is failed");
151 if (!dbus_message_append_args(msg,
152 DBUS_TYPE_INT32, &type,
153 DBUS_TYPE_INT32, &_pid,
154 DBUS_TYPE_INVALID)) {
155 _ERR("dbus_message_append_args is failed. type = %d, pid = %d",
157 dbus_message_unref(msg);
161 if (!dbus_connection_send(conn, msg, NULL))
162 _ERR("dbus_connection_send is failed");
164 dbus_message_unref(msg);
167 static GSList *g_winnode_list;
169 #if defined(MEMORY_FLUSH_ACTIVATE)
170 static Eina_Bool __appcore_memory_flush_cb(void *data)
172 struct ui_priv *ui = (struct ui_priv *)data;
174 appcore_flush_memory();
177 return ECORE_CALLBACK_CANCEL;
180 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
182 if (ui->state == AS_PAUSED)
183 appcore_flush_memory();
190 static void __appcore_timer_add(struct ui_priv *ui)
192 ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
195 static void __appcore_timer_del(struct ui_priv *ui)
198 ecore_timer_del(ui->mftimer);
205 static int __appcore_low_memory_post_cb(ui_priv *ui)
210 #define __appcore_timer_add(ui) 0
211 #define __appcore_timer_del(ui) 0
215 static void __appcore_efl_memory_flush_cb(void)
217 _DBG("[APP %d] __appcore_efl_memory_flush_cb()", _pid);
218 elm_cache_all_flush();
221 static void wl_raise_win(void)
223 Ecore_Wl_Window *win;
224 unsigned int win_id = appcore_get_main_window();
226 _DBG("Raise window: %d", win_id);
227 win = ecore_wl_window_find(win_id);
228 ecore_wl_window_activate(win);
231 static void wl_pause_win(void)
233 Ecore_Wl_Window *win;
234 GSList *wlist = g_winnode_list;
235 struct win_node *entry = NULL;
237 _DBG("Pause window");
242 _DBG("Pause window: %d", entry->win);
243 win = ecore_wl_window_find(entry->win);
244 ecore_wl_window_iconified_set(win, EINA_TRUE);
252 static void __do_app(enum app_event event, void *data, bundle * b)
255 struct ui_priv *ui = data;
257 _DBG("[APP %d] Event: %d", _pid, event);
258 _ret_if(ui == NULL || event >= AE_MAX);
259 _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
260 _as_name[ui->state]);
262 if (event == AE_MEM_FLUSH) {
267 if (event == AE_LOWMEM_POST) {
268 if (__appcore_low_memory_post_cb(ui) == 0)
272 if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
273 __appcore_timer_del(ui);
275 if (ui->state == AS_DYING) {
276 _ERR("Skip the event in dying state");
280 if (event == AE_TERMINATE) {
281 _DBG("[APP %d] TERMINATE", _pid);
283 aul_status_update(STATUS_DYING);
287 if (event == AE_RAISE) {
289 x_raise_win(getpid());
290 #elif defined(WAYLAND)
296 if (event == AE_LOWER) {
298 x_pause_win(getpid());
299 #elif defined(WAYLAND)
305 _ret_if(ui->ops == NULL);
309 _DBG("[APP %d] RESET", _pid);
310 ui->pending_data = bundle_dup(b);
311 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]", ui->name);
313 r = ui->ops->reset(b, ui->ops->data);
314 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]", ui->name);
317 first_launch = FALSE;
319 _INFO("[APP %d] App already running, raise the window", _pid);
321 x_raise_win(getpid());
326 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
330 if (ui->state == AS_RUNNING) {
331 _DBG("[APP %d] PAUSE", _pid);
333 r = ui->ops->pause(ui->ops->data);
334 ui->state = AS_PAUSED;
335 if (r >= 0 && resource_reclaiming == TRUE)
336 __appcore_timer_add(ui);
338 /* TODO : rotation stop */
339 /* r = appcore_pause_rotation_cb(); */
340 aul_status_update(STATUS_BG);
341 _send_to_resourced(PROC_STATUS_BACKGRD);
344 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
346 if (ui->state == AS_PAUSED || ui->state == AS_CREATED) {
347 _DBG("[APP %d] RESUME", _pid);
349 if (ui->state == AS_CREATED) {
350 bundle_free(ui->pending_data);
351 ui->pending_data = NULL;
355 ui->ops->resume(ui->ops->data);
356 ui->state = AS_RUNNING;
358 /*TODO : rotation start*/
359 /* r = appcore_resume_rotation_cb(); */
360 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
362 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
364 aul_status_update(STATUS_VISIBLE);
365 _send_to_resourced(PROC_STATUS_FOREGRD);
367 case AE_TERMINATE_BGAPP:
368 if (ui->state == AS_PAUSED) {
369 _DBG("[APP %d] is paused. TERMINATE", _pid);
370 ui->state = AS_DYING;
371 aul_status_update(STATUS_DYING);
373 } else if (ui->state == AS_RUNNING) {
374 _DBG("[APP %d] is running.", _pid);
376 _DBG("[APP %d] is another state", _pid);
385 static struct ui_ops efl_ops = {
390 static bool __check_visible(void)
393 struct win_node *entry = NULL;
395 _DBG("[EVENT_TEST][EVENT] __check_visible\n");
397 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
399 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
400 if (entry->bfobscured == FALSE)
407 static GSList *__find_win(unsigned int win)
412 for (iter = g_winnode_list; iter; iter = g_slist_next(iter)) {
414 if (t && t->win == win)
422 static bool __add_win(unsigned int win)
427 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
432 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
436 t = calloc(1, sizeof(struct win_node));
441 t->bfobscured = FALSE;
443 g_winnode_list = g_slist_append(g_winnode_list, t);
447 #elif defined(WAYLAND)
448 static bool __add_win(unsigned int win, unsigned int surf)
453 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
458 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
462 t = calloc(1, sizeof(struct win_node));
468 t->bfobscured = FALSE;
470 g_winnode_list = g_slist_append(g_winnode_list, t);
476 static bool __delete_win(unsigned int win)
483 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
489 g_winnode_list = g_slist_delete_link(g_winnode_list, f);
495 static bool __update_win(unsigned int win, bool bfobscured)
500 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
506 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
510 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
512 t = (struct win_node *)f->data;
514 t->bfobscured = bfobscured;
516 g_winnode_list = g_slist_concat(g_winnode_list, f);
520 #elif defined(WAYLAND)
521 static bool __update_win(unsigned int win, unsigned int surf, bool bfobscured)
526 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
532 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
536 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
538 t = (struct win_node *)f->data;
542 t->bfobscured = bfobscured;
544 g_winnode_list = g_slist_concat(g_winnode_list, f);
552 static Ecore_X_Atom _WM_WINDOW_ROTATION_SUPPORTED = 0;
553 static Ecore_X_Atom _WM_WINDOW_ROTATION_CHANGE_REQUEST = 0;
555 static int __check_wm_rotation_support(void)
557 _DBG("Disable window manager rotation");
560 Ecore_X_Window root, win, win2;
563 if (!_WM_WINDOW_ROTATION_SUPPORTED) {
564 _WM_WINDOW_ROTATION_SUPPORTED =
565 ecore_x_atom_get("_E_WINDOW_ROTATION_SUPPORTED");
568 if (!_WM_WINDOW_ROTATION_CHANGE_REQUEST) {
569 _WM_WINDOW_ROTATION_CHANGE_REQUEST =
570 ecore_x_atom_get("_E_WINDOW_ROTATION_CHANGE_REQUEST");
573 root = ecore_x_window_root_first_get();
574 ret = ecore_x_window_prop_xid_get(root,
575 _WM_WINDOW_ROTATION_SUPPORTED,
578 if ((ret == 1) && (win)) {
579 ret = ecore_x_window_prop_xid_get(win,
580 _WM_WINDOW_ROTATION_SUPPORTED,
583 if ((ret == 1) && (win2 == win))
590 static void __set_wm_rotation_support(unsigned int win, unsigned int set)
593 struct win_node *entry = NULL;
596 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
599 ecore_x_window_prop_card32_set(entry->win,
600 _WM_WINDOW_ROTATION_SUPPORTED,
605 ecore_x_window_prop_card32_set(win,
606 _WM_WINDOW_ROTATION_SUPPORTED,
612 static Eina_Bool __show_cb(void *data, int type, void *event)
615 Ecore_Wl_Event_Window_Show *ev;
618 if (ev->parent_win != 0) {
619 /* This is child window. Skip!!! */
620 return ECORE_CALLBACK_PASS_ON;
623 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x, %d\n", ev->win, ev->data[0]);
625 if (!__find_win((unsigned int)ev->win))
626 __add_win((unsigned int)ev->win, (unsigned int)ev->data[0]);
628 __update_win((unsigned int)ev->win, (unsigned int)ev->data[0], FALSE);
631 Ecore_X_Event_Window_Show *ev;
633 Ecore_X_Window parent;
637 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
639 if (!__find_win((unsigned int)ev->win)) {
641 if ((priv.wm_rot_supported) && (1 == priv.rot_started))
642 __set_wm_rotation_support(ev->win, 1);
643 __add_win((unsigned int)ev->win);
645 __update_win((unsigned int)ev->win, FALSE);
649 appcore_group_attach();
650 return ECORE_CALLBACK_RENEW;
653 static Eina_Bool __hide_cb(void *data, int type, void *event)
656 Ecore_Wl_Event_Window_Hide *ev;
658 Ecore_X_Event_Window_Hide *ev;
664 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
666 if (__find_win((unsigned int)ev->win)) {
667 __delete_win((unsigned int)ev->win);
668 bvisibility = __check_visible();
669 if (!bvisibility && b_active == TRUE) {
670 _DBG(" Go to Pasue state \n");
672 __do_app(AE_PAUSE, data, NULL);
676 return ECORE_CALLBACK_RENEW;
680 static Eina_Bool __lower_cb(void *data, int type, void *event)
682 Ecore_Wl_Event_Window_Lower *ev;
684 if (!ev) return ECORE_CALLBACK_RENEW;
685 _DBG("ECORE_WL_EVENT_WINDOW_LOWER window id:%u\n", ev->win);
686 appcore_group_lower();
687 return ECORE_CALLBACK_RENEW;
691 static Eina_Bool __visibility_cb(void *data, int type, void *event)
694 Ecore_Wl_Event_Window_Visibility_Change *ev;
697 __update_win((unsigned int)ev->win, 0, ev->fully_obscured);
699 Ecore_X_Event_Window_Visibility_Change *ev;
704 __update_win((unsigned int)ev->win, ev->fully_obscured);
706 bvisibility = __check_visible();
708 _DBG("bvisibility %d, b_active %d", bvisibility, b_active);
710 if (bvisibility && b_active == FALSE) {
711 _DBG(" Go to Resume state\n");
713 __do_app(AE_RESUME, data, NULL);
715 } else if (!bvisibility && b_active == TRUE) {
716 _DBG(" Go to Pasue state \n");
718 __do_app(AE_PAUSE, data, NULL);
720 _DBG(" No change state \n");
722 return ECORE_CALLBACK_RENEW;
728 static Eina_Bool __cmsg_cb(void *data, int type, void *event)
730 struct ui_priv *ui = (struct ui_priv *)data;
731 Ecore_X_Event_Client_Message *e = event;
734 return ECORE_CALLBACK_PASS_ON;
737 return ECORE_CALLBACK_PASS_ON;
739 if (e->message_type == _WM_WINDOW_ROTATION_CHANGE_REQUEST) {
740 if ((ui->wm_rot_supported == 0)
741 || (ui->rot_started == 0)
742 || (ui->rot_cb == NULL)) {
743 return ECORE_CALLBACK_PASS_ON;
747 switch (e->data.l[1]) {
749 rm = APPCORE_RM_PORTRAIT_NORMAL;
752 rm = APPCORE_RM_LANDSCAPE_REVERSE;
755 rm = APPCORE_RM_PORTRAIT_REVERSE;
758 rm = APPCORE_RM_LANDSCAPE_NORMAL;
761 rm = APPCORE_RM_UNKNOWN;
767 if (APPCORE_RM_UNKNOWN != rm)
768 ui->rot_cb((void *)&rm, rm, ui->rot_cb_data);
771 return ECORE_CALLBACK_PASS_ON;
775 static void __add_climsg_cb(struct ui_priv *ui)
780 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, ui);
782 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, ui);
784 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE,
785 __visibility_cb, ui);
787 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_LOWER,
791 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, __show_cb, ui);
793 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_HIDE, __hide_cb, ui);
795 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE,
796 __visibility_cb, ui);
798 /* Add client message callback for WM_ROTATE */
799 if (!__check_wm_rotation_support()) {
800 ui->hcmsg = ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE,
802 ui->wm_rot_supported = 1;
803 appcore_set_wm_rotation(&wm_rotate);
808 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
813 if (argc == NULL || argv == NULL) {
814 _ERR("argc/argv is NULL");
819 #if !(GLIB_CHECK_VERSION(2, 36, 0))
822 elm_init(*argc, *argv);
824 hwacc = getenv("HWACC");
826 _DBG("elm_config_accel_preference_set is not called");
827 } else if (strcmp(hwacc, "USE") == 0) {
828 elm_config_accel_preference_set("hw");
829 _DBG("elm_config_accel_preference_set : hw");
830 } else if (strcmp(hwacc, "NOT_USE") == 0) {
831 elm_config_accel_preference_set("none");
832 _DBG("elm_config_accel_preference_set : none");
834 _DBG("elm_config_accel_preference_set is not called");
837 r = appcore_init(ui->name, &efl_ops, *argc, *argv);
838 _retv_if(r == -1, -1);
840 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
841 if (ui->ops && ui->ops->create) {
842 r = ui->ops->create(ui->ops->data);
844 _ERR("create() return error");
846 if (ui->ops && ui->ops->terminate)
847 ui->ops->terminate(ui->ops->data);
851 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
854 ui->state = AS_CREATED;
861 static void __after_loop(struct ui_priv *ui)
863 appcore_unset_rotation_cb();
866 if (ui->state == AS_RUNNING) {
867 _DBG("[APP %d] PAUSE before termination", _pid);
868 if (ui->ops && ui->ops->pause)
869 ui->ops->pause(ui->ops->data);
872 if (ui->ops && ui->ops->terminate)
873 ui->ops->terminate(ui->ops->data);
875 ui->state = AS_DYING;
878 ecore_event_handler_del(ui->hshow);
880 ecore_event_handler_del(ui->hhide);
882 ecore_event_handler_del(ui->hvchange);
885 ecore_event_handler_del(ui->hlower);
888 __appcore_timer_del(ui);
893 static int __set_data(struct ui_priv *ui, const char *name,
894 struct appcore_ops *ops)
897 _ERR("Mainloop already started");
902 if (name == NULL || name[0] == '\0') {
903 _ERR("Invalid name");
914 ui->name = strdup(name);
915 _retv_if(ui->name == NULL, -1);
918 ui->mfcb = __appcore_efl_memory_flush_cb;
922 ui->wm_rot_supported = 0;
925 ui->rot_cb_data = NULL;
926 ui->rot_mode = APPCORE_RM_UNKNOWN;
931 static void __unset_data(struct ui_priv *ui)
934 free((void *)ui->name);
936 memset(ui, 0, sizeof(struct ui_priv));
940 static int __wm_set_rotation_cb(int (*cb) (void *event_info, enum appcore_rm, void *), void *data)
947 if ((priv.wm_rot_supported) && (0 == priv.rot_started))
948 __set_wm_rotation_support(0, 1);
951 priv.rot_cb_data = data;
952 priv.rot_started = 1;
957 static int __wm_unset_rotation_cb(void)
959 if ((priv.wm_rot_supported) && (1 == priv.rot_started))
960 __set_wm_rotation_support(0, 0);
963 priv.rot_cb_data = NULL;
964 priv.rot_started = 0;
969 static int __wm_get_rotation_state(enum appcore_rm *curr)
976 *curr = priv.rot_mode;
981 static int __wm_pause_rotation_cb(void)
983 if ((priv.rot_started == 1) && (priv.wm_rot_supported))
984 __set_wm_rotation_support(0, 0);
986 priv.rot_started = 0;
991 static int __wm_resume_rotation_cb(void)
993 if ((priv.rot_started == 0) && (priv.wm_rot_supported))
994 __set_wm_rotation_support(0, 1);
996 priv.rot_started = 1;
1001 static struct ui_wm_rotate wm_rotate = {
1002 __wm_set_rotation_cb,
1003 __wm_unset_rotation_cb,
1004 __wm_get_rotation_state,
1005 __wm_pause_rotation_cb,
1006 __wm_resume_rotation_cb
1009 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
1010 struct appcore_ops *ops)
1014 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
1016 r = __set_data(&priv, name, ops);
1017 _retv_if(r == -1, -1);
1019 r = __before_loop(&priv, argc, argv);
1021 __unset_data(&priv);
1027 aul_status_update(STATUS_DYING);
1029 __after_loop(&priv);
1031 __unset_data(&priv);
1036 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable)
1038 resource_reclaiming = enable;
1043 EXPORT_API int appcore_set_app_state(int state)
1052 EXPORT_API unsigned int appcore_get_main_window(void)
1054 struct win_node *entry = NULL;
1056 if (g_winnode_list != NULL) {
1057 entry = g_winnode_list->data;
1058 return (unsigned int) entry->win;
1064 #if defined(WAYLAND)
1065 EXPORT_API unsigned int appcore_get_main_surface(void)
1067 struct win_node *entry = NULL;
1069 if (g_winnode_list != NULL) {
1070 entry = g_winnode_list->data;
1071 return (unsigned int) entry->surf;