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>
31 #include <X11/Xatom.h>
35 #include <Ecore_Evas.h>
36 #include <Ecore_Input_Evas.h>
37 #include <Elementary.h>
38 #include <glib-object.h>
43 #include "appcore-internal.h"
44 #include "appcore-efl.h"
46 #define SYSMAN_MAXSTR 100
47 #define SYSMAN_MAXARG 16
48 #define SYSNOTI_SOCKET_PATH "/tmp/sn"
49 #define RETRY_READ_COUNT 10
51 #define PREDEF_BACKGRD "backgrd"
52 #define PREDEF_FOREGRD "foregrd"
65 char *argv[SYSMAN_MAXARG];
70 static bool resource_reclaiming = TRUE;
76 Ecore_Event_Handler *hshow;
77 Ecore_Event_Handler *hhide;
78 Ecore_Event_Handler *hvchange;
79 Ecore_Event_Handler *hcmsg; /* WM_ROTATE */
81 Ecore_Timer *mftimer; /* Ecore Timer for memory flushing */
83 struct appcore_ops *ops;
84 void (*mfcb) (void); /* Memory Flushing Callback */
89 int (*rot_cb) (enum appcore_rm, void *);
91 enum appcore_rm rot_mode;
94 static struct ui_priv priv;
96 static const char *_ae_name[AE_MAX] = {
97 [AE_UNKNOWN] = "UNKNOWN",
98 [AE_CREATE] = "CREATE",
99 [AE_TERMINATE] = "TERMINATE",
100 [AE_PAUSE] = "PAUSE",
101 [AE_RESUME] = "RESUME",
102 [AE_RESET] = "RESET",
103 [AE_LOWMEM_POST] = "LOWMEM_POST",
104 [AE_MEM_FLUSH] = "MEM_FLUSH",
107 static const char *_as_name[] = {
109 [AS_CREATED] = "CREATED",
110 [AS_RUNNING] = "RUNNING",
111 [AS_PAUSED] = "PAUSED",
112 [AS_DYING] = "DYING",
115 static bool b_active = 0;
121 static struct ui_wm_rotate wm_rotate;
123 static inline int send_int(int fd, int val)
125 return write(fd, &val, sizeof(int));
128 static inline int send_str(int fd, char *str)
134 ret = write(fd, &len, sizeof(int));
137 if (len > SYSMAN_MAXSTR)
139 write(fd, &len, sizeof(int));
140 ret = write(fd, str, len);
145 static int sysnoti_send(struct sysnoti *msg)
147 _ERR("--- %s: start", __FUNCTION__);
153 struct sockaddr_un clientaddr;
156 client_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
157 if (client_sockfd == -1) {
158 _ERR("%s: socket create failed\n", __FUNCTION__);
161 bzero(&clientaddr, sizeof(clientaddr));
162 clientaddr.sun_family = AF_UNIX;
163 strncpy(clientaddr.sun_path, SYSNOTI_SOCKET_PATH, sizeof(clientaddr.sun_path) - 1);
164 client_len = sizeof(clientaddr);
166 if (connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) <
168 _ERR("%s: connect failed\n", __FUNCTION__);
169 close(client_sockfd);
173 send_int(client_sockfd, msg->pid);
174 send_int(client_sockfd, msg->cmd);
175 send_str(client_sockfd, msg->type);
176 send_str(client_sockfd, msg->path);
177 send_int(client_sockfd, msg->argc);
178 for (i = 0; i < msg->argc; i++)
179 send_str(client_sockfd, msg->argv[i]);
181 _ERR("--- %s: read", __FUNCTION__);
182 while (retry_count < RETRY_READ_COUNT) {
183 r = read(client_sockfd, &result, sizeof(int));
185 if (errno == EINTR) {
186 _ERR("Re-read for error(EINTR)");
190 _ERR("Read fail for str length");
197 if (retry_count == RETRY_READ_COUNT) {
198 _ERR("Read retry failed");
201 close(client_sockfd);
202 _ERR("--- %s: end", __FUNCTION__);
206 static int _call_predef_action(const char *type, int num, ...)
208 _ERR("--- %s: start", __FUNCTION__);
216 if (type == NULL || num > SYSMAN_MAXARG) {
221 msg = malloc(sizeof(struct sysnoti));
224 /* Do something for not enought memory error */
229 msg->cmd = CALL_SYSMAN_ACTION;
230 msg->type = (char *)type;
234 va_start(argptr, num);
235 for (i = 0; i < num; i++) {
236 args = va_arg(argptr, char *);
241 _ERR("--- %s: send msg", __FUNCTION__);
242 ret = sysnoti_send(msg);
245 _ERR("--- %s: end", __FUNCTION__);
249 static int _inform_foregrd(void)
252 snprintf(buf, sizeof(buf), "%d", getpid());
253 return _call_predef_action(PREDEF_FOREGRD, 1, buf);
256 static int _inform_backgrd(void)
259 snprintf(buf, sizeof(buf), "%d", getpid());
260 return _call_predef_action(PREDEF_BACKGRD, 1, buf);
264 static int WIN_COMP(gconstpointer data1, gconstpointer data2)
266 struct win_node *a = (struct win_node *)data1;
267 struct win_node *b = (struct win_node *)data2;
268 return (int)((a->win)-(b->win));
271 GSList *g_winnode_list = NULL;
273 #if defined(MEMORY_FLUSH_ACTIVATE)
274 static Eina_Bool __appcore_memory_flush_cb(void *data)
276 struct ui_priv *ui = (struct ui_priv *)data;
278 appcore_flush_memory();
281 return ECORE_CALLBACK_CANCEL;
284 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
286 if (ui->state == AS_PAUSED) {
287 appcore_flush_memory();
295 static void __appcore_timer_add(struct ui_priv *ui)
297 ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
300 static void __appcore_timer_del(struct ui_priv *ui)
303 ecore_timer_del(ui->mftimer);
310 static int __appcore_low_memory_post_cb(ui_priv *ui)
315 #define __appcore_timer_add(ui) 0
316 #define __appcore_timer_del(ui) 0
320 static void __appcore_efl_memory_flush_cb(void)
322 _DBG("[APP %d] __appcore_efl_memory_flush_cb()", _pid);
323 elm_cache_all_flush();
326 static void __do_app(enum app_event event, void *data, bundle * b)
329 struct ui_priv *ui = data;
331 _DBG("[APP %d] Event: %d", _pid, event);
332 _ret_if(ui == NULL || event >= AE_MAX);
333 _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
334 _as_name[ui->state]);
336 if (event == AE_MEM_FLUSH) {
341 if (event == AE_LOWMEM_POST) {
342 if (__appcore_low_memory_post_cb(ui) == 0)
346 if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
347 __appcore_timer_del(ui);
349 if (event == AE_TERMINATE) {
350 _DBG("[APP %d] TERMINATE", _pid);
351 ui->state = AS_DYING;
356 _ret_if(ui->ops == NULL);
360 _DBG("[APP %d] RESET", _pid);
361 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]",
364 r = ui->ops->reset(b, ui->ops->data);
365 ui->state = AS_RUNNING;
366 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
370 if (ui->state == AS_RUNNING) {
371 _DBG("[APP %d] PAUSE", _pid);
373 r = ui->ops->pause(ui->ops->data);
374 ui->state = AS_PAUSED;
375 if(r >= 0 && resource_reclaiming == TRUE)
376 __appcore_timer_add(ui);
378 /* TODO : rotation stop */
379 //r = appcore_pause_rotation_cb();
384 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
386 if (ui->state == AS_PAUSED) {
387 _DBG("[APP %d] RESUME", _pid);
389 r = ui->ops->resume(ui->ops->data);
390 ui->state = AS_RUNNING;
392 /*TODO : rotation start*/
393 //r = appcore_resume_rotation_cb();
394 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
396 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
407 static struct ui_ops efl_ops = {
413 static bool __check_visible(void)
416 struct win_node *entry = NULL;
418 _DBG("[EVENT_TEST][EVENT] __check_visible\n");
420 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
422 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
423 if(entry->bfobscured == FALSE)
429 static bool __exist_win(unsigned int win)
431 struct win_node temp;
436 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
445 static bool __add_win(unsigned int win)
450 t = calloc(1, sizeof(struct win_node));
455 t->bfobscured = FALSE;
457 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
459 f = g_slist_find_custom(g_winnode_list, t, WIN_COMP);
463 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
468 g_winnode_list = g_slist_append(g_winnode_list, t);
474 static bool __delete_win(unsigned int win)
476 struct win_node temp;
481 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
484 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
489 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
496 static bool __update_win(unsigned int win, bool bfobscured)
498 struct win_node temp;
503 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
508 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
512 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
516 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
520 t = calloc(1, sizeof(struct win_node));
525 t->bfobscured = bfobscured;
527 g_winnode_list = g_slist_append(g_winnode_list, t);
534 static Ecore_X_Atom _WM_WINDOW_ROTATION_SUPPORTED = 0;
535 static Ecore_X_Atom _WM_WINDOW_ROTATION_CHANGE_REQUEST = 0;
537 static int __check_wm_rotation_support(void)
539 _DBG("Disable window manager rotation");
542 Ecore_X_Window root, win, win2;
545 if (!_WM_WINDOW_ROTATION_SUPPORTED) {
546 _WM_WINDOW_ROTATION_SUPPORTED =
547 ecore_x_atom_get("_E_WINDOW_ROTATION_SUPPORTED");
550 if (!_WM_WINDOW_ROTATION_CHANGE_REQUEST) {
551 _WM_WINDOW_ROTATION_CHANGE_REQUEST =
552 ecore_x_atom_get("_E_WINDOW_ROTATION_CHANGE_REQUEST");
555 root = ecore_x_window_root_first_get();
556 ret = ecore_x_window_prop_xid_get(root,
557 _WM_WINDOW_ROTATION_SUPPORTED,
560 if ((ret == 1) && (win))
562 ret = ecore_x_window_prop_xid_get(win,
563 _WM_WINDOW_ROTATION_SUPPORTED,
566 if ((ret == 1) && (win2 == win))
573 static void __set_wm_rotation_support(unsigned int win, unsigned int set)
576 struct win_node *entry = NULL;
579 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
582 ecore_x_window_prop_card32_set(entry->win,
583 _WM_WINDOW_ROTATION_SUPPORTED,
588 ecore_x_window_prop_card32_set(win,
589 _WM_WINDOW_ROTATION_SUPPORTED,
594 Ecore_X_Atom atom_parent;
596 static Eina_Bool __show_cb(void *data, int type, void *event)
598 Ecore_X_Event_Window_Show *ev;
600 Ecore_X_Window parent;
604 ret = ecore_x_window_prop_window_get(ev->win, atom_parent, &parent, 1);
607 // This is child window. Skip!!!
608 return ECORE_CALLBACK_PASS_ON;
611 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
613 if (!__exist_win((unsigned int)ev->win)) {
615 if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
616 __set_wm_rotation_support(ev->win, 1);
618 __add_win((unsigned int)ev->win);
621 __update_win((unsigned int)ev->win, FALSE);
623 return ECORE_CALLBACK_RENEW;
626 static Eina_Bool __hide_cb(void *data, int type, void *event)
628 Ecore_X_Event_Window_Hide *ev;
633 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
635 if (__exist_win((unsigned int)ev->win)) {
636 __delete_win((unsigned int)ev->win);
638 bvisibility = __check_visible();
639 if (!bvisibility && b_active == 1) {
640 _DBG(" Go to Pasue state \n");
642 __do_app(AE_PAUSE, data, NULL);
646 return ECORE_CALLBACK_RENEW;
649 static Eina_Bool __visibility_cb(void *data, int type, void *event)
651 Ecore_X_Event_Window_Visibility_Change *ev;
656 __update_win((unsigned int)ev->win, ev->fully_obscured);
657 bvisibility = __check_visible();
659 if (bvisibility && b_active == 0) {
660 _DBG(" Go to Resume state\n");
662 __do_app(AE_RESUME, data, NULL);
664 } else if (!bvisibility && b_active == 1) {
665 _DBG(" Go to Pasue state \n");
667 __do_app(AE_PAUSE, data, NULL);
669 _DBG(" No change state \n");
671 return ECORE_CALLBACK_RENEW;
676 static Eina_Bool __cmsg_cb(void *data, int type, void *event)
678 struct ui_priv *ui = (struct ui_priv *)data;
679 Ecore_X_Event_Client_Message *e = event;
681 if (!ui) return ECORE_CALLBACK_PASS_ON;
682 if (e->format != 32) return ECORE_CALLBACK_PASS_ON;
683 if (e->message_type == _WM_WINDOW_ROTATION_CHANGE_REQUEST) {
684 if ((0 == ui->wm_rot_supported) ||
685 (0 == ui->rot_started) ||
686 (NULL == ui->rot_cb)) {
687 return ECORE_CALLBACK_PASS_ON;
691 switch (e->data.l[1])
693 case 0: rm = APPCORE_RM_PORTRAIT_NORMAL; break;
694 case 90: rm = APPCORE_RM_LANDSCAPE_REVERSE; break;
695 case 180: rm = APPCORE_RM_PORTRAIT_REVERSE; break;
696 case 270: rm = APPCORE_RM_LANDSCAPE_NORMAL; break;
697 default: rm = APPCORE_RM_UNKNOWN; break;
702 if (APPCORE_RM_UNKNOWN != rm) {
703 ui->rot_cb(rm, ui->rot_cb_data);
707 return ECORE_CALLBACK_PASS_ON;
710 static void __add_climsg_cb(struct ui_priv *ui)
714 atom_parent = ecore_x_atom_get("_E_PARENT_BORDER_WINDOW");
721 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, __show_cb, ui);
723 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_HIDE, __hide_cb, ui);
725 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE,
726 __visibility_cb, ui);
728 /* Add client message callback for WM_ROTATE */
729 if(!__check_wm_rotation_support())
732 ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, __cmsg_cb, ui);
733 ui->wm_rot_supported = 1;
734 appcore_set_wm_rotation(&wm_rotate);
738 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
743 if (argc == NULL || argv == NULL) {
744 _ERR("argc/argv is NULL");
750 elm_init(*argc, *argv);
752 hwacc = getenv("HWACC");
755 _DBG("elm_config_preferred_engine_set is not called");
756 } else if(strcmp(hwacc, "USE") == 0) {
757 elm_config_preferred_engine_set("opengl_x11");
758 _DBG("elm_config_preferred_engine_set : opengl_x11");
759 } else if(strcmp(hwacc, "NOT_USE") == 0) {
760 elm_config_preferred_engine_set("software_x11");
761 _DBG("elm_config_preferred_engine_set : software_x11");
763 _DBG("elm_config_preferred_engine_set is not called");
766 r = appcore_init(ui->name, &efl_ops, *argc, *argv);
767 _retv_if(r == -1, -1);
769 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
770 if (ui->ops && ui->ops->create) {
771 r = ui->ops->create(ui->ops->data);
773 _ERR("create() return error");
778 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
781 ui->state = AS_CREATED;
788 static void __after_loop(struct ui_priv *ui)
790 appcore_unset_rotation_cb();
793 if (ui->ops && ui->ops->terminate)
794 ui->ops->terminate(ui->ops->data);
797 ecore_event_handler_del(ui->hshow);
799 ecore_event_handler_del(ui->hhide);
801 ecore_event_handler_del(ui->hvchange);
803 __appcore_timer_del(ui);
808 static int __set_data(struct ui_priv *ui, const char *name,
809 struct appcore_ops *ops)
812 _ERR("Mainloop already started");
817 if (name == NULL || name[0] == '\0') {
818 _ERR("Invalid name");
829 ui->name = strdup(name);
830 _retv_if(ui->name == NULL, -1);
834 ui->mfcb = __appcore_efl_memory_flush_cb;
839 ui->wm_rot_supported = 0;
842 ui->rot_cb_data = NULL;
843 ui->rot_mode = APPCORE_RM_UNKNOWN;
848 static void __unset_data(struct ui_priv *ui)
851 free((void *)ui->name);
853 memset(ui, 0, sizeof(struct ui_priv));
857 static int __wm_set_rotation_cb(int (*cb) (enum appcore_rm, void *), void *data)
864 if ((priv.wm_rot_supported) && (0 == priv.rot_started)) {
865 __set_wm_rotation_support(0, 1);
869 priv.rot_cb_data = data;
870 priv.rot_started = 1;
875 static int __wm_unset_rotation_cb(void)
877 if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
878 __set_wm_rotation_support(0, 0);
882 priv.rot_cb_data = NULL;
883 priv.rot_started = 0;
888 static int __wm_get_rotation_state(enum appcore_rm *curr)
895 *curr = priv.rot_mode;
900 static int __wm_pause_rotation_cb(void)
902 if ((1 == priv.rot_started) && (priv.wm_rot_supported)) {
903 __set_wm_rotation_support(0, 0);
906 priv.rot_started = 0;
911 static int __wm_resume_rotation_cb(void)
913 if ((0 == priv.rot_started) && (priv.wm_rot_supported)) {
914 __set_wm_rotation_support(0, 1);
917 priv.rot_started = 1;
922 static struct ui_wm_rotate wm_rotate = {
923 __wm_set_rotation_cb,
924 __wm_unset_rotation_cb,
925 __wm_get_rotation_state,
926 __wm_pause_rotation_cb,
927 __wm_resume_rotation_cb
930 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
931 struct appcore_ops *ops)
935 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
937 r = __set_data(&priv, name, ops);
938 _retv_if(r == -1, -1);
940 r = __before_loop(&priv, argc, argv);
948 aul_status_update(STATUS_DYING);
957 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable)
959 resource_reclaiming = enable;