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>
34 #include <Ecore_Wayland.h>
36 #include <X11/Xatom.h>
42 #include <Ecore_Evas.h>
43 #include <Ecore_Input_Evas.h>
44 #include <Elementary.h>
45 #include <glib-object.h>
50 #include "appcore-internal.h"
51 #include "appcore-efl.h"
53 #define SYSMAN_MAXSTR 100
54 #define SYSMAN_MAXARG 16
55 #define SYSNOTI_SOCKET_PATH "/tmp/sn"
56 #define RETRY_READ_COUNT 10
58 #define PREDEF_BACKGRD "backgrd"
59 #define PREDEF_FOREGRD "foregrd"
72 char *argv[SYSMAN_MAXARG];
77 static bool resource_reclaiming = TRUE;
78 static int tmp_val = 0;
85 Ecore_Event_Handler *hshow;
86 Ecore_Event_Handler *hhide;
87 Ecore_Event_Handler *hvchange;
88 Ecore_Event_Handler *hcmsg; /* WM_ROTATE */
90 Ecore_Timer *mftimer; /* Ecore Timer for memory flushing */
92 struct appcore_ops *ops;
93 void (*mfcb) (void); /* Memory Flushing Callback */
98 int (*rot_cb) (enum appcore_rm, void *);
100 enum appcore_rm rot_mode;
103 static struct ui_priv priv;
105 static const char *_ae_name[AE_MAX] = {
106 [AE_UNKNOWN] = "UNKNOWN",
107 [AE_CREATE] = "CREATE",
108 [AE_TERMINATE] = "TERMINATE",
109 [AE_PAUSE] = "PAUSE",
110 [AE_RESUME] = "RESUME",
111 [AE_RESET] = "RESET",
112 [AE_LOWMEM_POST] = "LOWMEM_POST",
113 [AE_MEM_FLUSH] = "MEM_FLUSH",
116 static const char *_as_name[] = {
118 [AS_CREATED] = "CREATED",
119 [AS_RUNNING] = "RUNNING",
120 [AS_PAUSED] = "PAUSED",
121 [AS_DYING] = "DYING",
124 static bool b_active = 0;
130 static struct ui_wm_rotate wm_rotate;
131 static Eina_Bool __visibility_cb(void *data, int type, void *event);
133 static inline int send_int(int fd, int val)
135 return write(fd, &val, sizeof(int));
138 static inline int send_str(int fd, char *str)
144 ret = write(fd, &len, sizeof(int));
147 if (len > SYSMAN_MAXSTR)
149 write(fd, &len, sizeof(int));
150 ret = write(fd, str, len);
155 static int sysnoti_send(struct sysnoti *msg)
157 _ERR("--- %s: start", __FUNCTION__);
163 struct sockaddr_un clientaddr;
166 client_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
167 if (client_sockfd == -1) {
168 _ERR("%s: socket create failed\n", __FUNCTION__);
171 bzero(&clientaddr, sizeof(clientaddr));
172 clientaddr.sun_family = AF_UNIX;
173 strncpy(clientaddr.sun_path, SYSNOTI_SOCKET_PATH, sizeof(clientaddr.sun_path) - 1);
174 client_len = sizeof(clientaddr);
176 if (connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) <
178 _ERR("%s: connect failed\n", __FUNCTION__);
179 close(client_sockfd);
183 send_int(client_sockfd, msg->pid);
184 send_int(client_sockfd, msg->cmd);
185 send_str(client_sockfd, msg->type);
186 send_str(client_sockfd, msg->path);
187 send_int(client_sockfd, msg->argc);
188 for (i = 0; i < msg->argc; i++)
189 send_str(client_sockfd, msg->argv[i]);
191 _ERR("--- %s: read", __FUNCTION__);
192 while (retry_count < RETRY_READ_COUNT) {
193 r = read(client_sockfd, &result, sizeof(int));
195 if (errno == EINTR) {
196 _ERR("Re-read for error(EINTR)");
200 _ERR("Read fail for str length");
207 if (retry_count == RETRY_READ_COUNT) {
208 _ERR("Read retry failed");
211 close(client_sockfd);
212 _ERR("--- %s: end", __FUNCTION__);
216 static int _call_predef_action(const char *type, int num, ...)
218 _ERR("--- %s: start", __FUNCTION__);
226 if (type == NULL || num > SYSMAN_MAXARG) {
231 msg = malloc(sizeof(struct sysnoti));
234 /* Do something for not enought memory error */
239 msg->cmd = CALL_SYSMAN_ACTION;
240 msg->type = (char *)type;
244 va_start(argptr, num);
245 for (i = 0; i < num; i++) {
246 args = va_arg(argptr, char *);
251 _ERR("--- %s: send msg", __FUNCTION__);
252 ret = sysnoti_send(msg);
255 _ERR("--- %s: end", __FUNCTION__);
259 static int _inform_foregrd(void)
262 snprintf(buf, sizeof(buf), "%d", getpid());
263 return _call_predef_action(PREDEF_FOREGRD, 1, buf);
266 static int _inform_backgrd(void)
269 snprintf(buf, sizeof(buf), "%d", getpid());
270 return _call_predef_action(PREDEF_BACKGRD, 1, buf);
274 static int WIN_COMP(gconstpointer data1, gconstpointer data2)
276 struct win_node *a = (struct win_node *)data1;
277 struct win_node *b = (struct win_node *)data2;
278 return (int)((a->win)-(b->win));
281 GSList *g_winnode_list = NULL;
283 #if defined(MEMORY_FLUSH_ACTIVATE)
284 static Eina_Bool __appcore_memory_flush_cb(void *data)
286 struct ui_priv *ui = (struct ui_priv *)data;
288 appcore_flush_memory();
291 return ECORE_CALLBACK_CANCEL;
294 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
296 if (ui->state == AS_PAUSED) {
297 appcore_flush_memory();
305 static void __appcore_timer_add(struct ui_priv *ui)
307 ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
310 static void __appcore_timer_del(struct ui_priv *ui)
313 ecore_timer_del(ui->mftimer);
320 static int __appcore_low_memory_post_cb(ui_priv *ui)
325 #define __appcore_timer_add(ui) 0
326 #define __appcore_timer_del(ui) 0
330 static void __appcore_efl_memory_flush_cb(void)
332 _DBG("[APP %d] __appcore_efl_memory_flush_cb()", _pid);
333 elm_cache_all_flush();
336 static void __do_app(enum app_event event, void *data, bundle * b)
339 struct ui_priv *ui = data;
341 _DBG("[APP %d] Event: %d", _pid, event);
342 _ret_if(ui == NULL || event >= AE_MAX);
343 _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
344 _as_name[ui->state]);
346 if (event == AE_MEM_FLUSH) {
351 if (event == AE_LOWMEM_POST) {
352 if (__appcore_low_memory_post_cb(ui) == 0)
356 if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
357 __appcore_timer_del(ui);
359 if (event == AE_TERMINATE) {
360 _DBG("[APP %d] TERMINATE", _pid);
361 ui->state = AS_DYING;
366 _ret_if(ui->ops == NULL);
370 _DBG("[APP %d] RESET", _pid);
371 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]",
374 r = ui->ops->reset(b, ui->ops->data);
375 ui->state = AS_RUNNING;
376 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
380 if (ui->state == AS_RUNNING) {
381 _DBG("[APP %d] PAUSE", _pid);
383 r = ui->ops->pause(ui->ops->data);
384 ui->state = AS_PAUSED;
385 if(r >= 0 && resource_reclaiming == TRUE)
386 __appcore_timer_add(ui);
388 /* TODO : rotation stop */
389 //r = appcore_pause_rotation_cb();
394 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
396 if (ui->state == AS_PAUSED || tmp_val == 1) {
397 _DBG("[APP %d] RESUME", _pid);
399 r = ui->ops->resume(ui->ops->data);
400 ui->state = AS_RUNNING;
403 /*TODO : rotation start*/
404 //r = appcore_resume_rotation_cb();
405 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
407 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
418 static struct ui_ops efl_ops = {
424 static bool __check_visible(void)
427 struct win_node *entry = NULL;
429 _DBG("[EVENT_TEST][EVENT] __check_visible\n");
431 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
433 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
434 if(entry->bfobscured == FALSE)
440 static bool __exist_win(unsigned int win)
442 struct win_node temp;
447 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
456 static bool __add_win(unsigned int win)
461 t = calloc(1, sizeof(struct win_node));
466 t->bfobscured = FALSE;
468 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
470 f = g_slist_find_custom(g_winnode_list, t, WIN_COMP);
474 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
479 g_winnode_list = g_slist_append(g_winnode_list, t);
485 static bool __delete_win(unsigned int win)
487 struct win_node temp;
492 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
495 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
500 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
507 static bool __update_win(unsigned int win, bool bfobscured)
509 struct win_node temp;
514 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
519 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
523 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
527 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
531 t = calloc(1, sizeof(struct win_node));
536 t->bfobscured = bfobscured;
538 g_winnode_list = g_slist_append(g_winnode_list, t);
546 static Ecore_X_Atom _WM_WINDOW_ROTATION_SUPPORTED = 0;
547 static Ecore_X_Atom _WM_WINDOW_ROTATION_CHANGE_REQUEST = 0;
549 static int __check_wm_rotation_support(void)
551 _DBG("Disable window manager rotation");
554 Ecore_X_Window root, win, win2;
557 if (!_WM_WINDOW_ROTATION_SUPPORTED) {
558 _WM_WINDOW_ROTATION_SUPPORTED =
559 ecore_x_atom_get("_E_WINDOW_ROTATION_SUPPORTED");
562 if (!_WM_WINDOW_ROTATION_CHANGE_REQUEST) {
563 _WM_WINDOW_ROTATION_CHANGE_REQUEST =
564 ecore_x_atom_get("_E_WINDOW_ROTATION_CHANGE_REQUEST");
567 root = ecore_x_window_root_first_get();
568 ret = ecore_x_window_prop_xid_get(root,
569 _WM_WINDOW_ROTATION_SUPPORTED,
572 if ((ret == 1) && (win))
574 ret = ecore_x_window_prop_xid_get(win,
575 _WM_WINDOW_ROTATION_SUPPORTED,
578 if ((ret == 1) && (win2 == win))
585 static void __set_wm_rotation_support(unsigned int win, unsigned int set)
588 struct win_node *entry = NULL;
591 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
594 ecore_x_window_prop_card32_set(entry->win,
595 _WM_WINDOW_ROTATION_SUPPORTED,
600 ecore_x_window_prop_card32_set(win,
601 _WM_WINDOW_ROTATION_SUPPORTED,
606 Ecore_X_Atom atom_parent;
609 static Eina_Bool __show_cb(void *data, int type, void *event)
612 Ecore_Wl_Event_Window_Activate *ev;
616 if (ev->parent_win != 0)
618 // This is child window. Skip!!!
619 return ECORE_CALLBACK_PASS_ON;
622 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
624 if (!__exist_win((unsigned int)ev->win))
625 __add_win((unsigned int)ev->win);
627 __update_win((unsigned int)ev->win, FALSE);
629 __visibility_cb(data, type, event);
631 Ecore_X_Event_Window_Show *ev;
633 Ecore_X_Window parent;
637 ret = ecore_x_window_prop_window_get(ev->win, atom_parent, &parent, 1);
640 // This is child window. Skip!!!
641 return ECORE_CALLBACK_PASS_ON;
644 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
646 if (!__exist_win((unsigned int)ev->win)) {
648 if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
649 __set_wm_rotation_support(ev->win, 1);
651 __add_win((unsigned int)ev->win);
654 __update_win((unsigned int)ev->win, FALSE);
657 return ECORE_CALLBACK_RENEW;
660 static Eina_Bool __hide_cb(void *data, int type, void *event)
663 Ecore_Wl_Event_Window_Deactivate *ev;
668 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
670 if (__exist_win((unsigned int)ev->win)) {
671 __delete_win((unsigned int)ev->win);
673 bvisibility = __check_visible();
674 if (!bvisibility && b_active == 1) {
675 _DBG(" Go to Pasue state \n");
677 __do_app(AE_PAUSE, data, NULL);
681 Ecore_X_Event_Window_Hide *ev;
686 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
688 if (__exist_win((unsigned int)ev->win)) {
689 __delete_win((unsigned int)ev->win);
691 bvisibility = __check_visible();
692 if (!bvisibility && b_active == 1) {
693 _DBG(" Go to Pasue state \n");
695 __do_app(AE_PAUSE, data, NULL);
700 return ECORE_CALLBACK_RENEW;
703 static Eina_Bool __visibility_cb(void *data, int type, void *event)
706 Ecore_Wl_Event_Window_Activate *ev;
711 __update_win((unsigned int)ev->win, ev->fobscured);
713 Ecore_X_Event_Window_Visibility_Change *ev;
718 __update_win((unsigned int)ev->win, ev->fully_obscured);
720 bvisibility = __check_visible();
722 if (bvisibility && b_active == 0) {
723 _DBG(" Go to Resume state\n");
725 __do_app(AE_RESUME, data, NULL);
727 } else if (!bvisibility && b_active == 1) {
728 _DBG(" Go to Pasue state \n");
730 __do_app(AE_PAUSE, data, NULL);
732 _DBG(" No change state \n");
734 return ECORE_CALLBACK_RENEW;
740 static Eina_Bool __cmsg_cb(void *data, int type, void *event)
742 struct ui_priv *ui = (struct ui_priv *)data;
743 Ecore_X_Event_Client_Message *e = event;
745 if (!ui) return ECORE_CALLBACK_PASS_ON;
746 if (e->format != 32) return ECORE_CALLBACK_PASS_ON;
747 if (e->message_type == _WM_WINDOW_ROTATION_CHANGE_REQUEST) {
748 if ((0 == ui->wm_rot_supported) ||
749 (0 == ui->rot_started) ||
750 (NULL == ui->rot_cb)) {
751 return ECORE_CALLBACK_PASS_ON;
755 switch (e->data.l[1])
757 case 0: rm = APPCORE_RM_PORTRAIT_NORMAL; break;
758 case 90: rm = APPCORE_RM_LANDSCAPE_REVERSE; break;
759 case 180: rm = APPCORE_RM_PORTRAIT_REVERSE; break;
760 case 270: rm = APPCORE_RM_LANDSCAPE_NORMAL; break;
761 default: rm = APPCORE_RM_UNKNOWN; break;
766 if (APPCORE_RM_UNKNOWN != rm) {
767 ui->rot_cb(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_ACTIVATE, __show_cb, ui);
782 ecore_event_handler_add(ECORE_WL_EVENT_WINDOW_DEACTIVATE, __hide_cb, ui);
784 atom_parent = ecore_x_atom_get("_E_PARENT_BORDER_WINDOW");
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())
802 ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, __cmsg_cb, ui);
803 ui->wm_rot_supported = 1;
804 appcore_set_wm_rotation(&wm_rotate);
809 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
814 if (argc == NULL || argv == NULL) {
815 _ERR("argc/argv is NULL");
821 elm_init(*argc, *argv);
823 hwacc = getenv("HWACC");
826 _DBG("elm_config_preferred_engine_set is not called");
827 } else if(strcmp(hwacc, "USE") == 0) {
829 elm_config_preferred_engine_set("wayland_egl");
830 _DBG("elm_config_preferred_engine_set : wayland_egl");
832 elm_config_preferred_engine_set("opengl_x11");
833 _DBG("elm_config_preferred_engine_set : opengl_x11");
835 } else if(strcmp(hwacc, "NOT_USE") == 0) {
837 elm_config_preferred_engine_set("wayland_shm");
838 _DBG("elm_config_preferred_engine_set : wayland_shm");
840 elm_config_preferred_engine_set("software_x11");
841 _DBG("elm_config_preferred_engine_set : software_x11");
844 _DBG("elm_config_preferred_engine_set is not called");
847 r = appcore_init(ui->name, &efl_ops, *argc, *argv);
848 _retv_if(r == -1, -1);
850 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
851 if (ui->ops && ui->ops->create) {
852 r = ui->ops->create(ui->ops->data);
854 _ERR("create() return error");
859 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
862 ui->state = AS_CREATED;
869 static void __after_loop(struct ui_priv *ui)
871 appcore_unset_rotation_cb();
874 if (ui->ops && ui->ops->terminate)
875 ui->ops->terminate(ui->ops->data);
878 ecore_event_handler_del(ui->hshow);
880 ecore_event_handler_del(ui->hhide);
882 ecore_event_handler_del(ui->hvchange);
884 __appcore_timer_del(ui);
889 static int __set_data(struct ui_priv *ui, const char *name,
890 struct appcore_ops *ops)
893 _ERR("Mainloop already started");
898 if (name == NULL || name[0] == '\0') {
899 _ERR("Invalid name");
910 ui->name = strdup(name);
911 _retv_if(ui->name == NULL, -1);
915 ui->mfcb = __appcore_efl_memory_flush_cb;
920 ui->wm_rot_supported = 0;
923 ui->rot_cb_data = NULL;
924 ui->rot_mode = APPCORE_RM_UNKNOWN;
929 static void __unset_data(struct ui_priv *ui)
932 free((void *)ui->name);
934 memset(ui, 0, sizeof(struct ui_priv));
938 static int __wm_set_rotation_cb(int (*cb) (enum appcore_rm, void *), void *data)
945 if ((priv.wm_rot_supported) && (0 == priv.rot_started)) {
946 __set_wm_rotation_support(0, 1);
950 priv.rot_cb_data = data;
951 priv.rot_started = 1;
956 static int __wm_unset_rotation_cb(void)
958 if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
959 __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 ((1 == priv.rot_started) && (priv.wm_rot_supported)) {
984 __set_wm_rotation_support(0, 0);
987 priv.rot_started = 0;
992 static int __wm_resume_rotation_cb(void)
994 if ((0 == priv.rot_started) && (priv.wm_rot_supported)) {
995 __set_wm_rotation_support(0, 1);
998 priv.rot_started = 1;
1003 static struct ui_wm_rotate wm_rotate = {
1004 __wm_set_rotation_cb,
1005 __wm_unset_rotation_cb,
1006 __wm_get_rotation_state,
1007 __wm_pause_rotation_cb,
1008 __wm_resume_rotation_cb
1011 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
1012 struct appcore_ops *ops)
1016 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
1018 r = __set_data(&priv, name, ops);
1019 _retv_if(r == -1, -1);
1021 r = __before_loop(&priv, argc, argv);
1023 __unset_data(&priv);
1029 aul_status_update(STATUS_DYING);
1031 __after_loop(&priv);
1033 __unset_data(&priv);
1038 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable)
1040 resource_reclaiming = enable;
1045 EXPORT_API int appcore_set_app_state(int state)