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>
37 #include <X11/Xatom.h>
43 #include <Ecore_Evas.h>
44 #include <Ecore_Input_Evas.h>
45 #include <Elementary.h>
46 #include <glib-object.h>
49 #include <dbus/dbus.h>
52 #include "appcore-internal.h"
53 #include "appcore-efl.h"
57 static bool resource_reclaiming = TRUE;
58 static int tmp_val = 0;
60 enum proc_status_type { /** cgroup command type **/
63 PROC_STATUS_TERMINATE,
72 Ecore_Event_Handler *hshow;
73 Ecore_Event_Handler *hhide;
74 Ecore_Event_Handler *hvchange;
75 Ecore_Event_Handler *hcmsg; /* WM_ROTATE */
77 Ecore_Timer *mftimer; /* Ecore Timer for memory flushing */
79 struct appcore_ops *ops;
80 void (*mfcb) (void); /* Memory Flushing Callback */
85 int (*rot_cb) (void *event_info, enum appcore_rm, void *);
87 enum appcore_rm rot_mode;
91 static struct ui_priv priv;
93 static const char *_ae_name[AE_MAX] = {
94 [AE_UNKNOWN] = "UNKNOWN",
95 [AE_CREATE] = "CREATE",
96 [AE_TERMINATE] = "TERMINATE",
98 [AE_RESUME] = "RESUME",
100 [AE_LOWMEM_POST] = "LOWMEM_POST",
101 [AE_MEM_FLUSH] = "MEM_FLUSH",
104 static const char *_as_name[] = {
106 [AS_CREATED] = "CREATED",
107 [AS_RUNNING] = "RUNNING",
108 [AS_PAUSED] = "PAUSED",
109 [AS_DYING] = "DYING",
112 static bool b_active = FALSE;
113 static bool first_launch = TRUE;
123 static struct ui_wm_rotate wm_rotate;
124 static Eina_Bool __visibility_cb(void *data, int type, void *event);
127 static void _send_to_resourced(enum proc_status_type type)
129 DBusConnection *conn;
131 DBusError dbus_error;
133 dbus_error_init(&dbus_error);
135 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
137 _ERR("dbus_bus_get failed : [%s]", dbus_error.message);
138 dbus_error_free(&dbus_error);
142 msg = dbus_message_new_signal("/Org/Tizen/ResourceD/Process",
143 "org.tizen.resourced.process",
146 _ERR("dbus_message_new_signal is failed");
150 if (!dbus_message_append_args(msg,
151 DBUS_TYPE_INT32, &type,
152 DBUS_TYPE_INT32, &_pid,
153 DBUS_TYPE_INVALID)) {
154 _ERR("dbus_message_append_args is failed. type = %d, pid = %d",
156 dbus_message_unref(msg);
160 if (!dbus_connection_send (conn, msg, NULL)) {
161 _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();
191 static void __appcore_timer_add(struct ui_priv *ui)
193 ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
196 static void __appcore_timer_del(struct ui_priv *ui)
199 ecore_timer_del(ui->mftimer);
206 static int __appcore_low_memory_post_cb(ui_priv *ui)
211 #define __appcore_timer_add(ui) 0
212 #define __appcore_timer_del(ui) 0
216 static void __appcore_efl_memory_flush_cb(void)
218 _DBG("[APP %d] __appcore_efl_memory_flush_cb()", _pid);
219 elm_cache_all_flush();
222 static void wl_raise_win()
224 Ecore_Wl_Window *win;
225 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);
232 static void __do_app(enum app_event event, void *data, bundle * b)
235 struct ui_priv *ui = data;
237 _DBG("[APP %d] Event: %d", _pid, event);
238 _ret_if(ui == NULL || event >= AE_MAX);
239 _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
240 _as_name[ui->state]);
242 if (event == AE_MEM_FLUSH) {
247 if (event == AE_LOWMEM_POST) {
248 if (__appcore_low_memory_post_cb(ui) == 0)
252 if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
253 __appcore_timer_del(ui);
255 if (ui->state == AS_DYING) {
256 _ERR("Skip the event in dying state");
260 if (event == AE_TERMINATE) {
261 _DBG("[APP %d] TERMINATE", _pid);
263 aul_status_update(STATUS_DYING);
267 if (event == AE_RAISE) {
269 x_raise_win(getpid());
276 if (event == AE_LOWER) {
278 x_pause_win(getpid());
281 /* TODO: wayland support */
284 _ret_if(ui->ops == NULL);
288 _DBG("[APP %d] RESET", _pid);
289 ui->pending_data = bundle_dup(b);
290 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]", ui->name);
292 r = ui->ops->reset(b, ui->ops->data);
293 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]", ui->name);
296 first_launch = FALSE;
298 _INFO("[APP %d] App already running, raise the window", _pid);
300 x_raise_win(getpid());
305 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
309 if (ui->state == AS_RUNNING) {
310 _DBG("[APP %d] PAUSE", _pid);
312 r = ui->ops->pause(ui->ops->data);
313 ui->state = AS_PAUSED;
314 if(r >= 0 && resource_reclaiming == TRUE)
315 __appcore_timer_add(ui);
317 /* TODO : rotation stop */
318 //r = appcore_pause_rotation_cb();
319 aul_status_update(STATUS_BG);
320 _send_to_resourced(PROC_STATUS_BACKGRD);
323 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
325 if (ui->state == AS_PAUSED || ui->state == AS_CREATED) {
326 _DBG("[APP %d] RESUME", _pid);
328 if (ui->state == AS_CREATED) {
329 appcore_group_reset(ui->pending_data);
330 bundle_free(ui->pending_data);
331 ui->pending_data = NULL;
333 appcore_group_resume();
336 if (ui->ops->resume) {
337 ui->ops->resume(ui->ops->data);
339 ui->state = AS_RUNNING;
341 /*TODO : rotation start*/
342 //r = appcore_resume_rotation_cb();
343 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
345 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
347 aul_status_update(STATUS_VISIBLE);
348 _send_to_resourced(PROC_STATUS_FOREGRD);
350 case AE_TERMINATE_BGAPP:
351 if (ui->state == AS_PAUSED) {
352 _DBG("[APP %d] is paused. TERMINATE", _pid);
353 ui->state = AS_DYING;
354 aul_status_update(STATUS_DYING);
356 } else if (ui->state == AS_RUNNING) {
357 _DBG("[APP %d] is running.", _pid);
359 _DBG("[APP %d] is another state", _pid);
368 static struct ui_ops efl_ops = {
374 static bool __check_visible(void)
377 struct win_node *entry = NULL;
379 _DBG("[EVENT_TEST][EVENT] __check_visible\n");
381 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
383 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
384 if(entry->bfobscured == FALSE)
390 static GSList *__find_win(unsigned int win)
395 for (iter = g_winnode_list; iter; iter = g_slist_next(iter)) {
397 if (t && t->win == win)
405 static bool __add_win(unsigned int win)
410 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
415 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
419 t = calloc(1, sizeof(struct win_node));
424 t->bfobscured = FALSE;
426 g_winnode_list = g_slist_append(g_winnode_list, t);
431 static bool __add_win(unsigned int win, unsigned int surf)
436 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
441 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
445 t = calloc(1, sizeof(struct win_node));
451 t->bfobscured = FALSE;
453 g_winnode_list = g_slist_append(g_winnode_list, t);
459 static bool __delete_win(unsigned int win)
466 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
472 g_winnode_list = g_slist_delete_link(g_winnode_list, f);
478 static bool __update_win(unsigned int win, bool bfobscured)
483 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
489 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
493 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
495 t = (struct win_node *)f->data;
497 t->bfobscured = bfobscured;
499 g_winnode_list = g_slist_concat(g_winnode_list, f);
504 static bool __update_win(unsigned int win, unsigned int surf, bool bfobscured)
509 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
515 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
519 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
521 t = (struct win_node *)f->data;
525 t->bfobscured = bfobscured;
527 g_winnode_list = g_slist_concat(g_winnode_list, f);
535 static Ecore_X_Atom _WM_WINDOW_ROTATION_SUPPORTED = 0;
536 static Ecore_X_Atom _WM_WINDOW_ROTATION_CHANGE_REQUEST = 0;
538 static int __check_wm_rotation_support(void)
540 _DBG("Disable window manager rotation");
543 Ecore_X_Window root, win, win2;
546 if (!_WM_WINDOW_ROTATION_SUPPORTED) {
547 _WM_WINDOW_ROTATION_SUPPORTED =
548 ecore_x_atom_get("_E_WINDOW_ROTATION_SUPPORTED");
551 if (!_WM_WINDOW_ROTATION_CHANGE_REQUEST) {
552 _WM_WINDOW_ROTATION_CHANGE_REQUEST =
553 ecore_x_atom_get("_E_WINDOW_ROTATION_CHANGE_REQUEST");
556 root = ecore_x_window_root_first_get();
557 ret = ecore_x_window_prop_xid_get(root,
558 _WM_WINDOW_ROTATION_SUPPORTED,
561 if ((ret == 1) && (win))
563 ret = ecore_x_window_prop_xid_get(win,
564 _WM_WINDOW_ROTATION_SUPPORTED,
567 if ((ret == 1) && (win2 == win))
574 static void __set_wm_rotation_support(unsigned int win, unsigned int set)
577 struct win_node *entry = NULL;
580 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
583 ecore_x_window_prop_card32_set(entry->win,
584 _WM_WINDOW_ROTATION_SUPPORTED,
589 ecore_x_window_prop_card32_set(win,
590 _WM_WINDOW_ROTATION_SUPPORTED,
597 static Eina_Bool __show_cb(void *data, int type, void *event)
600 Ecore_Wl_Event_Window_Show *ev;
603 if (ev->parent_win != 0)
605 // This is child window. Skip!!!
606 return ECORE_CALLBACK_PASS_ON;
609 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x, %d\n", ev->win, ev->data[0]);
611 if (!__find_win((unsigned int)ev->win))
612 __add_win((unsigned int)ev->win, (unsigned int)ev->data[0]);
614 __update_win((unsigned int)ev->win, (unsigned int)ev->data[0], FALSE);
617 Ecore_X_Event_Window_Show *ev;
619 Ecore_X_Window parent;
623 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
625 if (!__find_win((unsigned int)ev->win)) {
627 if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
628 __set_wm_rotation_support(ev->win, 1);
630 __add_win((unsigned int)ev->win);
633 __update_win((unsigned int)ev->win, FALSE);
636 return ECORE_CALLBACK_RENEW;
639 static Eina_Bool __hide_cb(void *data, int type, void *event)
642 Ecore_Wl_Event_Window_Hide *ev;
644 Ecore_X_Event_Window_Hide *ev;
650 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
652 if (__find_win((unsigned int)ev->win)) {
653 __delete_win((unsigned int)ev->win);
654 bvisibility = __check_visible();
655 if (!bvisibility && b_active == TRUE) {
656 _DBG(" Go to Pasue state \n");
658 __do_app(AE_PAUSE, data, NULL);
662 return ECORE_CALLBACK_RENEW;
665 static Eina_Bool __visibility_cb(void *data, int type, void *event)
668 Ecore_Wl_Event_Window_Visibility_Change *ev;
671 __update_win((unsigned int)ev->win, 0, ev->fully_obscured);
673 Ecore_X_Event_Window_Visibility_Change *ev;
678 __update_win((unsigned int)ev->win, ev->fully_obscured);
680 bvisibility = __check_visible();
682 _DBG("bvisibility %d, b_active %d", bvisibility, b_active);
684 if (bvisibility && b_active == FALSE) {
685 _DBG(" Go to Resume state\n");
687 __do_app(AE_RESUME, data, NULL);
689 } else if (!bvisibility && b_active == TRUE) {
690 _DBG(" Go to Pasue state \n");
692 __do_app(AE_PAUSE, data, NULL);
694 _DBG(" No change state \n");
696 return ECORE_CALLBACK_RENEW;
702 static Eina_Bool __cmsg_cb(void *data, int type, void *event)
704 struct ui_priv *ui = (struct ui_priv *)data;
705 Ecore_X_Event_Client_Message *e = event;
707 if (!ui) return ECORE_CALLBACK_PASS_ON;
708 if (e->format != 32) return ECORE_CALLBACK_PASS_ON;
709 if (e->message_type == _WM_WINDOW_ROTATION_CHANGE_REQUEST) {
710 if ((0 == ui->wm_rot_supported) ||
711 (0 == ui->rot_started) ||
712 (NULL == ui->rot_cb)) {
713 return ECORE_CALLBACK_PASS_ON;
717 switch (e->data.l[1])
719 case 0: rm = APPCORE_RM_PORTRAIT_NORMAL; break;
720 case 90: rm = APPCORE_RM_LANDSCAPE_REVERSE; break;
721 case 180: rm = APPCORE_RM_PORTRAIT_REVERSE; break;
722 case 270: rm = APPCORE_RM_LANDSCAPE_NORMAL; break;
723 default: rm = APPCORE_RM_UNKNOWN; break;
728 if (APPCORE_RM_UNKNOWN != rm) {
729 ui->rot_cb((void *)&rm, rm, ui->rot_cb_data);
733 return ECORE_CALLBACK_PASS_ON;
737 static void __add_climsg_cb(struct ui_priv *ui)
742 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_SHOW, __show_cb, ui);
744 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_HIDE, __hide_cb, ui);
746 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_VISIBILITY_CHANGE,
747 __visibility_cb, ui);
750 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, __show_cb, ui);
752 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_HIDE, __hide_cb, ui);
754 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE,
755 __visibility_cb, ui);
757 /* Add client message callback for WM_ROTATE */
758 if(!__check_wm_rotation_support())
761 ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, __cmsg_cb, ui);
762 ui->wm_rot_supported = 1;
763 appcore_set_wm_rotation(&wm_rotate);
768 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
773 if (argc == NULL || argv == NULL) {
774 _ERR("argc/argv is NULL");
780 elm_init(*argc, *argv);
782 hwacc = getenv("HWACC");
785 _DBG("elm_config_preferred_engine_set is not called");
786 } else if(strcmp(hwacc, "USE") == 0) {
788 elm_config_preferred_engine_set("wayland_egl");
789 _DBG("elm_config_preferred_engine_set : wayland_egl");
791 elm_config_preferred_engine_set("opengl_x11");
792 _DBG("elm_config_preferred_engine_set : opengl_x11");
794 } else if(strcmp(hwacc, "NOT_USE") == 0) {
796 elm_config_preferred_engine_set("wayland_shm");
797 _DBG("elm_config_preferred_engine_set : wayland_shm");
799 elm_config_preferred_engine_set("software_x11");
800 _DBG("elm_config_preferred_engine_set : software_x11");
803 _DBG("elm_config_preferred_engine_set is not called");
806 r = appcore_init(ui->name, &efl_ops, *argc, *argv);
807 _retv_if(r == -1, -1);
809 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
810 if (ui->ops && ui->ops->create) {
811 r = ui->ops->create(ui->ops->data);
813 _ERR("create() return error");
815 if (ui->ops && ui->ops->terminate)
816 ui->ops->terminate(ui->ops->data);
820 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
823 ui->state = AS_CREATED;
830 static void __after_loop(struct ui_priv *ui)
832 appcore_unset_rotation_cb();
835 if (ui->state == AS_RUNNING) {
836 _DBG("[APP %d] PAUSE before termination", _pid);
837 if (ui->ops && ui->ops->pause)
838 ui->ops->pause(ui->ops->data);
841 if (ui->ops && ui->ops->terminate)
842 ui->ops->terminate(ui->ops->data);
844 ui->state = AS_DYING;
847 ecore_event_handler_del(ui->hshow);
849 ecore_event_handler_del(ui->hhide);
851 ecore_event_handler_del(ui->hvchange);
853 __appcore_timer_del(ui);
858 static int __set_data(struct ui_priv *ui, const char *name,
859 struct appcore_ops *ops)
862 _ERR("Mainloop already started");
867 if (name == NULL || name[0] == '\0') {
868 _ERR("Invalid name");
879 ui->name = strdup(name);
880 _retv_if(ui->name == NULL, -1);
884 ui->mfcb = __appcore_efl_memory_flush_cb;
889 ui->wm_rot_supported = 0;
892 ui->rot_cb_data = NULL;
893 ui->rot_mode = APPCORE_RM_UNKNOWN;
898 static void __unset_data(struct ui_priv *ui)
901 free((void *)ui->name);
903 memset(ui, 0, sizeof(struct ui_priv));
907 static int __wm_set_rotation_cb(int (*cb) (void *event_info, enum appcore_rm, void *), void *data)
914 if ((priv.wm_rot_supported) && (0 == priv.rot_started)) {
915 __set_wm_rotation_support(0, 1);
919 priv.rot_cb_data = data;
920 priv.rot_started = 1;
925 static int __wm_unset_rotation_cb(void)
927 if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
928 __set_wm_rotation_support(0, 0);
932 priv.rot_cb_data = NULL;
933 priv.rot_started = 0;
938 static int __wm_get_rotation_state(enum appcore_rm *curr)
945 *curr = priv.rot_mode;
950 static int __wm_pause_rotation_cb(void)
952 if ((1 == priv.rot_started) && (priv.wm_rot_supported)) {
953 __set_wm_rotation_support(0, 0);
956 priv.rot_started = 0;
961 static int __wm_resume_rotation_cb(void)
963 if ((0 == priv.rot_started) && (priv.wm_rot_supported)) {
964 __set_wm_rotation_support(0, 1);
967 priv.rot_started = 1;
972 static struct ui_wm_rotate wm_rotate = {
973 __wm_set_rotation_cb,
974 __wm_unset_rotation_cb,
975 __wm_get_rotation_state,
976 __wm_pause_rotation_cb,
977 __wm_resume_rotation_cb
980 EXPORT_API int appcore_efl_init(const char *name, int *argc, char ***argv,
981 struct appcore_ops *ops)
985 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
987 r = __set_data(&priv, name, ops);
988 _retv_if(r == -1, -1);
990 r = __before_loop(&priv, argc, argv);
999 EXPORT_API void appcore_efl_fini(void)
1001 aul_status_update(STATUS_DYING);
1003 __after_loop(&priv);
1005 __unset_data(&priv);
1008 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
1009 struct appcore_ops *ops)
1013 r = appcore_efl_init(name, argc, argv, ops);
1014 _retv_if(r == -1, -1);
1023 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable)
1025 resource_reclaiming = enable;
1030 EXPORT_API int appcore_set_app_state(int state)
1039 EXPORT_API unsigned int appcore_get_main_window()
1041 struct win_node *entry = NULL;
1043 if (g_winnode_list != NULL) {
1044 entry = g_winnode_list->data;
1045 return (unsigned int) entry->win;
1050 EXPORT_API unsigned int appcore_get_main_surface()
1052 struct win_node *entry = NULL;
1054 if (g_winnode_list != NULL) {
1055 entry = g_winnode_list->data;
1056 return (unsigned int) entry->surf;