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;
90 static struct ui_priv priv;
92 static const char *_ae_name[AE_MAX] = {
93 [AE_UNKNOWN] = "UNKNOWN",
94 [AE_CREATE] = "CREATE",
95 [AE_TERMINATE] = "TERMINATE",
97 [AE_RESUME] = "RESUME",
99 [AE_LOWMEM_POST] = "LOWMEM_POST",
100 [AE_MEM_FLUSH] = "MEM_FLUSH",
103 static const char *_as_name[] = {
105 [AS_CREATED] = "CREATED",
106 [AS_RUNNING] = "RUNNING",
107 [AS_PAUSED] = "PAUSED",
108 [AS_DYING] = "DYING",
111 static bool b_active = FALSE;
112 static bool first_launch = TRUE;
119 static struct ui_wm_rotate wm_rotate;
120 static Eina_Bool __visibility_cb(void *data, int type, void *event);
123 static void _send_to_resourced(enum proc_status_type type)
125 DBusConnection *conn;
127 DBusError dbus_error;
129 dbus_error_init(&dbus_error);
131 conn = dbus_bus_get(DBUS_BUS_SYSTEM, &dbus_error);
133 _ERR("dbus_bus_get failed : [%s]", dbus_error.message);
134 dbus_error_free(&dbus_error);
138 msg = dbus_message_new_signal("/Org/Tizen/ResourceD/Process",
139 "org.tizen.resourced.process",
142 _ERR("dbus_message_new_signal is failed");
146 if (!dbus_message_append_args(msg,
147 DBUS_TYPE_INT32, &type,
148 DBUS_TYPE_INT32, &_pid,
149 DBUS_TYPE_INVALID)) {
150 _ERR("dbus_message_append_args is failed. type = %d, pid = %d",
152 dbus_message_unref(msg);
156 if (!dbus_connection_send (conn, msg, NULL)) {
157 _ERR("dbus_connection_send is failed");
160 dbus_message_unref(msg);
163 static GSList *g_winnode_list;
165 #if defined(MEMORY_FLUSH_ACTIVATE)
166 static Eina_Bool __appcore_memory_flush_cb(void *data)
168 struct ui_priv *ui = (struct ui_priv *)data;
170 appcore_flush_memory();
173 return ECORE_CALLBACK_CANCEL;
176 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
178 if (ui->state == AS_PAUSED) {
179 appcore_flush_memory();
187 static void __appcore_timer_add(struct ui_priv *ui)
189 ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
192 static void __appcore_timer_del(struct ui_priv *ui)
195 ecore_timer_del(ui->mftimer);
202 static int __appcore_low_memory_post_cb(ui_priv *ui)
207 #define __appcore_timer_add(ui) 0
208 #define __appcore_timer_del(ui) 0
212 static void __appcore_efl_memory_flush_cb(void)
214 _DBG("[APP %d] __appcore_efl_memory_flush_cb()", _pid);
215 elm_cache_all_flush();
218 static void __do_app(enum app_event event, void *data, bundle * b)
221 struct ui_priv *ui = data;
223 _DBG("[APP %d] Event: %d", _pid, event);
224 _ret_if(ui == NULL || event >= AE_MAX);
225 _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
226 _as_name[ui->state]);
228 if (event == AE_MEM_FLUSH) {
233 if (event == AE_LOWMEM_POST) {
234 if (__appcore_low_memory_post_cb(ui) == 0)
238 if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
239 __appcore_timer_del(ui);
241 if (event == AE_TERMINATE) {
242 _DBG("[APP %d] TERMINATE", _pid);
243 ui->state = AS_DYING;
248 _ret_if(ui->ops == NULL);
252 _DBG("[APP %d] RESET", _pid);
253 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]", ui->name);
255 r = ui->ops->reset(b, ui->ops->data);
256 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]", ui->name);
259 first_launch = FALSE;
260 _INFO("[APP %d] Initial Launching, call the resume_cb", _pid);
262 r = ui->ops->resume(ui->ops->data);
264 _INFO("[APP %d] App already running, raise the window", _pid);
266 x_raise_win(getpid());
268 if (ui->state == AS_PAUSED) {
269 _INFO("[APP %d] Call the resume_cb", _pid);
271 r = ui->ops->resume(ui->ops->data);
275 ui->state = AS_RUNNING;
276 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
280 if (ui->state == AS_RUNNING) {
281 _DBG("[APP %d] PAUSE", _pid);
283 r = ui->ops->pause(ui->ops->data);
284 ui->state = AS_PAUSED;
285 if(r >= 0 && resource_reclaiming == TRUE)
286 __appcore_timer_add(ui);
288 /* TODO : rotation stop */
289 //r = appcore_pause_rotation_cb();
290 _send_to_resourced(PROC_STATUS_BACKGRD);
293 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
295 if (ui->state == AS_PAUSED || tmp_val == 1) {
296 _DBG("[APP %d] RESUME", _pid);
298 r = ui->ops->resume(ui->ops->data);
299 ui->state = AS_RUNNING;
302 /*TODO : rotation start*/
303 //r = appcore_resume_rotation_cb();
304 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
306 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
308 _send_to_resourced(PROC_STATUS_FOREGRD);
316 static struct ui_ops efl_ops = {
322 static bool __check_visible(void)
325 struct win_node *entry = NULL;
327 _DBG("[EVENT_TEST][EVENT] __check_visible\n");
329 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
331 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
332 if(entry->bfobscured == FALSE)
338 static GSList *__find_win(unsigned int win)
343 for (iter = g_winnode_list; iter; iter = g_slist_next(iter)) {
345 if (t && t->win == win)
352 static bool __add_win(unsigned int win)
357 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
362 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
366 t = calloc(1, sizeof(struct win_node));
371 t->bfobscured = FALSE;
373 g_winnode_list = g_slist_append(g_winnode_list, t);
378 static bool __delete_win(unsigned int win)
385 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
390 g_winnode_list = g_slist_delete_link(g_winnode_list, f);
397 static bool __update_win(unsigned int win, bool bfobscured)
402 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
408 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
412 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
416 t->bfobscured = bfobscured;
418 g_winnode_list = g_slist_concat(g_winnode_list, f);
426 static Ecore_X_Atom _WM_WINDOW_ROTATION_SUPPORTED = 0;
427 static Ecore_X_Atom _WM_WINDOW_ROTATION_CHANGE_REQUEST = 0;
429 static int __check_wm_rotation_support(void)
431 _DBG("Disable window manager rotation");
434 Ecore_X_Window root, win, win2;
437 if (!_WM_WINDOW_ROTATION_SUPPORTED) {
438 _WM_WINDOW_ROTATION_SUPPORTED =
439 ecore_x_atom_get("_E_WINDOW_ROTATION_SUPPORTED");
442 if (!_WM_WINDOW_ROTATION_CHANGE_REQUEST) {
443 _WM_WINDOW_ROTATION_CHANGE_REQUEST =
444 ecore_x_atom_get("_E_WINDOW_ROTATION_CHANGE_REQUEST");
447 root = ecore_x_window_root_first_get();
448 ret = ecore_x_window_prop_xid_get(root,
449 _WM_WINDOW_ROTATION_SUPPORTED,
452 if ((ret == 1) && (win))
454 ret = ecore_x_window_prop_xid_get(win,
455 _WM_WINDOW_ROTATION_SUPPORTED,
458 if ((ret == 1) && (win2 == win))
465 static void __set_wm_rotation_support(unsigned int win, unsigned int set)
468 struct win_node *entry = NULL;
471 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
474 ecore_x_window_prop_card32_set(entry->win,
475 _WM_WINDOW_ROTATION_SUPPORTED,
480 ecore_x_window_prop_card32_set(win,
481 _WM_WINDOW_ROTATION_SUPPORTED,
488 static Eina_Bool __show_cb(void *data, int type, void *event)
491 Ecore_Wl_Event_Window_Activate *ev;
495 if (ev->parent_win != 0)
497 // This is child window. Skip!!!
498 return ECORE_CALLBACK_PASS_ON;
501 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
503 if (!__find_win((unsigned int)ev->win))
504 __add_win((unsigned int)ev->win);
506 __update_win((unsigned int)ev->win, FALSE);
508 __visibility_cb(data, type, event);
510 Ecore_X_Event_Window_Show *ev;
512 Ecore_X_Window parent;
516 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
518 if (!__find_win((unsigned int)ev->win)) {
520 if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
521 __set_wm_rotation_support(ev->win, 1);
523 __add_win((unsigned int)ev->win);
526 __update_win((unsigned int)ev->win, FALSE);
529 return ECORE_CALLBACK_RENEW;
532 static Eina_Bool __hide_cb(void *data, int type, void *event)
535 Ecore_Wl_Event_Window_Deactivate *ev;
540 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
542 if (__find_win((unsigned int)ev->win)) {
543 __delete_win((unsigned int)ev->win);
545 bvisibility = __check_visible();
546 if (!bvisibility && b_active == TRUE) {
547 _DBG(" Go to Pasue state \n");
549 __do_app(AE_PAUSE, data, NULL);
553 Ecore_X_Event_Window_Hide *ev;
558 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
560 if (__find_win((unsigned int)ev->win)) {
561 __delete_win((unsigned int)ev->win);
562 bvisibility = __check_visible();
563 if (!bvisibility && b_active == TRUE) {
564 _DBG(" Go to Pasue state \n");
566 __do_app(AE_PAUSE, data, NULL);
571 return ECORE_CALLBACK_RENEW;
574 static Eina_Bool __visibility_cb(void *data, int type, void *event)
577 Ecore_Wl_Event_Window_Activate *ev;
582 __update_win((unsigned int)ev->win, ev->fobscured);
584 Ecore_X_Event_Window_Visibility_Change *ev;
589 __update_win((unsigned int)ev->win, ev->fully_obscured);
591 bvisibility = __check_visible();
593 _DBG("bvisibility %d, b_active %d", bvisibility, b_active);
595 if (bvisibility && b_active == FALSE) {
596 _DBG(" Go to Resume state\n");
598 __do_app(AE_RESUME, data, NULL);
600 } else if (!bvisibility && b_active == TRUE) {
601 _DBG(" Go to Pasue state \n");
603 __do_app(AE_PAUSE, data, NULL);
605 _DBG(" No change state \n");
607 return ECORE_CALLBACK_RENEW;
613 static Eina_Bool __cmsg_cb(void *data, int type, void *event)
615 struct ui_priv *ui = (struct ui_priv *)data;
616 Ecore_X_Event_Client_Message *e = event;
618 if (!ui) return ECORE_CALLBACK_PASS_ON;
619 if (e->format != 32) return ECORE_CALLBACK_PASS_ON;
620 if (e->message_type == _WM_WINDOW_ROTATION_CHANGE_REQUEST) {
621 if ((0 == ui->wm_rot_supported) ||
622 (0 == ui->rot_started) ||
623 (NULL == ui->rot_cb)) {
624 return ECORE_CALLBACK_PASS_ON;
628 switch (e->data.l[1])
630 case 0: rm = APPCORE_RM_PORTRAIT_NORMAL; break;
631 case 90: rm = APPCORE_RM_LANDSCAPE_REVERSE; break;
632 case 180: rm = APPCORE_RM_PORTRAIT_REVERSE; break;
633 case 270: rm = APPCORE_RM_LANDSCAPE_NORMAL; break;
634 default: rm = APPCORE_RM_UNKNOWN; break;
639 if (APPCORE_RM_UNKNOWN != rm) {
640 ui->rot_cb((void *)&rm, rm, ui->rot_cb_data);
644 return ECORE_CALLBACK_PASS_ON;
648 static void __add_climsg_cb(struct ui_priv *ui)
653 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_ACTIVATE, __show_cb, ui);
655 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_DEACTIVATE, __hide_cb, ui);
658 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, __show_cb, ui);
660 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_HIDE, __hide_cb, ui);
662 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE,
663 __visibility_cb, ui);
665 /* Add client message callback for WM_ROTATE */
666 if(!__check_wm_rotation_support())
669 ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, __cmsg_cb, ui);
670 ui->wm_rot_supported = 1;
671 appcore_set_wm_rotation(&wm_rotate);
676 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
681 if (argc == NULL || argv == NULL) {
682 _ERR("argc/argv is NULL");
688 elm_init(*argc, *argv);
690 hwacc = getenv("HWACC");
693 _DBG("elm_config_preferred_engine_set is not called");
694 } else if(strcmp(hwacc, "USE") == 0) {
696 elm_config_preferred_engine_set("wayland_egl");
697 _DBG("elm_config_preferred_engine_set : wayland_egl");
699 elm_config_preferred_engine_set("opengl_x11");
700 _DBG("elm_config_preferred_engine_set : opengl_x11");
702 } else if(strcmp(hwacc, "NOT_USE") == 0) {
704 elm_config_preferred_engine_set("wayland_shm");
705 _DBG("elm_config_preferred_engine_set : wayland_shm");
707 elm_config_preferred_engine_set("software_x11");
708 _DBG("elm_config_preferred_engine_set : software_x11");
711 _DBG("elm_config_preferred_engine_set is not called");
714 r = appcore_init(ui->name, &efl_ops, *argc, *argv);
715 _retv_if(r == -1, -1);
717 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
718 if (ui->ops && ui->ops->create) {
719 r = ui->ops->create(ui->ops->data);
721 _ERR("create() return error");
726 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
729 ui->state = AS_CREATED;
736 static void __after_loop(struct ui_priv *ui)
738 appcore_unset_rotation_cb();
741 if (ui->state == AS_RUNNING) {
742 _DBG("[APP %d] PAUSE before termination", _pid);
743 if (ui->ops && ui->ops->pause)
744 ui->ops->pause(ui->ops->data);
747 if (ui->ops && ui->ops->terminate)
748 ui->ops->terminate(ui->ops->data);
751 ecore_event_handler_del(ui->hshow);
753 ecore_event_handler_del(ui->hhide);
755 ecore_event_handler_del(ui->hvchange);
757 __appcore_timer_del(ui);
762 static int __set_data(struct ui_priv *ui, const char *name,
763 struct appcore_ops *ops)
766 _ERR("Mainloop already started");
771 if (name == NULL || name[0] == '\0') {
772 _ERR("Invalid name");
783 ui->name = strdup(name);
784 _retv_if(ui->name == NULL, -1);
788 ui->mfcb = __appcore_efl_memory_flush_cb;
793 ui->wm_rot_supported = 0;
796 ui->rot_cb_data = NULL;
797 ui->rot_mode = APPCORE_RM_UNKNOWN;
802 static void __unset_data(struct ui_priv *ui)
805 free((void *)ui->name);
807 memset(ui, 0, sizeof(struct ui_priv));
811 static int __wm_set_rotation_cb(int (*cb) (void *event_info, enum appcore_rm, void *), void *data)
818 if ((priv.wm_rot_supported) && (0 == priv.rot_started)) {
819 __set_wm_rotation_support(0, 1);
823 priv.rot_cb_data = data;
824 priv.rot_started = 1;
829 static int __wm_unset_rotation_cb(void)
831 if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
832 __set_wm_rotation_support(0, 0);
836 priv.rot_cb_data = NULL;
837 priv.rot_started = 0;
842 static int __wm_get_rotation_state(enum appcore_rm *curr)
849 *curr = priv.rot_mode;
854 static int __wm_pause_rotation_cb(void)
856 if ((1 == priv.rot_started) && (priv.wm_rot_supported)) {
857 __set_wm_rotation_support(0, 0);
860 priv.rot_started = 0;
865 static int __wm_resume_rotation_cb(void)
867 if ((0 == priv.rot_started) && (priv.wm_rot_supported)) {
868 __set_wm_rotation_support(0, 1);
871 priv.rot_started = 1;
876 static struct ui_wm_rotate wm_rotate = {
877 __wm_set_rotation_cb,
878 __wm_unset_rotation_cb,
879 __wm_get_rotation_state,
880 __wm_pause_rotation_cb,
881 __wm_resume_rotation_cb
884 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
885 struct appcore_ops *ops)
889 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
891 r = __set_data(&priv, name, ops);
892 _retv_if(r == -1, -1);
894 r = __before_loop(&priv, argc, argv);
902 aul_status_update(STATUS_DYING);
911 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable)
913 resource_reclaiming = enable;
918 EXPORT_API int appcore_set_app_state(int state)