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>
51 #include "appcore-internal.h"
52 #include "appcore-efl.h"
54 #define SYSMAN_MAXSTR 100
55 #define SYSMAN_MAXARG 16
56 #define SYSNOTI_SOCKET_PATH "/tmp/sn"
57 #define RETRY_READ_COUNT 10
59 #define PREDEF_BACKGRD "backgrd"
60 #define PREDEF_FOREGRD "foregrd"
73 char *argv[SYSMAN_MAXARG];
78 static bool resource_reclaiming = TRUE;
79 static int tmp_val = 0;
86 Ecore_Event_Handler *hshow;
87 Ecore_Event_Handler *hhide;
88 Ecore_Event_Handler *hvchange;
89 Ecore_Event_Handler *hcmsg; /* WM_ROTATE */
91 Ecore_Timer *mftimer; /* Ecore Timer for memory flushing */
93 struct appcore_ops *ops;
94 void (*mfcb) (void); /* Memory Flushing Callback */
99 int (*rot_cb) (enum appcore_rm, void *);
101 enum appcore_rm rot_mode;
104 static struct ui_priv priv;
106 static const char *_ae_name[AE_MAX] = {
107 [AE_UNKNOWN] = "UNKNOWN",
108 [AE_CREATE] = "CREATE",
109 [AE_TERMINATE] = "TERMINATE",
110 [AE_PAUSE] = "PAUSE",
111 [AE_RESUME] = "RESUME",
112 [AE_RESET] = "RESET",
113 [AE_LOWMEM_POST] = "LOWMEM_POST",
114 [AE_MEM_FLUSH] = "MEM_FLUSH",
117 static const char *_as_name[] = {
119 [AS_CREATED] = "CREATED",
120 [AS_RUNNING] = "RUNNING",
121 [AS_PAUSED] = "PAUSED",
122 [AS_DYING] = "DYING",
125 static bool b_active = 0;
131 static struct ui_wm_rotate wm_rotate;
132 static Eina_Bool __visibility_cb(void *data, int type, void *event);
134 static inline int send_int(int fd, int val)
136 return write(fd, &val, sizeof(int));
139 static inline int send_str(int fd, char *str)
145 ret = write(fd, &len, sizeof(int));
148 if (len > SYSMAN_MAXSTR)
150 write(fd, &len, sizeof(int));
151 ret = write(fd, str, len);
156 static int sysnoti_send(struct sysnoti *msg)
158 _ERR("--- %s: start", __FUNCTION__);
164 struct sockaddr_un clientaddr;
167 client_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
168 if (client_sockfd == -1) {
169 _ERR("%s: socket create failed\n", __FUNCTION__);
172 bzero(&clientaddr, sizeof(clientaddr));
173 clientaddr.sun_family = AF_UNIX;
174 strncpy(clientaddr.sun_path, SYSNOTI_SOCKET_PATH, sizeof(clientaddr.sun_path) - 1);
175 client_len = sizeof(clientaddr);
177 if (connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) <
179 _ERR("%s: connect failed\n", __FUNCTION__);
180 close(client_sockfd);
184 send_int(client_sockfd, msg->pid);
185 send_int(client_sockfd, msg->cmd);
186 send_str(client_sockfd, msg->type);
187 send_str(client_sockfd, msg->path);
188 send_int(client_sockfd, msg->argc);
189 for (i = 0; i < msg->argc; i++)
190 send_str(client_sockfd, msg->argv[i]);
192 _ERR("--- %s: read", __FUNCTION__);
193 while (retry_count < RETRY_READ_COUNT) {
194 r = read(client_sockfd, &result, sizeof(int));
196 if (errno == EINTR) {
197 _ERR("Re-read for error(EINTR)");
201 _ERR("Read fail for str length");
208 if (retry_count == RETRY_READ_COUNT) {
209 _ERR("Read retry failed");
212 close(client_sockfd);
213 _ERR("--- %s: end", __FUNCTION__);
217 static int _call_predef_action(const char *type, int num, ...)
219 _ERR("--- %s: start", __FUNCTION__);
227 if (type == NULL || num > SYSMAN_MAXARG) {
232 msg = malloc(sizeof(struct sysnoti));
235 /* Do something for not enought memory error */
240 msg->cmd = CALL_SYSMAN_ACTION;
241 msg->type = (char *)type;
245 va_start(argptr, num);
246 for (i = 0; i < num; i++) {
247 args = va_arg(argptr, char *);
252 _ERR("--- %s: send msg", __FUNCTION__);
253 ret = sysnoti_send(msg);
256 _ERR("--- %s: end", __FUNCTION__);
260 static int _inform_foregrd(void)
263 snprintf(buf, sizeof(buf), "%d", getpid());
264 return _call_predef_action(PREDEF_FOREGRD, 1, buf);
267 static int _inform_backgrd(void)
270 snprintf(buf, sizeof(buf), "%d", getpid());
271 return _call_predef_action(PREDEF_BACKGRD, 1, buf);
275 static int WIN_COMP(gconstpointer data1, gconstpointer data2)
277 struct win_node *a = (struct win_node *)data1;
278 struct win_node *b = (struct win_node *)data2;
279 return (int)((a->win)-(b->win));
282 GSList *g_winnode_list = NULL;
284 #if defined(MEMORY_FLUSH_ACTIVATE)
285 static Eina_Bool __appcore_memory_flush_cb(void *data)
287 struct ui_priv *ui = (struct ui_priv *)data;
289 appcore_flush_memory();
292 return ECORE_CALLBACK_CANCEL;
295 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
297 if (ui->state == AS_PAUSED) {
298 appcore_flush_memory();
306 static void __appcore_timer_add(struct ui_priv *ui)
308 ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
311 static void __appcore_timer_del(struct ui_priv *ui)
314 ecore_timer_del(ui->mftimer);
321 static int __appcore_low_memory_post_cb(ui_priv *ui)
326 #define __appcore_timer_add(ui) 0
327 #define __appcore_timer_del(ui) 0
331 static void __appcore_efl_memory_flush_cb(void)
333 _DBG("[APP %d] __appcore_efl_memory_flush_cb()", _pid);
334 elm_cache_all_flush();
337 static void __do_app(enum app_event event, void *data, bundle * b)
340 struct ui_priv *ui = data;
342 _DBG("[APP %d] Event: %d", _pid, event);
343 _ret_if(ui == NULL || event >= AE_MAX);
344 _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
345 _as_name[ui->state]);
347 if (event == AE_MEM_FLUSH) {
352 if (event == AE_LOWMEM_POST) {
353 if (__appcore_low_memory_post_cb(ui) == 0)
357 if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
358 __appcore_timer_del(ui);
360 if (event == AE_TERMINATE) {
361 _DBG("[APP %d] TERMINATE", _pid);
362 ui->state = AS_DYING;
367 _ret_if(ui->ops == NULL);
371 _DBG("[APP %d] RESET", _pid);
372 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]",
375 r = ui->ops->reset(b, ui->ops->data);
376 ui->state = AS_RUNNING;
377 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
381 if (ui->state == AS_RUNNING) {
382 _DBG("[APP %d] PAUSE", _pid);
384 r = ui->ops->pause(ui->ops->data);
385 ui->state = AS_PAUSED;
386 if(r >= 0 && resource_reclaiming == TRUE)
387 __appcore_timer_add(ui);
389 /* TODO : rotation stop */
390 //r = appcore_pause_rotation_cb();
395 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
397 if (ui->state == AS_PAUSED || tmp_val == 1) {
398 _DBG("[APP %d] RESUME", _pid);
400 r = ui->ops->resume(ui->ops->data);
401 ui->state = AS_RUNNING;
404 /*TODO : rotation start*/
405 //r = appcore_resume_rotation_cb();
406 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
408 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
419 static struct ui_ops efl_ops = {
425 static bool __check_visible(void)
428 struct win_node *entry = NULL;
430 _DBG("[EVENT_TEST][EVENT] __check_visible\n");
432 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
434 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
435 if(entry->bfobscured == FALSE)
441 static bool __exist_win(unsigned int win)
443 struct win_node temp;
448 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
457 static bool __add_win(unsigned int win)
462 t = calloc(1, sizeof(struct win_node));
467 t->bfobscured = FALSE;
469 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
471 f = g_slist_find_custom(g_winnode_list, t, WIN_COMP);
475 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
480 g_winnode_list = g_slist_append(g_winnode_list, t);
486 static bool __delete_win(unsigned int win)
488 struct win_node temp;
493 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
496 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
501 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
508 static bool __update_win(unsigned int win, bool bfobscured)
510 struct win_node temp;
515 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
520 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
524 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
528 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
532 t = calloc(1, sizeof(struct win_node));
537 t->bfobscured = bfobscured;
539 g_winnode_list = g_slist_append(g_winnode_list, t);
547 static Ecore_X_Atom _WM_WINDOW_ROTATION_SUPPORTED = 0;
548 static Ecore_X_Atom _WM_WINDOW_ROTATION_CHANGE_REQUEST = 0;
550 static int __check_wm_rotation_support(void)
552 _DBG("Disable window manager rotation");
555 Ecore_X_Window root, win, win2;
558 if (!_WM_WINDOW_ROTATION_SUPPORTED) {
559 _WM_WINDOW_ROTATION_SUPPORTED =
560 ecore_x_atom_get("_E_WINDOW_ROTATION_SUPPORTED");
563 if (!_WM_WINDOW_ROTATION_CHANGE_REQUEST) {
564 _WM_WINDOW_ROTATION_CHANGE_REQUEST =
565 ecore_x_atom_get("_E_WINDOW_ROTATION_CHANGE_REQUEST");
568 root = ecore_x_window_root_first_get();
569 ret = ecore_x_window_prop_xid_get(root,
570 _WM_WINDOW_ROTATION_SUPPORTED,
573 if ((ret == 1) && (win))
575 ret = ecore_x_window_prop_xid_get(win,
576 _WM_WINDOW_ROTATION_SUPPORTED,
579 if ((ret == 1) && (win2 == win))
586 static void __set_wm_rotation_support(unsigned int win, unsigned int set)
589 struct win_node *entry = NULL;
592 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
595 ecore_x_window_prop_card32_set(entry->win,
596 _WM_WINDOW_ROTATION_SUPPORTED,
601 ecore_x_window_prop_card32_set(win,
602 _WM_WINDOW_ROTATION_SUPPORTED,
607 Ecore_X_Atom atom_parent;
610 static Eina_Bool __show_cb(void *data, int type, void *event)
613 Ecore_Wl_Event_Window_Activate *ev;
617 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\n", ev->win);
625 if (!__exist_win((unsigned int)ev->win))
626 __add_win((unsigned int)ev->win);
628 __update_win((unsigned int)ev->win, FALSE);
630 __visibility_cb(data, type, event);
632 Ecore_X_Event_Window_Show *ev;
634 Ecore_X_Window parent;
638 ret = ecore_x_window_prop_window_get(ev->win, atom_parent, &parent, 1);
641 // This is child window. Skip!!!
642 return ECORE_CALLBACK_PASS_ON;
645 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
647 if (!__exist_win((unsigned int)ev->win)) {
649 if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
650 __set_wm_rotation_support(ev->win, 1);
652 __add_win((unsigned int)ev->win);
655 __update_win((unsigned int)ev->win, FALSE);
658 return ECORE_CALLBACK_RENEW;
661 static Eina_Bool __hide_cb(void *data, int type, void *event)
664 Ecore_Wl_Event_Window_Deactivate *ev;
669 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
671 if (__exist_win((unsigned int)ev->win)) {
672 __delete_win((unsigned int)ev->win);
674 bvisibility = __check_visible();
675 if (!bvisibility && b_active == 1) {
676 _DBG(" Go to Pasue state \n");
678 __do_app(AE_PAUSE, data, NULL);
682 Ecore_X_Event_Window_Hide *ev;
687 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
689 if (__exist_win((unsigned int)ev->win)) {
690 __delete_win((unsigned int)ev->win);
692 bvisibility = __check_visible();
693 if (!bvisibility && b_active == 1) {
694 _DBG(" Go to Pasue state \n");
696 __do_app(AE_PAUSE, data, NULL);
701 return ECORE_CALLBACK_RENEW;
704 static Eina_Bool __visibility_cb(void *data, int type, void *event)
707 Ecore_Wl_Event_Window_Activate *ev;
712 __update_win((unsigned int)ev->win, ev->fobscured);
714 Ecore_X_Event_Window_Visibility_Change *ev;
719 __update_win((unsigned int)ev->win, ev->fully_obscured);
721 bvisibility = __check_visible();
723 if (bvisibility && b_active == 0) {
724 _DBG(" Go to Resume state\n");
726 __do_app(AE_RESUME, data, NULL);
728 } else if (!bvisibility && b_active == 1) {
729 _DBG(" Go to Pasue state \n");
731 __do_app(AE_PAUSE, data, NULL);
733 _DBG(" No change state \n");
735 return ECORE_CALLBACK_RENEW;
741 static Eina_Bool __cmsg_cb(void *data, int type, void *event)
743 struct ui_priv *ui = (struct ui_priv *)data;
744 Ecore_X_Event_Client_Message *e = event;
746 if (!ui) return ECORE_CALLBACK_PASS_ON;
747 if (e->format != 32) return ECORE_CALLBACK_PASS_ON;
748 if (e->message_type == _WM_WINDOW_ROTATION_CHANGE_REQUEST) {
749 if ((0 == ui->wm_rot_supported) ||
750 (0 == ui->rot_started) ||
751 (NULL == ui->rot_cb)) {
752 return ECORE_CALLBACK_PASS_ON;
756 switch (e->data.l[1])
758 case 0: rm = APPCORE_RM_PORTRAIT_NORMAL; break;
759 case 90: rm = APPCORE_RM_LANDSCAPE_REVERSE; break;
760 case 180: rm = APPCORE_RM_PORTRAIT_REVERSE; break;
761 case 270: rm = APPCORE_RM_LANDSCAPE_NORMAL; break;
762 default: rm = APPCORE_RM_UNKNOWN; break;
767 if (APPCORE_RM_UNKNOWN != rm) {
768 ui->rot_cb(rm, ui->rot_cb_data);
772 return ECORE_CALLBACK_PASS_ON;
776 static void __add_climsg_cb(struct ui_priv *ui)
781 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_ACTIVATE, __show_cb, ui);
783 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_DEACTIVATE, __hide_cb, ui);
785 atom_parent = ecore_x_atom_get("_E_PARENT_BORDER_WINDOW");
792 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, __show_cb, ui);
794 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_HIDE, __hide_cb, ui);
796 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE,
797 __visibility_cb, ui);
799 /* Add client message callback for WM_ROTATE */
800 if(!__check_wm_rotation_support())
803 ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, __cmsg_cb, ui);
804 ui->wm_rot_supported = 1;
805 appcore_set_wm_rotation(&wm_rotate);
810 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
815 if (argc == NULL || argv == NULL) {
816 _ERR("argc/argv is NULL");
822 elm_init(*argc, *argv);
824 hwacc = getenv("HWACC");
827 _DBG("elm_config_preferred_engine_set is not called");
828 } else if(strcmp(hwacc, "USE") == 0) {
830 elm_config_preferred_engine_set("wayland_egl");
831 _DBG("elm_config_preferred_engine_set : wayland_egl");
833 elm_config_preferred_engine_set("opengl_x11");
834 _DBG("elm_config_preferred_engine_set : opengl_x11");
836 } else if(strcmp(hwacc, "NOT_USE") == 0) {
838 elm_config_preferred_engine_set("wayland_shm");
839 _DBG("elm_config_preferred_engine_set : wayland_shm");
841 elm_config_preferred_engine_set("software_x11");
842 _DBG("elm_config_preferred_engine_set : software_x11");
845 _DBG("elm_config_preferred_engine_set is not called");
848 r = appcore_init(ui->name, &efl_ops, *argc, *argv);
849 _retv_if(r == -1, -1);
851 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
852 if (ui->ops && ui->ops->create) {
853 r = ui->ops->create(ui->ops->data);
855 _ERR("create() return error");
860 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
863 ui->state = AS_CREATED;
870 static void __after_loop(struct ui_priv *ui)
872 appcore_unset_rotation_cb();
875 if (ui->ops && ui->ops->terminate)
876 ui->ops->terminate(ui->ops->data);
879 ecore_event_handler_del(ui->hshow);
881 ecore_event_handler_del(ui->hhide);
883 ecore_event_handler_del(ui->hvchange);
885 __appcore_timer_del(ui);
890 static int __set_data(struct ui_priv *ui, const char *name,
891 struct appcore_ops *ops)
894 _ERR("Mainloop already started");
899 if (name == NULL || name[0] == '\0') {
900 _ERR("Invalid name");
911 ui->name = strdup(name);
912 _retv_if(ui->name == NULL, -1);
916 ui->mfcb = __appcore_efl_memory_flush_cb;
921 ui->wm_rot_supported = 0;
924 ui->rot_cb_data = NULL;
925 ui->rot_mode = APPCORE_RM_UNKNOWN;
930 static void __unset_data(struct ui_priv *ui)
933 free((void *)ui->name);
935 memset(ui, 0, sizeof(struct ui_priv));
939 static int __wm_set_rotation_cb(int (*cb) (enum appcore_rm, void *), void *data)
946 if ((priv.wm_rot_supported) && (0 == priv.rot_started)) {
947 __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);
964 priv.rot_cb_data = NULL;
965 priv.rot_started = 0;
970 static int __wm_get_rotation_state(enum appcore_rm *curr)
977 *curr = priv.rot_mode;
982 static int __wm_pause_rotation_cb(void)
984 if ((1 == priv.rot_started) && (priv.wm_rot_supported)) {
985 __set_wm_rotation_support(0, 0);
988 priv.rot_started = 0;
993 static int __wm_resume_rotation_cb(void)
995 if ((0 == priv.rot_started) && (priv.wm_rot_supported)) {
996 __set_wm_rotation_support(0, 1);
999 priv.rot_started = 1;
1004 static struct ui_wm_rotate wm_rotate = {
1005 __wm_set_rotation_cb,
1006 __wm_unset_rotation_cb,
1007 __wm_get_rotation_state,
1008 __wm_pause_rotation_cb,
1009 __wm_resume_rotation_cb
1012 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
1013 struct appcore_ops *ops)
1017 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
1019 r = __set_data(&priv, name, ops);
1020 _retv_if(r == -1, -1);
1022 r = __before_loop(&priv, argc, argv);
1024 __unset_data(&priv);
1030 aul_status_update(STATUS_DYING);
1032 __after_loop(&priv);
1034 __unset_data(&priv);
1039 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable)
1041 resource_reclaiming = enable;
1046 EXPORT_API int appcore_set_app_state(int state)