fixed build error
[platform/core/appfw/app-core.git] / src / appcore-efl.c
1 /*
2  *  app-core
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jayoun Lee <airjany@samsung.com>, Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
7  *
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <sys/types.h>
23 #include <sys/socket.h>
24 #include <sys/un.h>
25 #include <stdio.h>
26 #include <unistd.h>
27 #include <stdarg.h>
28 #include <errno.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <X11/Xatom.h>
32 #include <X11/Xlib.h>
33 #include <Ecore_X.h>
34 #include <Ecore.h>
35 #include <Ecore_Evas.h>
36 #include <Ecore_Input_Evas.h>
37 #include <Elementary.h>
38 #include <glib-object.h>
39 #include <malloc.h>
40 #include <glib.h>
41 #include <stdbool.h>
42 #include "appcore-internal.h"
43 #include "appcore-efl.h"
44
45 #define SYSMAN_MAXSTR 100
46 #define SYSMAN_MAXARG 16
47 #define SYSNOTI_SOCKET_PATH "/tmp/sn"
48 #define RETRY_READ_COUNT        10
49
50 #define PREDEF_BACKGRD                          "backgrd"
51 #define PREDEF_FOREGRD                          "foregrd"
52
53 enum sysnoti_cmd {
54         ADD_SYSMAN_ACTION,
55         CALL_SYSMAN_ACTION
56 };
57
58 struct sysnoti {
59         int pid;
60         int cmd;
61         char *type;
62         char *path;
63         int argc;
64         char *argv[SYSMAN_MAXARG];
65 };
66
67 static pid_t _pid;
68
69 static bool resource_reclaiming = TRUE;
70
71 struct ui_priv {
72         const char *name;
73         enum app_state state;
74
75         Ecore_Event_Handler *hshow;
76         Ecore_Event_Handler *hhide;
77         Ecore_Event_Handler *hvchange;
78         Ecore_Event_Handler *hcmsg; /* WM_ROTATE */
79
80         Ecore_Timer *mftimer;   /* Ecore Timer for memory flushing */
81
82         struct appcore_ops *ops;
83         void (*mfcb) (void);    /* Memory Flushing Callback */
84
85         /* WM_ROTATE */
86         int wm_rot_supported;
87         int rot_started;
88         int (*rot_cb) (enum appcore_rm, void *);
89         void *rot_cb_data;
90         enum appcore_rm rot_mode;
91 };
92
93 static struct ui_priv priv;
94
95 static const char *_ae_name[AE_MAX] = {
96         [AE_UNKNOWN] = "UNKNOWN",
97         [AE_CREATE] = "CREATE",
98         [AE_TERMINATE] = "TERMINATE",
99         [AE_PAUSE] = "PAUSE",
100         [AE_RESUME] = "RESUME",
101         [AE_RESET] = "RESET",
102         [AE_LOWMEM_POST] = "LOWMEM_POST",
103         [AE_MEM_FLUSH] = "MEM_FLUSH",
104 };
105
106 static const char *_as_name[] = {
107         [AS_NONE] = "NONE",
108         [AS_CREATED] = "CREATED",
109         [AS_RUNNING] = "RUNNING",
110         [AS_PAUSED] = "PAUSED",
111         [AS_DYING] = "DYING",
112 };
113
114 static bool b_active = 0;
115 struct win_node {
116         unsigned int win;
117         bool bfobscured;
118 };
119
120 static struct ui_wm_rotate wm_rotate;
121
122 static inline int send_int(int fd, int val)
123 {
124         return write(fd, &val, sizeof(int));
125 }
126
127 static inline int send_str(int fd, char *str)
128 {
129         int len;
130         int ret;
131         if (str == NULL) {
132                 len = 0;
133                 ret = write(fd, &len, sizeof(int));
134         } else {
135                 len = strlen(str);
136                 if (len > SYSMAN_MAXSTR)
137                         len = SYSMAN_MAXSTR;
138                 write(fd, &len, sizeof(int));
139                 ret = write(fd, str, len);
140         }
141         return ret;
142 }
143
144 static int sysnoti_send(struct sysnoti *msg)
145 {
146         _ERR("--- %s: start", __FUNCTION__);
147         int client_len;
148         int client_sockfd;
149         int result;
150         int r;
151         int retry_count = 0;
152         struct sockaddr_un clientaddr;
153         int i;
154
155         client_sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
156         if (client_sockfd == -1) {
157                 _ERR("%s: socket create failed\n", __FUNCTION__);
158                 return -1;
159         }
160         bzero(&clientaddr, sizeof(clientaddr));
161         clientaddr.sun_family = AF_UNIX;
162         strncpy(clientaddr.sun_path, SYSNOTI_SOCKET_PATH, sizeof(clientaddr.sun_path) - 1);
163         client_len = sizeof(clientaddr);
164
165         if (connect(client_sockfd, (struct sockaddr *)&clientaddr, client_len) <
166             0) {
167                 _ERR("%s: connect failed\n", __FUNCTION__);
168                 close(client_sockfd);
169                 return -1;
170         }
171
172         send_int(client_sockfd, msg->pid);
173         send_int(client_sockfd, msg->cmd);
174         send_str(client_sockfd, msg->type);
175         send_str(client_sockfd, msg->path);
176         send_int(client_sockfd, msg->argc);
177         for (i = 0; i < msg->argc; i++)
178                 send_str(client_sockfd, msg->argv[i]);
179
180         _ERR("--- %s: read", __FUNCTION__);
181         while (retry_count < RETRY_READ_COUNT) {
182                 r = read(client_sockfd, &result, sizeof(int));
183                 if (r < 0) {
184                         if (errno == EINTR) {
185                                 _ERR("Re-read for error(EINTR)");
186                                 retry_count++;
187                                 continue;
188                         }
189                         _ERR("Read fail for str length");
190                         result = -1;
191                         break;
192
193                 }
194                 break;
195         }
196         if (retry_count == RETRY_READ_COUNT) {
197                 _ERR("Read retry failed");
198         }
199
200         close(client_sockfd);
201         _ERR("--- %s: end", __FUNCTION__);
202         return result;
203 }
204
205 static int _call_predef_action(const char *type, int num, ...)
206 {
207         _ERR("--- %s: start", __FUNCTION__);
208         struct sysnoti *msg;
209         int ret;
210         va_list argptr;
211
212         int i;
213         char *args = NULL;
214
215         if (type == NULL || num > SYSMAN_MAXARG) {
216                 errno = EINVAL;
217                 return -1;
218         }
219
220         msg = malloc(sizeof(struct sysnoti));
221
222         if (msg == NULL) {
223                 /* Do something for not enought memory error */
224                 return -1;
225         }
226
227         msg->pid = getpid();
228         msg->cmd = CALL_SYSMAN_ACTION;
229         msg->type = (char *)type;
230         msg->path = NULL;
231
232         msg->argc = num;
233         va_start(argptr, num);
234         for (i = 0; i < num; i++) {
235                 args = va_arg(argptr, char *);
236                 msg->argv[i] = args;
237         }
238         va_end(argptr);
239
240         _ERR("--- %s: send msg", __FUNCTION__);
241         ret = sysnoti_send(msg);
242         free(msg);
243
244         _ERR("--- %s: end", __FUNCTION__);
245         return ret;
246 }
247
248 static int _inform_foregrd(void)
249 {
250         char buf[255];
251         snprintf(buf, sizeof(buf), "%d", getpid());
252         return _call_predef_action(PREDEF_FOREGRD, 1, buf);
253 }
254
255 static int _inform_backgrd(void)
256 {
257         char buf[255];
258         snprintf(buf, sizeof(buf), "%d", getpid());
259         return _call_predef_action(PREDEF_BACKGRD, 1, buf);
260 }
261
262
263 static int WIN_COMP(gconstpointer data1, gconstpointer data2)
264 {
265         struct win_node *a = (struct win_node *)data1;
266         struct win_node *b = (struct win_node *)data2;
267         return (int)((a->win)-(b->win));
268 }
269
270 GSList *g_winnode_list = NULL;
271
272 #if defined(MEMORY_FLUSH_ACTIVATE)
273 static Eina_Bool __appcore_memory_flush_cb(void *data)
274 {
275         struct ui_priv *ui = (struct ui_priv *)data;
276
277         appcore_flush_memory();
278         ui->mftimer = NULL;
279
280         return ECORE_CALLBACK_CANCEL;
281 }
282
283 static int __appcore_low_memory_post_cb(struct ui_priv *ui)
284 {
285         if (ui->state == AS_PAUSED) {
286                 appcore_flush_memory();
287         } else {
288                 malloc_trim(0);
289         }
290
291         return 0;
292 }
293
294 static void __appcore_timer_add(struct ui_priv *ui)
295 {
296         ui->mftimer = ecore_timer_add(5, __appcore_memory_flush_cb, ui);
297 }
298
299 static void __appcore_timer_del(struct ui_priv *ui)
300 {
301         if (ui->mftimer) {
302                 ecore_timer_del(ui->mftimer);
303                 ui->mftimer = NULL;
304         }
305 }
306
307 #else
308
309 static int __appcore_low_memory_post_cb(ui_priv *ui)
310 {
311         return -1;
312 }
313
314 #define __appcore_timer_add(ui) 0
315 #define __appcore_timer_del(ui) 0
316
317 #endif
318
319 static void __appcore_efl_memory_flush_cb(void)
320 {
321         _DBG("[APP %d]   __appcore_efl_memory_flush_cb()", _pid);
322         elm_cache_all_flush();
323 }
324
325 static void __do_app(enum app_event event, void *data, bundle * b)
326 {
327         int r = -1;
328         struct ui_priv *ui = data;
329
330         _DBG("[APP %d] Event: %d", _pid, event);
331         _ret_if(ui == NULL || event >= AE_MAX);
332         _DBG("[APP %d] Event: %s State: %s", _pid, _ae_name[event],
333              _as_name[ui->state]);
334
335         if (event == AE_MEM_FLUSH) {
336                 ui->mfcb();
337                 return;
338         }
339
340         if (event == AE_LOWMEM_POST) {
341                 if (__appcore_low_memory_post_cb(ui) == 0)
342                         return;
343         }
344
345         if (!(ui->state == AS_PAUSED && event == AE_PAUSE))
346                 __appcore_timer_del(ui);
347
348         if (event == AE_TERMINATE) {
349                 _DBG("[APP %d] TERMINATE", _pid);
350                 ui->state = AS_DYING;
351                 elm_exit();
352                 return;
353         }
354
355         _ret_if(ui->ops == NULL);
356
357         switch (event) {
358         case AE_RESET:
359                 _DBG("[APP %d] RESET", _pid);
360                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:start]",
361                     ui->name);
362                 if (ui->ops->reset)
363                         r = ui->ops->reset(b, ui->ops->data);
364                 ui->state = AS_RUNNING;
365                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:reset:done]",
366                     ui->name);
367                 break;
368         case AE_PAUSE:
369                 if (ui->state == AS_RUNNING) {
370                         _DBG("[APP %d] PAUSE", _pid);
371                         if (ui->ops->pause)
372                                 r = ui->ops->pause(ui->ops->data);
373                         ui->state = AS_PAUSED;
374                         if(r >= 0 && resource_reclaiming == TRUE)
375                                 __appcore_timer_add(ui);
376                 }
377                 /* TODO : rotation stop */
378                 //r = appcore_pause_rotation_cb();
379
380                 _inform_backgrd();
381                 break;
382         case AE_RESUME:
383                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:start]",
384                     ui->name);
385                 if (ui->state == AS_PAUSED) {
386                         _DBG("[APP %d] RESUME", _pid);
387                         if (ui->ops->resume)
388                                 r = ui->ops->resume(ui->ops->data);
389                         ui->state = AS_RUNNING;
390                 }
391                 /*TODO : rotation start*/
392                 //r = appcore_resume_rotation_cb();
393                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:resume:done]",
394                     ui->name);
395                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:Launching:done]",
396                     ui->name);
397                 _inform_foregrd();
398
399                 break;
400         default:
401                 /* do nothing */
402                 break;
403         }
404 }
405
406 static struct ui_ops efl_ops = {
407         .data = &priv,
408         .cb_app = __do_app,
409 };
410
411
412 static bool __check_visible(void)
413 {
414         GSList *iter = NULL;
415         struct win_node *entry = NULL;
416
417         _DBG("[EVENT_TEST][EVENT] __check_visible\n");
418         
419         for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
420                 entry = iter->data;     
421                 _DBG("win : %x obscured : %d\n", entry->win, entry->bfobscured);
422                 if(entry->bfobscured == FALSE)
423                         return TRUE;            
424         }
425         return FALSE;
426 }
427
428 static bool __exist_win(unsigned int win)
429 {
430         struct win_node temp;
431         GSList *f;
432
433         temp.win = win;
434
435         f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
436         if (f == NULL) {
437                 return FALSE;
438         } else {
439                 return TRUE;
440         }
441
442 }
443
444 static bool __add_win(unsigned int win)
445 {
446         struct win_node *t;
447         GSList *f;
448
449         t = calloc(1, sizeof(struct win_node));
450         if (t == NULL)
451                 return FALSE;
452
453         t->win = win;
454         t->bfobscured = FALSE;
455
456         _DBG("[EVENT_TEST][EVENT] __add_win WIN:%x\n", win);
457
458         f = g_slist_find_custom(g_winnode_list, t, WIN_COMP);
459
460         if (f) {
461                 errno = ENOENT;
462                 _DBG("[EVENT_TEST][EVENT] ERROR There is already window : %x \n", win);
463                 free(t);
464                 return 0;
465         }
466
467         g_winnode_list = g_slist_append(g_winnode_list, t);
468
469         return TRUE;
470
471 }
472
473 static bool __delete_win(unsigned int win)
474 {
475         struct win_node temp;
476         GSList *f;
477
478         temp.win = win;
479
480         f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
481         if (f == NULL) {
482                 errno = ENOENT;
483                 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n",
484                      win);
485                 return 0;
486         }
487
488         g_winnode_list = g_slist_remove_link(g_winnode_list, f);
489
490         free(f->data);
491
492         return TRUE;
493 }
494
495 static bool __update_win(unsigned int win, bool bfobscured)
496 {
497         struct win_node temp;
498         GSList *f;
499
500         struct win_node *t;
501
502         _DBG("[EVENT_TEST][EVENT] __update_win WIN:%x fully_obscured %d\n", win,
503              bfobscured);
504
505         temp.win = win;
506
507         f = g_slist_find_custom(g_winnode_list, &temp, WIN_COMP);
508
509         if (f == NULL) {
510                 errno = ENOENT;
511                 _DBG("[EVENT_TEST][EVENT] ERROR There is no window : %x \n", win);
512                 return FALSE;
513         }
514
515         g_winnode_list = g_slist_remove_link(g_winnode_list, f);
516
517         free(f->data);
518
519         t = calloc(1, sizeof(struct win_node));
520         if (t == NULL)
521                 return FALSE;
522
523         t->win = win;
524         t->bfobscured = bfobscured;
525
526         g_winnode_list = g_slist_append(g_winnode_list, t);
527         
528         return TRUE;
529
530 }
531
532 /* WM_ROTATE */
533 static Ecore_X_Atom _WM_WINDOW_ROTATION_SUPPORTED = 0;
534 static Ecore_X_Atom _WM_WINDOW_ROTATION_CHANGE_REQUEST = 0;
535
536 static int __check_wm_rotation_support(void)
537 {
538         Ecore_X_Window root, win, win2;
539         int ret;
540
541         if (!_WM_WINDOW_ROTATION_SUPPORTED) {
542                 _WM_WINDOW_ROTATION_SUPPORTED =
543                                         ecore_x_atom_get("_E_WINDOW_ROTATION_SUPPORTED");
544         }
545
546         if (!_WM_WINDOW_ROTATION_CHANGE_REQUEST) {
547                 _WM_WINDOW_ROTATION_CHANGE_REQUEST =
548                                         ecore_x_atom_get("_E_WINDOW_ROTATION_CHANGE_REQUEST");
549         }
550
551         root = ecore_x_window_root_first_get();
552         ret = ecore_x_window_prop_xid_get(root,
553                         _WM_WINDOW_ROTATION_SUPPORTED,
554                         ECORE_X_ATOM_WINDOW,
555                         &win, 1);
556         if ((ret == 1) && (win))
557         {
558                 ret = ecore_x_window_prop_xid_get(win,
559                                 _WM_WINDOW_ROTATION_SUPPORTED,
560                                 ECORE_X_ATOM_WINDOW,
561                                 &win2, 1);
562                 if ((ret == 1) && (win2 == win))
563                         return 0;
564         }
565
566         return -1;
567 }
568
569 static void __set_wm_rotation_support(unsigned int win, unsigned int set)
570 {
571         GSList *iter = NULL;
572         struct win_node *entry = NULL;
573
574         if (0 == win) {
575                 for (iter = g_winnode_list; iter != NULL; iter = g_slist_next(iter)) {
576                         entry = iter->data;
577                         if (entry->win) {
578                                 ecore_x_window_prop_card32_set(entry->win,
579                                                 _WM_WINDOW_ROTATION_SUPPORTED,
580                                                 &set, 1);
581                         }
582                 }
583         } else {
584                 ecore_x_window_prop_card32_set(win,
585                                 _WM_WINDOW_ROTATION_SUPPORTED,
586                                 &set, 1);
587         }
588 }
589
590 Ecore_X_Atom atom_parent;
591
592 static Eina_Bool __show_cb(void *data, int type, void *event)
593 {
594         Ecore_X_Event_Window_Show *ev;
595         int ret;
596         Ecore_X_Window parent;
597
598         ev = event;
599
600         ret = ecore_x_window_prop_window_get(ev->win, atom_parent, &parent, 1);
601         if (ret != 1)
602         {
603                 // This is child window. Skip!!!
604                 return ECORE_CALLBACK_PASS_ON;
605         }
606
607         _DBG("[EVENT_TEST][EVENT] GET SHOW EVENT!!!. WIN:%x\n", ev->win);
608
609         if (!__exist_win((unsigned int)ev->win)) {
610                 /* WM_ROTATE */
611                 if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
612                         __set_wm_rotation_support(ev->win, 1);
613                 }
614                 __add_win((unsigned int)ev->win);
615         }
616         else
617                 __update_win((unsigned int)ev->win, FALSE);
618
619         return ECORE_CALLBACK_RENEW;
620 }
621
622 static Eina_Bool __hide_cb(void *data, int type, void *event)
623 {
624         Ecore_X_Event_Window_Hide *ev;
625         int bvisibility = 0;
626
627         ev = event;
628
629         _DBG("[EVENT_TEST][EVENT] GET HIDE EVENT!!!. WIN:%x\n", ev->win);
630
631         if (__exist_win((unsigned int)ev->win)) {
632                 __delete_win((unsigned int)ev->win);
633                 
634                 bvisibility = __check_visible();
635                 if (!bvisibility && b_active == 1) {
636                         _DBG(" Go to Pasue state \n");
637                         b_active = 0;
638                         __do_app(AE_PAUSE, data, NULL);
639                 }
640         }
641
642         return ECORE_CALLBACK_RENEW;
643 }
644
645 static Eina_Bool __visibility_cb(void *data, int type, void *event)
646 {
647         Ecore_X_Event_Window_Visibility_Change *ev;
648         int bvisibility = 0;
649
650         ev = event;
651
652         __update_win((unsigned int)ev->win, ev->fully_obscured);
653         bvisibility = __check_visible();
654
655         if (bvisibility && b_active == 0) {
656                 _DBG(" Go to Resume state\n");
657                 b_active = 1;
658                 __do_app(AE_RESUME, data, NULL);
659
660         } else if (!bvisibility && b_active == 1) {
661                 _DBG(" Go to Pasue state \n");
662                 b_active = 0;
663                 __do_app(AE_PAUSE, data, NULL);
664         } else
665                 _DBG(" No change state \n");
666
667         return ECORE_CALLBACK_RENEW;
668
669 }
670
671 /* WM_ROTATE */
672 static Eina_Bool __cmsg_cb(void *data, int type, void *event)
673 {
674         struct ui_priv *ui = (struct ui_priv *)data;
675         Ecore_X_Event_Client_Message *e = event;
676
677         if (!ui) return ECORE_CALLBACK_PASS_ON;
678         if (e->format != 32) return ECORE_CALLBACK_PASS_ON;
679         if (e->message_type == _WM_WINDOW_ROTATION_CHANGE_REQUEST) {
680                 if ((0 == ui->wm_rot_supported) ||
681                         (0 == ui->rot_started) ||
682                         (NULL == ui->rot_cb)) {
683                         return ECORE_CALLBACK_PASS_ON;
684                 }
685
686                 enum appcore_rm rm;
687                 switch (e->data.l[1])
688                 {
689                         case   0: rm = APPCORE_RM_PORTRAIT_NORMAL;   break;
690                         case  90: rm = APPCORE_RM_LANDSCAPE_REVERSE; break;
691                         case 180: rm = APPCORE_RM_PORTRAIT_REVERSE;  break;
692                         case 270: rm = APPCORE_RM_LANDSCAPE_NORMAL;  break;
693                         default:  rm = APPCORE_RM_UNKNOWN;           break;
694                 }
695
696                 ui->rot_mode = rm;
697
698                 if (APPCORE_RM_UNKNOWN != rm) {
699                         ui->rot_cb(rm, ui->rot_cb_data);
700                 }
701         }
702
703         return ECORE_CALLBACK_PASS_ON;
704 }
705
706 static void __add_climsg_cb(struct ui_priv *ui)
707 {
708         _ret_if(ui == NULL);
709
710         atom_parent = ecore_x_atom_get("_E_PARENT_BORDER_WINDOW");
711         if (!atom_parent)
712         {
713                 // Do Error Handling
714         }
715
716         ui->hshow =
717             ecore_event_handler_add(ECORE_X_EVENT_WINDOW_SHOW, __show_cb, ui);
718         ui->hhide =
719             ecore_event_handler_add(ECORE_X_EVENT_WINDOW_HIDE, __hide_cb, ui);
720         ui->hvchange =
721             ecore_event_handler_add(ECORE_X_EVENT_WINDOW_VISIBILITY_CHANGE,
722                                     __visibility_cb, ui);
723
724         /* Add client message callback for WM_ROTATE */
725         if(!__check_wm_rotation_support())
726         {
727                 ui->hcmsg =
728                         ecore_event_handler_add(ECORE_X_EVENT_CLIENT_MESSAGE, __cmsg_cb, ui);
729                 ui->wm_rot_supported = 1;
730                 appcore_set_wm_rotation(&wm_rotate);
731         }
732 }
733
734 static int __before_loop(struct ui_priv *ui, int *argc, char ***argv)
735 {
736         int r;
737         char *hwacc = NULL;
738
739         if (argc == NULL || argv == NULL) {
740                 _ERR("argc/argv is NULL");
741                 errno = EINVAL;
742                 return -1;
743         }
744
745         g_type_init();
746         elm_init(*argc, *argv);
747
748         hwacc = getenv("HWACC");
749
750         if(hwacc == NULL) {
751                 _DBG("elm_config_preferred_engine_set is not called");
752         } else if(strcmp(hwacc, "USE") == 0) {
753                 elm_config_preferred_engine_set("opengl_x11");
754                 _DBG("elm_config_preferred_engine_set : opengl_x11");
755         } else if(strcmp(hwacc, "NOT_USE") == 0) {
756                 elm_config_preferred_engine_set("software_x11");
757                 _DBG("elm_config_preferred_engine_set : software_x11");
758         } else {
759                 _DBG("elm_config_preferred_engine_set is not called");
760         }
761
762         r = appcore_init(ui->name, &efl_ops, *argc, *argv);
763         _retv_if(r == -1, -1);
764
765         LOG(LOG_DEBUG, "LAUNCH", "[%s:Platform:appcore_init:done]", ui->name);
766         if (ui->ops && ui->ops->create) {
767                 r = ui->ops->create(ui->ops->data);
768                 if (r == -1) {
769                         _ERR("create() return error");
770                         appcore_exit();
771                         errno = ECANCELED;
772                         return -1;
773                 }
774                 LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:create:done]",
775                     ui->name);
776         }
777         ui->state = AS_CREATED;
778
779         __add_climsg_cb(ui);
780
781         return 0;
782 }
783
784 static void __after_loop(struct ui_priv *ui)
785 {
786         appcore_unset_rotation_cb();
787         appcore_exit();
788
789         if (ui->ops && ui->ops->terminate)
790                 ui->ops->terminate(ui->ops->data);
791
792         if (ui->hshow)
793                 ecore_event_handler_del(ui->hshow);
794         if (ui->hhide)
795                 ecore_event_handler_del(ui->hhide);
796         if (ui->hvchange)
797                 ecore_event_handler_del(ui->hvchange);
798
799         __appcore_timer_del(ui);
800
801         elm_shutdown();
802 }
803
804 static int __set_data(struct ui_priv *ui, const char *name,
805                     struct appcore_ops *ops)
806 {
807         if (ui->name) {
808                 _ERR("Mainloop already started");
809                 errno = EINPROGRESS;
810                 return -1;
811         }
812
813         if (name == NULL || name[0] == '\0') {
814                 _ERR("Invalid name");
815                 errno = EINVAL;
816                 return -1;
817         }
818
819         if (ops == NULL) {
820                 _ERR("ops is NULL");
821                 errno = EINVAL;
822                 return -1;
823         }
824
825         ui->name = strdup(name);
826         _retv_if(ui->name == NULL, -1);
827
828         ui->ops = ops;
829
830         ui->mfcb = __appcore_efl_memory_flush_cb;
831
832         _pid = getpid();
833
834         /* WM_ROTATE */
835         ui->wm_rot_supported = 0;
836         ui->rot_started = 0;
837         ui->rot_cb = NULL;
838         ui->rot_cb_data = NULL;
839         ui->rot_mode = APPCORE_RM_UNKNOWN;
840
841         return 0;
842 }
843
844 static void __unset_data(struct ui_priv *ui)
845 {
846         if (ui->name)
847                 free((void *)ui->name);
848
849         memset(ui, 0, sizeof(struct ui_priv));
850 }
851
852 /* WM_ROTATE */
853 static int __wm_set_rotation_cb(int (*cb) (enum appcore_rm, void *), void *data)
854 {
855         if (cb == NULL) {
856                 errno = EINVAL;
857                 return -1;
858         }
859
860         if ((priv.wm_rot_supported) && (0 == priv.rot_started)) {
861                 __set_wm_rotation_support(0, 1);
862         }
863
864         priv.rot_cb = cb;
865         priv.rot_cb_data = data;
866         priv.rot_started = 1;
867
868         return 0;
869 }
870
871 static int __wm_unset_rotation_cb(void)
872 {
873         if ((priv.wm_rot_supported) && (1 == priv.rot_started)) {
874                 __set_wm_rotation_support(0, 0);
875         }
876
877         priv.rot_cb = NULL;
878         priv.rot_cb_data = NULL;
879         priv.rot_started = 0;
880
881         return 0;
882 }
883
884 static int __wm_get_rotation_state(enum appcore_rm *curr)
885 {
886         if (curr == NULL) {
887                 errno = EINVAL;
888                 return -1;
889         }
890
891         *curr = priv.rot_mode;
892
893         return 0;
894 }
895
896 static int __wm_pause_rotation_cb(void)
897 {
898         if ((1 == priv.rot_started) && (priv.wm_rot_supported)) {
899                 __set_wm_rotation_support(0, 0);
900         }
901
902         priv.rot_started = 0;
903
904         return 0;
905 }
906
907 static int __wm_resume_rotation_cb(void)
908 {
909         if ((0 == priv.rot_started) && (priv.wm_rot_supported)) {
910                 __set_wm_rotation_support(0, 1);
911         }
912
913         priv.rot_started = 1;
914
915         return 0;
916 }
917
918 static struct ui_wm_rotate wm_rotate = {
919         __wm_set_rotation_cb,
920         __wm_unset_rotation_cb,
921         __wm_get_rotation_state,
922         __wm_pause_rotation_cb,
923         __wm_resume_rotation_cb
924 };
925
926 EXPORT_API int appcore_efl_main(const char *name, int *argc, char ***argv,
927                                 struct appcore_ops *ops)
928 {
929         int r;
930
931         LOG(LOG_DEBUG, "LAUNCH", "[%s:Application:main:done]", name);
932
933         r = __set_data(&priv, name, ops);
934         _retv_if(r == -1, -1);
935
936         r = __before_loop(&priv, argc, argv);
937         if (r == -1) {
938                 __unset_data(&priv);
939                 return -1;
940         }
941
942         elm_run();
943
944         __after_loop(&priv);
945
946         __unset_data(&priv);
947
948         return 0;
949 }
950
951 EXPORT_API int appcore_set_system_resource_reclaiming(bool enable)
952 {
953         resource_reclaiming = enable;
954
955         return 0;
956 }