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;
71 static int tmp_val = 0;
78 Ecore_Event_Handler *hshow;
79 Ecore_Event_Handler *hhide;
80 Ecore_Event_Handler *hvchange;
81 Ecore_Event_Handler *hcmsg; /* WM_ROTATE */
83 Ecore_Timer *mftimer; /* Ecore Timer for memory flushing */
85 struct appcore_ops *ops;
86 void (*mfcb) (void); /* Memory Flushing Callback */
91 int (*rot_cb) (enum appcore_rm, void *);
93 enum appcore_rm rot_mode;
96 static struct ui_priv priv;
98 static const char *_ae_name[AE_MAX] = {
99 [AE_UNKNOWN] = "UNKNOWN",
100 [AE_CREATE] = "CREATE",
101 [AE_TERMINATE] = "TERMINATE",
102 [AE_PAUSE] = "PAUSE",
103 [AE_RESUME] = "RESUME",
104 [AE_RESET] = "RESET",
105 [AE_LOWMEM_POST] = "LOWMEM_POST",
106 [AE_MEM_FLUSH] = "MEM_FLUSH",
109 static const char *_as_name[] = {
111 [AS_CREATED] = "CREATED",
112 [AS_RUNNING] = "RUNNING",
113 [AS_PAUSED] = "PAUSED",
114 [AS_DYING] = "DYING",
117 static bool b_active = 0;
123 static struct ui_wm_rotate wm_rotate;
125 static inline int send_int(int fd, int val)
127 return write(fd, &val, sizeof(int));
130 static inline int send_str(int fd, char *str)
136 ret = write(fd, &len, sizeof(int));
139 if (len > SYSMAN_MAXSTR)
141 write(fd, &len, sizeof(int));
142 ret = write(fd, str, len);
147 static int sysnoti_send(struct sysnoti *msg)
149 _ERR("--- %s: start", __FUNCTION__);
155 struct sockaddr_un clientaddr;
158 client_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
159 if (client_sockfd == -1) {
160 _ERR("%s: socket create failed\n", __FUNCTION__);
163 bzero(&clientaddr, sizeof(clientaddr));
164 clientaddr.sun_family = AF_UNIX;
165 strncpy(clientaddr.sun_path, SYSNOTI_SOCKET_PATH, sizeof(clientaddr.sun_path) - 1);
166 client_len = sizeof(clientaddr);
168 if (connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) <
170 _ERR("%s: connect failed\n", __FUNCTION__);
171 close(client_sockfd);
175 send_int(client_sockfd, msg->pid);
176 send_int(client_sockfd, msg->cmd);
177 send_str(client_sockfd, msg->type);
178 send_str(client_sockfd, msg->path);
179 send_int(client_sockfd, msg->argc);
180 for (i = 0; i < msg->argc; i++)
181 send_str(client_sockfd, msg->argv[i]);
183 _ERR("--- %s: read", __FUNCTION__);
184 while (retry_count < RETRY_READ_COUNT) {
185 r = read(client_sockfd, &result, sizeof(int));
187 if (errno == EINTR) {
188 _ERR("Re-read for error(EINTR)");
192 _ERR("Read fail for str length");
199 if (retry_count == RETRY_READ_COUNT) {
200 _ERR("Read retry failed");
203 close(client_sockfd);
204 _ERR("--- %s: end", __FUNCTION__);
208 static int _call_predef_action(const char *type, int num, ...)
210 _ERR("--- %s: start", __FUNCTION__);
218 if (type == NULL || num > SYSMAN_MAXARG) {
223 msg = malloc(sizeof(struct sysnoti));
226 /* Do something for not enought memory error */
231 msg->cmd = CALL_SYSMAN_ACTION;
232 msg->type = (char *)type;
236 va_start(argptr, num);
237 for (i = 0; i < num; i++) {
238 args = va_arg(argptr, char *);
243 _ERR("--- %s: send msg", __FUNCTION__);
244 ret = sysnoti_send(msg);
247 _ERR("--- %s: end", __FUNCTION__);
251 static int _inform_foregrd(void)
254 snprintf(buf, sizeof(buf), "%d", getpid());
255 return _call_predef_action(PREDEF_FOREGRD, 1, buf);
258 static int _inform_backgrd(void)
261 snprintf(buf, sizeof(buf), "%d", getpid());
262 return _call_predef_action(PREDEF_BACKGRD, 1, buf);
266 static int WIN_COMP(gconstpointer data1, gconstpointer data2)
268 struct win_node *a = (struct win_node *)data1;
269 struct win_node *b = (struct win_node *)data2;
270 return (int)((a->win)-(b->win));
273 GSList *g_winnode_list = NULL;
275 #if defined(MEMORY_FLUSH_ACTIVATE)
276 static Eina_Bool __appcore_memory_flush_cb(void *data)
278 struct ui_priv *ui = (struct ui_priv *)data;
280 appcore_flush_memory();
283 return ECORE_CALLBACK_CANCEL;
286 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
288 if (ui->state == AS_PAUSED) {
289 appcore_flush_memory();
297 static void __appcore_timer_add(struct ui_priv *ui)
299 ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
302 static void __appcore_timer_del(struct ui_priv *ui)
305 ecore_timer_del(ui->mftimer);
312 static int __appcore_low_memory_post_cb(ui_priv *ui)
317 #define __appcore_timer_add(ui) 0
318 #define __appcore_timer_del(ui) 0
322 static void __appcore_efl_memory_flush_cb(void)
324 _DBG("[APP %d] __appcore_efl_memory_flush_cb()", _pid);
325 elm_cache_all_flush();
328 static void __do_app(enum app_event event, void *data, bundle * b)
331 struct ui_priv *ui = data;
333 _DBG("[APP %d] Event: %d", _pid, event);
334 _ret_if(ui == NULL || event >= AE_MAX);
335 _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
336 _as_name[ui->state]);
338 if (event == AE_MEM_FLUSH) {
343 if (event == AE_LOWMEM_POST) {
344 if (__appcore_low_memory_post_cb(ui) == 0)
348 if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
349 __appcore_timer_del(ui);
351 if (event == AE_TERMINATE) {
352 _DBG("[APP %d] TERMINATE", _pid);
353 ui->state = AS_DYING;
358 _ret_if(ui->ops == NULL);
362 _DBG("[APP %d] RESET", _pid);
363 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]",
366 r = ui->ops->reset(b, ui->ops->data);
367 ui->state = AS_RUNNING;
368 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
372 if (ui->state == AS_RUNNING) {
373 _DBG("[APP %d] PAUSE", _pid);
375 r = ui->ops->pause(ui->ops->data);
376 ui->state = AS_PAUSED;
377 if(r >= 0 && resource_reclaiming == TRUE)
378 __appcore_timer_add(ui);
380 /* TODO : rotation stop */
381 //r = appcore_pause_rotation_cb();
386 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
388 if (ui->state == AS_PAUSED || tmp_val == 1) {
389 _DBG("[APP %d] RESUME", _pid);
391 r = ui->ops->resume(ui->ops->data);
392 ui->state = AS_RUNNING;
395 /*TODO : rotation start*/
396 //r = appcore_resume_rotation_cb();
397 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
399 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
410 static struct ui_ops efl_ops = {
416 static bool __check_visible(void)
419 struct win_node *entry = NULL;
421 _DBG("[EVENT_TEST][EVENT] __check_visible\n");
423 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
425 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
426 if(entry->bfobscured == FALSE)
432 static bool __exist_win(unsigned int win)
434 struct win_node temp;
439 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
448 static bool __add_win(unsigned int win)
453 t = calloc(1, sizeof(struct win_node));
458 t->bfobscured = FALSE;
460 _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
462 f = g_slist_find_custom(g_winnode_list, t, WIN_COMP);
466 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
471 g_winnode_list = g_slist_append(g_winnode_list, t);
477 static bool __delete_win(unsigned int win)
479 struct win_node temp;
484 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
487 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
492 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
499 static bool __update_win(unsigned int win, bool bfobscured)
501 struct win_node temp;
506 _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
511 f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
515 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
519 g_winnode_list = g_slist_remove_link(g_winnode_list, f);
523 t = calloc(1, sizeof(struct win_node));
528 t->bfobscured = bfobscured;
530 g_winnode_list = g_slist_append(g_winnode_list, t);
537 static Ecore_X_Atom _WM_WINDOW_ROTATION_SUPPORTED = 0;
538 static Ecore_X_Atom _WM_WINDOW_ROTATION_CHANGE_REQUEST = 0;
540 static int __check_wm_rotation_support(void)
542 _DBG("Disable window manager rotation");
545 Ecore_X_Window root, win, win2;
548 if (!_WM_WINDOW_ROTATION_SUPPORTED) {
549 _WM_WINDOW_ROTATION_SUPPORTED =
550 ecore_x_atom_get("_E_WINDOW_ROTATION_SUPPORTED");
553 if (!_WM_WINDOW_ROTATION_CHANGE_REQUEST) {
554 _WM_WINDOW_ROTATION_CHANGE_REQUEST =
555 ecore_x_atom_get("_E_WINDOW_ROTATION_CHANGE_REQUEST");
558 root = ecore_x_window_root_first_get();
559 ret = ecore_x_window_prop_xid_get(root,
560 _WM_WINDOW_ROTATION_SUPPORTED,
563 if ((ret == 1) && (win))
565 ret = ecore_x_window_prop_xid_get(win,
566 _WM_WINDOW_ROTATION_SUPPORTED,
569 if ((ret == 1) && (win2 == win))
576 static void __set_wm_rotation_support(unsigned int win, unsigned int set)
579 struct win_node *entry = NULL;
582 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
585 ecore_x_window_prop_card32_set(entry->win,
586 _WM_WINDOW_ROTATION_SUPPORTED,
591 ecore_x_window_prop_card32_set(win,
592 _WM_WINDOW_ROTATION_SUPPORTED,
597 Ecore_X_Atom atom_parent;
599 static Eina_Bool __show_cb(void *data, int type, void *event)
601 Ecore_X_Event_Window_Show *ev;
603 Ecore_X_Window parent;
607 ret = ecore_x_window_prop_window_get(ev->win, atom_parent, &parent, 1);
610 // This is child window. Skip!!!
611 return ECORE_CALLBACK_PASS_ON;
614 _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
616 if (!__exist_win((unsigned int)ev->win)) {
618 if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
619 __set_wm_rotation_support(ev->win, 1);
621 __add_win((unsigned int)ev->win);
624 __update_win((unsigned int)ev->win, FALSE);
626 return ECORE_CALLBACK_RENEW;
629 static Eina_Bool __hide_cb(void *data, int type, void *event)
631 Ecore_X_Event_Window_Hide *ev;
636 _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
638 if (__exist_win((unsigned int)ev->win)) {
639 __delete_win((unsigned int)ev->win);
641 bvisibility = __check_visible();
642 if (!bvisibility && b_active == 1) {
643 _DBG(" Go to Pasue state \n");
645 __do_app(AE_PAUSE, data, NULL);
649 return ECORE_CALLBACK_RENEW;
652 static Eina_Bool __visibility_cb(void *data, int type, void *event)
654 Ecore_X_Event_Window_Visibility_Change *ev;
659 __update_win((unsigned int)ev->win, ev->fully_obscured);
660 bvisibility = __check_visible();
662 if (bvisibility && b_active == 0) {
663 _DBG(" Go to Resume state\n");
665 __do_app(AE_RESUME, data, NULL);
667 } else if (!bvisibility && b_active == 1) {
668 _DBG(" Go to Pasue state \n");
670 __do_app(AE_PAUSE, data, NULL);
672 _DBG(" No change state \n");
674 return ECORE_CALLBACK_RENEW;
679 static Eina_Bool __cmsg_cb(void *data, int type, void *event)
681 struct ui_priv *ui = (struct ui_priv *)data;
682 Ecore_X_Event_Client_Message *e = event;
684 if (!ui) return ECORE_CALLBACK_PASS_ON;
685 if (e->format != 32) return ECORE_CALLBACK_PASS_ON;
686 if (e->message_type == _WM_WINDOW_ROTATION_CHANGE_REQUEST) {
687 if ((0 == ui->wm_rot_supported) ||
688 (0 == ui->rot_started) ||
689 (NULL == ui->rot_cb)) {
690 return ECORE_CALLBACK_PASS_ON;
694 switch (e->data.l[1])
696 case 0: rm = APPCORE_RM_PORTRAIT_NORMAL; break;
697 case 90: rm = APPCORE_RM_LANDSCAPE_REVERSE; break;
698 case 180: rm = APPCORE_RM_PORTRAIT_REVERSE; break;
699 case 270: rm = APPCORE_RM_LANDSCAPE_NORMAL; break;
700 default: rm = APPCORE_RM_UNKNOWN; break;
705 if (APPCORE_RM_UNKNOWN != rm) {
706 ui->rot_cb(rm, ui->rot_cb_data);
710 return ECORE_CALLBACK_PASS_ON;
713 static void __add_climsg_cb(struct ui_priv *ui)
717 atom_parent = ecore_x_atom_get("_E_PARENT_BORDER_WINDOW");
724 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, __show_cb, ui);
726 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_HIDE, __hide_cb, ui);
728 ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE,
729 __visibility_cb, ui);
731 /* Add client message callback for WM_ROTATE */
732 if(!__check_wm_rotation_support())
735 ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, __cmsg_cb, ui);
736 ui->wm_rot_supported = 1;
737 appcore_set_wm_rotation(&wm_rotate);
741 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
746 if (argc == NULL || argv == NULL) {
747 _ERR("argc/argv is NULL");
753 elm_init(*argc, *argv);
755 hwacc = getenv("HWACC");
758 _DBG("elm_config_preferred_engine_set is not called");
759 } else if(strcmp(hwacc, "USE") == 0) {
760 elm_config_preferred_engine_set("opengl_x11");
761 _DBG("elm_config_preferred_engine_set : opengl_x11");
762 } else if(strcmp(hwacc, "NOT_USE") == 0) {
763 elm_config_preferred_engine_set("software_x11");
764 _DBG("elm_config_preferred_engine_set : software_x11");
766 _DBG("elm_config_preferred_engine_set is not called");
769 r = appcore_init(ui->name, &efl_ops, *argc, *argv);
770 _retv_if(r == -1, -1);
772 LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
773 if (ui->ops && ui->ops->create) {
774 r = ui->ops->create(ui->ops->data);
776 _ERR("create() return error");
781 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
784 ui->state = AS_CREATED;
791 static void __after_loop(struct ui_priv *ui)
793 appcore_unset_rotation_cb();
796 if (ui->ops && ui->ops->terminate)
797 ui->ops->terminate(ui->ops->data);
800 ecore_event_handler_del(ui->hshow);
802 ecore_event_handler_del(ui->hhide);
804 ecore_event_handler_del(ui->hvchange);
806 __appcore_timer_del(ui);
811 static int __set_data(struct ui_priv *ui, const char *name,
812 struct appcore_ops *ops)
815 _ERR("Mainloop already started");
820 if (name == NULL || name[0] == '\0') {
821 _ERR("Invalid name");
832 ui->name = strdup(name);
833 _retv_if(ui->name == NULL, -1);
837 ui->mfcb = __appcore_efl_memory_flush_cb;
842 ui->wm_rot_supported = 0;
845 ui->rot_cb_data = NULL;
846 ui->rot_mode = APPCORE_RM_UNKNOWN;
851 static void __unset_data(struct ui_priv *ui)
854 free((void *)ui->name);
856 memset(ui, 0, sizeof(struct ui_priv));
860 static int __wm_set_rotation_cb(int (*cb) (enum appcore_rm, void *), void *data)
867 if ((priv.wm_rot_supported) && (0 == priv.rot_started)) {
868 __set_wm_rotation_support(0, 1);
872 priv.rot_cb_data = data;
873 priv.rot_started = 1;
878 static int __wm_unset_rotation_cb(void)
880 if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
881 __set_wm_rotation_support(0, 0);
885 priv.rot_cb_data = NULL;
886 priv.rot_started = 0;
891 static int __wm_get_rotation_state(enum appcore_rm *curr)
898 *curr = priv.rot_mode;
903 static int __wm_pause_rotation_cb(void)
905 if ((1 == priv.rot_started) && (priv.wm_rot_supported)) {
906 __set_wm_rotation_support(0, 0);
909 priv.rot_started = 0;
914 static int __wm_resume_rotation_cb(void)
916 if ((0 == priv.rot_started) && (priv.wm_rot_supported)) {
917 __set_wm_rotation_support(0, 1);
920 priv.rot_started = 1;
925 static struct ui_wm_rotate wm_rotate = {
926 __wm_set_rotation_cb,
927 __wm_unset_rotation_cb,
928 __wm_get_rotation_state,
929 __wm_pause_rotation_cb,
930 __wm_resume_rotation_cb
933 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
934 struct appcore_ops *ops)
938 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
940 r = __set_data(&priv, name, ops);
941 _retv_if(r == -1, -1);
943 r = __before_loop(&priv, argc, argv);
951 aul_status_update(STATUS_DYING);
960 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable)
962 resource_reclaiming = enable;
967 EXPORT_API int appcore_set_app_state(int state)