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 <X11/Xatom.h>
39 #include <Ecore_Evas.h>
40 #include <Ecore_Input_Evas.h>
41 #include <Elementary.h>
42 #include <glib-object.h>
47 #include "appcore-internal.h"
48 #include "appcore-efl.h"
50 #define SYSMAN_MAXSTR 100
51 #define SYSMAN_MAXARG 16
52 #define SYSNOTI_SOCKET_PATH "/tmp/sn"
53 #define RETRY_READ_COUNT 10
55 #define PREDEF_BACKGRD "backgrd"
56 #define PREDEF_FOREGRD "foregrd"
69 char *argv[SYSMAN_MAXARG];
74 static bool resource_reclaiming = TRUE;
75 static int tmp_val = 0;
82 Ecore_Event_Handler *hshow;
83 Ecore_Event_Handler *hhide;
84 Ecore_Event_Handler *hvchange;
85 Ecore_Event_Handler *hcmsg; /* WM_ROTATE */
87 Ecore_Timer *mftimer; /* Ecore Timer for memory flushing */
89 struct appcore_ops *ops;
90 void (*mfcb) (void); /* Memory Flushing Callback */
95 int (*rot_cb) (enum appcore_rm, void *);
97 enum appcore_rm rot_mode;
100 static struct ui_priv priv;
102 static const char *_ae_name[AE_MAX] = {
103 [AE_UNKNOWN] = "UNKNOWN",
104 [AE_CREATE] = "CREATE",
105 [AE_TERMINATE] = "TERMINATE",
106 [AE_PAUSE] = "PAUSE",
107 [AE_RESUME] = "RESUME",
108 [AE_RESET] = "RESET",
109 [AE_LOWMEM_POST] = "LOWMEM_POST",
110 [AE_MEM_FLUSH] = "MEM_FLUSH",
113 static const char *_as_name[] = {
115 [AS_CREATED] = "CREATED",
116 [AS_RUNNING] = "RUNNING",
117 [AS_PAUSED] = "PAUSED",
118 [AS_DYING] = "DYING",
121 static bool b_active = 0;
127 static struct ui_wm_rotate wm_rotate;
129 static inline int send_int(int fd, int val)
131 return write(fd, &val, sizeof(int));
134 static inline int send_str(int fd, char *str)
140 ret = write(fd, &len, sizeof(int));
143 if (len > SYSMAN_MAXSTR)
145 write(fd, &len, sizeof(int));
146 ret = write(fd, str, len);
151 static int sysnoti_send(struct sysnoti *msg)
153 _ERR("--- %s: start", __FUNCTION__);
159 struct sockaddr_un clientaddr;
162 client_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
163 if (client_sockfd == -1) {
164 _ERR("%s: socket create failed\n", __FUNCTION__);
167 bzero(&clientaddr, sizeof(clientaddr));
168 clientaddr.sun_family = AF_UNIX;
169 strncpy(clientaddr.sun_path, SYSNOTI_SOCKET_PATH, sizeof(clientaddr.sun_path) - 1);
170 client_len = sizeof(clientaddr);
172 if (connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) <
174 _ERR("%s: connect failed\n", __FUNCTION__);
175 close(client_sockfd);
179 send_int(client_sockfd, msg->pid);
180 send_int(client_sockfd, msg->cmd);
181 send_str(client_sockfd, msg->type);
182 send_str(client_sockfd, msg->path);
183 send_int(client_sockfd, msg->argc);
184 for (i = 0; i < msg->argc; i++)
185 send_str(client_sockfd, msg->argv[i]);
187 _ERR("--- %s: read", __FUNCTION__);
188 while (retry_count < RETRY_READ_COUNT) {
189 r = read(client_sockfd, &result, sizeof(int));
191 if (errno == EINTR) {
192 _ERR("Re-read for error(EINTR)");
196 _ERR("Read fail for str length");
203 if (retry_count == RETRY_READ_COUNT) {
204 _ERR("Read retry failed");
207 close(client_sockfd);
208 _ERR("--- %s: end", __FUNCTION__);
212 static int _call_predef_action(const char *type, int num, ...)
214 _ERR("--- %s: start", __FUNCTION__);
222 if (type == NULL || num > SYSMAN_MAXARG) {
227 msg = malloc(sizeof(struct sysnoti));
230 /* Do something for not enought memory error */
235 msg->cmd = CALL_SYSMAN_ACTION;
236 msg->type = (char *)type;
240 va_start(argptr, num);
241 for (i = 0; i < num; i++) {
242 args = va_arg(argptr, char *);
247 _ERR("--- %s: send msg", __FUNCTION__);
248 ret = sysnoti_send(msg);
251 _ERR("--- %s: end", __FUNCTION__);
255 static int _inform_foregrd(void)
258 snprintf(buf, sizeof(buf), "%d", getpid());
259 return _call_predef_action(PREDEF_FOREGRD, 1, buf);
262 static int _inform_backgrd(void)
265 snprintf(buf, sizeof(buf), "%d", getpid());
266 return _call_predef_action(PREDEF_BACKGRD, 1, buf);
270 static int WIN_COMP(gconstpointer data1, gconstpointer data2)
272 struct win_node *a = (struct win_node *)data1;
273 struct win_node *b = (struct win_node *)data2;
274 return (int)((a->win)-(b->win));
277 GSList *g_winnode_list = NULL;
279 #if defined(MEMORY_FLUSH_ACTIVATE)
280 static Eina_Bool __appcore_memory_flush_cb(void *data)
282 struct ui_priv *ui = (struct ui_priv *)data;
284 appcore_flush_memory();
287 return ECORE_CALLBACK_CANCEL;
290 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
292 if (ui->state == AS_PAUSED) {
293 appcore_flush_memory();
301 static void __appcore_timer_add(struct ui_priv *ui)
303 ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
306 static void __appcore_timer_del(struct ui_priv *ui)
309 ecore_timer_del(ui->mftimer);
316 static int __appcore_low_memory_post_cb(ui_priv *ui)
321 #define __appcore_timer_add(ui) 0
322 #define __appcore_timer_del(ui) 0
326 static void __appcore_efl_memory_flush_cb(void)
328 _DBG("[APP %d] __appcore_efl_memory_flush_cb()", _pid);
329 elm_cache_all_flush();
332 static void __do_app(enum app_event event, void *data, bundle * b)
335 struct ui_priv *ui = data;
337 _DBG("[APP %d] Event: %d", _pid, event);
338 _ret_if(ui == NULL || event >= AE_MAX);
339 _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
340 _as_name[ui->state]);
342 if (event == AE_MEM_FLUSH) {
347 if (event == AE_LOWMEM_POST) {
348 if (__appcore_low_memory_post_cb(ui) == 0)
352 if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
353 __appcore_timer_del(ui);
355 if (event == AE_TERMINATE) {
356 _DBG("[APP %d] TERMINATE", _pid);
357 ui->state = AS_DYING;
362 _ret_if(ui->ops == NULL);
366 _DBG("[APP %d] RESET", _pid);
367 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]",
370 r = ui->ops->reset(b, ui->ops->data);
371 ui->state = AS_RUNNING;
372 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
376 if (ui->state == AS_RUNNING) {
377 _DBG("[APP %d] PAUSE", _pid);
379 r = ui->ops->pause(ui->ops->data);
380 ui->state = AS_PAUSED;
381 if(r >= 0 && resource_reclaiming == TRUE)
382 __appcore_timer_add(ui);
384 /* TODO : rotation stop */
385 //r = appcore_pause_rotation_cb();
390 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
392 if (ui->state == AS_PAUSED || tmp_val == 1) {
393 _DBG("[APP %d] RESUME", _pid);
395 r = ui->ops->resume(ui->ops->data);
396 ui->state = AS_RUNNING;
399 /*TODO : rotation start*/
400 //r = appcore_resume_rotation_cb();
401 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
403 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
414 static struct ui_ops efl_ops = {
420 static bool __check_visible(void)
423 struct win_node *entry = NULL;
425 _DBG("[EVENT_TEST][EVENT] __check_visible\n");
427 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
429 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
430 if(entry->bfobscured == FALSE)
436 static bool __exist_win(unsigned int win)
438 struct win_node temp;
443 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
452 static bool __add_win(unsigned int win)
457 t = calloc(1, sizeof(struct win_node));
462 t->bfobscured = FALSE;
464 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
466 f = g_slist_find_custom(g_winnode_list, t, WIN_COMP);
470 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
475 g_winnode_list = g_slist_append(g_winnode_list, t);
481 static bool __delete_win(unsigned int win)
483 struct win_node temp;
488 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
491 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
496 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
503 static bool __update_win(unsigned int win, bool bfobscured)
505 struct win_node temp;
510 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
515 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
519 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
523 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
527 t = calloc(1, sizeof(struct win_node));
532 t->bfobscured = bfobscured;
534 g_winnode_list = g_slist_append(g_winnode_list, t);
542 static Ecore_X_Atom _WM_WINDOW_ROTATION_SUPPORTED = 0;
543 static Ecore_X_Atom _WM_WINDOW_ROTATION_CHANGE_REQUEST = 0;
545 static int __check_wm_rotation_support(void)
547 _DBG("Disable window manager rotation");
550 Ecore_X_Window root, win, win2;
553 if (!_WM_WINDOW_ROTATION_SUPPORTED) {
554 _WM_WINDOW_ROTATION_SUPPORTED =
555 ecore_x_atom_get("_E_WINDOW_ROTATION_SUPPORTED");
558 if (!_WM_WINDOW_ROTATION_CHANGE_REQUEST) {
559 _WM_WINDOW_ROTATION_CHANGE_REQUEST =
560 ecore_x_atom_get("_E_WINDOW_ROTATION_CHANGE_REQUEST");
563 root = ecore_x_window_root_first_get();
564 ret = ecore_x_window_prop_xid_get(root,
565 _WM_WINDOW_ROTATION_SUPPORTED,
568 if ((ret == 1) && (win))
570 ret = ecore_x_window_prop_xid_get(win,
571 _WM_WINDOW_ROTATION_SUPPORTED,
574 if ((ret == 1) && (win2 == win))
581 static void __set_wm_rotation_support(unsigned int win, unsigned int set)
584 struct win_node *entry = NULL;
587 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
590 ecore_x_window_prop_card32_set(entry->win,
591 _WM_WINDOW_ROTATION_SUPPORTED,
596 ecore_x_window_prop_card32_set(win,
597 _WM_WINDOW_ROTATION_SUPPORTED,
602 Ecore_X_Atom atom_parent;
605 static Eina_Bool __show_cb(void *data, int type, void *event)
608 Ecore_X_Event_Window_Show *ev;
610 Ecore_X_Window parent;
614 ret = ecore_x_window_prop_window_get(ev->win, atom_parent, &parent, 1);
617 // This is child window. Skip!!!
618 return ECORE_CALLBACK_PASS_ON;
621 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
623 if (!__exist_win((unsigned int)ev->win)) {
625 if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
626 __set_wm_rotation_support(ev->win, 1);
628 __add_win((unsigned int)ev->win);
631 __update_win((unsigned int)ev->win, FALSE);
634 return ECORE_CALLBACK_RENEW;
637 static Eina_Bool __hide_cb(void *data, int type, void *event)
640 Ecore_X_Event_Window_Hide *ev;
645 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
647 if (__exist_win((unsigned int)ev->win)) {
648 __delete_win((unsigned int)ev->win);
650 bvisibility = __check_visible();
651 if (!bvisibility && b_active == 1) {
652 _DBG(" Go to Pasue state \n");
654 __do_app(AE_PAUSE, data, NULL);
659 return ECORE_CALLBACK_RENEW;
662 static Eina_Bool __visibility_cb(void *data, int type, void *event)
665 Ecore_X_Event_Window_Visibility_Change *ev;
670 __update_win((unsigned int)ev->win, ev->fully_obscured);
671 bvisibility = __check_visible();
673 if (bvisibility && b_active == 0) {
674 _DBG(" Go to Resume state\n");
676 __do_app(AE_RESUME, data, NULL);
678 } else if (!bvisibility && b_active == 1) {
679 _DBG(" Go to Pasue state \n");
681 __do_app(AE_PAUSE, data, NULL);
683 _DBG(" No change state \n");
686 return ECORE_CALLBACK_RENEW;
692 static Eina_Bool __cmsg_cb(void *data, int type, void *event)
694 struct ui_priv *ui = (struct ui_priv *)data;
695 Ecore_X_Event_Client_Message *e = event;
697 if (!ui) return ECORE_CALLBACK_PASS_ON;
698 if (e->format != 32) return ECORE_CALLBACK_PASS_ON;
699 if (e->message_type == _WM_WINDOW_ROTATION_CHANGE_REQUEST) {
700 if ((0 == ui->wm_rot_supported) ||
701 (0 == ui->rot_started) ||
702 (NULL == ui->rot_cb)) {
703 return ECORE_CALLBACK_PASS_ON;
707 switch (e->data.l[1])
709 case 0: rm = APPCORE_RM_PORTRAIT_NORMAL; break;
710 case 90: rm = APPCORE_RM_LANDSCAPE_REVERSE; break;
711 case 180: rm = APPCORE_RM_PORTRAIT_REVERSE; break;
712 case 270: rm = APPCORE_RM_LANDSCAPE_NORMAL; break;
713 default: rm = APPCORE_RM_UNKNOWN; break;
718 if (APPCORE_RM_UNKNOWN != rm) {
719 ui->rot_cb(rm, ui->rot_cb_data);
723 return ECORE_CALLBACK_PASS_ON;
727 static void __add_climsg_cb(struct ui_priv *ui)
731 atom_parent = ecore_x_atom_get("_E_PARENT_BORDER_WINDOW");
738 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, __show_cb, ui);
740 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_HIDE, __hide_cb, ui);
742 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE,
743 __visibility_cb, ui);
745 /* Add client message callback for WM_ROTATE */
746 if(!__check_wm_rotation_support())
749 ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, __cmsg_cb, ui);
750 ui->wm_rot_supported = 1;
751 appcore_set_wm_rotation(&wm_rotate);
756 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
761 if (argc == NULL || argv == NULL) {
762 _ERR("argc/argv is NULL");
768 elm_init(*argc, *argv);
770 hwacc = getenv("HWACC");
773 _DBG("elm_config_preferred_engine_set is not called");
774 } else if(strcmp(hwacc, "USE") == 0) {
776 elm_config_preferred_engine_set("wayland_egl");
777 _DBG("elm_config_preferred_engine_set : wayland_egl");
779 elm_config_preferred_engine_set("opengl_x11");
780 _DBG("elm_config_preferred_engine_set : opengl_x11");
782 } else if(strcmp(hwacc, "NOT_USE") == 0) {
784 elm_config_preferred_engine_set("wayland_shm");
785 _DBG("elm_config_preferred_engine_set : wayland_shm");
787 elm_config_preferred_engine_set("software_x11");
788 _DBG("elm_config_preferred_engine_set : software_x11");
791 _DBG("elm_config_preferred_engine_set is not called");
794 r = appcore_init(ui->name, &efl_ops, *argc, *argv);
795 _retv_if(r == -1, -1);
797 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
798 if (ui->ops && ui->ops->create) {
799 r = ui->ops->create(ui->ops->data);
801 _ERR("create() return error");
806 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
809 ui->state = AS_CREATED;
816 static void __after_loop(struct ui_priv *ui)
818 appcore_unset_rotation_cb();
821 if (ui->ops && ui->ops->terminate)
822 ui->ops->terminate(ui->ops->data);
825 ecore_event_handler_del(ui->hshow);
827 ecore_event_handler_del(ui->hhide);
829 ecore_event_handler_del(ui->hvchange);
831 __appcore_timer_del(ui);
836 static int __set_data(struct ui_priv *ui, const char *name,
837 struct appcore_ops *ops)
840 _ERR("Mainloop already started");
845 if (name == NULL || name[0] == '\0') {
846 _ERR("Invalid name");
857 ui->name = strdup(name);
858 _retv_if(ui->name == NULL, -1);
862 ui->mfcb = __appcore_efl_memory_flush_cb;
867 ui->wm_rot_supported = 0;
870 ui->rot_cb_data = NULL;
871 ui->rot_mode = APPCORE_RM_UNKNOWN;
876 static void __unset_data(struct ui_priv *ui)
879 free((void *)ui->name);
881 memset(ui, 0, sizeof(struct ui_priv));
885 static int __wm_set_rotation_cb(int (*cb) (enum appcore_rm, void *), void *data)
892 if ((priv.wm_rot_supported) && (0 == priv.rot_started)) {
893 __set_wm_rotation_support(0, 1);
897 priv.rot_cb_data = data;
898 priv.rot_started = 1;
903 static int __wm_unset_rotation_cb(void)
905 if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
906 __set_wm_rotation_support(0, 0);
910 priv.rot_cb_data = NULL;
911 priv.rot_started = 0;
916 static int __wm_get_rotation_state(enum appcore_rm *curr)
923 *curr = priv.rot_mode;
928 static int __wm_pause_rotation_cb(void)
930 if ((1 == priv.rot_started) && (priv.wm_rot_supported)) {
931 __set_wm_rotation_support(0, 0);
934 priv.rot_started = 0;
939 static int __wm_resume_rotation_cb(void)
941 if ((0 == priv.rot_started) && (priv.wm_rot_supported)) {
942 __set_wm_rotation_support(0, 1);
945 priv.rot_started = 1;
950 static struct ui_wm_rotate wm_rotate = {
951 __wm_set_rotation_cb,
952 __wm_unset_rotation_cb,
953 __wm_get_rotation_state,
954 __wm_pause_rotation_cb,
955 __wm_resume_rotation_cb
958 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
959 struct appcore_ops *ops)
963 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
965 r = __set_data(&priv, name, ops);
966 _retv_if(r == -1, -1);
968 r = __before_loop(&priv, argc, argv);
976 aul_status_update(STATUS_DYING);
985 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable)
987 resource_reclaiming = enable;
992 EXPORT_API int appcore_set_app_state(int state)